redis / jedis

Redis Java client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream when init JedisCluster using java test container

ThinhLe30 opened this issue · comments

I need to build a redis cluster and build a JedisCluster successfully for testing purpose, following:

  1. Here the test container:
package cellutions.redis;
import org.apache.flink.util.NetUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;

import java.util.*;

public class RedisClusterContainer extends GenericContainer<RedisClusterContainer> {

    public static final List<Integer> ports = Arrays.asList(
            NetUtils.getAvailablePort().getPort(),
            NetUtils.getAvailablePort().getPort(),
            NetUtils.getAvailablePort().getPort(),
            NetUtils.getAvailablePort().getPort(),
            NetUtils.getAvailablePort().getPort(),
            NetUtils.getAvailablePort().getPort()
    );

    public static final String local = "0.0.0.0";

    private static final List<String> environments = Arrays.asList(
            "INITIAL_PORT=" +  ports.get(0).toString(),
            "MASTERS="+ "3",
            "SLAVES_PER_MASTER="+ "1",
            "SENTINEL="+ "false",
            "REDIS_CLUSTER_IP="+ local,
            "IP="+ local,
            "BIND_ADDRESS="+ local
    );

    public RedisClusterContainer() {
        this("grokzen/redis-cluster:6.0.7");
    }

    public RedisClusterContainer(String imageName) {
        super(imageName);
    }

    @Override
    protected void configure() {
        this.setStartupAttempts(3);
        this.setExposedPorts(ports);
        this.setEnv(environments);
        this.waitingFor(new LogMessageWaitStrategy().withRegEx("(?s).*==> /var/log/supervisor/redis.*$"));
    }
}
  1. Here the test code:
public class RedisClusterTest {


    @Test
    public void testRedisCluser() throws Exception {
        RedisClusterContainer redisClusterContainer = new RedisClusterContainer();
        redisClusterContainer.start();
        Set<HostAndPort> jedisClusterNodes = new HashSet<>();
        for (int i = 0; i < RedisClusterContainer.ports.size(); i++) {
            jedisClusterNodes.add(new HostAndPort(redisClusterContainer.getContainerIpAddress(), redisClusterContainer.getMappedPort(RedisClusterContainer.ports.get(i))));
        }
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes);
        redisClusterContainer.stop();
    }


}

I got the error:

redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.

	at redis.clients.jedis.util.RedisInputStream.ensureFill(RedisInputStream.java:248)
	at redis.clients.jedis.util.RedisInputStream.readByte(RedisInputStream.java:47)
	at redis.clients.jedis.Protocol.process(Protocol.java:136)
	at redis.clients.jedis.Protocol.read(Protocol.java:222)
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:350)
	at redis.clients.jedis.Connection.getOne(Connection.java:332)
	at redis.clients.jedis.Connection.executeCommand(Connection.java:137)
	at redis.clients.jedis.Jedis.set(Jedis.java:4893)
	at cellutions.redis.RedisClusterTest.testRedisCluser(RedisClusterTest.java:35)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

Anyone can help me. Thank a lot.
env:
os: macos
jdk 17
jedis version 5.1.2
redis cluster docker image: grokzen/redis-cluster:6.0.7
java test container version: 1.19.7w

Update: resolve it by:

public class RedisClusterContainer extends FixedHostPortGenericContainer<RedisClusterContainer> {
    public static final List<Integer> REDIS_PORTS = Arrays.asList(6000, 6001, 6002, 6003, 6004, 6005);
    private static final String REDIS_CLUSTER_IMAGE = "grokzen/redis-cluster";
    private static final String IMAGE_VERSION = "6.0.7";

    public RedisClusterContainer() {
        super(REDIS_CLUSTER_IMAGE + ":" + IMAGE_VERSION);
        for (Integer port : REDIS_PORTS) {
            withFixedExposedPort(port, port);
        }
        withEnv("IP", "0.0.0.0");
        withEnv("INITIAL_PORT", "6000");
        waitingFor(new LogMessageWaitStrategy().withRegEx("(?s).*Background AOF rewrite finished successfully.*$"));
    }
}

Test

@Container
    private static final RedisClusterContainer redisClusterContainer = new RedisClusterContainer();
    GenericObjectPoolConfig config;
    Set<HostAndPort> jedisClusterNode;

    @Before
    public void setup() {
        String host = redisClusterContainer.getContainerIpAddress();
        redisClusterContainer.start();
        config = new GenericObjectPoolConfig();
        config.setMaxTotal(30);
        config.setMaxWaitMillis(2000);
        jedisClusterNode = new HashSet<>();
        for (Integer port : REDIS_PORTS) {
            jedisClusterNode.add(new HostAndPort(host, port));
        }
    }

    @Test
    public void testRedisCluster() {
        // Test code here
        JedisCluster jc = new JedisCluster(jedisClusterNode, config);
        jc.set("foo", "bar");
        String foo = jc.get("foo");
        Assert.assertEquals(foo, "bar");
    }