5252import java .util .Properties ;
5353import java .util .Random ;
5454import java .util .concurrent .TimeUnit ;
55+ import java .util .concurrent .atomic .AtomicReference ;
5556
5657import static org .junit .Assert .*;
5758/**
@@ -130,7 +131,7 @@ protected static void stopContainer(ContainerInfo info) throws Exception {
130131 protected static short currentPortNumber = BASE_PORT_NUMBER ;
131132
132133 private static List <DependencyContainer > dependencyImages = new ArrayList <>();
133- protected static ContainerInfo currentContainerInfo = null ;
134+ protected static AtomicReference < ContainerInfo > currentContainerInfo = new AtomicReference <>() ;
134135 protected static Deque <ContainerInfo > allContainers = new ArrayDeque <>();
135136 protected static String currentImageName ;
136137 protected static short appServerPort ;
@@ -193,7 +194,7 @@ protected void starting(Description description) {
193194 @ Override
194195 protected void failed (Throwable t , Description description ) {
195196 // NOTE this happens after @After :)
196- String containerId = currentContainerInfo .getContainerId ();
197+ String containerId = currentContainerInfo .get (). getContainerId ();
197198 System .out .println ("Test failure detected." );
198199 printContainerLogs (containerId );
199200 }
@@ -230,13 +231,14 @@ public static void configureShutdownHook() {
230231 Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
231232 @ Override
232233 public void run () {
233- if (currentContainerInfo == null ) {
234+ final ContainerInfo containerInfo = currentContainerInfo .get ();
235+ if (containerInfo == null ) {
234236 return ;
235237 }
236238 try {
237- stopContainer (currentContainerInfo );
239+ stopContainer (containerInfo );
238240 } catch (Exception e ) {
239- System .err .println ("Error while stopping container id=" +currentContainerInfo .getContainerId ()+". This must be stopped manually." );
241+ System .err .println ("Error while stopping container id=" +containerInfo .getContainerId ()+". This must be stopped manually." );
240242 e .printStackTrace ();
241243 }
242244 }
@@ -290,19 +292,20 @@ protected void finished(Description description) {
290292 public static void configureEnvironment (final String appServer , final String os , final String jreVersion ) throws Exception {
291293 System .out .println ("Preparing environment..." );
292294 try {
293- if (currentContainerInfo != null ) {
295+ final ContainerInfo containerInfo = currentContainerInfo .get ();
296+ if (containerInfo != null ) {
294297 // test cleanup didn't take...try to clean up
295- if (docker .isContainerRunning (currentContainerInfo .getContainerId ())) {
296- System .err .println ("From last test run, container is still running: " + currentContainerInfo );
298+ if (docker .isContainerRunning (containerInfo .getContainerId ())) {
299+ System .err .println ("From last test run, container is still running: " + containerInfo );
297300 try {
298- docker .stopContainer (currentContainerInfo .getContainerId ());
301+ docker .stopContainer (containerInfo .getContainerId ());
299302 } catch (Exception e ) {
300303 System .err .println ("Couldn't clean up environment. Must be done manually." );
301304 throw e ;
302305 }
303306 } else {
304307 // container must have stopped after timeout reached.
305- currentContainerInfo = null ;
308+ currentContainerInfo . set ( null ) ;
306309 }
307310 }
308311 checkParams (appServer , os , jreVersion );
@@ -340,10 +343,11 @@ protected static String getBaseUrl() {
340343 }
341344
342345 protected static void waitForApplicationToStart () throws Exception {
346+ final ContainerInfo containerInfo = currentContainerInfo .get ();
343347 try {
344348 System .out .printf ("Test app health check: Waiting for %s to start...%n" , warFileName );
345349 waitForUrlWithRetries (getBaseUrl (), APPLICATION_READY_TIMEOUT_SECONDS , TimeUnit .SECONDS ,
346- String .format ("%s on %s" , getAppContext (), currentContainerInfo .getImageName ()),
350+ String .format ("%s on %s" , getAppContext (), containerInfo .getImageName ()),
347351 HEALTH_CHECK_RETRIES );
348352 System .out .println ("Test app health check complete." );
349353 if (requestCaptureEnabled ) {
@@ -359,7 +363,7 @@ public boolean apply(Envelope input) {
359363 System .out .println ("Clearing any RequestData from health check." );
360364 }
361365 } catch (Exception e ) {
362- docker .printContainerLogs (currentContainerInfo .getContainerId ());
366+ docker .printContainerLogs (containerInfo .getContainerId ());
363367 throw e ;
364368 }
365369 mockedIngestion .resetData ();
@@ -425,7 +429,11 @@ public boolean apply(@Nullable Envelope input) {
425429 if (deviceId == null ) {
426430 return true ;
427431 }
428- final boolean belongsToCurrentContainer = currentContainerInfo .getContainerId ().startsWith (deviceId );
432+ final ContainerInfo containerInfo = currentContainerInfo .get ();
433+ if (containerInfo == null ) { // ignore telemetry in after container is cleaned up.
434+ return false ;
435+ }
436+ final boolean belongsToCurrentContainer = containerInfo .getContainerId ().startsWith (deviceId );
429437 if (!belongsToCurrentContainer ) {
430438 System .out .println ("Telemetry from previous container" );
431439 }
@@ -517,25 +525,26 @@ private static void startTestApplicationContainer() throws Exception {
517525 assertFalse ("'containerId' was null/empty attempting to start container: " +currentImageName , Strings .isNullOrEmpty (containerId ));
518526 System .out .printf ("Container started: %s (%s)%n" , currentImageName , containerId );
519527
520- currentContainerInfo = new ContainerInfo (containerId , currentImageName );
528+ final ContainerInfo containerInfo = new ContainerInfo (containerId , currentImageName );
529+ currentContainerInfo .set (containerInfo );
521530 try {
522531 String url = String .format ("http://localhost:%s/" , String .valueOf (appServerPort ));
523532 System .out .printf ("Verifying appserver has started (%s)...%n" , url );
524- allContainers .push (currentContainerInfo );
533+ allContainers .push (containerInfo );
525534 waitForUrlWithRetries (url , APPSERVER_HEALTH_CHECK_TIMEOUT , TimeUnit .SECONDS , String .format ("app server on image '%s'" , currentImageName ), HEALTH_CHECK_RETRIES );
526535 System .out .println ("App server is ready." );
527536 }
528537 catch (Exception e ) {
529538 System .err .println ("Error starting app server" );
530- if (docker .isContainerRunning (currentContainerInfo .getContainerId ())) {
539+ if (docker .isContainerRunning (containerInfo .getContainerId ())) {
531540 System .out .println ("Container is not running." );
532- allContainers .remove (currentContainerInfo );
541+ allContainers .remove (containerInfo );
533542 } else {
534543 System .out .println ("Yet, the container is running." );
535544 }
536545 System .out .println ("Printing container logs: " );
537546 System .out .println ("# LOGS START =========================" );
538- docker .printContainerLogs (currentContainerInfo .getContainerId ());
547+ docker .printContainerLogs (containerInfo .getContainerId ());
539548 System .out .println ("# LOGS END ===========================" );
540549 throw e ;
541550 }
@@ -607,9 +616,9 @@ public static void stopAllContainers() throws Exception {
607616 List <ContainerInfo > failedToStop = new ArrayList <>();
608617 while (!allContainers .isEmpty ()) {
609618 ContainerInfo c = allContainers .pop ();
610- if (currentContainerInfo == c ) {
619+ if (currentContainerInfo . get () == c ) {
611620 System .out .println ("Cleaning up app container" );
612- currentContainerInfo = null ;
621+ currentContainerInfo . set ( null ) ;
613622 }
614623 stopContainer (c );
615624 if (docker .isContainerRunning (c .getContainerId ())) {
@@ -618,11 +627,12 @@ public static void stopAllContainers() throws Exception {
618627 }
619628 }
620629
621- if (currentContainerInfo != null ) {
630+ final ContainerInfo containerInfo = currentContainerInfo .get ();
631+ if (containerInfo != null ) {
622632 System .err .println ("Could not find app container in stack. Stopping..." );
623- stopContainer (currentContainerInfo );
624- if (!docker .isContainerRunning (currentContainerInfo .getContainerId ())) {
625- currentContainerInfo = null ;
633+ stopContainer (containerInfo );
634+ if (!docker .isContainerRunning (containerInfo .getContainerId ())) {
635+ currentContainerInfo . set ( null ) ;
626636 }
627637 }
628638
0 commit comments