ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/OfferDrainToLoops.java
Revision: 1.1
Committed: Wed Sep 29 12:38:42 2010 UTC (13 years, 7 months ago) by dl
Branch: MAIN
Log Message:
Add test

File Contents

# Content
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
19 * CA 95054 USA or visit www.sun.com if you need additional information or
20 * have any questions.
21 */
22
23 /*
24 * This file is available under and governed by the GNU General Public
25 * License version 2 only, as published by the Free Software Foundation.
26 * However, the following notice accompanied the original version of this
27 * file:
28 *
29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 * Expert Group and released to the public domain, as explained at
31 * http://creativecommons.org/licenses/publicdomain
32 */
33
34 /*
35 * @test
36 * @bug 6805775 6815766
37 * @run main OfferDrainToLoops 300
38 * @summary Test concurrent offer vs. drainTo
39 */
40
41 import java.util.*;
42 import java.util.concurrent.*;
43 import java.util.concurrent.atomic.*;
44 //import jsr166y.*;
45
46 @SuppressWarnings({"unchecked", "rawtypes"})
47 public class OfferDrainToLoops {
48 final long testDurationMillisDefault = 10L * 1000L;
49 final long testDurationMillis;
50
51 OfferDrainToLoops(String[] args) {
52 testDurationMillis = (args.length > 0) ?
53 Long.valueOf(args[0]) : testDurationMillisDefault;
54 }
55
56 void checkNotContainsNull(Iterable it) {
57 for (Object x : it)
58 check(x != null);
59 }
60
61 void test(String[] args) throws Throwable {
62 test(new LinkedBlockingQueue());
63 test(new LinkedBlockingQueue(2000));
64 test(new LinkedBlockingDeque());
65 test(new LinkedBlockingDeque(2000));
66 test(new ArrayBlockingQueue(2000));
67 test(new LinkedTransferQueue());
68 }
69
70 void test(final BlockingQueue q) throws Throwable {
71 System.out.println(q.getClass().getSimpleName());
72 final long testDurationNanos = testDurationMillis * 1000L * 1000L;
73 final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
74 final long timeoutMillis = 10L * 1000L;
75
76 /** Poor man's bounded buffer. */
77 final AtomicLong approximateCount = new AtomicLong(0L);
78
79 abstract class CheckedThread extends Thread {
80 CheckedThread() {
81 setDaemon(true);
82 start();
83 }
84 /** Polls for quitting time. */
85 protected boolean quittingTime() {
86 return System.nanoTime() - quittingTimeNanos > 0;
87 }
88 /** Polls occasionally for quitting time. */
89 protected boolean quittingTime(long i) {
90 return (i % 1024) == 0 && quittingTime();
91 }
92 abstract protected void realRun();
93 public void run() {
94 try { realRun(); } catch (Throwable t) { unexpected(t); }
95 }
96 }
97
98 Thread offerer = new CheckedThread() {
99 protected void realRun() {
100 long c = 0;
101 for (long i = 0; ! quittingTime(i); i++) {
102 if (q.offer(c)) {
103 c++;
104 if ((c % 1024) == 0) {
105 approximateCount.getAndAdd(1024);
106 while (approximateCount.get() > 10000)
107 Thread.yield();
108 }
109 } else {
110 Thread.yield();
111 }}}};
112
113 Thread drainer = new CheckedThread() {
114 protected void realRun() {
115 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
116 while (! quittingTime()) {
117 List list = new ArrayList();
118 int n = rnd.nextBoolean() ?
119 q.drainTo(list) :
120 q.drainTo(list, 100);
121 approximateCount.getAndAdd(-n);
122 equal(list.size(), n);
123 for (int j = 0; j < n - 1; j++)
124 equal((Long) list.get(j) + 1L, list.get(j + 1));
125 Thread.yield();
126 }}};
127
128 Thread scanner = new CheckedThread() {
129 protected void realRun() {
130 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
131 while (! quittingTime()) {
132 switch (rnd.nextInt(3)) {
133 case 0: checkNotContainsNull(q); break;
134 case 1: q.size(); break;
135 case 2:
136 Long[] a = (Long[]) q.toArray(new Long[0]);
137 int n = a.length;
138 for (int j = 0; j < n - 1; j++) {
139 check(a[j] < a[j+1]);
140 check(a[j] != null);
141 }
142 break;
143 }
144 Thread.yield();
145 }}};
146
147 for (Thread thread : new Thread[] { offerer, drainer, scanner }) {
148 thread.join(timeoutMillis + testDurationMillis);
149 check(! thread.isAlive());
150 }
151 }
152
153 //--------------------- Infrastructure ---------------------------
154 volatile int passed = 0, failed = 0;
155 void pass() {passed++;}
156 void fail() {failed++; Thread.dumpStack();}
157 void fail(String msg) {System.err.println(msg); fail();}
158 void unexpected(Throwable t) {failed++; t.printStackTrace();}
159 void check(boolean cond) {if (cond) pass(); else fail();}
160 void equal(Object x, Object y) {
161 if (x == null ? y == null : x.equals(y)) pass();
162 else fail(x + " not equal to " + y);}
163 public static void main(String[] args) throws Throwable {
164 new OfferDrainToLoops(args).instanceMain(args);}
165 public void instanceMain(String[] args) throws Throwable {
166 try {test(args);} catch (Throwable t) {unexpected(t);}
167 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
168 if (failed > 0) throw new AssertionError("Some tests failed");}
169 }