-
Notifications
You must be signed in to change notification settings - Fork 554
/
MpqSanityTest.java
660 lines (601 loc) · 18.3 KB
/
MpqSanityTest.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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package org.jctools.queues;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.After;
import org.junit.Test;
import org.jctools.queues.atomic.AtomicQueueFactory;
import org.jctools.queues.spec.ConcurrentQueueSpec;
import org.jctools.queues.spec.Ordering;
import org.jctools.queues.spec.Preference;
import org.jctools.util.Pow2;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
public abstract class MpqSanityTest
{
public static final int SIZE = 8192 * 2;
static final int CONCURRENT_TEST_DURATION = 500;
static final int TEST_TIMEOUT = 30000;
private final MessagePassingQueue<Integer> queue;
private final ConcurrentQueueSpec spec;
int count = 0;
Integer p;
public MpqSanityTest(ConcurrentQueueSpec spec, MessagePassingQueue<Integer> queue)
{
this.queue = queue;
this.spec = spec;
}
public static Object[] makeMpq(int producers, int consumers, int capacity, Ordering ordering, Queue<Integer> q)
{
ConcurrentQueueSpec spec = new ConcurrentQueueSpec(producers, consumers, capacity, ordering,
Preference.NONE);
if (q == null)
{
q = QueueFactory.newQueue(spec);
}
return new Object[] {spec, q};
}
public static Object[] makeAtomic(int producers, int consumers, int capacity, Ordering ordering, Queue<Integer> q)
{
ConcurrentQueueSpec spec = new ConcurrentQueueSpec(producers, consumers, capacity, ordering,
Preference.NONE);
if (q == null)
{
q = AtomicQueueFactory.newQueue(spec);
}
return new Object[] {spec, q};
}
@After
public void clear() throws InterruptedException
{
queue.clear();
assertTrue(queue.isEmpty());
assertTrue(queue.size() == 0);
}
@Test(expected = NullPointerException.class)
public void relaxedOfferNullResultsInNPE()
{
queue.relaxedOffer(null);
}
@Test
public void sanity()
{
for (int i = 0; i < SIZE; i++)
{
assertNull(queue.relaxedPoll());
assertTrue(queue.isEmpty());
assertTrue(queue.size() == 0);
}
int i = 0;
while (i < SIZE && queue.relaxedOffer(i))
{
i++;
}
int size = i;
assertEquals(size, queue.size());
if (spec.ordering == Ordering.FIFO)
{
// expect FIFO
i = 0;
Integer p;
Integer e;
while ((p = queue.relaxedPeek()) != null)
{
e = queue.relaxedPoll();
assertEquals(p, e);
assertEquals(size - (i + 1), queue.size());
assertEquals(i++, e.intValue());
}
assertEquals(size, i);
}
else
{
// expect sum of elements is (size - 1) * size / 2 = 0 + 1 + .... + (size - 1)
int sum = (size - 1) * size / 2;
i = 0;
Integer e;
while ((e = queue.relaxedPoll()) != null)
{
assertEquals(--size, queue.size());
sum -= e;
}
assertEquals(0, sum);
}
}
int sum;
@Test
public void sanityDrainBatch()
{
assertEquals(0, queue.drain(e ->
{
}, SIZE));
assertTrue(queue.isEmpty());
assertTrue(queue.size() == 0);
count = 0;
sum = 0;
int i = queue.fill(() ->
{
final int val = count++;
sum += val;
return val;
}, SIZE);
final int size = i;
assertEquals(size, queue.size());
if (spec.ordering == Ordering.FIFO)
{
// expect FIFO
count = 0;
int drainCount = 0;
i = 0;
do
{
i += drainCount = queue.drain(e ->
{
assertEquals(count++, e.intValue());
});
}
while (drainCount != 0);
assertEquals(size, i);
assertTrue(queue.isEmpty());
assertTrue(queue.size() == 0);
}
else
{
int drainCount = 0;
i = 0;
do
{
i += drainCount = queue.drain(e ->
{
sum -= e.intValue();
});
}
while (drainCount != 0);
assertEquals(size, i);
assertTrue(queue.isEmpty());
assertTrue(queue.size() == 0);
assertEquals(0, sum);
}
}
@Test
public void testSizeIsTheNumberOfOffers()
{
int currentSize = 0;
while (currentSize < SIZE && queue.relaxedOffer(currentSize))
{
currentSize++;
assertFalse(queue.isEmpty());
assertTrue(queue.size() == currentSize);
}
if (spec.isBounded())
{
assertEquals(spec.capacity, currentSize);
}
else
{
assertEquals(SIZE, currentSize);
}
}
@Test
public void supplyMessageUntilFull()
{
assumeThat(spec.isBounded(), is(Boolean.TRUE));
final Val instances = new Val();
instances.value = 0;
final MessagePassingQueue.Supplier<Integer> messageFactory = () -> instances.value++;
final int capacity = queue.capacity();
int filled = 0;
while (filled < capacity)
{
filled += queue.fill(messageFactory, capacity - filled);
}
assertEquals(instances.value, capacity);
final int noItems = queue.fill(messageFactory, 1);
assertEquals(noItems, 0);
assertEquals(instances.value, capacity);
}
@Test
public void whenFirstInThenFirstOut()
{
assumeThat(spec.ordering, is(Ordering.FIFO));
// Arrange
int i = 0;
while (i < SIZE && queue.relaxedOffer(i))
{
i++;
}
final int size = queue.size();
// Act
i = 0;
Integer prev;
while ((prev = queue.relaxedPeek()) != null)
{
final Integer item = queue.relaxedPoll();
assertThat(item, is(prev));
assertEquals((size - (i + 1)), queue.size());
assertThat(item, is(i));
i++;
}
// Assert
assertThat(i, is(size));
}
@Test
public void test_FIFO_PRODUCER_Ordering() throws Exception
{
assumeThat(spec.ordering, is((Ordering.FIFO)));
// Arrange
int i = 0;
while (i < SIZE && queue.relaxedOffer(i))
{
i++;
}
int size = queue.size();
// Act
// expect sum of elements is (size - 1) * size / 2 = 0 + 1 + .... + (size - 1)
int sum = (size - 1) * size / 2;
Integer e;
while ((e = queue.relaxedPoll()) != null)
{
size--;
assertEquals(size, queue.size());
sum -= e;
}
// Assert
assertThat(sum, is(0));
}
@Test
public void whenOfferItemAndPollItemThenSameInstanceReturnedAndQueueIsEmpty()
{
assertTrue(queue.isEmpty());
assertTrue(queue.size() == 0);
// Act
final Integer e = new Integer(1876876);
queue.relaxedOffer(e);
assertFalse(queue.isEmpty());
assertEquals(1, queue.size());
final Integer oh = queue.relaxedPoll();
assertEquals(e, oh);
// Assert
assertTrue(queue.isEmpty());
assertTrue(queue.size() == 0);
}
@Test
public void testPowerOf2Capacity()
{
assumeThat(spec.isBounded(), is(true));
int n = Pow2.roundToPowerOfTwo(spec.capacity);
for (int i = 0; i < n; i++)
{
assertTrue("Failed to insert:" + i, queue.relaxedOffer(i));
}
assertFalse(queue.relaxedOffer(n));
}
@Test(timeout = TEST_TIMEOUT)
public void testHappensBefore() throws Exception
{
final AtomicBoolean stop = new AtomicBoolean();
final MessagePassingQueue q = queue;
final Val fail = new Val();
final Runnable runnable = new Runnable()
{
@Override
public void run()
{
while (!stop.get())
{
for (int i = 1; i <= 10; i++)
{
Val v = new Val();
v.value = i;
q.relaxedOffer(v);
}
// slow down the producer, this will make the queue mostly empty encouraging visibility
// issues.
Thread.yield();
}
}
};
Thread[] producers = producers(runnable);
Thread consumer = new Thread(new Runnable()
{
@Override
public void run()
{
while (!stop.get())
{
for (int i = 0; i < 10; i++)
{
Val v1 = (Val) q.relaxedPeek();
if (v1 != null && v1.value == 0)
{
fail.value = 1;
stop.set(true);
}
else
{
continue;
}
Val v2 = (Val) q.relaxedPoll();
if (v2 == null || v1 != v2)
{
fail.value = 2;
stop.set(true);
}
}
}
}
});
stopAll(stop, producers, consumer);
assertEquals("reordering detected", 0, fail.value);
}
@Test(timeout = TEST_TIMEOUT)
public void testHappensBeforePerpetualDrain() throws Exception
{
final AtomicBoolean stop = new AtomicBoolean();
final MessagePassingQueue q = queue;
final Val fail = new Val();
final Runnable runnable = new Runnable()
{
@Override
public void run()
{
while (!stop.get())
{
for (int i = 1; i <= 10; i++)
{
Val v = new Val();
v.value = i;
q.relaxedOffer(v);
}
// slow down the producer, this will make the queue mostly empty encouraging visibility
// issues.
Thread.yield();
}
}
};
Thread[] producers = producers(runnable);
Thread consumer = new Thread(new Runnable()
{
@Override
public void run()
{
while (!stop.get())
{
q.drain(e ->
{
Val v = (Val) e;
if (v != null && v.value == 0)
{
fail.value = 1;
stop.set(true);
}
if (v == null)
{
fail.value = 1;
stop.set(true);
System.out.println("Unexpected: v == null");
}
}, idle ->
{
return idle;
}, () ->
{
return !stop.get();
});
}
}
});
stopAll(stop, producers, consumer);
assertEquals("reordering detected", 0, fail.value);
}
@Test(timeout = TEST_TIMEOUT)
public void testHappensBeforePerpetualFill() throws Exception
{
final AtomicBoolean stop = new AtomicBoolean();
final MessagePassingQueue q = queue;
final Val fail = new Val();
final Runnable runnable = new Runnable()
{
int counter;
@Override
public void run()
{
counter = 1;
q.fill(() ->
{
Val v = new Val();
v.value = 1 + (counter++ % 10);
return v;
}, e ->
{
return e;
}, () ->
{
// slow down the producer, this will make the queue mostly empty encouraging visibility
// issues.
Thread.yield();
return !stop.get();
});
}
};
Thread[] producers = producers(runnable);
Thread consumer = new Thread(new Runnable()
{
@Override
public void run()
{
while (!stop.get())
{
for (int i = 0; i < 10; i++)
{
Val v1 = (Val) q.relaxedPeek();
int r;
if (v1 != null && (r = v1.value) == 0)
{
fail.value = 1;
stop.set(true);
}
else
{
continue;
}
Val v2 = (Val) q.relaxedPoll();
if (v2 == null || v1 != v2)
{
fail.value = 1;
stop.set(true);
}
}
}
}
});
stopAll(stop, producers, consumer);
assertEquals("reordering detected", 0, fail.value);
}
@Test(timeout = TEST_TIMEOUT)
public void testHappensBeforePerpetualFillDrain() throws Exception
{
final AtomicBoolean stop = new AtomicBoolean();
final MessagePassingQueue q = queue;
final Val fail = new Val();
final Runnable runnable = new Runnable()
{
int counter;
@Override
public void run()
{
counter = 1;
q.fill(() ->
{
Val v = new Val();
v.value = 1 + (counter++ % 10);
return v;
}, e ->
{
return e;
}, () ->
{ // slow down the producer, this will make the queue mostly empty encouraging
// visibility issues.
Thread.yield();
return !stop.get();
});
}
};
Thread[] producers = producers(runnable);
Thread consumer = new Thread(new Runnable()
{
@Override
public void run()
{
while (!stop.get())
{
q.drain(e ->
{
Val v = (Val) e;
if (v != null && v.value == 0)
{
fail.value = 1;
stop.set(true);
}
if (v == null)
{
fail.value = 1;
stop.set(true);
System.out.println("Unexpected: v == null");
}
}, idle ->
{
return idle;
}, () ->
{
return !stop.get();
});
}
}
});
stopAll(stop, producers, consumer);
assertEquals("reordering detected", 0, fail.value);
queue.clear();
}
@Test(timeout = TEST_TIMEOUT)
public void testRelaxedOfferPollObservedSize() throws Exception
{
final int capacity = spec.capacity == 0 ? Integer.MAX_VALUE : spec.capacity;
final AtomicBoolean stop = new AtomicBoolean();
final MessagePassingQueue<Integer> q = queue;
final Val fail = new Val();
final Runnable runnable = new Runnable()
{
@Override
public void run()
{
while (!stop.get())
{
q.relaxedOffer(1);
q.relaxedPoll();
}
}
};
final Thread[] producersConsumers;
if (!spec.isMpmc())
{
producersConsumers = new Thread[1];
}
else
{
producersConsumers = new Thread[Runtime.getRuntime().availableProcessors() - 1];
}
for (int i = 0; i < producersConsumers.length; i++)
{
producersConsumers[i] = new Thread(runnable);
}
Thread observer = new Thread(new Runnable()
{
@Override
public void run()
{
final int max = Math.min(producersConsumers.length, capacity);
while (!stop.get())
{
int size = q.size();
if (size < 0 && size > max)
{
fail.value++;
}
}
}
});
stopAll(stop, producersConsumers, observer);
assertEquals("Unexpected size observed", 0, fail.value);
}
private void stopAll(AtomicBoolean stop, Thread[] producers, Thread consumer) throws InterruptedException
{
for (Thread t : producers) t.start();
consumer.start();
Thread.sleep(CONCURRENT_TEST_DURATION);
stop.set(true);
for (Thread t : producers) t.join();
consumer.join();
}
private Thread[] producers(Runnable runnable)
{
Thread[] producers;
if (spec.producers == 1)
{
producers = new Thread[1];
}
else
{
producers = new Thread[Runtime.getRuntime().availableProcessors() - 1];
}
for (int i = 0; i < producers.length; i++)
{
producers[i] = new Thread(runnable);
}
return producers;
}
static final class Val
{
public int value;
}
}