@@ -45,18 +45,19 @@ class ConseqExecutorTest {
45
45
@ Test
46
46
void exceptionallyCompletedSubmitShouldNotStopOtherTaskExecution () {
47
47
List <Future <SpyingTask >> resultFutures ;
48
- ConseqExecutor conseqExecutor = ConseqExecutor .instance ();
49
48
List <SpyingTask > tasks = TestUtils .createRunnableSpyingTasks (TASK_COUNT );
50
49
UUID sameSequenceKey = UUID .randomUUID ();
51
50
resultFutures = new ArrayList <>();
52
51
int cancelTaskIdx = 1 ;
53
52
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 );
58
60
}
59
- resultFutures .add (taskFuture );
60
61
}
61
62
int cancelledCount = TestUtils .cancellationCount (resultFutures );
62
63
int normalCompleteCount = TestUtils .normalCompletionCount (resultFutures );
@@ -79,21 +80,23 @@ void executeRunsAllTasksOfSameSequenceKeyInSequence() {
79
80
80
81
@ Test
81
82
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
+ }
87
89
}
88
90
89
91
@ Test
90
92
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
+ }
97
100
}
98
101
99
102
@ Test
@@ -103,13 +106,14 @@ void provideConcurrencyAmongDifferentSequenceKeys() {
103
106
long sameKeyStartTimeMillis ;
104
107
long sameKeyEndTimeMillis ;
105
108
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
+ }
113
117
114
118
awaitAllComplete (sameTasks );
115
119
long differentKeysEndTimeMillis = System .currentTimeMillis ();
@@ -123,10 +127,11 @@ void submitConcurrencyBoundedByMaxConcurrency() {
123
127
int maxConcurrency = 10 ;
124
128
int taskCount = maxConcurrency * 2 ;
125
129
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
+ }
130
135
final long actualThreadCount = TestUtils .actualExecutionThreadCountIfAllCompleteNormal (futures );
131
136
assertEquals (taskCount , actualThreadCount );
132
137
assertEquals (0 , actualThreadCount % maxConcurrency );
@@ -135,10 +140,11 @@ void submitConcurrencyBoundedByMaxConcurrency() {
135
140
@ Test
136
141
void submitConcurrencyBoundedByTotalTaskCount () {
137
142
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
+ }
142
148
final long actualThreadCount = TestUtils .actualExecutionThreadCountIfAllCompleteNormal (futures );
143
149
assertTrue (actualThreadCount <= TASK_COUNT );
144
150
}
0 commit comments