-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathWindowTest.java
More file actions
547 lines (462 loc) · 22.4 KB
/
Copy pathWindowTest.java
File metadata and controls
547 lines (462 loc) · 22.4 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package com.cloudhopper.commons.util.windowing;
/*
* #%L
* ch-commons-util
* %%
* Copyright (C) 2012 Cloudhopper by Twitter
* %%
* 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.
* #L%
*/
// third party imports
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// my imports
//import net.cloudhopper.commons.util.ByteBuffer;
/**
* Tests Windowing class and packages.
* @author joelauer (twitter: @jjlauer or <a href="http://twitter.com/jjlauer" target=window>http://twitter.com/jjlauer</a>)
*/
public class WindowTest {
private static final Logger logger = LoggerFactory.getLogger(WindowTest.class);
@Test
public void usage() throws Exception {
Window<Integer,String,String> window = new Window<Integer,String,String>(1);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(0, window.getSize());
WindowFuture<Integer,String,String> future0 = window.offer(0, "Request0", 100);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(1, window.getSize());
Assert.assertEquals(0, window.getFreeSize());
Assert.assertEquals(1, future0.getWindowSize());
window.cancel(0);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(0, window.getSize());
Assert.assertEquals(1, window.getFreeSize());
WindowFuture<Integer,String,String> future1 = window.offer(1, "Request1", 100);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(1, window.getSize());
Assert.assertEquals(0, window.getFreeSize());
Assert.assertEquals(1, future1.getWindowSize());
window.complete(1, "Response1");
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(0, window.getSize());
Assert.assertEquals(1, window.getFreeSize());
WindowFuture<Integer,String,String> future2 = window.offer(2, "Request2", 100);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(1, window.getSize());
Assert.assertEquals(0, window.getFreeSize());
Assert.assertFalse(future2.isDone());
Assert.assertFalse(future2.isCancelled());
Assert.assertFalse(future2.isSuccess());
Assert.assertNull(future2.getCause());
Assert.assertEquals(1, future2.getWindowSize());
// trigger this from the future now
future2.fail(new Exception("Test Cause"));
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(0, window.getSize());
Assert.assertEquals(1, window.getFreeSize());
Assert.assertTrue(future2.isDone());
Assert.assertFalse(future2.isCancelled());
Assert.assertFalse(future2.isSuccess());
Assert.assertNotNull(future2.getCause());
WindowFuture<Integer,String,String> future3 = window.offer(3, "Request3", 100);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(1, window.getSize());
Assert.assertEquals(0, window.getFreeSize());
Assert.assertFalse(future3.isDone());
Assert.assertFalse(future3.isCancelled());
Assert.assertFalse(future3.isSuccess());
Assert.assertNull(future3.getCause());
Assert.assertEquals(1, future3.getWindowSize());
// trigger this from the future now
long now = System.currentTimeMillis();
future3.complete("Response3", now);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(0, window.getSize());
Assert.assertEquals(1, window.getFreeSize());
Assert.assertTrue(future3.isDone());
Assert.assertFalse(future3.isCancelled());
Assert.assertTrue(future3.isSuccess());
Assert.assertNull(future3.getCause());
Assert.assertEquals(now, future3.getDoneTimestamp());
WindowFuture<Integer,String,String> future4 = window.offer(4, "Request4", 100);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(1, window.getSize());
Assert.assertEquals(0, window.getFreeSize());
Assert.assertFalse(future4.isDone());
Assert.assertFalse(future4.isCancelled());
Assert.assertFalse(future4.isSuccess());
Assert.assertNull(future4.getCause());
Assert.assertEquals(1, future4.getWindowSize());
// trigger this from the future now
future4.cancel(now);
Assert.assertEquals(1, window.getMaxSize());
Assert.assertEquals(0, window.getSize());
Assert.assertEquals(1, window.getFreeSize());
Assert.assertTrue(future4.isDone());
Assert.assertTrue(future4.isCancelled());
Assert.assertFalse(future4.isSuccess());
Assert.assertNull(future4.getCause());
Assert.assertEquals(now, future4.getDoneTimestamp());
}
@Test
public void requestFutureAndWindowFuture() throws Exception {
Window<Integer,String,String> window = new Window<Integer,String,String>(1);
Integer i = new Integer(1);
String request = "Request"+i;
WindowFuture<Integer,String,String> requestFuture = window.offer(i, request, 100);
Assert.assertEquals(new Integer(i), requestFuture.getKey());
Assert.assertEquals(request, requestFuture.getRequest());
Assert.assertEquals(true, requestFuture.getAcceptTimestamp() > 0);
Assert.assertEquals(null, requestFuture.getResponse());
Assert.assertFalse(requestFuture.hasDoneTimestamp());
Assert.assertEquals(0, requestFuture.getDoneTimestamp());
Assert.assertEquals(false, requestFuture.isCancelled());
Assert.assertEquals(false, requestFuture.isDone());
Assert.assertEquals(false, requestFuture.isSuccess());
Assert.assertEquals(-1, requestFuture.getAcceptToDoneTime());
// this should timeout waiting for a response
Assert.assertFalse(requestFuture.await(100));
// mimic a response is received
String response = "Response"+i;
WindowFuture<Integer,String,String> responseFuture = window.complete(i, response);
Assert.assertEquals(new Integer(i), responseFuture.getKey());
Assert.assertEquals(request, responseFuture.getRequest());
Assert.assertEquals(true, responseFuture.getAcceptTimestamp() > 0);
Assert.assertEquals(response, responseFuture.getResponse());
Assert.assertEquals(true, responseFuture.getDoneTimestamp() > 0);
Assert.assertEquals(false, requestFuture.isCancelled());
Assert.assertEquals(true, requestFuture.isDone());
Assert.assertEquals(true, requestFuture.isSuccess());
Assert.assertEquals(true, requestFuture.getAcceptToDoneTime() > 0);
// this should succeed now since a response was received
Assert.assertTrue(requestFuture.await(100));
String response0 = requestFuture.getResponse();
Assert.assertEquals(response, response0);
Assert.assertEquals(request, requestFuture.getRequest());
Assert.assertEquals(true, requestFuture.getAcceptTimestamp() > 0);
Assert.assertEquals(response, requestFuture.getResponse());
Assert.assertEquals(true, requestFuture.getDoneTimestamp() > 0);
Assert.assertEquals(false, requestFuture.isCancelled());
Assert.assertEquals(true, requestFuture.isDone());
Assert.assertEquals(true, requestFuture.isSuccess());
Assert.assertEquals(true, requestFuture.getAcceptToDoneTime() > 0);
}
@Test
public void filledWindowThrowsOfferTimeoutException() throws Exception {
Window<Integer,String,String> window = new Window<Integer,String,String>(1);
WindowFuture<Integer,String,String> requestFuture0 = window.offer(0, "Request0", 100);
try {
// this should timeout waiting for a slot
WindowFuture<Integer,String,String> requestFuture1 = window.offer(1, "Request1", 100);
Assert.fail();
} catch (OfferTimeoutException e) {
// correct behavior
}
}
@Test
public void awaitTimesout() throws Exception {
Window<Integer,String,String> window = new Window<Integer,String,String>(1);
WindowFuture<Integer,String,String> requestFuture0 = window.offer(0, "Request0", 100);
// this should timeout waiting for a response
Assert.assertFalse(requestFuture0.await(100));
}
@Test
public void duplicateKeyThrowsDuplicateKeyException() throws Exception {
Window<Integer,String,String> window = new Window<Integer,String,String>(1);
WindowFuture<Integer,String,String> requestFuture0 = window.offer(0, "Request0", 100);
try {
WindowFuture<Integer,String,String> requestFuture1 = window.offer(0, "Request0", 100);;
Assert.fail();
} catch (DuplicateKeyException e) {
// correct behavior
}
}
@Test
public void waitingFlagNotOriginallySetButAddedOnAwait() throws Exception {
Window<Integer,String,String> window = new Window<Integer,String,String>(1);
WindowFuture<Integer,String,String> requestFuture0 = window.offer(0, "Request0", 100);
Assert.assertEquals(WindowFuture.CALLER_NOT_WAITING, requestFuture0.getCallerStateHint());
Assert.assertFalse(requestFuture0.isCallerWaiting());
// now the caller was mistaken that they weren't waiting, when they actually
// start to wait, we'll set the waiting flag to true
Assert.assertFalse(requestFuture0.await(30));
Assert.assertEquals(WindowFuture.CALLER_WAITING_TIMEOUT, requestFuture0.getCallerStateHint());
}
@Test
public void cancel() throws Exception {
Window<Integer,String,String> window = new Window<Integer,String,String>(1);
Integer i = new Integer(1);
String request = "Request"+i;
WindowFuture<Integer,String,String> requestFuture = window.offer(i, request, 100);
// cancel it
WindowFuture<Integer,String,String> responseFuture = window.cancel(i);
Assert.assertEquals(new Integer(i), responseFuture.getKey());
Assert.assertEquals(request, responseFuture.getRequest());
Assert.assertEquals(true, responseFuture.getAcceptTimestamp() > 0);
Assert.assertEquals(null, responseFuture.getResponse());
Assert.assertEquals(true, responseFuture.getDoneTimestamp() > 0);
Assert.assertEquals(true, requestFuture.isCancelled());
Assert.assertEquals(true, requestFuture.isDone());
Assert.assertEquals(false, requestFuture.isSuccess());
Assert.assertTrue(requestFuture.getAcceptToDoneTime() >= 0);
// this should not timeout waiting for a response
Assert.assertTrue(requestFuture.await(50));
}
@Test
public void cancelAll() throws Exception {
int count = 5;
Window<Integer,String,String> window = new Window<Integer,String,String>(count);
String[] requests = new String[count];
for (int i = 0; i < count; i++) {
requests[i] = "Request" + i;
}
for (int i = 0; i < 3; i++) {
window.offer(i, requests[i], 100);
}
Assert.assertEquals(3, window.getSize());
// are there requests pending?
List<WindowFuture<Integer,String,String>> cancelled = window.cancelAll();
Assert.assertEquals(0, window.getSize());
Assert.assertEquals(3, cancelled.size());
for (int i = 0; i < 3; i++) {
WindowFuture<Integer,String,String> value = cancelled.get(i);
Assert.assertEquals("Request"+value.getKey(), value.getRequest());
Assert.assertEquals(true, value.getAcceptTimestamp() > 0);
Assert.assertEquals(null, value.getResponse());
Assert.assertEquals(true, value.getDoneTimestamp() > 0);
Assert.assertEquals(true, value.isCancelled());
Assert.assertEquals(true, value.isDone());
Assert.assertEquals(false, value.isSuccess());
}
}
public static class RequestThread extends Thread {
private Window<Integer,String,String> window;
private BlockingQueue<Integer> requestQueue;
public int id;
public int requestsPerThread;
public Throwable throwable;
public RequestThread(Window<Integer,String,String> window, BlockingQueue<Integer> requestQueue, int id, int requestsPerThread) {
this.window = window;
this.requestQueue = requestQueue;
this.id = id;
this.requestsPerThread = requestsPerThread;
}
@Override
public void run() {
try {
for (int x = 0; x < requestsPerThread; x++) {
//To get a distinct value of i, we multiply id by the total number of requests per thread and add x to it
Integer i = Integer.valueOf(""+id*requestsPerThread+""+x);
String request = "Request"+i;
// logger.debug("adding request " + i);
WindowFuture<Integer,String,String> requestFuture = window.offer(i, request, 1000);
Assert.assertEquals(i, requestFuture.getKey());
Assert.assertEquals(request, requestFuture.getRequest());
Assert.assertEquals(true, requestFuture.getAcceptTimestamp() > 0);
Assert.assertEquals(null, requestFuture.getResponse());
Assert.assertEquals(0, requestFuture.getDoneTimestamp());
Assert.assertEquals(false, requestFuture.isCancelled());
Assert.assertEquals(false, requestFuture.isDone());
Assert.assertEquals(false, requestFuture.isSuccess());
Assert.assertEquals(-1, requestFuture.getAcceptToDoneTime());
requestQueue.add(i);
requestFuture.await(100);
Assert.assertEquals(request, requestFuture.getRequest());
Assert.assertEquals(true, requestFuture.getAcceptTimestamp() > 0);
Assert.assertEquals("Response"+i, requestFuture.getResponse());
//Assert.assertEquals(null, requestFuture.getResponse());
Assert.assertEquals(true, requestFuture.getDoneTimestamp() > 0);
Assert.assertEquals(false, requestFuture.isCancelled());
Assert.assertEquals(true, requestFuture.isDone());
}
} catch (Throwable t) {
logger.error("", t);
this.throwable = t;
return;
}
}
}
public static class ResponseThread extends Thread {
private Window<Integer,String,String> window;
private BlockingQueue<Integer> requestQueue;
public int total;
public Throwable throwable;
public ResponseThread(Window<Integer,String,String> window, BlockingQueue<Integer> requestQueue, int total) {
this.window = window;
this.requestQueue = requestQueue;
this.total = total;
}
@Override
public void run() {
try {
for (int x = 0; x < total; x++) {
//Integer i = new Integer(x);
Integer i = requestQueue.poll(100, TimeUnit.MILLISECONDS);
if (i == null) {
throw new Exception("Integer was null in ResponseThread");
}
String response = "Response"+i;
// logger.debug("adding response " + i);
WindowFuture<Integer,String,String> responseFuture = window.complete(i, response);
Assert.assertEquals(new Integer(i), responseFuture.getKey());
Assert.assertEquals("Request"+i, responseFuture.getRequest());
Assert.assertEquals(true, responseFuture.getAcceptTimestamp() > 0);
Assert.assertEquals(response, responseFuture.getResponse());
Assert.assertEquals(true, responseFuture.getDoneTimestamp() > 0);
Assert.assertEquals(false, responseFuture.isCancelled());
Assert.assertEquals(true, responseFuture.isDone());
Assert.assertEquals(true, responseFuture.isSuccess());
}
} catch (Throwable t) {
logger.error("", t);
this.throwable = t;
return;
}
}
}
@Test
public void simulatedMultithreadedProcessing() throws Exception {
final Window<Integer,String,String> window = new Window<Integer,String,String>(5);
final int requestThreadCount = 8;
final int requestsPerThread = 10000;
final BlockingQueue<Integer> requestQueue = new LinkedBlockingQueue<Integer>();
RequestThread[] requestThreads = new RequestThread[requestThreadCount];
for (int i = 0; i < requestThreadCount; i++) {
requestThreads[i] = new RequestThread(window, requestQueue, i, requestsPerThread);
}
ResponseThread responseThread = new ResponseThread(window, requestQueue, requestThreadCount*requestsPerThread);
// start 'em
for (RequestThread requestThread : requestThreads) {
requestThread.start();
}
responseThread.start();
// wait for them to finish
for (RequestThread requestThread : requestThreads) {
requestThread.join();
}
responseThread.join();
// make sure everything was successful
for (int i = 0; i < requestThreadCount; i++) {
if (requestThreads[i].throwable != null) {
logger.error("", requestThreads[i].throwable);
}
Assert.assertNull("RequestThread " + i + " throwable wasn't null: " + requestThreads[i].throwable, requestThreads[i].throwable);
}
if (responseThread.throwable != null) {
logger.error("", responseThread.throwable);
}
Assert.assertNull("ResponseThread throwable wasn't null", responseThread.throwable);
Assert.assertEquals(0, window.getSize());
}
@Test
public void abortOffering() throws Exception {
// test that terminating slot waiters early works as expected
final Window<Integer,String,String> window = new Window<Integer,String,String>(2);
// first two items should work
Assert.assertFalse(window.abortPendingOffers());
window.offer(1, "Request1", 100);
Assert.assertFalse(window.abortPendingOffers());
window.offer(2, "Request2", 100);
Assert.assertFalse(window.abortPendingOffers());
// third item should fail
try {
window.offer(3, "Request3", 20);
Assert.fail();
} catch (OfferTimeoutException e) {
// correct behavior
}
// start up 3 other threads that will all be waiting too
Thread[] waiters = new Thread[3];
for (int i = 0; i < waiters.length; i++) {
final int x = i;
waiters[i] = new Thread() {
@Override
public void run() {
try {
window.offer(4+x, "Request" + (4+x), 5000);
Assert.fail();
} catch (PendingOfferAbortedException e) {
// correct behavior
} catch (Exception e) {
logger.error("", e);
Assert.fail();
}
}
};
waiters[i].start();
}
// start up a thread that will call "terminateSlotWaiters" in 300 ms
Thread terminator = new Thread() {
@Override
public void run() {
try {
Thread.sleep(300);
} catch (Exception e) { }
try {
Assert.assertEquals(4, window.getPendingOfferCount());
boolean hadWaiters = window.abortPendingOffers();
logger.debug("hadWaiters: " + hadWaiters);
Assert.assertTrue(hadWaiters);
} catch (Exception e) {
logger.error("", e);
Assert.fail();
}
}
};
terminator.start();
// now wait for a slot up to 5 seconds (the thread we spawned earlier
// should definitely cause it to terminate early)
try {
window.offer(3, "Request3", 5000);
Assert.fail();
} catch (PendingOfferAbortedException e) {
// correct behavior
}
// make sure everything is finished
terminator.join();
for (Thread t : waiters) {
t.join();
}
// next call to terminate slot waiters shouldn't do anything
Assert.assertEquals(0, window.getPendingOfferCount());
Assert.assertFalse(window.abortPendingOffers());
window.complete(1, "Response1");
Assert.assertFalse(window.abortPendingOffers());
window.offer(4, "Request4", 100);
Assert.assertFalse(window.abortPendingOffers());
}
@Test
public void abortOfferingCalledWithNoWaitingOfferers() throws Exception {
final Window<Integer,String,String> window = new Window<Integer,String,String>(2);
window.offer(0, "Request0", 100);
window.abortPendingOffers();
window.offer(1, "Request1", 100);
}
@Test
public void invalidArguments() throws Exception {
final Window<Integer,String,String> window = new Window<Integer,String,String>(1);
try {
window.offer(1, "test1", -1);
Assert.fail();
} catch (IllegalArgumentException e) {
// correct behavior
}
}
}