Skip to content

Commit

Permalink
Test JedisURIHelper#getClientSideCache(URI) (#3835)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed May 6, 2024
1 parent 82c0226 commit 6a1dfc8
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 159 deletions.
6 changes: 3 additions & 3 deletions src/main/java/redis/clients/jedis/util/JedisURIHelper.java
Expand Up @@ -113,15 +113,15 @@ public static ClientSideCache getClientSideCache(URI uri) {
try {
maxSize = Integer.parseInt(val);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Value of cache_max_size must be an integer.", nfe);
throw new IllegalArgumentException("Value of cache_max_size must be an integer (no of commands).", nfe);
}
break;

case "cache_ttl":
try {
ttl = Integer.parseInt(val);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Value of cache_ttl must be an integer denoting seconds.", nfe);
throw new IllegalArgumentException("Value of cache_ttl must be an integer (in seconds).", nfe);
}
break;
}
Expand All @@ -132,7 +132,7 @@ public static ClientSideCache getClientSideCache(URI uri) {
return null;
}
if (!guava && !caffeine && (maxSize != null || ttl != null)) {
throw new IllegalArgumentException("The cache library (guava OR caffeine) must be selected.");
throw new IllegalArgumentException("A supported caching library (guava OR caffeine) must be selected.");
}
if (ZERO_INTEGER.equals(ttl)) {
ttl = null; // below, only null will be checked
Expand Down
Expand Up @@ -5,50 +5,14 @@
import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.function.Supplier;
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.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.csc.util.AllowAndDenyListWithStringKeys;

public class AllowAndDenyListClientSideCacheTest {

protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1);

protected Jedis control;

@Before
public void setUp() throws Exception {
control = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared").build());
control.flushAll();
}

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

private static final Supplier<JedisClientConfig> clientConfig
= () -> DefaultJedisClientConfig.builder().resp3().password("foobared").build();

private static final Supplier<GenericObjectPoolConfig<Connection>> singleConnectionPoolConfig
= () -> {
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(1);
return poolConfig;
};
public class AllowAndDenyListClientSideCacheTest extends ClientSideCacheTestBase {

@Test
public void none() {
Expand Down
Expand Up @@ -8,50 +8,15 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;

import java.net.URI;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
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.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.util.JedisURIHelper;

public class CaffeineClientSideCacheTest extends ClientSideCacheTestBase {

public class CaffeineClientSideCacheTest {

protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1);

protected Jedis control;

@Before
public void setUp() throws Exception {
control = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared").build());
control.flushAll();
}

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

private static final Supplier<JedisClientConfig> clientConfig
= () -> DefaultJedisClientConfig.builder().resp3().password("foobared").build();

private static final Supplier<GenericObjectPoolConfig<Connection>> singleConnectionPoolConfig
= () -> {
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(1);
return poolConfig;
};

@Test
public void simple() {
CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10).build();
Expand All @@ -62,12 +27,12 @@ public void simple() {
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ?
}
}

@Test
public void individualCommandsAndThenStats() {

Cache caffeine = Caffeine.newBuilder().recordStats().build();

try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
new CaffeineClientSideCache(caffeine, new OpenHftCommandHasher()),
singleConnectionPoolConfig.get())) {
Expand All @@ -84,7 +49,7 @@ public void individualCommandsAndThenStats() {
assertNull(jedis.get("foo"));
assertEquals(0, caffeine.estimatedSize());
}

CacheStats stats = caffeine.stats();
assertEquals(1L, stats.hitCount());
assertThat(stats.missCount(), Matchers.greaterThan(0L));
Expand Down Expand Up @@ -131,4 +96,25 @@ public void timeToLive() throws InterruptedException {
assertThat(caffeine.stats().evictionCount(), Matchers.equalTo((long) count));
}

@Test
public void uriSimple() {
URI uri = URI.create(baseUrl + "?cache_lib=caffeine");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.instanceOf(CaffeineClientSideCache.class));
}

@Test
public void uriAllParams() {
URI uri = URI.create(baseUrl + "?cache_lib=caffeine&cache_max_size=1000&cache_ttl=10");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.instanceOf(CaffeineClientSideCache.class));
}

@Test
public void uriMaxSizeZeroMeansNull() {
URI uri = URI.create(baseUrl + "?cache_lib=caffeine&cache_max_size=0");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.nullValue());
}

}
@@ -1,53 +1,21 @@
package redis.clients.jedis.csc;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.Supplier;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
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.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.util.JedisURIHelper;

public class ClientSideCacheFunctionalityTest {

protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1);

protected Jedis control;

@Before
public void setUp() throws Exception {
control = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared").build());
control.flushAll();
}

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

private static final Supplier<JedisClientConfig> clientConfig
= () -> DefaultJedisClientConfig.builder().resp3().password("foobared").build();

private static final Supplier<GenericObjectPoolConfig<Connection>> singleConnectionPoolConfig
= () -> {
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(1);
return poolConfig;
};
public class ClientSideCacheFunctionalityTest extends ClientSideCacheTestBase {

@Test
public void flushEntireCache() {
Expand Down Expand Up @@ -108,4 +76,34 @@ public void multiKeyOperation() {
}
}

@Test
public void uriNoParam() {
URI uri = URI.create(baseUrl + "?");
assertNull(JedisURIHelper.getClientSideCache(uri));
}

@Test
public void uriUnknownLib() {
URI uri = URI.create(baseUrl + "?cache_lib=unknown");
IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
() -> JedisURIHelper.getClientSideCache(uri));
assertEquals("Unsupported library unknown", iae.getMessage());
}

@Test
public void uriNoLib() {
String[] otherParams
= new String[]{
"?cache_max_size=1000",
"?cache_ttl=10",
"?cache_max_size=1000&cache_ttl=10"
};
Arrays.stream(otherParams).forEach(urlParams -> {
URI uri = URI.create(baseUrl + urlParams);
IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
() -> JedisURIHelper.getClientSideCache(uri));
assertEquals("A supported caching library (guava OR caffeine) must be selected.", iae.getMessage());
});
}

}
45 changes: 45 additions & 0 deletions src/test/java/redis/clients/jedis/csc/ClientSideCacheTestBase.java
@@ -0,0 +1,45 @@
package redis.clients.jedis.csc;

import java.util.function.Supplier;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.After;
import org.junit.Before;

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.Jedis;
import redis.clients.jedis.JedisClientConfig;

abstract class ClientSideCacheTestBase {

protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1);

protected static final String baseUrl = "redis://:foobared@" + hnp.toString() + "/";

protected Jedis control;

@Before
public void setUp() throws Exception {
control = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared").build());
control.flushAll();
}

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

protected static final Supplier<JedisClientConfig> clientConfig
= () -> DefaultJedisClientConfig.builder().resp3().password("foobared").build();

protected static final Supplier<GenericObjectPoolConfig<Connection>> singleConnectionPoolConfig
= () -> {
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
poolConfig.setMaxTotal(1);
return poolConfig;
};

}

0 comments on commit 6a1dfc8

Please sign in to comment.