Raining Man
Raining Man

Reputation: 1

Valkey / Redis Testcontainer Config

i am unsure how to setup Testcontainers with valkey in my spring project. As redis is no more open source, i chose to implement caching with valkey 8.0.0 instead. But i am having trouble setting up testcointainers and now i am wondering what i am doing wrong, and if i just should have used an older open source version of redis anyway, or switch to some other open source key value store that has better documentation. I am unsure how popular valkey even is at this point.

I tried using the RedisContainer for valkey, but it did not seem to work, as i think i need to use different command to start a valkey container compared to a redis one.

Here is my Code

@ActiveProfiles("integration-test")
@Testcontainers
@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
public abstract class BaseIntegrationTest {

    @Autowired
    protected MockMvc mockMvc;

    @Autowired
    protected IntegrationTestDataConfig integrationTestDataConfig;

    @Container
    @ServiceConnection
    static final PostgreSQLContainer<?> POSTGRES_CONTAINER = new PostgreSQLContainer<>("postgres:latest")
        .withDatabaseName("testdb")
        .withUsername("test")
        .withPassword("test");

    @Container
    static final GenericContainer<?> valkeyContainer = new GenericContainer<>("valkey/valkey:8.0.0")
        .withExposedPorts(6379)
        .withCommand("valkey-server", "/etc/valkey/valkey.conf")
        .withClasspathResourceMapping("config/valkey.conf", "/etc/valkey/valkey.conf", BindMode.READ_ONLY)
        .waitingFor(Wait.forLogMessage(".*Ready to accept connections.*\\n", 1));

    @DynamicPropertySource
    static void registerValkeyProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.data.redis.host", valkeyContainer::getHost);
        registry.add("spring.data.redis.port", () -> valkeyContainer.getMappedPort(6379));
    }

}

As you can se i am using a GenericContainer, i am unsure if i am using the right comand by copying the config file or if this unnesecary alltogether

this is the conf file

bind 0.0.0.0 -::1
protected-mode no

and these are the the .yml properties that should be overwritten for the test environment:

spring:
  data:
    redis:
      host: ${VALKEY_HOST:<censored>}
      port: ${VALKEY_PORT:<censored>}
      password: ${VALKEY_PASSWORD:<censored>}

Currently the test container starts fine, but my lettuce connectoin factory does not seem to be able to resolve the address and port

2025-02-25T11:35:11.159+01:00  INFO 15996 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was localhost/127.0.0.1:52306
2025-02-25T11:35:11.168+01:00  WARN 15996 --- [ioEventLoop-6-2] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [localhost/<unresolved>:52306]: Connection closed prematurely

I confirmed through logs that the randomly mapped port (e.g., 52306) is the same as valkeyContainer.getMappedPort(6379), but the connection fails almost immediately. I’m not sure if I’m using the correct command (valkey-server /etc/valkey/valkey.conf), if the valkey.conf content is correct, or if there’s an additional environment variable required by Valkey.

  1. Am i correctly using a GenericContainer, or are there redis or valkey specific Containers that i should use instead?

  2. Is my Congig file correct?

  3. Am i using the correct properties in my .yml file?

  4. Should i switch to older versions of Redis or another key value store (Currently using Valkey for an autosave feature of a long form)?

  5. It was hard for me to find good documentation or guides, where can i find more information about valkey testcontainers and their setup?

  6. i tried to use RedisContainers, but the Testcontainer would not even start

  7. used different bind config like bind 127.0.0.1 -::1

  8. added StartupTimeout of 10 seconds

Upvotes: 0

Views: 44

Answers (1)

Raining Man
Raining Man

Reputation: 1

Never mind, this was just a spring configuratin problem. The same as in this question: Testcontainers loss of connection after some tests running

I was missing the @DirtiesContesxt annotation

Upvotes: 0

Related Questions