Skip to content

Commit d80a848

Browse files
Merge pull request #48203 from holly-cummins/more-redis-devservices-tests
Add more test coverage for Redis dev services
2 parents 1d88758 + 968eff8 commit d80a848

File tree

2 files changed

+97
-9
lines changed

2 files changed

+97
-9
lines changed

integration-tests/redis-devservices/src/test/java/io/quarkus/redis/devservices/continuoustesting/it/DevServicesRedisContinuousTestingTest.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Arrays;
1111
import java.util.Collection;
1212
import java.util.List;
13+
import java.util.stream.Collectors;
1314

1415
import org.jboss.shrinkwrap.api.ShrinkWrap;
1516
import org.jboss.shrinkwrap.api.asset.StringAsset;
@@ -25,8 +26,13 @@
2526
import io.quarkus.redis.devservices.it.PlainQuarkusTest;
2627
import io.quarkus.test.ContinuousTestingTestUtils;
2728
import io.quarkus.test.QuarkusDevModeTest;
28-
import io.quarkus.test.devservices.redis.TestResource;
29+
import io.quarkus.test.devservices.redis.BundledResource;
2930

31+
/**
32+
* Note that if this test is specifically selected on the command line with -Dtest=DevServicesRedisContinuousTestingTest, that
33+
* will override the maven executions and cause it to run twice.
34+
* That doesn't help debug anything.
35+
*/
3036
public class DevServicesRedisContinuousTestingTest {
3137

3238
static final String DEVSERVICES_DISABLED_PROPERTIES = ContinuousTestingTestUtils.appProperties(
@@ -41,7 +47,7 @@ public class DevServicesRedisContinuousTestingTest {
4147
@RegisterExtension
4248
public static QuarkusDevModeTest test = new QuarkusDevModeTest()
4349
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
44-
.addClasses(TestResource.class)
50+
.addClass(BundledResource.class)
4551
.addAsResource(new StringAsset(ContinuousTestingTestUtils.appProperties("")),
4652
"application.properties"))
4753
.setTestArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(PlainQuarkusTest.class));
@@ -65,7 +71,7 @@ public void testContinuousTestingDisablesDevServicesWhenPropertiesChange() {
6571

6672
// This tests behaviour in dev mode proper (rather than continuous testing)
6773
@Test
68-
public void testDevModeServiceConfigRefresh() {
74+
public void testDevModeServiceUpdatesContainersOnConfigChange() {
6975
List<Container> started = getRedisContainers();
7076
// Interacting with the app will force a refresh
7177
ping();
@@ -75,20 +81,64 @@ public void testDevModeServiceConfigRefresh() {
7581
assertTrue(Arrays.stream(container.getPorts()).noneMatch(p -> p.getPublicPort() == 6377),
7682
"Expected random port, but got: " + Arrays.toString(container.getPorts()));
7783

84+
int newPort = 6388;
85+
// Continuous tests and dev mode should *not* share containers, even if the port is fixed
86+
// Specify that the fixed port is for dev mode
7887
test.modifyResourceFile("application.properties",
79-
s -> ContinuousTestingTestUtils.appProperties(FIXED_PORT_PROPERTIES));
88+
s -> ContinuousTestingTestUtils.appProperties("quarkus.redis.devservices.port=" + newPort));
8089

8190
// Force another refresh
8291
ping();
8392
List<Container> newContainers = getRedisContainersExcludingExisting(started);
84-
assertEquals(1, newContainers.size()); // this can be wrong
85-
Container newContainer = newContainers.get(0);
86-
assertTrue(Arrays.stream(newContainer.getPorts()).anyMatch(p -> p.getPublicPort() == 6377),
87-
"Expected port 6377, but got: " + Arrays.toString(newContainer.getPorts()));
93+
94+
assertEquals(1, newContainers.size(),
95+
"New containers: "
96+
+ prettyPrintContainerList(newContainers)
97+
+ "\n Old containers: " + prettyPrintContainerList(started) + "\n All containers: "
98+
+ prettyPrintContainerList(getAllContainers())); // this can be wrong
99+
// We need to inspect the dev-mode container; we don't have a non-brittle way of distinguishing them, so just look in them all
100+
boolean hasRightPort = newContainers.stream()
101+
.anyMatch(newContainer -> hasPublicPort(newContainer, newPort));
102+
assertTrue(hasRightPort,
103+
"Expected port " + newPort + ", but got: "
104+
+ newContainers.stream().map(c -> Arrays.toString(c.getPorts())).collect(Collectors.joining(", ")));
105+
}
106+
107+
@Test
108+
public void testDevModeServiceDoesNotRestartContainersOnCodeChange() {
109+
List<Container> started = getRedisContainers();
110+
ping();
111+
112+
assertFalse(started.isEmpty());
113+
Container container = started.get(0);
114+
assertTrue(Arrays.stream(container.getPorts()).noneMatch(p -> p.getPublicPort() == 6377),
115+
"Expected random port 6377, but got: " + Arrays.toString(container.getPorts()));
116+
117+
// Make a change that shouldn't affect dev services
118+
test.modifySourceFile(BundledResource.class, s -> s.replaceAll("OK", "poink"));
119+
120+
ping();
121+
122+
List<Container> newContainers = getRedisContainersExcludingExisting(started);
123+
124+
// No new containers should have spawned
125+
assertEquals(0, newContainers.size(),
126+
"New containers: " + newContainers + "\n Old containers: " + started + "\n All containers: "
127+
+ getAllContainers()); // this can be wrong
128+
}
129+
130+
private static String prettyPrintContainerList(List<Container> newContainers) {
131+
return newContainers.stream()
132+
.map(c -> Arrays.toString(c.getPorts()) + "--" + Arrays.toString(c.getNames()))
133+
.collect(Collectors.joining(", "));
134+
}
135+
136+
private static boolean hasPublicPort(Container newContainer, int newPort) {
137+
return Arrays.stream(newContainer.getPorts()).anyMatch(p -> p.getPublicPort() == newPort);
88138
}
89139

90140
void ping() {
91-
when().get("/ping").then()
141+
when().get("/bundled/ping").then()
92142
.statusCode(200)
93143
.body(is("PONG"));
94144
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.quarkus.test.devservices.redis;
2+
3+
import jakarta.inject.Inject;
4+
import jakarta.ws.rs.GET;
5+
import jakarta.ws.rs.Path;
6+
import jakarta.ws.rs.PathParam;
7+
8+
import io.quarkus.redis.datasource.RedisDataSource;
9+
10+
/**
11+
* This class exists to get bundled up in the Arquillian bundle. If we want to modify it, some source has to exist in the test
12+
* source set.
13+
*/
14+
@Path("/bundled")
15+
public class BundledResource {
16+
17+
@Inject
18+
RedisDataSource redis;
19+
20+
@GET
21+
@Path("/ping")
22+
public String ping() {
23+
return redis.execute("ping").toString();
24+
}
25+
26+
@GET
27+
@Path("/set/{key}/{value}")
28+
public String set(@PathParam("key") String key, @PathParam("value") String value) {
29+
redis.value(String.class).set(key, value);
30+
return "OK";
31+
}
32+
33+
@GET
34+
@Path("/get/{key}")
35+
public String get(@PathParam("key") String key) {
36+
return redis.value(String.class).get(key);
37+
}
38+
}

0 commit comments

Comments
 (0)