test/jdk/java/net/httpclient/websocket/Abort.java
changeset 58289 3a79d4cccbcb
parent 49765 ee6f7a61f3a5
equal deleted inserted replaced
58288:48e480e56aad 58289:3a79d4cccbcb
     1 /*
     1 /*
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    27  * @run testng/othervm
    27  * @run testng/othervm
    28  *      -Djdk.internal.httpclient.websocket.debug=true
    28  *      -Djdk.internal.httpclient.websocket.debug=true
    29  *       Abort
    29  *       Abort
    30  */
    30  */
    31 
    31 
    32 import org.testng.annotations.AfterTest;
       
    33 import org.testng.annotations.Test;
    32 import org.testng.annotations.Test;
    34 
    33 
    35 import java.io.IOException;
    34 import java.io.IOException;
    36 import java.net.ProtocolException;
    35 import java.net.ProtocolException;
    37 import java.net.http.WebSocket;
    36 import java.net.http.WebSocket;
    54 
    53 
    55     private static final Class<NullPointerException> NPE = NullPointerException.class;
    54     private static final Class<NullPointerException> NPE = NullPointerException.class;
    56     private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
    55     private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
    57     private static final Class<IOException> IOE = IOException.class;
    56     private static final Class<IOException> IOE = IOException.class;
    58 
    57 
    59     private DummyWebSocketServer server;
       
    60     private WebSocket webSocket;
       
    61 
       
    62     @AfterTest
       
    63     public void cleanup() {
       
    64         server.close();
       
    65         webSocket.abort();
       
    66     }
       
    67 
    58 
    68     @Test
    59     @Test
    69     public void onOpenThenAbort() throws Exception {
    60     public void onOpenThenAbort() throws Exception {
    70         int[] bytes = new int[]{
    61         int[] bytes = new int[]{
    71                 0x88, 0x00, // opcode=close
    62                 0x88, 0x00, // opcode=close
    72         };
    63         };
    73         server = Support.serverWithCannedData(bytes);
    64         try (var server = Support.serverWithCannedData(bytes)) {
    74         server.open();
    65             server.open();
    75         // messages are available
    66             // messages are available
    76         MockListener listener = new MockListener() {
    67             MockListener listener = new MockListener() {
    77             @Override
    68                 @Override
    78             protected void onOpen0(WebSocket webSocket) {
    69                 protected void onOpen0(WebSocket webSocket) {
    79                 // unbounded request
    70                     // unbounded request
    80                 webSocket.request(Long.MAX_VALUE);
    71                     webSocket.request(Long.MAX_VALUE);
    81                 webSocket.abort();
    72                     webSocket.abort();
    82             }
    73                 }
    83         };
    74             };
    84         webSocket = newHttpClient().newWebSocketBuilder()
    75             var webSocket = newHttpClient().newWebSocketBuilder()
    85                 .buildAsync(server.getURI(), listener)
    76                     .buildAsync(server.getURI(), listener)
    86                 .join();
    77                     .join();
    87         TimeUnit.SECONDS.sleep(5);
    78             try {
    88         List<MockListener.Invocation> inv = listener.invocationsSoFar();
    79                 TimeUnit.SECONDS.sleep(5);
    89         // no more invocations after onOpen as WebSocket was aborted
    80                 List<MockListener.Invocation> inv = listener.invocationsSoFar();
    90         assertEquals(inv, List.of(MockListener.Invocation.onOpen(webSocket)));
    81                 // no more invocations after onOpen as WebSocket was aborted
       
    82                 assertEquals(inv, List.of(MockListener.Invocation.onOpen(webSocket)));
       
    83             } finally {
       
    84                 webSocket.abort();
       
    85             }
       
    86         }
    91     }
    87     }
    92 
    88 
    93     @Test
    89     @Test
    94     public void onOpenThenOnTextThenAbort() throws Exception {
    90     public void onOpenThenOnTextThenAbort() throws Exception {
    95         int[] bytes = new int[]{
    91         int[] bytes = new int[]{
    96                 0x81, 0x00, // opcode=text, fin=true
    92                 0x81, 0x00, // opcode=text, fin=true
    97                 0x88, 0x00, // opcode=close
    93                 0x88, 0x00, // opcode=close
    98         };
    94         };
    99         server = Support.serverWithCannedData(bytes);
    95         try (var server = Support.serverWithCannedData(bytes)) {
   100         server.open();
    96             server.open();
   101         MockListener listener = new MockListener() {
    97             MockListener listener = new MockListener() {
   102             @Override
    98                 @Override
   103             protected void onOpen0(WebSocket webSocket) {
    99                 protected void onOpen0(WebSocket webSocket) {
   104                 // unbounded request
   100                     // unbounded request
   105                 webSocket.request(Long.MAX_VALUE);
   101                     webSocket.request(Long.MAX_VALUE);
   106             }
   102                 }
   107 
   103 
   108             @Override
   104                 @Override
   109             protected CompletionStage<?> onText0(WebSocket webSocket,
   105                 protected CompletionStage<?> onText0(WebSocket webSocket,
   110                                                  CharSequence message,
   106                                                      CharSequence message,
   111                                                  boolean last) {
   107                                                      boolean last) {
   112                 webSocket.abort();
   108                     webSocket.abort();
   113                 return super.onText0(webSocket, message, last);
   109                     return super.onText0(webSocket, message, last);
   114             }
   110                 }
   115         };
   111             };
   116         webSocket = newHttpClient().newWebSocketBuilder()
   112             var webSocket = newHttpClient().newWebSocketBuilder()
   117                 .buildAsync(server.getURI(), listener)
   113                     .buildAsync(server.getURI(), listener)
   118                 .join();
   114                     .join();
   119         TimeUnit.SECONDS.sleep(5);
   115             try {
   120         List<MockListener.Invocation> inv = listener.invocationsSoFar();
   116                 TimeUnit.SECONDS.sleep(5);
   121         // no more invocations after onOpen, onBinary as WebSocket was aborted
   117                 List<MockListener.Invocation> inv = listener.invocationsSoFar();
   122         List<MockListener.Invocation> expected = List.of(
   118                 // no more invocations after onOpen, onBinary as WebSocket was aborted
   123                 MockListener.Invocation.onOpen(webSocket),
   119                 List<MockListener.Invocation> expected = List.of(
   124                 MockListener.Invocation.onText(webSocket, "", true));
   120                         MockListener.Invocation.onOpen(webSocket),
   125         assertEquals(inv, expected);
   121                         MockListener.Invocation.onText(webSocket, "", true));
       
   122                 assertEquals(inv, expected);
       
   123             } finally {
       
   124                 webSocket.abort();
       
   125             }
       
   126         }
   126     }
   127     }
   127 
   128 
   128     @Test
   129     @Test
   129     public void onOpenThenOnBinaryThenAbort() throws Exception {
   130     public void onOpenThenOnBinaryThenAbort() throws Exception {
   130         int[] bytes = new int[]{
   131         int[] bytes = new int[]{
   131                 0x82, 0x00, // opcode=binary, fin=true
   132                 0x82, 0x00, // opcode=binary, fin=true
   132                 0x88, 0x00, // opcode=close
   133                 0x88, 0x00, // opcode=close
   133         };
   134         };
   134         server = Support.serverWithCannedData(bytes);
   135         try (var server = Support.serverWithCannedData(bytes)) {
   135         server.open();
   136             server.open();
   136         MockListener listener = new MockListener() {
   137             MockListener listener = new MockListener() {
   137             @Override
   138                 @Override
   138             protected void onOpen0(WebSocket webSocket) {
   139                 protected void onOpen0(WebSocket webSocket) {
   139                 // unbounded request
   140                     // unbounded request
   140                 webSocket.request(Long.MAX_VALUE);
   141                     webSocket.request(Long.MAX_VALUE);
   141             }
   142                 }
   142 
   143 
   143             @Override
   144                 @Override
   144             protected CompletionStage<?> onBinary0(WebSocket webSocket,
   145                 protected CompletionStage<?> onBinary0(WebSocket webSocket,
   145                                                    ByteBuffer message,
   146                                                        ByteBuffer message,
   146                                                    boolean last) {
   147                                                        boolean last) {
   147                 webSocket.abort();
   148                     webSocket.abort();
   148                 return super.onBinary0(webSocket, message, last);
   149                     return super.onBinary0(webSocket, message, last);
   149             }
   150                 }
   150         };
   151             };
   151         webSocket = newHttpClient().newWebSocketBuilder()
   152             var webSocket = newHttpClient().newWebSocketBuilder()
   152                 .buildAsync(server.getURI(), listener)
   153                     .buildAsync(server.getURI(), listener)
   153                 .join();
   154                     .join();
   154         TimeUnit.SECONDS.sleep(5);
   155             try {
   155         List<MockListener.Invocation> inv = listener.invocationsSoFar();
   156                 TimeUnit.SECONDS.sleep(5);
   156         // no more invocations after onOpen, onBinary as WebSocket was aborted
   157                 List<MockListener.Invocation> inv = listener.invocationsSoFar();
   157         List<MockListener.Invocation> expected = List.of(
   158                 // no more invocations after onOpen, onBinary as WebSocket was aborted
   158                 MockListener.Invocation.onOpen(webSocket),
   159                 List<MockListener.Invocation> expected = List.of(
   159                 MockListener.Invocation.onBinary(webSocket, ByteBuffer.allocate(0), true));
   160                         MockListener.Invocation.onOpen(webSocket),
   160         assertEquals(inv, expected);
   161                         MockListener.Invocation.onBinary(webSocket, ByteBuffer.allocate(0), true));
       
   162                 assertEquals(inv, expected);
       
   163             } finally {
       
   164                 webSocket.abort();
       
   165             }
       
   166         }
   161     }
   167     }
   162 
   168 
   163     @Test
   169     @Test
   164     public void onOpenThenOnPingThenAbort() throws Exception {
   170     public void onOpenThenOnPingThenAbort() throws Exception {
   165         int[] bytes = {
   171         int[] bytes = {
   166                 0x89, 0x00, // opcode=ping
   172                 0x89, 0x00, // opcode=ping
   167                 0x88, 0x00, // opcode=close
   173                 0x88, 0x00, // opcode=close
   168         };
   174         };
   169         server = Support.serverWithCannedData(bytes);
   175         try (var server = Support.serverWithCannedData(bytes)) {
   170         server.open();
   176             server.open();
   171         MockListener listener = new MockListener() {
   177             MockListener listener = new MockListener() {
   172             @Override
   178                 @Override
   173             protected void onOpen0(WebSocket webSocket) {
   179                 protected void onOpen0(WebSocket webSocket) {
   174                 // unbounded request
   180                     // unbounded request
   175                 webSocket.request(Long.MAX_VALUE);
   181                     webSocket.request(Long.MAX_VALUE);
   176             }
   182                 }
   177 
   183 
   178             @Override
   184                 @Override
   179             protected CompletionStage<?> onPing0(WebSocket webSocket,
   185                 protected CompletionStage<?> onPing0(WebSocket webSocket,
   180                                                  ByteBuffer message) {
   186                                                      ByteBuffer message) {
   181                 webSocket.abort();
   187                     webSocket.abort();
   182                 return super.onPing0(webSocket, message);
   188                     return super.onPing0(webSocket, message);
   183             }
   189                 }
   184         };
   190             };
   185         webSocket = newHttpClient().newWebSocketBuilder()
   191             var webSocket = newHttpClient().newWebSocketBuilder()
   186                 .buildAsync(server.getURI(), listener)
   192                     .buildAsync(server.getURI(), listener)
   187                 .join();
   193                     .join();
   188         TimeUnit.SECONDS.sleep(5);
   194             try {
   189         List<MockListener.Invocation> inv = listener.invocationsSoFar();
   195                 TimeUnit.SECONDS.sleep(5);
   190         // no more invocations after onOpen, onPing as WebSocket was aborted
   196                 List<MockListener.Invocation> inv = listener.invocationsSoFar();
   191         List<MockListener.Invocation> expected = List.of(
   197                 // no more invocations after onOpen, onPing as WebSocket was aborted
   192                 MockListener.Invocation.onOpen(webSocket),
   198                 List<MockListener.Invocation> expected = List.of(
   193                 MockListener.Invocation.onPing(webSocket, ByteBuffer.allocate(0)));
   199                         MockListener.Invocation.onOpen(webSocket),
   194         assertEquals(inv, expected);
   200                         MockListener.Invocation.onPing(webSocket, ByteBuffer.allocate(0)));
       
   201                 assertEquals(inv, expected);
       
   202             } finally {
       
   203                 webSocket.abort();
       
   204             }
       
   205         }
   195     }
   206     }
   196 
   207 
   197     @Test
   208     @Test
   198     public void onOpenThenOnPongThenAbort() throws Exception {
   209     public void onOpenThenOnPongThenAbort() throws Exception {
   199         int[] bytes = {
   210         int[] bytes = {
   200                 0x8a, 0x00, // opcode=pong
   211                 0x8a, 0x00, // opcode=pong
   201                 0x88, 0x00, // opcode=close
   212                 0x88, 0x00, // opcode=close
   202         };
   213         };
   203         server = Support.serverWithCannedData(bytes);
   214         try (var server = Support.serverWithCannedData(bytes)) {
   204         server.open();
   215             server.open();
   205         MockListener listener = new MockListener() {
   216             MockListener listener = new MockListener() {
   206             @Override
   217                 @Override
   207             protected void onOpen0(WebSocket webSocket) {
   218                 protected void onOpen0(WebSocket webSocket) {
   208                 // unbounded request
   219                     // unbounded request
   209                 webSocket.request(Long.MAX_VALUE);
   220                     webSocket.request(Long.MAX_VALUE);
   210             }
   221                 }
   211 
   222 
   212             @Override
   223                 @Override
   213             protected CompletionStage<?> onPong0(WebSocket webSocket,
   224                 protected CompletionStage<?> onPong0(WebSocket webSocket,
   214                                                  ByteBuffer message) {
   225                                                      ByteBuffer message) {
   215                 webSocket.abort();
   226                     webSocket.abort();
   216                 return super.onPong0(webSocket, message);
   227                     return super.onPong0(webSocket, message);
   217             }
   228                 }
   218         };
   229             };
   219         webSocket = newHttpClient().newWebSocketBuilder()
   230             var webSocket = newHttpClient().newWebSocketBuilder()
   220                 .buildAsync(server.getURI(), listener)
   231                     .buildAsync(server.getURI(), listener)
   221                 .join();
   232                     .join();
   222         TimeUnit.SECONDS.sleep(5);
   233             try {
   223         List<MockListener.Invocation> inv = listener.invocationsSoFar();
   234                 TimeUnit.SECONDS.sleep(5);
   224         // no more invocations after onOpen, onPong as WebSocket was aborted
   235                 List<MockListener.Invocation> inv = listener.invocationsSoFar();
   225         List<MockListener.Invocation> expected = List.of(
   236                 // no more invocations after onOpen, onPong as WebSocket was aborted
   226                 MockListener.Invocation.onOpen(webSocket),
   237                 List<MockListener.Invocation> expected = List.of(
   227                 MockListener.Invocation.onPong(webSocket, ByteBuffer.allocate(0)));
   238                         MockListener.Invocation.onOpen(webSocket),
   228         assertEquals(inv, expected);
   239                         MockListener.Invocation.onPong(webSocket, ByteBuffer.allocate(0)));
       
   240                 assertEquals(inv, expected);
       
   241             } finally {
       
   242                 webSocket.abort();
       
   243             }
       
   244         }
   229     }
   245     }
   230 
   246 
   231     @Test
   247     @Test
   232     public void onOpenThenOnCloseThenAbort() throws Exception {
   248     public void onOpenThenOnCloseThenAbort() throws Exception {
   233         int[] bytes = {
   249         int[] bytes = {
   234                 0x88, 0x00, // opcode=close
   250                 0x88, 0x00, // opcode=close
   235                 0x8a, 0x00, // opcode=pong
   251                 0x8a, 0x00, // opcode=pong
   236         };
   252         };
   237         server = Support.serverWithCannedData(bytes);
   253         try (var server = Support.serverWithCannedData(bytes)) {
   238         server.open();
   254             server.open();
   239         MockListener listener = new MockListener() {
   255             MockListener listener = new MockListener() {
   240             @Override
   256                 @Override
   241             protected void onOpen0(WebSocket webSocket) {
   257                 protected void onOpen0(WebSocket webSocket) {
   242                 // unbounded request
   258                     // unbounded request
   243                 webSocket.request(Long.MAX_VALUE);
   259                     webSocket.request(Long.MAX_VALUE);
   244             }
   260                 }
   245 
   261 
   246             @Override
   262                 @Override
   247             protected CompletionStage<?> onClose0(WebSocket webSocket,
   263                 protected CompletionStage<?> onClose0(WebSocket webSocket,
   248                                                   int statusCode,
   264                                                       int statusCode,
   249                                                   String reason) {
   265                                                       String reason) {
   250                 webSocket.abort();
   266                     webSocket.abort();
   251                 return super.onClose0(webSocket, statusCode, reason);
   267                     return super.onClose0(webSocket, statusCode, reason);
   252             }
   268                 }
   253         };
   269             };
   254         webSocket = newHttpClient().newWebSocketBuilder()
   270             var webSocket = newHttpClient().newWebSocketBuilder()
   255                 .buildAsync(server.getURI(), listener)
   271                     .buildAsync(server.getURI(), listener)
   256                 .join();
   272                     .join();
   257         TimeUnit.SECONDS.sleep(5);
   273             try {
   258         List<MockListener.Invocation> inv = listener.invocationsSoFar();
   274                 TimeUnit.SECONDS.sleep(5);
   259         // no more invocations after onOpen, onClose
   275                 List<MockListener.Invocation> inv = listener.invocationsSoFar();
   260         List<MockListener.Invocation> expected = List.of(
   276                 // no more invocations after onOpen, onClose
   261                 MockListener.Invocation.onOpen(webSocket),
   277                 List<MockListener.Invocation> expected = List.of(
   262                 MockListener.Invocation.onClose(webSocket, 1005, ""));
   278                         MockListener.Invocation.onOpen(webSocket),
   263         assertEquals(inv, expected);
   279                         MockListener.Invocation.onClose(webSocket, 1005, ""));
       
   280                 assertEquals(inv, expected);
       
   281             } finally {
       
   282                 webSocket.abort();
       
   283             }
       
   284         }
   264     }
   285     }
   265 
   286 
   266     @Test
   287     @Test
   267     public void onOpenThenOnErrorThenAbort() throws Exception {
   288     public void onOpenThenOnErrorThenAbort() throws Exception {
   268         // A header of 128 bytes long Ping (which is a protocol error)
   289         // A header of 128 bytes long Ping (which is a protocol error)
   269         int[] badPingHeader = new int[]{0x89, 0x7e, 0x00, 0x80};
   290         int[] badPingHeader = new int[]{0x89, 0x7e, 0x00, 0x80};
   270         int[] closeMessage = new int[]{0x88, 0x00};
   291         int[] closeMessage = new int[]{0x88, 0x00};
   271         int[] bytes = new int[badPingHeader.length + 128 + closeMessage.length];
   292         int[] bytes = new int[badPingHeader.length + 128 + closeMessage.length];
   272         System.arraycopy(badPingHeader, 0, bytes, 0, badPingHeader.length);
   293         System.arraycopy(badPingHeader, 0, bytes, 0, badPingHeader.length);
   273         System.arraycopy(closeMessage, 0, bytes, badPingHeader.length + 128, closeMessage.length);
   294         System.arraycopy(closeMessage, 0, bytes, badPingHeader.length + 128, closeMessage.length);
   274         server = Support.serverWithCannedData(bytes);
   295         try (var server = Support.serverWithCannedData(bytes)) {
   275         server.open();
   296             server.open();
   276         MockListener listener = new MockListener() {
   297             MockListener listener = new MockListener() {
   277             @Override
   298                 @Override
   278             protected void onOpen0(WebSocket webSocket) {
   299                 protected void onOpen0(WebSocket webSocket) {
   279                 // unbounded request
   300                     // unbounded request
   280                 webSocket.request(Long.MAX_VALUE);
   301                     webSocket.request(Long.MAX_VALUE);
   281             }
   302                 }
   282 
   303 
   283             @Override
   304                 @Override
   284             protected void onError0(WebSocket webSocket, Throwable error) {
   305                 protected void onError0(WebSocket webSocket, Throwable error) {
   285                 webSocket.abort();
   306                     webSocket.abort();
   286                 super.onError0(webSocket, error);
   307                     super.onError0(webSocket, error);
   287             }
   308                 }
   288         };
   309             };
   289         webSocket = newHttpClient().newWebSocketBuilder()
   310             var webSocket = newHttpClient().newWebSocketBuilder()
   290                 .buildAsync(server.getURI(), listener)
   311                     .buildAsync(server.getURI(), listener)
   291                 .join();
   312                     .join();
   292         TimeUnit.SECONDS.sleep(5);
   313             try {
   293         List<MockListener.Invocation> inv = listener.invocationsSoFar();
   314                 TimeUnit.SECONDS.sleep(5);
   294         // no more invocations after onOpen, onError
   315                 List<MockListener.Invocation> inv = listener.invocationsSoFar();
   295         List<MockListener.Invocation> expected = List.of(
   316                 // no more invocations after onOpen, onError
   296                 MockListener.Invocation.onOpen(webSocket),
   317                 List<MockListener.Invocation> expected = List.of(
   297                 MockListener.Invocation.onError(webSocket, ProtocolException.class));
   318                         MockListener.Invocation.onOpen(webSocket),
   298         System.out.println("actual invocations:" + Arrays.toString(inv.toArray()));
   319                         MockListener.Invocation.onError(webSocket, ProtocolException.class));
   299         assertEquals(inv, expected);
   320                 System.out.println("actual invocations:" + Arrays.toString(inv.toArray()));
       
   321                 assertEquals(inv, expected);
       
   322             } finally {
       
   323                 webSocket.abort();
       
   324             }
       
   325         }
   300     }
   326     }
   301 
   327 
   302     @Test
   328     @Test
   303     public void immediateAbort() throws Exception {
   329     public void immediateAbort() throws Exception {
   304         CompletableFuture<Void> messageReceived = new CompletableFuture<>();
   330         CompletableFuture<Void> messageReceived = new CompletableFuture<>();
   350 
   376 
   351         int[] bytes = new int[]{
   377         int[] bytes = new int[]{
   352                 0x82, 0x00, // opcode=binary, fin=true
   378                 0x82, 0x00, // opcode=binary, fin=true
   353                 0x88, 0x00, // opcode=close
   379                 0x88, 0x00, // opcode=close
   354         };
   380         };
   355         server = Support.serverWithCannedData(bytes);
   381         try (var server = Support.serverWithCannedData(bytes)) {
   356         server.open();
   382             server.open();
   357 
   383 
   358         WebSocket ws = newHttpClient()
   384             WebSocket ws = newHttpClient()
   359                 .newWebSocketBuilder()
   385                     .newWebSocketBuilder()
   360                 .buildAsync(server.getURI(), listener)
   386                     .buildAsync(server.getURI(), listener)
   361                 .join();
   387                     .join();
   362         for (int i = 0; i < 3; i++) {
   388             try {
   363             System.out.printf("iteration #%s%n", i);
   389                 for (int i = 0; i < 3; i++) {
   364             // after the first abort() each consecutive one must be a no-op,
   390                     System.out.printf("iteration #%s%n", i);
   365             // moreover, query methods should continue to return consistent
   391                     // after the first abort() each consecutive one must be a no-op,
   366             // values
   392                     // moreover, query methods should continue to return consistent
   367             for (int j = 0; j < 3; j++) {
   393                     // values
   368                 System.out.printf("abort #%s%n", j);
   394                     for (int j = 0; j < 3; j++) {
       
   395                         System.out.printf("abort #%s%n", j);
       
   396                         ws.abort();
       
   397                         assertTrue(ws.isInputClosed());
       
   398                         assertTrue(ws.isOutputClosed());
       
   399                         assertEquals(ws.getSubprotocol(), "");
       
   400                     }
       
   401                     // at this point valid requests MUST be a no-op:
       
   402                     for (int j = 0; j < 3; j++) {
       
   403                         System.out.printf("request #%s%n", j);
       
   404                         ws.request(1);
       
   405                         ws.request(2);
       
   406                         ws.request(8);
       
   407                         ws.request(Integer.MAX_VALUE);
       
   408                         ws.request(Long.MAX_VALUE);
       
   409                         // invalid requests MUST throw IAE:
       
   410                         assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
       
   411                         assertThrows(IAE, () -> ws.request(Long.MIN_VALUE));
       
   412                         assertThrows(IAE, () -> ws.request(-1));
       
   413                         assertThrows(IAE, () -> ws.request(0));
       
   414                     }
       
   415                 }
       
   416                 // even though there is a bunch of messages readily available on the
       
   417                 // wire we shouldn't have received any of them as we aborted before
       
   418                 // the first request
       
   419                 try {
       
   420                     messageReceived.get(5, TimeUnit.SECONDS);
       
   421                     fail();
       
   422                 } catch (TimeoutException expected) {
       
   423                     System.out.println("Finished waiting");
       
   424                 }
       
   425                 for (int i = 0; i < 3; i++) {
       
   426                     System.out.printf("send #%s%n", i);
       
   427                     Support.assertFails(IOE, ws.sendText("text!", false));
       
   428                     Support.assertFails(IOE, ws.sendText("text!", true));
       
   429                     Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false));
       
   430                     Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true));
       
   431                     Support.assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16)));
       
   432                     Support.assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16)));
       
   433                     Support.assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason"));
       
   434                     assertThrows(NPE, () -> ws.sendText(null, false));
       
   435                     assertThrows(NPE, () -> ws.sendText(null, true));
       
   436                     assertThrows(NPE, () -> ws.sendBinary(null, false));
       
   437                     assertThrows(NPE, () -> ws.sendBinary(null, true));
       
   438                     assertThrows(NPE, () -> ws.sendPing(null));
       
   439                     assertThrows(NPE, () -> ws.sendPong(null));
       
   440                     assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
       
   441                 }
       
   442             } finally {
   369                 ws.abort();
   443                 ws.abort();
   370                 assertTrue(ws.isInputClosed());
   444             }
   371                 assertTrue(ws.isOutputClosed());
       
   372                 assertEquals(ws.getSubprotocol(), "");
       
   373             }
       
   374             // at this point valid requests MUST be a no-op:
       
   375             for (int j = 0; j < 3; j++) {
       
   376                 System.out.printf("request #%s%n", j);
       
   377                 ws.request(1);
       
   378                 ws.request(2);
       
   379                 ws.request(8);
       
   380                 ws.request(Integer.MAX_VALUE);
       
   381                 ws.request(Long.MAX_VALUE);
       
   382                 // invalid requests MUST throw IAE:
       
   383                 assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
       
   384                 assertThrows(IAE, () -> ws.request(Long.MIN_VALUE));
       
   385                 assertThrows(IAE, () -> ws.request(-1));
       
   386                 assertThrows(IAE, () -> ws.request(0));
       
   387             }
       
   388         }
       
   389         // even though there is a bunch of messages readily available on the
       
   390         // wire we shouldn't have received any of them as we aborted before
       
   391         // the first request
       
   392         try {
       
   393             messageReceived.get(5, TimeUnit.SECONDS);
       
   394             fail();
       
   395         } catch (TimeoutException expected) {
       
   396             System.out.println("Finished waiting");
       
   397         }
       
   398         for (int i = 0; i < 3; i++) {
       
   399             System.out.printf("send #%s%n", i);
       
   400             Support.assertFails(IOE, ws.sendText("text!", false));
       
   401             Support.assertFails(IOE, ws.sendText("text!", true));
       
   402             Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false));
       
   403             Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true));
       
   404             Support.assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16)));
       
   405             Support.assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16)));
       
   406             Support.assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason"));
       
   407             assertThrows(NPE, () -> ws.sendText(null, false));
       
   408             assertThrows(NPE, () -> ws.sendText(null, true));
       
   409             assertThrows(NPE, () -> ws.sendBinary(null, false));
       
   410             assertThrows(NPE, () -> ws.sendBinary(null, true));
       
   411             assertThrows(NPE, () -> ws.sendPing(null));
       
   412             assertThrows(NPE, () -> ws.sendPong(null));
       
   413             assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
       
   414         }
   445         }
   415     }
   446     }
   416 }
   447 }