@@ -45,18 +45,19 @@ class ConseqExecutorTest {
4545 @ Test
4646 void exceptionallyCompletedSubmitShouldNotStopOtherTaskExecution () {
4747 List <Future <SpyingTask >> resultFutures ;
48- ConseqExecutor conseqExecutor = ConseqExecutor .instance ();
4948 List <SpyingTask > tasks = TestUtils .createRunnableSpyingTasks (TASK_COUNT );
5049 UUID sameSequenceKey = UUID .randomUUID ();
5150 resultFutures = new ArrayList <>();
5251 int cancelTaskIdx = 1 ;
5352 for (int i = 0 ; i < TASK_COUNT ; i ++) {
54- Future <SpyingTask > taskFuture =
55- conseqExecutor .submit (tasks .get (i ).toCallable (), sameSequenceKey );
56- if (i == cancelTaskIdx ) {
57- taskFuture .cancel (true );
53+ try (ConseqExecutor conseqExecutor = ConseqExecutor .instance ()) {
54+ Future <SpyingTask > taskFuture =
55+ conseqExecutor .submit (tasks .get (i ).toCallable (), sameSequenceKey );
56+ if (i == cancelTaskIdx ) {
57+ taskFuture .cancel (true );
58+ }
59+ resultFutures .add (taskFuture );
5860 }
59- resultFutures .add (taskFuture );
6061 }
6162 int cancelledCount = TestUtils .cancellationCount (resultFutures );
6263 int normalCompleteCount = TestUtils .normalCompletionCount (resultFutures );
@@ -79,21 +80,23 @@ void executeRunsAllTasksOfSameSequenceKeyInSequence() {
7980
8081 @ Test
8182 void noExecutorLingersOnRandomSequenceKeys () {
82- ConseqExecutor sut = ConseqExecutor .instance ();
83- List <SpyingTask > tasks = TestUtils .createRunnableSpyingTasks (100 );
84- tasks .parallelStream ().forEach (t -> sut .execute (t , UUID .randomUUID ()));
85- TestUtils .awaitAllComplete (tasks );
86- await ().until (sut ::noTaskPending );
83+ try (ConseqExecutor sut = ConseqExecutor .instance ()) {
84+ List <SpyingTask > tasks = TestUtils .createRunnableSpyingTasks (100 );
85+ tasks .parallelStream ().forEach (t -> sut .execute (t , UUID .randomUUID ()));
86+ TestUtils .awaitAllComplete (tasks );
87+ await ().until (sut ::noTaskPending );
88+ }
8789 }
8890
8991 @ Test
9092 void noExecutorLingersOnSameSequenceKey () {
91- ConseqExecutor sut = ConseqExecutor .instance ();
92- UUID sameSequenceKey = UUID .randomUUID ();
93- List <SpyingTask > tasks = TestUtils .createRunnableSpyingTasks (TASK_COUNT );
94- tasks .parallelStream ().forEach (t -> sut .execute (t , sameSequenceKey ));
95- TestUtils .awaitAllComplete (tasks );
96- await ().until (sut ::noTaskPending );
93+ try (ConseqExecutor sut = ConseqExecutor .instance ()) {
94+ UUID sameSequenceKey = UUID .randomUUID ();
95+ List <SpyingTask > tasks = TestUtils .createRunnableSpyingTasks (TASK_COUNT );
96+ tasks .parallelStream ().forEach (t -> sut .execute (t , sameSequenceKey ));
97+ TestUtils .awaitAllComplete (tasks );
98+ await ().until (sut ::noTaskPending );
99+ }
97100 }
98101
99102 @ Test
@@ -103,13 +106,14 @@ void provideConcurrencyAmongDifferentSequenceKeys() {
103106 long sameKeyStartTimeMillis ;
104107 long sameKeyEndTimeMillis ;
105108 long differentKeysStartTimeMillis ;
106- ConseqExecutor sut = ConseqExecutor .instance ();
107- sameKeyStartTimeMillis = System .currentTimeMillis ();
108- sameTasks .forEach (t -> sut .execute (t , sameSequenceKey ));
109- awaitAllComplete (sameTasks );
110- sameKeyEndTimeMillis = System .currentTimeMillis ();
111- differentKeysStartTimeMillis = System .currentTimeMillis ();
112- sameTasks .forEach (t -> sut .execute (t , UUID .randomUUID ()));
109+ try (ConseqExecutor sut = ConseqExecutor .instance ()) {
110+ sameKeyStartTimeMillis = System .currentTimeMillis ();
111+ sameTasks .forEach (t -> sut .execute (t , sameSequenceKey ));
112+ awaitAllComplete (sameTasks );
113+ sameKeyEndTimeMillis = System .currentTimeMillis ();
114+ differentKeysStartTimeMillis = System .currentTimeMillis ();
115+ sameTasks .forEach (t -> sut .execute (t , UUID .randomUUID ()));
116+ }
113117
114118 awaitAllComplete (sameTasks );
115119 long differentKeysEndTimeMillis = System .currentTimeMillis ();
@@ -123,10 +127,11 @@ void submitConcurrencyBoundedByMaxConcurrency() {
123127 int maxConcurrency = 10 ;
124128 int taskCount = maxConcurrency * 2 ;
125129 List <Future <SpyingTask >> futures ;
126- ConseqExecutor conseqExecutor = ConseqExecutor .instance (maxConcurrency );
127- futures = TestUtils .createRunnableSpyingTasks (taskCount ).stream ()
128- .map (task -> conseqExecutor .submit (task .toCallable (), UUID .randomUUID ()))
129- .toList ();
130+ try (ConseqExecutor conseqExecutor = ConseqExecutor .instance (maxConcurrency )) {
131+ futures = TestUtils .createRunnableSpyingTasks (taskCount ).stream ()
132+ .map (task -> conseqExecutor .submit (task .toCallable (), UUID .randomUUID ()))
133+ .toList ();
134+ }
130135 final long actualThreadCount = TestUtils .actualExecutionThreadCountIfAllCompleteNormal (futures );
131136 assertEquals (taskCount , actualThreadCount );
132137 assertEquals (0 , actualThreadCount % maxConcurrency );
@@ -135,10 +140,11 @@ void submitConcurrencyBoundedByMaxConcurrency() {
135140 @ Test
136141 void submitConcurrencyBoundedByTotalTaskCount () {
137142 List <Future <SpyingTask >> futures ;
138- ConseqExecutor conseqExecutor = ConseqExecutor .instance (TASK_COUNT * 10 );
139- futures = TestUtils .createRunnableSpyingTasks (TASK_COUNT ).stream ()
140- .map (task -> conseqExecutor .submit (task .toCallable (), UUID .randomUUID ()))
141- .toList ();
143+ try (ConseqExecutor conseqExecutor = ConseqExecutor .instance (TASK_COUNT * 10 )) {
144+ futures = TestUtils .createRunnableSpyingTasks (TASK_COUNT ).stream ()
145+ .map (task -> conseqExecutor .submit (task .toCallable (), UUID .randomUUID ()))
146+ .toList ();
147+ }
142148 final long actualThreadCount = TestUtils .actualExecutionThreadCountIfAllCompleteNormal (futures );
143149 assertTrue (actualThreadCount <= TASK_COUNT );
144150 }
0 commit comments