1010import java .util .Arrays ;
1111import java .util .Collection ;
1212import java .util .List ;
13+ import java .util .stream .Collectors ;
1314
1415import org .jboss .shrinkwrap .api .ShrinkWrap ;
1516import org .jboss .shrinkwrap .api .asset .StringAsset ;
2526import io .quarkus .redis .devservices .it .PlainQuarkusTest ;
2627import io .quarkus .test .ContinuousTestingTestUtils ;
2728import 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+ */
3036public 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 }
0 commit comments