test/jdk/java/net/httpclient/websocket/PendingBinaryPingClose.java
changeset 49765 ee6f7a61f3a5
child 49944 4690a2871b44
child 56451 9585061fdb04
equal deleted inserted replaced
49707:f7fd051519ac 49765:ee6f7a61f3a5
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @build DummyWebSocketServer
       
    27  * @run testng/othervm
       
    28  *      -Djdk.internal.httpclient.debug=true
       
    29  *      -Djdk.internal.httpclient.websocket.debug=true
       
    30  *       PendingBinaryPingClose
       
    31  */
       
    32 
       
    33 import org.testng.annotations.Test;
       
    34 
       
    35 import java.net.http.WebSocket;
       
    36 import java.nio.ByteBuffer;
       
    37 import java.util.concurrent.CompletableFuture;
       
    38 import java.util.concurrent.TimeUnit;
       
    39 import java.util.concurrent.TimeoutException;
       
    40 
       
    41 import static java.net.http.HttpClient.newHttpClient;
       
    42 
       
    43 public class PendingBinaryPingClose extends PendingOperations {
       
    44 
       
    45     CompletableFuture<WebSocket> cfBinary;
       
    46     CompletableFuture<WebSocket> cfPing;
       
    47     CompletableFuture<WebSocket> cfClose;
       
    48 
       
    49     @Test(dataProvider = "booleans")
       
    50     public void pendingBinaryPingClose(boolean last) throws Exception {
       
    51         repeatable(() -> {
       
    52             server = Support.notReadingServer();
       
    53             server.open();
       
    54             webSocket = newHttpClient().newWebSocketBuilder()
       
    55                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
    56                     .join();
       
    57             ByteBuffer data = ByteBuffer.allocate(65536);
       
    58             for (int i = 0; ; i++) {  // fill up the send buffer
       
    59                 long start = System.currentTimeMillis();
       
    60                 System.out.printf("begin cycle #%s at %s%n", i, start);
       
    61                 cfBinary = webSocket.sendBinary(data, last);
       
    62                 try {
       
    63                     cfBinary.get(MAX_WAIT_SEC, TimeUnit.SECONDS);
       
    64                     data.clear();
       
    65                 } catch (TimeoutException e) {
       
    66                     break;
       
    67                 } finally {
       
    68                     long stop = System.currentTimeMillis();
       
    69                     System.out.printf("end cycle #%s at %s (%s ms)%n", i, stop, stop - start);
       
    70                 }
       
    71             }
       
    72             assertFails(ISE, webSocket.sendText("", true));
       
    73             assertFails(ISE, webSocket.sendText("", false));
       
    74             assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(0), true));
       
    75             assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(0), false));
       
    76             cfPing = webSocket.sendPing(ByteBuffer.allocate(125));
       
    77             assertHangs(cfPing);
       
    78             assertFails(ISE, webSocket.sendPing(ByteBuffer.allocate(125)));
       
    79             assertFails(ISE, webSocket.sendPong(ByteBuffer.allocate(125)));
       
    80             cfClose = webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok");
       
    81             assertHangs(cfClose);
       
    82             return null;
       
    83         }, () -> cfBinary.isDone() ? true : false);
       
    84         webSocket.abort();
       
    85         assertFails(IOE, cfBinary);
       
    86         assertFails(IOE, cfPing);
       
    87         assertFails(IOE, cfClose);
       
    88     }
       
    89 }