testcontainers和redis的连接超时

ojsjcaue  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(399)

我使用springboot、testcontainers、redis和junit5进行集成测试。我面临着一个奇怪的行为,当我完成所有的集成测试时,我总是让这个日志显示:
无法重新连接到[localhost:55133]:连接被拒绝:localhost/127.0.0.1:55133
这个例外是:

org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 1 minute(s)

at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:70)

但是我单独运行测试,我没有这种行为。
我使用junit5和junit5扩展来启动和停止我的redis容器:

public class RedisTestContainerExtension implements BeforeAllCallback, AfterAllCallback {

    private GenericContainer<?> redis;

    @Override
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
        redis = new GenericContainer<>(DockerImageName.parse("redis:5.0.3-alpine"))
                .withCommand("redis-server","--requirepass", "password")
                .waitingFor(Wait.forListeningPort())
                .withStartupTimeout(Duration.ofMinutes(2))
                .withExposedPorts(6379);
        redis.start();
        System.setProperty("APP_REDIS_CONVERSATIONS_HOST",redis.getHost());
        System.setProperty("APP_REDIS_CONVERSATIONS_PORT",redis.getFirstMappedPort().toString());
        System.setProperty("APP_REDIS_CONVERSATIONS_PASSWORD","password");
        System.setProperty("APP_REDIS_CONVERSATIONS_TTL","600m");
    }

    @Override
    public void afterAll(ExtensionContext extensionContext) throws Exception {
        if(redis != null){
            redis.stop();
        }
    }
}

我将此文件作为扩展名添加到集成测试中:

@ExtendWith({SpringExtension.class, RedisTestContainerExtension.class})
@SpringBootTest(classes = ConversationsApplication.class)
class MyIntegrationTest {
 ...
 }

有人能帮我解决这个问题吗。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题