Replace sleep with PollingConditions
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / executor / CpsNcmpTaskExecutorSpec.groovy
index 5559016..5f62aca 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2023 Nordix Foundation
+ *  Copyright (C) 2023-2024 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.AfterEach
 import org.junit.jupiter.api.BeforeEach
 import org.slf4j.LoggerFactory
 import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
 
 class CpsNcmpTaskExecutorSpec extends Specification {
 
@@ -37,23 +38,23 @@ class CpsNcmpTaskExecutorSpec extends Specification {
 
     @BeforeEach
     void setup() {
-        ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger);
-        logger.start();
+        ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).addAppender(logger)
+        logger.start()
     }
 
     @AfterEach
     void teardown() {
-        ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).detachAndStopAllAppenders();
+        ((Logger) LoggerFactory.getLogger(CpsNcmpTaskExecutor.class)).detachAndStopAllAppenders()
     }
 
     def 'Execute successful task.'() {
         when: 'task is executed'
             objectUnderTest.executeTask(taskSupplier(), enoughTime)
-        and: 'wait a little for async execution completion'
-            Thread.sleep(10)
         then: 'an event is logged with level INFO'
-            def loggingEvent = getLoggingEvent()
-            assert loggingEvent.level == Level.INFO
+            new PollingConditions().within(1) {
+                def loggingEvent = getLoggingEvent()
+                assert loggingEvent.level == Level.INFO
+            }
         and: 'the log indicates the task completed successfully'
             assert loggingEvent.formattedMessage == 'Async task completed successfully.'
     }
@@ -61,11 +62,11 @@ class CpsNcmpTaskExecutorSpec extends Specification {
     def 'Execute failing task.'() {
         when: 'task is executed'
             objectUnderTest.executeTask(taskSupplierForFailingTask(), enoughTime)
-        and: 'wait a little for async execution completion'
-            Thread.sleep(10)
         then: 'an event is logged with level ERROR'
-            def loggingEvent = getLoggingEvent()
-            assert loggingEvent.level == Level.ERROR
+            new PollingConditions().within(1) {
+                def loggingEvent = getLoggingEvent()
+                assert loggingEvent.level == Level.ERROR
+            }
         and: 'the original error message is logged'
             assert loggingEvent.formattedMessage.contains('original exception message')
     }