jdk/test/java/util/concurrent/BlockingQueue/LastElement.java
author dl
Mon, 02 Nov 2009 17:25:38 -0800
changeset 4110 ac033ba6ede4
parent 3708 f838f712922e
child 5506 202f599c92aa
permissions -rw-r--r--
6865582: jsr166y - jsr166 maintenance update 6865571: Add a lightweight task framework known as ForkJoin 6445158: Phaser - an improved CyclicBarrier 6865579: Add TransferQueue/LinkedTransferQueue Reviewed-by: martin, chegar, dice
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6215625
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check correct behavior when last element is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class LastElement {
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    35
    void test(String[] args) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        testQueue(new LinkedBlockingQueue<Integer>());
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    37
        testQueue(new LinkedBlockingDeque<Integer>());
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    38
        testQueue(new ArrayBlockingQueue<Integer>(10, true));
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    39
        testQueue(new ArrayBlockingQueue<Integer>(10, false));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    40
        testQueue(new LinkedTransferQueue<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    46
    void testQueue(BlockingQueue<Integer> q) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Integer one = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Integer two = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Integer three = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        q.put(one);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        q.put(two);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        check(! q.isEmpty() && q.size() == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        check(q.remove(one));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        check(q.remove(two));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        check(q.isEmpty() && q.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        q.put(three);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {check(q.take() == three);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        check(q.isEmpty() && q.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // iterator().remove()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        q.put(one);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        check(q.offer(two));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        check(! q.isEmpty() && q.size() == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Iterator<Integer> i = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        check(i.next() == one);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        check(i.next() == two);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        check(q.isEmpty() && q.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        q.put(three);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {check(q.take() == three);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        check(q.isEmpty() && q.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    79
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    80
    //--------------------- Infrastructure ---------------------------
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    81
    volatile int passed = 0, failed = 0;
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    82
    void pass() {passed++;}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    83
    void fail() {failed++; Thread.dumpStack();}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    84
    void fail(String msg) {System.err.println(msg); fail();}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    85
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    86
    void check(boolean cond) {if (cond) pass(); else fail();}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    87
    void equal(Object x, Object y) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    88
        if (x == null ? y == null : x.equals(y)) pass();
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    89
        else fail(x + " not equal to " + y);}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    90
    public static void main(String[] args) throws Throwable {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    91
        new LastElement().instanceMain(args);}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    92
    public void instanceMain(String[] args) throws Throwable {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    93
        try {test(args);} catch (Throwable t) {unexpected(t);}
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    94
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    95
        if (failed > 0) throw new AssertionError("Some tests failed");}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}