Skip to content

Commit

Permalink
examples for testcontainers with redis, testing RedisMap
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Sep 26, 2024
1 parent 8e242f5 commit 5f72689
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 40 deletions.
129 changes: 129 additions & 0 deletions src/test/java/lars/spielplatz/redis/RedisMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package lars.spielplatz.redis;

import io.lettuce.core.api.StatefulRedisConnection;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.jetbrains.annotations.Nullable;

public class RedisMap implements Map<String, String> {

// TODO: try out https://github.com/DanielYWoo/fast-object-pool
// TODO: try out https://github.com/chrisvest/stormpot

private final GenericObjectPool<StatefulRedisConnection<String, String>> redisPool;

public RedisMap(GenericObjectPool<StatefulRedisConnection<String, String>> redisPool) {
this.redisPool = redisPool;
}

@Override
public int size() {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
return connection.sync().keys("*").size();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public boolean isEmpty() {
return size() == 0;
}

@Override
public boolean containsKey(Object key) {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
return connection.sync().exists((String) key) == 1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public boolean containsValue(Object value) {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
List<String> keys = connection.sync().keys("*");
for (String key : keys) {
if (connection.sync().get(key).equals(value)) {
return true;
}
}
return false;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public String get(Object key) {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
return connection.sync().get((String) key);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public @Nullable String put(String key, String value) {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
return connection.sync().set((String) key, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public String remove(Object key) {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
String value = connection.sync().get((String) key);
connection.sync().del((String) key);
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void putAll(Map<? extends String, ? extends String> map) {
map.forEach(this::put);
}

@Override
public void clear() {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
connection.sync().flushall();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Set<String> keySet() {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
return Set.copyOf(connection.sync().keys("*"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Collection<String> values() {
return List.of();
}

@Override
public Set<Entry<String, String>> entrySet() {
try (StatefulRedisConnection<String, String> connection = redisPool.borrowObject()) {
List<String> keys = connection.sync().keys("*");
return keys.stream()
.map(key -> Map.entry(key, connection.sync().get(key)))
.collect(Collectors.toSet());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.lettuce.core.support.ConnectionPoolSupport;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -19,14 +20,14 @@
import org.testcontainers.utility.DockerImageName;

@DisabledOnOs(OS.WINDOWS) // no docker on github actions windows
public class RedisTestsTest {
public class RedisMapTest {

private static final GenericContainer<?> redis =
new GenericContainer<>(DockerImageName.parse("redis")).withReuse(true).withExposedPorts(6379);

private static GenericObjectPool<StatefulRedisConnection<String, String>> pool;
private static RedisClient redisClient;
private RedisTest redisKeyValueStorage;
private RedisMap redisMap;

@BeforeAll
static void setUpBeforeAll() {
Expand All @@ -40,12 +41,39 @@ static void setUpBeforeAll() {

@BeforeEach
void setUp() {
redisKeyValueStorage = new RedisTest(pool);
redisMap = new RedisMap(pool);
}

@AfterEach
void tearDown() {
redisClient.connect().sync().flushall();
}

@Test
void empty_map_is_empty() {
assertThat(redisMap.isEmpty()).isTrue();
}

@Test
void map_with_one_entry_is_not_empty() {
redisMap.put("key", "value");
assertThat(redisMap.isEmpty()).isFalse();
}

@Test
void empty_map_has_size_0() {
assertThat(redisMap.size()).isZero();
}

@Test
void map_with_one_entry_has_size_1() {
redisMap.put("key", "value");
assertThat(redisMap.size()).isEqualTo(1);
}

@Test
void update() {
redisKeyValueStorage.update();
assertThat(redisKeyValueStorage.get()).isEqualTo("value");
void map_contains_key() {
redisMap.put("key", "value");
assertThat(redisMap.containsKey("key")).isTrue();
}
}
32 changes: 0 additions & 32 deletions src/test/java/lars/spielplatz/redis/RedisTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

@Testcontainers
@EnabledOnOs({OS.LINUX, OS.MAC})
public class RedisTest {
public class LettuceExplorationTest {

private static final Logger log = getLogger(RedisTest.class);
private static final Logger log = getLogger(LettuceExplorationTest.class);

@Container
private GenericContainer<?> redis =
Expand Down

0 comments on commit 5f72689

Please sign in to comment.