forked from apache/activemq-artemis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderedExecutorSanityTest.java
250 lines (215 loc) · 9.15 KB
/
OrderedExecutorSanityTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.utils.actors;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandles;
import org.junit.Assert;
import org.junit.Test;
public class OrderedExecutorSanityTest {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Test
public void shouldExecuteTasksInOrder() throws InterruptedException {
final int threads = 3;
final int tasks = 100;
final long timeoutMillis = TimeUnit.SECONDS.toMillis(10);
final ExecutorService executorService = Executors.newFixedThreadPool(threads);
try {
final ArtemisExecutor executor = new OrderedExecutor(executorService);
//it can be not thread safe too
final List<Integer> results = new ArrayList<>(tasks);
final List<Integer> expectedResults = new ArrayList<>(tasks);
final CountDownLatch executed = new CountDownLatch(tasks);
for (int i = 0; i < tasks; i++) {
final int value = i;
executor.execute(() -> {
results.add(value);
executed.countDown();
});
expectedResults.add(value);
}
Assert.assertTrue("The tasks must be executed in " + timeoutMillis + " ms", executed.await(timeoutMillis, TimeUnit.MILLISECONDS));
Assert.assertArrayEquals("The processing of tasks must be ordered", expectedResults.toArray(), results.toArray());
} finally {
executorService.shutdown();
}
}
@Test
public void shouldShutdownNowDoNotExecuteFurtherTasks() throws InterruptedException {
final long timeoutMillis = TimeUnit.SECONDS.toMillis(10);
final ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
final OrderedExecutor executor = new OrderedExecutor(executorService);
final CountDownLatch executed = new CountDownLatch(1);
executor.execute(executed::countDown);
Assert.assertTrue("The task must be executed in " + timeoutMillis + " ms", executed.await(timeoutMillis, TimeUnit.MILLISECONDS));
executor.shutdownNow();
Assert.assertEquals("There are no remaining tasks to be executed", 0, executor.remaining());
//from now on new tasks won't be executed
executor.execute(() -> System.out.println("this will never happen"));
//to avoid memory leaks the executor must take care of the new submitted tasks immediatly
Assert.assertEquals("Any new task submitted after death must be collected", 0, executor.remaining());
} finally {
executorService.shutdown();
}
}
@Test
public void shutdownNowOnDelegateExecutor() throws Exception {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
final OrderedExecutor executor = new OrderedExecutor(executorService);
final CyclicBarrier latch = new CyclicBarrier(2);
final AtomicInteger numberOfTasks = new AtomicInteger(0);
final CountDownLatch ran = new CountDownLatch(1);
executor.execute(() -> {
try {
latch.await(1, TimeUnit.MINUTES);
numberOfTasks.set(executor.shutdownNow());
ran.countDown();
} catch (Exception e) {
e.printStackTrace();
}
});
for (int i = 0; i < 100; i++) {
executor.execute(() -> System.out.println("Dont worry, this will never happen"));
}
latch.await();
ran.await(1, TimeUnit.SECONDS);
Assert.assertEquals(100, numberOfTasks.get());
Assert.assertEquals(ProcessorBase.STATE_FORCED_SHUTDOWN, executor.status());
Assert.assertEquals(0, executor.remaining());
} finally {
executorService.shutdown();
}
}
@Test
public void shutdownNowWithBlocked() throws Exception {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
final OrderedExecutor executor = new OrderedExecutor(executorService);
final CyclicBarrier latch = new CyclicBarrier(2);
final CyclicBarrier secondlatch = new CyclicBarrier(2);
final CountDownLatch ran = new CountDownLatch(1);
executor.execute(() -> {
try {
latch.await(1, TimeUnit.MINUTES);
secondlatch.await(1, TimeUnit.MINUTES);
ran.countDown();
} catch (Exception e) {
e.printStackTrace();
}
});
for (int i = 0; i < 100; i++) {
executor.execute(() -> System.out.println("Dont worry, this will never happen"));
}
latch.await();
try {
Assert.assertEquals(100, executor.shutdownNow());
} finally {
secondlatch.await();
}
Assert.assertEquals(ProcessorBase.STATE_FORCED_SHUTDOWN, executor.status());
Assert.assertEquals(0, executor.remaining());
} finally {
executorService.shutdown();
}
}
@Test
public void testMeasure() throws InterruptedException {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
final OrderedExecutor executor = new OrderedExecutor(executorService);
int MAX_LOOP = 1_000_000;
// extend the number for longer numbers
int runs = 10;
for (int i = 0; i < runs; i++) {
long start = System.nanoTime();
final CountDownLatch executed = new CountDownLatch(MAX_LOOP);
for (int l = 0; l < MAX_LOOP; l++) {
executor.execute(executed::countDown);
}
Assert.assertTrue(executed.await(1, TimeUnit.MINUTES));
long end = System.nanoTime();
long elapsed = (end - start);
log.info("execution {} in {} milliseconds", i, TimeUnit.NANOSECONDS.toMillis(elapsed));
}
} finally {
executorService.shutdown();
}
}
@Test
public void testFair() throws InterruptedException {
AtomicInteger errors = new AtomicInteger(0);
final ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
final ArtemisExecutor executor = new OrderedExecutor(executorService).setFair(true);
final ArtemisExecutor executor2 = new OrderedExecutor(executorService).setFair(true);
CountDownLatch latchDone1 = new CountDownLatch(1);
CountDownLatch latchBlock1 = new CountDownLatch(1);
CountDownLatch latchDone2 = new CountDownLatch(1);
CountDownLatch latchDone3 = new CountDownLatch(1);
CountDownLatch latchBlock3 = new CountDownLatch(1);
executor.execute(() -> {
try {
log.info("Exec 1");
latchDone1.countDown();
latchBlock1.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
}
});
Assert.assertTrue(latchDone1.await(10, TimeUnit.SECONDS));
executor.execute(() -> {
try {
// Exec 2 is supposed to yield to Exec3, so Exec3 will happen first
log.info("Exec 2");
latchDone2.countDown();
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
}
});
executor2.execute(() -> {
try {
log.info("Exec 3");
latchDone3.countDown();
latchBlock3.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
}
});
latchBlock1.countDown();
Assert.assertTrue(latchDone3.await(10, TimeUnit.SECONDS));
Assert.assertFalse(latchDone2.await(1, TimeUnit.MILLISECONDS));
latchBlock3.countDown();
Assert.assertTrue(latchDone2.await(10, TimeUnit.SECONDS));
Assert.assertEquals(0, errors.get());
} finally {
executorService.shutdownNow();
}
}
}