1010import io .kestra .core .queues .TestQueueFactory ;
1111import io .kestra .core .repositories .ExecutionRepositoryInterface ;
1212import io .kestra .core .runners .TestRunner ;
13+ import io .kestra .core .utils .ListUtils ;
1314import io .kestra .core .utils .TestsUtils ;
14- import io .micronaut .core .annotation .NonNull ;
15- import io .micronaut .inject .BeanDefinition ;
1615import io .micronaut .inject .qualifiers .Qualifiers ;
1716import io .micronaut .test .annotation .MicronautTestValue ;
1817import io .micronaut .test .extensions .junit5 .MicronautJunit5Extension ;
1918import lombok .extern .slf4j .Slf4j ;
2019import org .junit .jupiter .api .extension .ExtensionContext ;
2120import org .junit .platform .commons .support .AnnotationSupport ;
2221
23- import java .util .ArrayList ;
22+ import java .util .Collections ;
2423import java .util .ConcurrentModificationException ;
2524import java .util .List ;
25+ import java .util .Optional ;
2626
2727@ Slf4j
2828public class KestraTestExtension extends MicronautJunit5Extension {
29- public static final ThreadLocal <List <Execution >> testExecutions = ThreadLocal .withInitial (ArrayList ::new );
30-
3129 private static final ExtensionContext .Namespace NAMESPACE = ExtensionContext .Namespace .create (KestraTestExtension .class );
3230
3331 @ Override
@@ -77,14 +75,6 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
7775 }
7876 }
7977
80- @ Override
81- public void beforeTestExecution (ExtensionContext context ) {
82- if (applicationContext .containsBean (TestQueueFactory .class )) {
83- TestQueueFactory testQueueFactory = applicationContext .getBean (TestQueueFactory .class );
84- testQueueFactory .setTestExecutionsList (testExecutions .get ());
85- }
86- }
87-
8878 @ Override
8979 public void afterTestExecution (ExtensionContext context ) throws Exception {
9080 super .afterTestExecution (context );
@@ -94,24 +84,27 @@ public void afterTestExecution(ExtensionContext context) throws Exception {
9484 KestraTest kestraTest = context .getTestClass ()
9585 .orElseThrow ()
9686 .getAnnotation (KestraTest .class );
97- if (!testExecutions .get ().isEmpty () &&
98- kestraTest .startRunner ()
87+ Optional <TestQueueFactory > testQueueFactory = Optional .of (applicationContext .containsBean (TestQueueFactory .class )).flatMap (contains -> contains ? Optional .of (applicationContext .getBean (TestQueueFactory .class )) : Optional .empty ());
88+ List <Execution > testExecutions = testQueueFactory .map (TestQueueFactory ::getTestExecutions ).orElse (Collections .emptyList ());
89+ if (!testExecutions .isEmpty ()
9990 && applicationContext .containsBean (ExecutionRepositoryInterface .class )
10091 && applicationContext .containsBean (QueueInterface .class , Qualifiers .byName (QueueFactoryInterface .KILL_NAMED ))) {
10192 ExecutionRepositoryInterface executionRepository = applicationContext .getBean (ExecutionRepositoryInterface .class );
10293 QueueInterface <ExecutionKilled > killQueue = applicationContext .getBean (QueueInterface .class , Qualifiers .byName (QueueFactoryInterface .KILL_NAMED ));
10394
104- retryingExecutionKill (executionRepository , killQueue , 10 );
95+ retryingExecutionKill (testExecutions , executionRepository , killQueue , 10 );
10596
106- testExecutions .get (). clear ();
97+ testExecutions .clear ();
10798 }
10899 }
109100
110- private void retryingExecutionKill (ExecutionRepositoryInterface executionRepository , QueueInterface <ExecutionKilled > killQueue , int retriesLeft ) throws InterruptedException {
101+
102+ private void retryingExecutionKill (List <Execution > testExecutions , ExecutionRepositoryInterface executionRepository , QueueInterface <ExecutionKilled > killQueue , int retriesLeft ) throws InterruptedException {
111103 try {
112- testExecutions .get ().stream ()
113- .flatMap (launchedExecution -> executionRepository .findById (launchedExecution .getTenantId (), launchedExecution .getId ()).stream ())
114- .filter (inRepository -> inRepository .getState ().isRunning () || inRepository .getState ().isPaused () || inRepository .getState ().isQueued ())
104+ ListUtils .distinctByKey (
105+ testExecutions .stream ().flatMap (launchedExecution -> executionRepository .findById (launchedExecution .getTenantId (), launchedExecution .getId ()).stream ()).toList (),
106+ Execution ::getId
107+ ).stream ().filter (inRepository -> inRepository .getState ().isRunning () || inRepository .getState ().isPaused () || inRepository .getState ().isQueued ())
115108 .forEach (inRepository -> {
116109 log .warn ("Execution {} is still running after test execution, killing it" , inRepository .getId ());
117110 try {
@@ -133,7 +126,7 @@ private void retryingExecutionKill(ExecutionRepositoryInterface executionReposit
133126 return ;
134127 }
135128 Thread .sleep (100 );
136- retryingExecutionKill (executionRepository , killQueue , retriesLeft - 1 );
129+ retryingExecutionKill (testExecutions , executionRepository , killQueue , retriesLeft - 1 );
137130 }
138131 }
139132}
0 commit comments