ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/OfferDrainToLoops.java
Revision: 1.4
Committed: Mon Sep 14 22:12:04 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +0 -27 lines
Log Message:
fix legal notice

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