jdk/test/java/util/concurrent/BlockingQueue/Interrupt.java
author dl
Thu, 03 Mar 2016 10:43:07 -0800
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
child 43522 f9c6f543c4db
permissions -rw-r--r--
8150523: improve jtreg test timeout handling, especially -timeout: Reviewed-by: martin, psandoz, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
     2
 * Copyright (c) 2006, 2014, 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
 * @author Martin Buchholz
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    29
 * @library /lib/testlibrary/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    32
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    33
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    34
import java.util.ArrayList;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    35
import java.util.List;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    36
import java.util.concurrent.ArrayBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    37
import java.util.concurrent.BlockingDeque;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    38
import java.util.concurrent.BlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    39
import java.util.concurrent.LinkedBlockingDeque;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    40
import java.util.concurrent.LinkedBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    41
import java.util.concurrent.SynchronousQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    42
import java.util.concurrent.Executor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    43
import java.util.concurrent.ScheduledThreadPoolExecutor;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    44
import jdk.testlibrary.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class Interrupt {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    47
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void checkInterrupted0(Iterable<Fun> fs, Executor ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (Fun f : fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                ex.execute(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                        final Thread thisThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                        public void run() { thisThread.interrupt(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                f.f();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                fail("Expected InterruptedException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                check(! Thread.interrupted());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 24692
diff changeset
    63
    static void checkInterrupted(Iterable<Fun> fs)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 24692
diff changeset
    64
            throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        final Executor immediateExecutor = new Executor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                public void execute(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    r.run(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        final ScheduledThreadPoolExecutor stpe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            = new ScheduledThreadPoolExecutor(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        final Executor delayedExecutor = new Executor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                public void execute(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    stpe.schedule(r, 20, MILLISECONDS); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        checkInterrupted0(fs, immediateExecutor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        checkInterrupted0(fs, delayedExecutor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        stpe.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    76
        check(stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static void testQueue(final BlockingQueue<Object> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            final BlockingDeque<Object> deq =
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    82
                (q instanceof BlockingDeque<?>) ?
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
    83
                (BlockingDeque<Object>) q : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            List<Fun> fs = new ArrayList<Fun>();
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
    86
            fs.add(() -> q.take());
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    87
            fs.add(() -> q.poll(LONG_DELAY_MS, MILLISECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (deq != null) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
    89
                fs.add(() -> deq.takeFirst());
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
    90
                fs.add(() -> deq.takeLast());
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    91
                fs.add(() -> deq.pollFirst(LONG_DELAY_MS, MILLISECONDS));
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    92
                fs.add(() -> deq.pollLast(LONG_DELAY_MS, MILLISECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            checkInterrupted(fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            // fill q to capacity, to ensure insertions will block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            while (q.remainingCapacity() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                try { q.put(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            fs.clear();
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
   103
            fs.add(() -> q.put(1));
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   104
            fs.add(() -> q.offer(1, LONG_DELAY_MS, MILLISECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (deq != null) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
   106
                fs.add(() -> deq.putFirst(1));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
   107
                fs.add(() -> deq.putLast(1));
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   108
                fs.add(() -> deq.offerFirst(1, LONG_DELAY_MS, MILLISECONDS));
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   109
                fs.add(() -> deq.offerLast(1, LONG_DELAY_MS, MILLISECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            checkInterrupted(fs);
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   112
        } catch (Throwable t) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 24692
diff changeset
   113
            System.out.printf("Failed: %s%n", q.getClass().getSimpleName());
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 24692
diff changeset
   114
            unexpected(t);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 24692
diff changeset
   115
        } finally {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 24692
diff changeset
   116
            Thread.interrupted();       // clear interrupts, just in case
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   117
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static void realMain(final String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        testQueue(new SynchronousQueue<Object>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        testQueue(new ArrayBlockingQueue<Object>(1,false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        testQueue(new ArrayBlockingQueue<Object>(1,true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        testQueue(new LinkedBlockingQueue<Object>(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        testQueue(new LinkedBlockingDeque<Object>(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 22952
diff changeset
   142
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}