test/jdk/java/net/httpclient/websocket/PendingTextPongClose.java
branchhttp-client-branch
changeset 56325 195d2970d981
child 56335 7e56c39fa1fa
equal deleted inserted replaced
56324:3edf200fff01 56325:195d2970d981
       
     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.websocket.debug=true
       
    29  *       PendingTextPongClose
       
    30  */
       
    31 
       
    32 import org.testng.annotations.Test;
       
    33 
       
    34 import java.net.http.WebSocket;
       
    35 import java.nio.ByteBuffer;
       
    36 import java.nio.CharBuffer;
       
    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 PendingTextPongClose extends PendingOperations {
       
    44 
       
    45     @Test(dataProvider = "booleans")
       
    46     public void pendingTextPongClose(boolean last) throws Exception {
       
    47         server = Support.notReadingServer();
       
    48         server.open();
       
    49         webSocket = newHttpClient().newWebSocketBuilder()
       
    50                 .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
    51                 .join();
       
    52         CharBuffer data = CharBuffer.allocate(65536);
       
    53         CompletableFuture<WebSocket> cfText;
       
    54         for (int i = 0; ; i++) {  // fill up the send buffer
       
    55             long start = System.currentTimeMillis();
       
    56             System.out.printf("begin cycle #%s at %s%n", i, start);
       
    57             cfText = webSocket.sendText(data, last);
       
    58             try {
       
    59                 cfText.get(5, TimeUnit.SECONDS);
       
    60                 data.clear();
       
    61             } catch (TimeoutException e) {
       
    62                 break;
       
    63             } finally {
       
    64                 long stop = System.currentTimeMillis();
       
    65                 System.out.printf("end cycle #%s at %s (%s ms)%n", i, stop, stop - start);
       
    66             }
       
    67         }
       
    68         assertFails(ISE, webSocket.sendText("", true));
       
    69         assertFails(ISE, webSocket.sendText("", false));
       
    70         assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(0), true));
       
    71         assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(0), false));
       
    72         CompletableFuture<WebSocket> cfPong = webSocket.sendPong(ByteBuffer.allocate(125));
       
    73         assertHangs(cfPong);
       
    74         assertFails(ISE, webSocket.sendPing(ByteBuffer.allocate(125)));
       
    75         assertFails(ISE, webSocket.sendPong(ByteBuffer.allocate(125)));
       
    76         CompletableFuture<WebSocket> cfClose = webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok");
       
    77         assertHangs(cfClose);
       
    78         webSocket.abort();
       
    79         assertFails(IOE, cfText);
       
    80         assertFails(IOE, cfPong);
       
    81         assertFails(IOE, cfClose);
       
    82     }
       
    83 }