forked from linkedin/parseq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTasks.java
More file actions
282 lines (234 loc) · 7.5 KB
/
TestTasks.java
File metadata and controls
282 lines (234 loc) · 7.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* 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.PromiseListener;
import com.linkedin.parseq.promise.Promises;
import com.linkedin.parseq.trace.codec.json.JsonTraceCodec;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import static com.linkedin.parseq.TestUtil.value;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;
/**
* @author Chris Pettitt (cpettitt@linkedin.com)
* @author Chi Chan (ckchan@linkedin.com)
*/
public class TestTasks extends BaseEngineTest
{
@Test
public void testTaskThatThrows() throws InterruptedException
{
final Exception error = new Exception();
final Task<?> task = new BaseTask<Object>()
{
@Override
protected Promise<Object> run(final Context context) throws Exception
{
throw error;
}
};
getEngine().run(task);
assertTrue(task.await(5, TimeUnit.SECONDS));
assertTrue(task.isFailed());
assertEquals(error, task.getError());
}
@Test
public void testAwait() throws InterruptedException
{
final String value = "value";
final Task<String> task = value("value", value);
final AtomicReference<Boolean> resultRef = new AtomicReference<Boolean>(false);
task.addListener(new PromiseListener<String>()
{
@Override
public void onResolved(Promise<String> stringPromise)
{
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
//ignore
} finally
{
resultRef.set(true);
}
}
});
getEngine().run(task);
assertTrue(task.await(5, TimeUnit.SECONDS));
assertEquals(Boolean.TRUE, resultRef.get());
}
@Test
public void testTimeoutTaskWithTimeout() throws InterruptedException
{
// This task will not complete on its own, which allows us to test the timeout
final Task<String> task = new BaseTask<String>("task")
{
@Override
protected Promise<? extends String> run(
final Context context) throws Exception
{
return Promises.settable();
}
};
final Task<String> timeoutTask = Tasks.timeoutWithError(200, TimeUnit.MILLISECONDS, task);
getEngine().run(timeoutTask);
assertTrue(timeoutTask.await(5, TimeUnit.SECONDS));
assertTrue(timeoutTask.isFailed());
assertTrue(timeoutTask.getError() instanceof TimeoutException);
assertTrue(task.await(5, TimeUnit.SECONDS));
// The original task should also be failed - this time with an early finish
// exception.
assertTrue(task.isFailed());
assertTrue(task.getError() instanceof EarlyFinishException);
}
@Test
public void testTimeoutTaskWithoutTimeout() throws InterruptedException
{
final String value = "value";
final Task<String> task = Tasks.callable("task", new Callable<String>()
{
@Override
public String call() throws Exception
{
return value;
}
});
final Task<String> timeoutTask = Tasks.timeoutWithError(200, TimeUnit.MILLISECONDS, task);
getEngine().run(timeoutTask);
assertTrue(timeoutTask.await(5, TimeUnit.SECONDS));
assertEquals(value, task.get());
// The wrapping task should get the same value as the wrapped task
assertEquals(value, timeoutTask.get());
}
@Test
public void testTimeoutTaskWithError() throws InterruptedException
{
final Exception error = new Exception();
final Task<String> task = Tasks.callable("task", new Callable<String>()
{
@Override
public String call() throws Exception
{
throw error;
}
});
final Task<String> timeoutTask = Tasks.timeoutWithError(2000, TimeUnit.MILLISECONDS, task);
getEngine().run(timeoutTask);
// We should complete with an error almost immediately
assertTrue(timeoutTask.await(100, TimeUnit.MILLISECONDS));
assertTrue(timeoutTask.isFailed());
assertEquals(error, timeoutTask.getError());
}
@Test
public void testTimeoutTaskWithoutTimeoutWhenManyTasksAreOnQueeu() throws InterruptedException, IOException
{
final String value = "value";
List<Task<String>> tasks = new ArrayList<Task<String>>();
for (int i = 0; i < 50; i++) {
Task<String> t = Tasks.callable("task", new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(1); //this task is "busy" for 1 millisecond
return value;
}
});
tasks.add(Tasks.timeoutWithError(50, TimeUnit.MILLISECONDS, t));
}
final Task<?> timeoutTask = Tasks.par(tasks);
getEngine().run(timeoutTask);
assertTrue(timeoutTask.await(5, TimeUnit.SECONDS));
System.out.println(new JsonTraceCodec().encode(timeoutTask.getTrace()));
//tasks should not time out
assertEquals(false, timeoutTask.isFailed());
}
@Test
public void testSetPriorityBelowMinValue()
{
try
{
TestUtil.noop().setPriority(Priority.MIN_PRIORITY - 1);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
// Expected case
}
}
@Test
public void testSetPriorityAboveMaxValue()
{
try
{
TestUtil.noop().setPriority(Priority.MAX_PRIORITY + 1);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
// Expected case
}
}
@Test
public void testThrowableCallableNoError() throws InterruptedException
{
final Integer magic = 0x5f3759df;
final ThrowableCallable<Integer> callable = new ThrowableCallable<Integer>()
{
@Override
public Integer call() throws Throwable
{
return magic;
}
};
final Task<Integer> task = Tasks.callable("magic", callable);
getEngine().run(task);
task.await(100, TimeUnit.MILLISECONDS);
assertTrue(task.isDone());
assertFalse(task.isFailed());
assertEquals(magic, task.get());
assertEquals("magic", task.getName());
}
@Test
public void testThrowableCallableWithError() throws InterruptedException
{
final Throwable throwable = new Throwable();
final ThrowableCallable<Integer> callable = new ThrowableCallable<Integer>()
{
@Override
public Integer call() throws Throwable
{
throw throwable;
}
};
final Task<Integer> task = Tasks.callable("error", callable);
getEngine().run(task);
task.await(100, TimeUnit.MILLISECONDS);
assertTrue(task.isDone());
assertTrue(task.isFailed());
assertEquals("Throwable should not be wrapped", throwable, task.getError());
assertEquals("error", task.getName());
}
}