forked from linkedin/parseq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestEngine.java
More file actions
239 lines (210 loc) · 7.57 KB
/
TestEngine.java
File metadata and controls
239 lines (210 loc) · 7.57 KB
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
/*
* Copyright 2012 LinkedIn, Inc
*
* Licensed 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 com.linkedin.parseq;
import com.linkedin.parseq.promise.Promise;
import com.linkedin.parseq.promise.Promises;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.linkedin.parseq.TestUtil.withDisabledLogging;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
/**
* @author Chris Pettitt
*/
public class TestEngine
{
private Engine _engine;
private ScheduledExecutorService _scheduler;
@BeforeMethod
public void setUp() throws Exception
{
final int numCores = Runtime.getRuntime().availableProcessors();
_scheduler = Executors.newScheduledThreadPool(numCores + 1);
_engine = new EngineBuilder()
.setTaskExecutor(_scheduler)
.setTimerScheduler(_scheduler)
.build();
}
@AfterMethod
public void tearDown() throws Exception
{
_engine.shutdown();
_engine.awaitTermination(50, TimeUnit.MILLISECONDS);
_engine = null;
_scheduler.shutdownNow();
_scheduler = null;
}
@Test
public void testShutdownWithNoTasks() throws InterruptedException
{
_engine.shutdown();
assertTrue(_engine.isShutdown());
assertTrue(_engine.awaitTermination(50, TimeUnit.MILLISECONDS));
assertTrue(_engine.isTerminated());
assertTrue(_engine.isShutdown());
}
@Test
public void testShutdownThenRunTask() throws InterruptedException
{
_engine.shutdown();
final Task<String> task = TestUtil.value("task executed");
_engine.run(task);
// Task should be cancelled immediately
assertTrue(task.await(50, TimeUnit.MILLISECONDS));
assertTrue(task.isFailed());
}
@Test
public void testShutdownWithRunningTask() throws InterruptedException
{
final CountDownLatch finishLatch = new CountDownLatch(1);
final String taskValue = "task executed";
final Task<String> task = new BaseTask<String>()
{
@Override
protected Promise<? extends String> run(final Context context) throws Exception
{
finishLatch.await();
return Promises.value(taskValue);
}
};
_engine.run(task);
_engine.shutdown();
// shutdown should not complete until after our task is done
assertFalse(_engine.awaitTermination(50, TimeUnit.MILLISECONDS));
assertTrue(_engine.isShutdown());
assertFalse(_engine.isTerminated());
finishLatch.countDown();
assertTrue(_engine.awaitTermination(50, TimeUnit.MILLISECONDS));
assertTrue(_engine.isShutdown());
assertTrue(_engine.isTerminated());
// Task should finish shortly
assertTrue(task.await(50, TimeUnit.MILLISECONDS));
assertEquals(taskValue, task.get());
}
@Test
public void testShutdownWithRunningAndSuccessorTask() throws InterruptedException
{
final CountDownLatch finishLatch = new CountDownLatch(1);
final String predValue = "task executed";
final String sucValue = "task executed";
final Task<String> predTask = new BaseTask<String>()
{
@Override
protected Promise<? extends String> run(final Context context) throws Exception
{
finishLatch.await();
return Promises.value(predValue);
}
};
final Task<String> sucTask = TestUtil.value(sucValue);
final Task<String> seq = Tasks.seq(predTask, sucTask);
_engine.run(seq);
_engine.shutdown();
// shutdown should not complete until after our task is done
assertFalse(_engine.awaitTermination(50, TimeUnit.MILLISECONDS));
assertTrue(_engine.isShutdown());
assertFalse(_engine.isTerminated());
finishLatch.countDown();
assertTrue(_engine.awaitTermination(50, TimeUnit.MILLISECONDS));
assertTrue(_engine.isShutdown());
assertTrue(_engine.isTerminated());
// Tasks should finish shortly
assertTrue(predTask.await(50, TimeUnit.MILLISECONDS));
assertEquals(predValue, predTask.get());
assertTrue(sucTask.await(50, TimeUnit.MILLISECONDS));
assertEquals(sucValue, sucTask.get());
}
@Test
public void testFailPlanExecution() throws InterruptedException
{
// This test ensures that if execution of a plan's serial executor loop
// fails, e.g. in the case that the underlying executor is saturated, that
// we fail the plan. To simplify this test, we constructor our own executor
// instead of using the default executor set up for test.
final ExecutorService executorService = new ThreadPoolExecutor(1, 1,
0, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.AbortPolicy());
final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
try
{
final Engine engine = new EngineBuilder()
.setTaskExecutor(executorService)
.setTimerScheduler(scheduledExecutorService)
.build();
// First we submit two tasks that will never finish. This saturates the
// underlying executor by using its only thread and saturating its
// single slot queue.
engine.run(neverEndingBlockingTask());
engine.run(neverEndingBlockingTask());
// Now we submit another task. The execution loop for this task will fail
// during submit to the underlying executor. We expect that it will be
// cancelled.
final Task<?> task = neverEndingBlockingTask();
withDisabledLogging(new Runnable()
{
@Override
public void run()
{
engine.run(task);
try
{
assertTrue(task.await(5, TimeUnit.SECONDS));
}
catch (InterruptedException e)
{
// Ignore.
}
}
});
assertTrue(task.isFailed());
assertTrue("Expected underlying exception to be instance of RejectedExecutionException, but was: " + task.getError().getCause(),
task.getError().getCause() instanceof RejectedExecutionException);
engine.shutdown();
}
finally
{
scheduledExecutorService.shutdownNow();
executorService.shutdownNow();
}
}
/**
* A task that blocks forever when it is executed, tying up whatever thread
* executes it.
*/
private Task<?> neverEndingBlockingTask()
{
return new BaseTask<Object>()
{
@Override
protected Promise<?> run(Context context) throws Throwable
{
new CountDownLatch(1).await();
return Promises.value("A value that should never be seen!");
}
};
}
}