Skip to content

Commit

Permalink
Add test cases on LocalTSOProvider (#32951)
Browse files Browse the repository at this point in the history
* Add test cases on LocalTSOProvider

* Add test cases on LocalTSOProvider
  • Loading branch information
terrymanu authored Sep 21, 2024
1 parent 74b0fb7 commit 6760535
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.globalclock.type.tso.provider.local;

import org.apache.shardingsphere.globalclock.provider.GlobalClockProvider;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class LocalTSOProviderTest {

private GlobalClockProvider tsoProvider;

@BeforeEach
void setUp() {
tsoProvider = TypedSPILoader.getService(GlobalClockProvider.class, "TSO.local");
}

@Test
void assertGetCurrentTimestamp() {
assertThat(tsoProvider.getCurrentTimestamp(), is(0L));
assertThat(tsoProvider.getNextTimestamp(), is(1L));
assertThat(tsoProvider.getCurrentTimestamp(), is(1L));
}

@Test
void assertGetInstanceByDefault() {
assertThat(TypedSPILoader.getService(GlobalClockProvider.class, null), instanceOf(LocalTSOProvider.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ public final class RedisTSOProvider implements TSOProvider {

private JedisPool jedisPool;

private Properties props;

@Override
public void init(final Properties props) {
this.props = props;
if (initialized.compareAndSet(false, true)) {
createJedisPool();
createJedisPool(props);
checkJedisPool();
initCSN();
}
}

private void createJedisPool() {
private void createJedisPool(final Properties props) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.parseInt(getValue(props, RedisTSOPropertyKey.MAX_IDLE)));
config.setMaxTotal(Integer.parseInt(getValue(props, RedisTSOPropertyKey.MAX_TOTAL)));
jedisPool = new JedisPool(config, getValue(props, RedisTSOPropertyKey.HOST),
Integer.parseInt(getValue(props, RedisTSOPropertyKey.PORT)), Integer.parseInt(getValue(props, RedisTSOPropertyKey.TIMEOUT_INTERVAL)),
getValue(props, RedisTSOPropertyKey.PASSWORD));
jedisPool = new JedisPool(config, getValue(props, RedisTSOPropertyKey.HOST), Integer.parseInt(getValue(props, RedisTSOPropertyKey.PORT)),
Integer.parseInt(getValue(props, RedisTSOPropertyKey.TIMEOUT_INTERVAL)), getValue(props, RedisTSOPropertyKey.PASSWORD));
}

private String getValue(final Properties props, final RedisTSOPropertyKey propertyKey) {
return props.containsKey(propertyKey.getKey()) ? props.getProperty(propertyKey.getKey()) : propertyKey.getDefaultValue();
}

private void checkJedisPool() {
Expand All @@ -77,10 +77,6 @@ private void initCSN() {
}
}

private String getValue(final Properties props, final RedisTSOPropertyKey propertyKey) {
return props.containsKey(propertyKey.getKey()) ? props.getProperty(propertyKey.getKey()) : propertyKey.getDefaultValue();
}

@Override
public long getCurrentTimestamp() {
try (Jedis jedis = jedisPool.getResource()) {
Expand Down

0 comments on commit 6760535

Please sign in to comment.