-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test GuavaCSC and CaffeineCSC (#3742)
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package redis.clients.jedis; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.google.common.cache.CacheBuilder; | ||
|
||
import java.util.function.Supplier; | ||
import net.openhft.hashing.LongHashFunction; | ||
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.util.CaffeineCSC; | ||
import redis.clients.jedis.util.GuavaCSC; | ||
|
||
public class ClientSideCacheLibsTest { | ||
|
||
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 guavaSimple() { | ||
GuavaCSC guava = GuavaCSC.builder().maximumSize(10).ttl(10).hashFunction(com.google.common.hash.Hashing.farmHashFingerprint64()).build(); | ||
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), guava)) { | ||
control.set("foo", "bar"); | ||
assertEquals("bar", jedis.get("foo")); | ||
control.del("foo"); | ||
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ? | ||
} | ||
} | ||
|
||
@Test | ||
public void guavaMore() { | ||
|
||
com.google.common.cache.Cache guava = CacheBuilder.newBuilder().recordStats().build(); | ||
|
||
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new GuavaCSC(guava), singleConnectionPoolConfig.get())) { | ||
control.set("foo", "bar"); | ||
assertEquals(0, guava.size()); | ||
assertEquals("bar", jedis.get("foo")); | ||
assertEquals(1, guava.size()); | ||
control.flushAll(); | ||
assertEquals(1, guava.size()); | ||
assertEquals("bar", jedis.get("foo")); | ||
assertEquals(1, guava.size()); | ||
jedis.ping(); | ||
assertEquals(0, guava.size()); | ||
assertNull(jedis.get("foo")); | ||
assertEquals(0, guava.size()); | ||
} | ||
|
||
com.google.common.cache.CacheStats stats = guava.stats(); | ||
assertEquals(1L, stats.hitCount()); | ||
assertThat(stats.missCount(), Matchers.greaterThan(0L)); | ||
} | ||
|
||
@Test | ||
public void caffeineSimple() { | ||
CaffeineCSC caffeine = CaffeineCSC.builder().maximumSize(10).ttl(10).hashFunction(LongHashFunction.xx()).build(); | ||
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) { | ||
control.set("foo", "bar"); | ||
assertEquals("bar", jedis.get("foo")); | ||
control.del("foo"); | ||
assertThat(jedis.get("foo"), Matchers.oneOf("bar", null)); // ? | ||
} | ||
} | ||
|
||
@Test | ||
public void caffeineMore() { | ||
|
||
com.github.benmanes.caffeine.cache.Cache caffeine = Caffeine.newBuilder().recordStats().build(); | ||
|
||
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CaffeineCSC(caffeine, LongHashFunction.city_1_1()), | ||
singleConnectionPoolConfig.get())) { | ||
control.set("foo", "bar"); | ||
assertEquals(0, caffeine.estimatedSize()); | ||
assertEquals("bar", jedis.get("foo")); | ||
assertEquals(1, caffeine.estimatedSize()); | ||
control.flushAll(); | ||
assertEquals(1, caffeine.estimatedSize()); | ||
assertEquals("bar", jedis.get("foo")); | ||
assertEquals(1, caffeine.estimatedSize()); | ||
jedis.ping(); | ||
assertEquals(0, caffeine.estimatedSize()); | ||
assertNull(jedis.get("foo")); | ||
assertEquals(0, caffeine.estimatedSize()); | ||
} | ||
|
||
com.github.benmanes.caffeine.cache.stats.CacheStats stats = caffeine.stats(); | ||
assertEquals(1L, stats.hitCount()); | ||
assertThat(stats.missCount(), Matchers.greaterThan(0L)); | ||
} | ||
} |