Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix client side cache tests #3799

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Connection;
import redis.clients.jedis.ConnectionPoolConfig;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.HostAndPorts;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisClusterTestBase;

public class JedisClusterClientSideCacheTest extends JedisClusterTestBase {
public class JedisClusterClientSideCacheTest {

private static final Set<HostAndPort> hnp = Arrays.asList(nodeInfo1, nodeInfo2, nodeInfo3).stream().collect(Collectors.toSet());
private static final Set<HostAndPort> hnp = new HashSet<>(HostAndPorts.getStableClusterServers());

private static final Supplier<JedisClientConfig> clientConfig
= () -> DefaultJedisClientConfig.builder().resp3().password("cluster").build();
Expand All @@ -36,12 +37,25 @@ public class JedisClusterClientSideCacheTest extends JedisClusterTestBase {
return poolConfig;
};

protected JedisCluster control;

@Before
public void setUp() throws Exception {
control = new JedisCluster(hnp, clientConfig.get());
control.flushAll();
}

@After
public void tearDown() throws Exception {
control.close();
}

@Test
public void simple() {
try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache())) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.del("foo");
control.del("foo");
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ?
}
}
Expand All @@ -51,11 +65,11 @@ public void simpleWithSimpleMap() {
HashMap<Long, Object> map = new HashMap<>();
try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache(map),
singleConnectionPoolConfig.get())) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertThat(map, Matchers.aMapWithSize(0));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
jedis.del("foo");
control.del("foo");
assertThat(map, Matchers.aMapWithSize(1));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
Expand All @@ -69,9 +83,9 @@ public void simpleWithSimpleMap() {
@Test
public void flushAll() {
try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache())) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.flushAll();
control.flushAll();
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ?
}
}
Expand All @@ -81,11 +95,11 @@ public void flushAllWithSimpleMap() {
HashMap<Long, Object> map = new HashMap<>();
try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache(map),
singleConnectionPoolConfig.get())) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertThat(map, Matchers.aMapWithSize(0));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
jedis.flushAll();
control.flushAll();
assertThat(map, Matchers.aMapWithSize(1));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.DefaultJedisClientConfig;
Expand All @@ -24,19 +26,32 @@ public class JedisSentineledClientSideCacheTest {
protected static final HostAndPort sentinel1 = HostAndPorts.getSentinelServers().get(1);
protected static final HostAndPort sentinel2 = HostAndPorts.getSentinelServers().get(3);

private static final Set<HostAndPort> sentinels = Arrays.asList(sentinel1, sentinel2).stream().collect(Collectors.toSet());
private static final Set<HostAndPort> sentinels = new HashSet<>(Arrays.asList(sentinel1, sentinel2));

private static final JedisClientConfig masterClientConfig = DefaultJedisClientConfig.builder().resp3().password("foobared").build();

private static final JedisClientConfig sentinelClientConfig = DefaultJedisClientConfig.builder().resp3().build();

protected JedisSentineled control;

@Before
public void setUp() throws Exception {
control = new JedisSentineled(MASTER_NAME, masterClientConfig, sentinels, sentinelClientConfig);
control.flushAll();
}

@After
public void tearDown() throws Exception {
control.close();
}

@Test
public void simple() {
try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(),
sentinels, sentinelClientConfig)) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.del("foo");
control.del("foo");
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ?
}
}
Expand All @@ -46,11 +61,11 @@ public void simpleWithSimpleMap() {
HashMap<Long, Object> map = new HashMap<>();
try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(map),
sentinels, sentinelClientConfig)) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertThat(map, Matchers.aMapWithSize(0));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
jedis.del("foo");
control.del("foo");
assertThat(map, Matchers.aMapWithSize(1));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
Expand All @@ -65,9 +80,9 @@ public void simpleWithSimpleMap() {
public void flushAll() {
try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(),
sentinels, sentinelClientConfig)) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.flushAll();
control.flushAll();
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ?
}
}
Expand All @@ -77,11 +92,11 @@ public void flushAllWithSimpleMap() {
HashMap<Long, Object> map = new HashMap<>();
try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(map),
sentinels, sentinelClientConfig)) {
jedis.set("foo", "bar");
control.set("foo", "bar");
assertThat(map, Matchers.aMapWithSize(0));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
jedis.flushAll();
control.flushAll();
assertThat(map, Matchers.aMapWithSize(1));
assertEquals("bar", jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(1));
Expand Down
Loading