ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/OfferDrainToLoops.java
Revision: 1.5
Committed: Sat Dec 31 22:25:18 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +9 -4 lines
Log Message:
organize imports

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 /*
8 * @test
9 * @bug 6805775 6815766
10 * @run main OfferDrainToLoops 300
11 * @summary Test concurrent offer vs. drainTo
12 */
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.ArrayBlockingQueue;
17 import java.util.concurrent.BlockingQueue;
18 import java.util.concurrent.LinkedBlockingDeque;
19 import java.util.concurrent.LinkedBlockingQueue;
20 import java.util.concurrent.LinkedTransferQueue;
21 import java.util.concurrent.ThreadLocalRandom;
22 import java.util.concurrent.atomic.AtomicLong;
23
24 @SuppressWarnings({"unchecked", "rawtypes"})
25 public class OfferDrainToLoops {
26 final long testDurationMillisDefault = 10L * 1000L;
27 final long testDurationMillis;
28
29 OfferDrainToLoops(String[] args) {
30 testDurationMillis = (args.length > 0) ?
31 Long.valueOf(args[0]) : testDurationMillisDefault;
32 }
33
34 void checkNotContainsNull(Iterable it) {
35 for (Object x : it)
36 check(x != null);
37 }
38
39 void test(String[] args) throws Throwable {
40 test(new LinkedBlockingQueue());
41 test(new LinkedBlockingQueue(2000));
42 test(new LinkedBlockingDeque());
43 test(new LinkedBlockingDeque(2000));
44 test(new ArrayBlockingQueue(2000));
45 test(new LinkedTransferQueue());
46 }
47
48 void test(final BlockingQueue q) throws Throwable {
49 System.out.println(q.getClass().getSimpleName());
50 final long testDurationNanos = testDurationMillis * 1000L * 1000L;
51 final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
52 final long timeoutMillis = 10L * 1000L;
53
54 /** Poor man's bounded buffer. */
55 final AtomicLong approximateCount = new AtomicLong(0L);
56
57 abstract class CheckedThread extends Thread {
58 CheckedThread() {
59 setDaemon(true);
60 start();
61 }
62 /** Polls for quitting time. */
63 protected boolean quittingTime() {
64 return System.nanoTime() - quittingTimeNanos > 0;
65 }
66 /** Polls occasionally for quitting time. */
67 protected boolean quittingTime(long i) {
68 return (i % 1024) == 0 && quittingTime();
69 }
70 protected abstract void realRun();
71 public void run() {
72 try { realRun(); } catch (Throwable t) { unexpected(t); }
73 }
74 }
75
76 Thread offerer = new CheckedThread() {
77 protected void realRun() {
78 long c = 0;
79 for (long i = 0; ! quittingTime(i); i++) {
80 if (q.offer(c)) {
81 c++;
82 if ((c % 1024) == 0) {
83 approximateCount.getAndAdd(1024);
84 while (approximateCount.get() > 10000)
85 Thread.yield();
86 }
87 } else {
88 Thread.yield();
89 }}}};
90
91 Thread drainer = new CheckedThread() {
92 protected void realRun() {
93 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
94 while (! quittingTime()) {
95 List list = new ArrayList();
96 int n = rnd.nextBoolean() ?
97 q.drainTo(list) :
98 q.drainTo(list, 100);
99 approximateCount.getAndAdd(-n);
100 equal(list.size(), n);
101 for (int j = 0; j < n - 1; j++)
102 equal((Long) list.get(j) + 1L, list.get(j + 1));
103 Thread.yield();
104 }}};
105
106 Thread scanner = new CheckedThread() {
107 protected void realRun() {
108 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
109 while (! quittingTime()) {
110 switch (rnd.nextInt(3)) {
111 case 0: checkNotContainsNull(q); break;
112 case 1: q.size(); break;
113 case 2:
114 Long[] a = (Long[]) q.toArray(new Long[0]);
115 int n = a.length;
116 for (int j = 0; j < n - 1; j++) {
117 check(a[j] < a[j+1]);
118 check(a[j] != null);
119 }
120 break;
121 }
122 Thread.yield();
123 }}};
124
125 for (Thread thread : new Thread[] { offerer, drainer, scanner }) {
126 thread.join(timeoutMillis + testDurationMillis);
127 check(! thread.isAlive());
128 }
129 }
130
131 //--------------------- Infrastructure ---------------------------
132 volatile int passed = 0, failed = 0;
133 void pass() {passed++;}
134 void fail() {failed++; Thread.dumpStack();}
135 void fail(String msg) {System.err.println(msg); fail();}
136 void unexpected(Throwable t) {failed++; t.printStackTrace();}
137 void check(boolean cond) {if (cond) pass(); else fail();}
138 void equal(Object x, Object y) {
139 if (x == null ? y == null : x.equals(y)) pass();
140 else fail(x + " not equal to " + y);}
141 public static void main(String[] args) throws Throwable {
142 new OfferDrainToLoops(args).instanceMain(args);}
143 public void instanceMain(String[] args) throws Throwable {
144 try {test(args);} catch (Throwable t) {unexpected(t);}
145 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
146 if (failed > 0) throw new AssertionError("Some tests failed");}
147 }