blob: a3804ed817ccd86b584012371220e1f9ec3035e1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6327342
27 * @summary Try to poll a DelayQueue with only unexpired elements
28 * @author Martin Buchholz
29 */
30
31import java.util.concurrent.*;
32
33public class PollUnexpired {
34 private static class Godot implements Delayed {
35 public long getDelay(TimeUnit unit) {return Long.MAX_VALUE;}
36 public int compareTo(Delayed other) {return 0;}
37 }
38
39 private static void realMain(String[] args) throws Throwable {
40 DelayQueue<Godot> q = new DelayQueue<Godot>();
41 for (int i = 0; i < 3; i++) {
42 equal(q.size(), i);
43 equal(q.poll(), null);
44 equal(q.size(), i);
45 equal(q.poll(100, TimeUnit.MILLISECONDS), null);
46 equal(q.size(), i);
47 q.add(new Godot());
48 }
49 }
50
51 //--------------------- Infrastructure ---------------------------
52 static volatile int passed = 0, failed = 0;
53 static void pass() {passed++;}
54 static void fail() {failed++; Thread.dumpStack();}
55 static void fail(String msg) {System.out.println(msg); fail();}
56 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
57 static void check(boolean cond) {if (cond) pass(); else fail();}
58 static void equal(Object x, Object y) {
59 if (x == null ? y == null : x.equals(y)) pass();
60 else fail(x + " not equal to " + y);}
61 public static void main(String[] args) throws Throwable {
62 try {realMain(args);} catch (Throwable t) {unexpected(t);}
63 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
64 if (failed > 0) throw new AssertionError("Some tests failed");}
65}