test/jdk/java/net/httpclient/http2/server/Queue.java
author dfuchs
Mon, 27 Nov 2017 16:40:01 +0000
branchhttp-client-branch
changeset 55892 9f345a976249
parent 55852 32f6aefec11e
child 55941 2d423c9b73bb
permissions -rw-r--r--
http-client-branch: improve coverage for ExceptionallyCloseable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     1
/*
55819
18e431209168 http-client-branch: Update copyright years in tests
chegar
parents: 55799
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     4
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    10
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    15
 * accompanied this code).
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    16
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    20
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    23
 * questions.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    24
 */
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    25
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    26
import java.io.IOException;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    27
import java.util.LinkedList;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    28
import java.util.stream.Stream;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    29
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    30
// Each stream has one of these for input. Each Http2Connection has one
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    31
// for output. Can be used blocking or asynchronously.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    32
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    33
public class Queue<T> implements ExceptionallyCloseable {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    34
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    35
    private final LinkedList<T> q = new LinkedList<>();
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    36
    private boolean closed = false;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    37
    private boolean closing = false;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    38
    private Throwable exception = null;
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    39
    private Runnable callback;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    40
    private boolean callbackDisabled = false;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    41
    private int waiters; // true if someone waiting
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    42
    private final T closeSentinel;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    43
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    44
    Queue(T closeSentinel) {
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    45
        this.closeSentinel = closeSentinel;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    46
    }
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    47
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    48
    public synchronized int size() {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    49
        return q.size();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    50
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    51
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    52
    public synchronized void put(T obj) throws IOException {
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    53
        if (closed || closing) {
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    54
            throw new IOException("stream closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    55
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    56
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    57
        q.add(obj);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    58
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    59
        if (waiters > 0) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    60
            notifyAll();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    61
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    62
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    63
        if (callbackDisabled) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    64
            return;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    65
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    66
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    67
        if (q.size() > 0 && callback != null) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    68
            // Note: calling callback while holding the lock is
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    69
            // dangerous and may lead to deadlocks.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    70
            callback.run();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    71
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    72
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    73
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    74
    // Other close() variants are immediate and abortive
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    75
    // This allows whatever is on Q to be processed first.
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    76
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    77
    public synchronized void orderlyClose() {
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    78
        if (closing || closed)
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    79
            return;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    80
        try {
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    81
            put(closeSentinel);
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    82
        } catch (IOException e) {
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    83
            e.printStackTrace();
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    84
        }
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    85
        closing = true;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
    86
    }
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    87
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    88
    @Override
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    89
    public synchronized void close() {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    90
        closed = true;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    91
        notifyAll();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    92
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    93
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    94
    @Override
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    95
    public synchronized void closeExceptionally(Throwable t) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    96
        if (exception == null) exception = t;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    97
        else if (t != null && t != exception) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    98
            if (!Stream.of(exception.getSuppressed())
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    99
                .filter(x -> x == t)
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   100
                .findFirst()
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   101
                .isPresent())
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   102
            {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   103
                exception.addSuppressed(t);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   104
            }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   105
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   106
        close();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   107
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   108
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   109
    public synchronized T take() throws IOException {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   110
        if (closed) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   111
            throw newIOException("stream closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   112
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   113
        try {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   114
            while (q.size() == 0) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   115
                waiters++;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   116
                wait();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   117
                if (closed) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   118
                    throw newIOException("Queue closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   119
                }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   120
                waiters--;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   121
            }
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
   122
            T item = q.removeFirst();
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
   123
            if (item.equals(closeSentinel)) {
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
   124
                closed = true;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
   125
                assert q.isEmpty();
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
   126
            }
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
   127
            return item;
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   128
        } catch (InterruptedException ex) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   129
            throw new IOException(ex);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   130
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   131
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   132
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   133
    public synchronized T poll() throws IOException {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   134
        if (closed) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   135
            throw newIOException("stream closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   136
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   137
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   138
        if (q.isEmpty()) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   139
            return null;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   140
        }
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55819
diff changeset
   141
        return take();
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   142
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   143
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   144
    private IOException newIOException(String msg) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   145
        if (exception == null) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   146
            return new IOException(msg);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   147
        } else {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   148
            return new IOException(msg, exception);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   149
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   150
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   151
}