Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : issue with starting in cluster mode #1596

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boot-ultimate-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.23.0</version>
<version>1.25.2</version>
<style>AOSP</style>
</googleJavaFormat>
</java>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.ultimateredis;

import com.example.ultimateredis.common.TestcontainersConfiguration;
import com.example.ultimateredis.utils.AppConstants;
import org.springframework.boot.SpringApplication;

class TestUltimateRedisApplication {

public static void main(String[] args) {
SpringApplication.from(UltimateRedisApplication::main)
.with(TestcontainersConfiguration.class)
.withAdditionalProfiles(AppConstants.PROFILE_CLUSTER)
.run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.assertj.MockMvcTester;

@ActiveProfiles({AppConstants.PROFILE_STANDALONE})
@SpringBootTest(
Expand All @@ -17,7 +17,7 @@
@AutoConfigureMockMvc
public abstract class AbstractIntegrationTest {

@Autowired protected MockMvc mockMvc;
@Autowired protected MockMvcTester mockMvcTester;

@Autowired protected ObjectMapper objectMapper;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,37 @@
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertyRegistrar;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {

@Bean
@Profile(AppConstants.PROFILE_CLUSTER)
RedisClusterContainer redisClusterContainer(DynamicPropertyRegistry dynamicPropertyRegistry) {
RedisClusterContainer redisClusterContainer =
new RedisClusterContainer(RedisClusterContainer.DEFAULT_IMAGE_NAME)
.withKeyspaceNotifications()
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(4));
redisClusterContainer.start();
RedisClusterContainer redisClusterContainer() {
return new RedisClusterContainer(RedisClusterContainer.DEFAULT_IMAGE_NAME)
.withKeyspaceNotifications()
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(4));
}

@Bean
@Profile(AppConstants.PROFILE_CLUSTER)
DynamicPropertyRegistrar dynamicPropertyRegistrar(RedisClusterContainer redisClusterContainer) {
List<String> list =
Arrays.stream(redisClusterContainer.getRedisURIs())
.map(s -> s.substring(8))
.toList();
dynamicPropertyRegistry.add("spring.data.redis.cluster.nodes", () -> list);
return redisClusterContainer;
return registry -> {
registry.add("spring.data.redis.cluster.nodes", () -> list);
};
}

@Bean
@ServiceConnection("redis")
@Profile(AppConstants.PROFILE_NOT_CLUSTER)
RedisContainer redisContainer() {
return new RedisContainer(DockerImageName.parse("redis").withTag("7.4.0-alpine"));
return new RedisContainer(DockerImageName.parse("redis").withTag("7.4.1-alpine"));
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.example.ultimateredis.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.example.ultimateredis.common.AbstractIntegrationTest;
import com.example.ultimateredis.model.AddRedisRequest;
import com.example.ultimateredis.model.GenericResponse;
import java.time.Duration;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
Expand All @@ -24,22 +21,30 @@ class RedisControllerTest extends AbstractIntegrationTest {
@Order(1)
void addRedisKeyValue() throws Exception {
AddRedisRequest addRedisRequest = new AddRedisRequest("junit", "JunitValue", 1);
this.mockMvc
.perform(
post("/v1/redis/add")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(addRedisRequest)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.response", is(Boolean.TRUE)));
this.mockMvcTester
.post()
.uri("/v1/redis/add")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(addRedisRequest))
.assertThat()
.hasStatus(HttpStatus.CREATED)
.bodyJson()
.convertTo(GenericResponse.class)
.satisfies(response -> assertThat(response.response()).isEqualTo(true));
}

@Test
@Order(2)
void getFromCache() throws Exception {
this.mockMvc
.perform(get("/v1/redis").param("key", "junit"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response", is("JunitValue")));
void getFromCache() {
this.mockMvcTester
.get()
.uri("/v1/redis")
.param("key", "junit")
.assertThat()
.hasStatusOk()
.bodyJson()
.convertTo(GenericResponse.class)
.satisfies(response -> assertThat(response.response()).isEqualTo("JunitValue"));
}

@Test
Expand All @@ -50,9 +55,16 @@ void expireFromCache() {
.atMost(Duration.ofSeconds(70))
.untilAsserted(
() ->
this.mockMvc
.perform(get("/v1/redis").param("key", "junit"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response", nullValue())));
this.mockMvcTester
.get()
.uri("/v1/redis")
.param("key", "junit")
.assertThat()
.hasStatusOk()
.bodyJson()
.convertTo(GenericResponse.class)
.satisfies(
response ->
assertThat(response.response()).isNull()));
}
}
Loading