Fix ListenerErrorTest

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
diff --git a/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/LevelFiveService.java b/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/LevelFiveService.java
index fa407da..a7f5d0c 100755
--- a/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/LevelFiveService.java
+++ b/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/LevelFiveService.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,6 +16,9 @@
 
 package org.glassfish.hk2.runlevel.tests.listener;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+
 import javax.annotation.PreDestroy;
 
 import org.glassfish.hk2.runlevel.RunLevel;
@@ -29,17 +32,22 @@
  */
 @RunLevel(5)
 public class LevelFiveService {
-    private boolean preDestroyCalled = false;
-    
+
+    private final CountDownLatch latch = new CountDownLatch(1);
+    private AtomicBoolean preDestroyCalled = new AtomicBoolean(false);
+
     @SuppressWarnings("unused")
     @PreDestroy
     private void preDestroy() {
-        preDestroyCalled = true;
-        
+        preDestroyCalled.set(true);
+        latch.countDown();
     }
     
     /* package */ boolean isPreDestroyCalled() {
-        return preDestroyCalled;
+        return preDestroyCalled.get();
     }
 
+    /* package */ CountDownLatch latch() {
+        return latch;
+    }
 }
diff --git a/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/ListenerErrorTest.java b/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/ListenerErrorTest.java
index 7aadbb8..138c414 100755
--- a/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/ListenerErrorTest.java
+++ b/hk2-runlevel/src/test/java/org/glassfish/hk2/runlevel/tests/listener/ListenerErrorTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,6 +16,8 @@
 
 package org.glassfish.hk2.runlevel.tests.listener;
 
+import java.util.concurrent.TimeUnit;
+
 import org.glassfish.hk2.api.Descriptor;
 import org.glassfish.hk2.api.MultiException;
 import org.glassfish.hk2.api.ServiceLocator;
@@ -165,9 +167,10 @@
     /**
      * Ensures the user can halt the downward level progression if a service
      * failed when going down
+     * @throws InterruptedException 
      */
     @Test
-    public void testHaltLevelRegressionOnError() {
+    public void testHaltLevelRegressionOnError() throws InterruptedException {
         ServiceLocator locator = Utilities.getServiceLocator(LevelFiveDownErrorService.class,
                 LevelFiveService.class,
                 OnProgressLevelChangerListener.class);
@@ -192,16 +195,19 @@
         OnProgressLevelChangerListener listener = locator.getService(OnProgressLevelChangerListener.class);
         
         Assert.assertEquals(4, listener.getLatestOnProgress());
-        
+
+        levelFiveService.latch().await(100, TimeUnit.MILLISECONDS);
+
         Assert.assertTrue(levelFiveService.isPreDestroyCalled());
     }
     
     /**
      * Ensures the user can halt the downward level progression if a service
      * failed when going down
+     * @throws InterruptedException 
      */
     @Test
-    public void testHaltLevelRegressionOnErrorNoThreads() {
+    public void testHaltLevelRegressionOnErrorNoThreads() throws InterruptedException {
         ServiceLocator locator = Utilities.getServiceLocator(LevelFiveDownErrorService.class,
                 LevelFiveService.class,
                 OnProgressLevelChangerListener.class);
@@ -228,6 +234,8 @@
         
         Assert.assertEquals(4, listener.getLatestOnProgress());
         
+        levelFiveService.latch().await(100, TimeUnit.MILLISECONDS);
+
         Assert.assertTrue(levelFiveService.isPreDestroyCalled());
     }