Redis embedded server for Java integration testing.
Forked from ozimov, which was forked from kstyrc
Maven Central:
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.13.0</version>
</dependency>
Running RedisServer is as simple as:
RedisServer redisServer = new RedisServer(6379);
redisServer.start();
// do some work
redisServer.stop();
You can also provide RedisServer with your own executable:
// 1) given explicit file (os-independence broken!)
RedisServer redisServer = new RedisServer("/path/to/your/redis", 6379);
// 2) given os-independent matrix
ExecutableProvider customProvider = new ExecutableProviderBuilder()
.put(OS.UNIX, "/path/to/unix/redis")
.put(OS.WINDOWS, Architecture.x86, "/path/to/windows/redis")
.put(OS.WINDOWS, Architecture.x86_64, "/path/to/windows/redis")
.put(OS.MAC_OS_X, Architecture.x86, "/path/to/macosx/redis")
.put(OS.MAC_OS_X, Architecture.x86_64, "/path/to/macosx/redis")
RedisServer redisServer = new RedisServer(6379, customProvider);
You can also use fluent API to create RedisServer:
RedisServer redisServer = RedisServer.builder()
.executableProvider(customRedisProvider)
.port(6379)
.slaveOf("locahost", 6378)
.configFile("/path/to/your/redis.conf")
.build();
Or even create simple redis.conf file from scratch:
RedisServer redisServer = RedisServer.builder()
.executableProvider(customRedisProvider)
.port(6379)
.setting("bind 127.0.0.1") // good for local development on Windows to prevent security popups
.slaveOf("locahost", 6378)
.setting("daemonize no")
.setting("appendonly no")
.setting("maxmemory 128M")
.build();
The library contains a pre-compiled binary for ARM architecture. However, this binary was generated using the QEMU emulator and did not work with the Redis tests. It is not known whether it will actually work.
The makefile contains a target (build-arm) that will generate a binary from Redis source. Use this to generate your own binary and then configure it.
final ExecutableProvider executables = new ExecutableProviderBuilder()
.addProvidedVersions()
.put(OS.UNIX, Architecture.aarch64, "/path/to/resource/redis-server-arm")
.build();
new RedisServer(Redis.DEFAULT_REDIS_PORT, executables);
Our Embedded Redis has support for HA Redis clusters with Sentinels and master-slave replication
A simple redis integration test with Redis cluster on ephemeral ports, with setup similar to that from production would look like this:
public class SomeIntegrationTestThatRequiresRedis {
private RedisCluster cluster;
private Set<String> jedisSentinelHosts;
@Before
public void setup() throws Exception {
//creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
cluster = RedisCluster.builder().ephemeral().sentinelCount(3).quorumSize(2)
.replicationGroup("master1", 1)
.replicationGroup("master2", 1)
.replicationGroup("master3", 1)
.build();
cluster.start();
//retrieve ports on which sentinels have been started, using a simple Jedis utility class
jedisSentinelHosts = JedisUtil.sentinelHosts(cluster);
}
@TestApp
public void test() throws Exception {
// testing code that requires redis running
JedisSentinelPool pool = new JedisSentinelPool("master1", jedisSentinelHosts);
}
@After
public void tearDown() throws Exception {
cluster.stop();
}
}
The above example starts Redis cluster on ephemeral ports, which you can later get with cluster.ports()
,
which will return a list of all ports of the cluster. You can also get ports of sentinels with cluster.sentinelPorts()
or servers with cluster.serverPorts()
. JedisUtil
class contains utility methods for use with Jedis client.
You can also start Redis cluster on predefined ports and even mix both approaches:
public class SomeIntegrationTestThatRequiresRedis {
private RedisCluster cluster;
@Before
public void setup() throws Exception {
final List<Integer> sentinels = Arrays.asList(26739, 26912);
final List<Integer> group1 = Arrays.asList(6667, 6668);
final List<Integer> group2 = Arrays.asList(6387, 6379);
//creates a cluster with 2 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
cluster = RedisCluster.builder().sentinelPorts(sentinels).quorumSize(2)
.serverPorts(group1).replicationGroup("master1", 1)
.serverPorts(group2).replicationGroup("master2", 1)
.ephemeralServers().replicationGroup("master3", 1)
.build();
cluster.start();
}
//(...)
The above will create and start a cluster with sentinels on ports 26739, 26912
, first replication group on 6667, 6668
,
second replication group on 6387, 6379
and third replication group on ephemeral ports.
When not provided with the desired redis executable, RedisServer runs os-dependent executable enclosed in jar. Currently it uses:
- Redis 6.2.7 in case of Linux/Unix x86 or arm64 (x86 source, arm64 source)
- Redis 6.2.6-v5 in case of Linux/Unix x64 (source)
- Redis 6.2.6-v5 in case of OSX x64 or arm64 (x64 source, arm64 source)
- Redis 5.0.14.1 in case of Windows x64 (source)
However, you should provide RedisServer with redis executable if you need specific version.
Licensed under the Apache License, Version 2.0