test/jdk/java/net/httpclient/websocket/PendingPingTextClose.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  *       PendingPingTextClose
       
    29  */
       
    30 
       
    31 // This test produce huge logs (14Mb+) so disable logging by default
       
    32 // *      -Djdk.internal.httpclient.debug=true
       
    33 // *      -Djdk.internal.httpclient.websocket.debug=true
       
    34 
       
    35 import org.testng.annotations.Test;
       
    36 
       
    37 import java.net.http.WebSocket;
       
    38 import java.nio.ByteBuffer;
       
    39 import java.util.concurrent.CompletableFuture;
       
    40 import java.util.concurrent.TimeUnit;
       
    41 import java.util.concurrent.TimeoutException;
       
    42 
       
    43 import static java.net.http.HttpClient.newHttpClient;
       
    44 
       
    45 public class PendingPingTextClose extends PendingOperations {
       
    46 
       
    47     CompletableFuture<WebSocket> cfText;
       
    48     CompletableFuture<WebSocket> cfPing;
       
    49     CompletableFuture<WebSocket> cfClose;
       
    50 
       
    51     @Test(dataProvider = "booleans")
       
    52     public void pendingPingTextClose(boolean last) throws Exception {
       
    53         repeatable( () -> {
       
    54             server = Support.notReadingServer();
       
    55             server.open();
       
    56             webSocket = newHttpClient().newWebSocketBuilder()
       
    57                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
    58                     .join();
       
    59             ByteBuffer data = ByteBuffer.allocate(125);
       
    60             for (int i = 0; ; i++) {  // fill up the send buffer
       
    61                 long start = System.currentTimeMillis();
       
    62                 System.out.printf("begin cycle #%s at %s%n", i, start);
       
    63                 cfPing = webSocket.sendPing(data);
       
    64                 try {
       
    65                     cfPing.get(MAX_WAIT_SEC, TimeUnit.SECONDS);
       
    66                     data.clear();
       
    67                 } catch (TimeoutException e) {
       
    68                     break;
       
    69                 } finally {
       
    70                     long stop = System.currentTimeMillis();
       
    71                     System.out.printf("end cycle #%s at %s (%s ms)%n", i, stop, stop - start);
       
    72                 }
       
    73             }
       
    74             assertFails(ISE, webSocket.sendPing(ByteBuffer.allocate(125)));
       
    75             assertFails(ISE, webSocket.sendPong(ByteBuffer.allocate(125)));
       
    76             cfText = webSocket.sendText("hello", last);
       
    77             assertHangs(cfText);
       
    78             cfClose = webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok");
       
    79             assertHangs(cfClose);
       
    80             return null;
       
    81         }, () -> cfPing.isDone() ? true : false);
       
    82         webSocket.abort();
       
    83         assertFails(IOE, cfPing);
       
    84         assertFails(IOE, cfText);
       
    85         assertFails(IOE, cfClose);
       
    86     }
       
    87 }