kstyrc / embedded-redis

Redis embedded server for Java integration testing
840 stars 368 forks source link

Redis hangs when using Custom Provider with Redis 4.0.* #90

Open AndyWilks79 opened 6 years ago

AndyWilks79 commented 6 years ago

When using a custom version of Redis, the code waits indefinitely for the Redis Server to start. This is because it is waiting for the following startup message as set on redis.embedded.RedisServer.class

REDIS_READY_PATTERN = ".The server is now ready to accept connections on port."

It appears as of version 4.0. that the final startup statement issued by Redis is now - Ready to accept connections.

As REDIS_READY_PATTERN is a final string and the method is protected access, it doesn't appear you can set the value when creating the server.

  @PostConstruct
  public void startRedis() throws Exception {

        RedisExecProvider customProvider = RedisExecProvider.defaultProvider()
                  .override(OS.UNIX, "/path/to/unix/redis")
                  .override(OS.WINDOWS, Architecture.x86, "/path/to/win/redis")
                  .override(OS.WINDOWS, Architecture.x86_64, "/path/to/win/redis")
                  .override(OS.MAC_OS_X, Architecture.x86, "/path/to/macosx/redis")
                  .override(OS.MAC_OS_X, Architecture.x86_64, "/path/to/macosx/redis");

        this.redisServer = RedisServer.builder()
                .port(port)
                .setting("maxmemory 128M")
                .setting("daemonize no")    
                .setting("appendonly no")
                .redisExecProvider(customProvider)
                .build();
                this.redisServer.start();
                System.out.println(this.redisServer.isActive());
  }
AndyWilks79 commented 6 years ago

The way I discovered this was by adding println statements to AbstractRedisInstance.class to show the response received when starting Redis.

private void awaitRedisServerReady() throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(redisProcess.getInputStream()));
    try {
        String outputLine;
        do {
            outputLine = reader.readLine();
            if (outputLine == null) {
                //Something goes wrong. Stream is ended before server was activated.
                throw new RuntimeException("Can't start redis server. Check logs for details.");
            }
            System.out.println(outputLine);
            System.out.println(redisReadyPattern());
        } while (!outputLine.matches(redisReadyPattern()));
    } finally {
        IOUtils.closeQuietly(reader);
    }
}
LaughXP commented 6 years ago

please try https://github.com/ozimov/embedded-redis.