jdk/test/java/util/concurrent/BlockingQueue/Interrupt.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7518 0282db800fe1
child 22952 4352deb511f5
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7518
diff changeset
     2
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3708
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3708
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3708
diff changeset
    21
 * questions.
2
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 6384064
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check proper handling of interrupts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm -XX:-UseVMInterruptibleIO Interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static java.util.concurrent.TimeUnit.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Interrupt {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static void checkInterrupted0(Iterable<Fun> fs, Executor ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        for (Fun f : fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                ex.execute(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                        final Thread thisThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                        public void run() { thisThread.interrupt(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                f.f();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                fail("Expected InterruptedException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                check(! Thread.interrupted());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void checkInterrupted(Iterable<Fun> fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        final Executor immediateExecutor = new Executor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                public void execute(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    r.run(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        final ScheduledThreadPoolExecutor stpe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            = new ScheduledThreadPoolExecutor(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        final Executor delayedExecutor = new Executor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                public void execute(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    stpe.schedule(r, 20, MILLISECONDS); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        checkInterrupted0(fs, immediateExecutor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        checkInterrupted0(fs, delayedExecutor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        stpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static void testQueue(final BlockingQueue<Object> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            final BlockingDeque<Object> deq =
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    69
                (q instanceof BlockingDeque<?>) ?
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    70
                (BlockingDeque<Object>) q : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            List<Fun> fs = new ArrayList<Fun>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    { q.take(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    { q.poll(60, SECONDS); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (deq != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                        { deq.takeFirst(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        { deq.takeLast(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        { deq.pollFirst(7, SECONDS); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        { deq.pollLast(7, SECONDS); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            checkInterrupted(fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // fill q to capacity, to ensure insertions will block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            while (q.remainingCapacity() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                try { q.put(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            fs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    { q.put(1); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    { q.offer(1, 7, SECONDS); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (deq != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        { deq.putFirst(1); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        { deq.putLast(1); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        { deq.offerFirst(1, 7, SECONDS); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                fs.add(new Fun() { void f() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        { deq.offerLast(1, 7, SECONDS); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            checkInterrupted(fs);
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   111
        } catch (Throwable t) {
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   112
          System.out.printf("Failed: %s%n", q.getClass().getSimpleName());
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   113
          unexpected(t);
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   114
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static void realMain(final String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        testQueue(new SynchronousQueue<Object>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        testQueue(new ArrayBlockingQueue<Object>(1,false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        testQueue(new ArrayBlockingQueue<Object>(1,true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        testQueue(new LinkedBlockingQueue<Object>(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        testQueue(new LinkedBlockingDeque<Object>(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (failed > 0) throw new AssertionError("Some tests failed");}
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   139
    private abstract static class Fun {abstract void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}