test/jdk/java/net/httpclient/websocket/Exceptionally.java
branchhttp-client-branch
changeset 56046 75d9bc7febf5
parent 56045 5c6e3b76d2ad
child 56047 2e86bda80f79
equal deleted inserted replaced
56045:5c6e3b76d2ad 56046:75d9bc7febf5
   132             assertThrows(IAE, () -> ws.request(-1));
   132             assertThrows(IAE, () -> ws.request(-1));
   133             assertThrows(IAE, () -> ws.request(0));
   133             assertThrows(IAE, () -> ws.request(0));
   134         }
   134         }
   135     }
   135     }
   136 
   136 
   137     @Test
       
   138     public void testIllegalStateOutstanding1() throws Exception {
       
   139         try (DummyWebSocketServer server = notReadingServer()) {
       
   140             server.open();
       
   141             WebSocket ws = newHttpClient()
       
   142                     .newWebSocketBuilder()
       
   143                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   144                     .join();
       
   145 
       
   146             ByteBuffer data = ByteBuffer.allocate(65536);
       
   147             for (int i = 0; ; i++) {
       
   148                 System.out.println("cycle #" + i);
       
   149                 try {
       
   150                     ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
       
   151                     data.clear();
       
   152                 } catch (TimeoutException e) {
       
   153                     break;
       
   154                 }
       
   155             }
       
   156             assertISE(ws.sendBinary(ByteBuffer.allocate(0), true));
       
   157             assertISE(ws.sendText("", true));
       
   158         }
       
   159     }
       
   160 
       
   161     @Test
       
   162     public void testIllegalStateOutstanding2() throws Exception {
       
   163         try (DummyWebSocketServer server = notReadingServer()) {
       
   164             server.open();
       
   165             WebSocket ws = newHttpClient()
       
   166                     .newWebSocketBuilder()
       
   167                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   168                     .join();
       
   169 
       
   170             CharBuffer data = CharBuffer.allocate(65536);
       
   171             for (int i = 0; ; i++) {
       
   172                 System.out.println("cycle #" + i);
       
   173                 try {
       
   174                     ws.sendText(data, true).get(10, TimeUnit.SECONDS);
       
   175                     data.clear();
       
   176                 } catch (TimeoutException e) {
       
   177                     break;
       
   178                 }
       
   179             }
       
   180             assertISE(ws.sendText("", true));
       
   181             assertISE(ws.sendBinary(ByteBuffer.allocate(0), true));
       
   182         }
       
   183     }
       
   184 
       
   185     private static DummyWebSocketServer notReadingServer() {
       
   186         return new DummyWebSocketServer() {
       
   187             @Override
       
   188             protected void serve(SocketChannel channel) throws IOException {
       
   189                 try {
       
   190                     Thread.sleep(Long.MAX_VALUE);
       
   191                 } catch (InterruptedException e) {
       
   192                     throw new IOException(e);
       
   193                 }
       
   194             }
       
   195         };
       
   196     }
       
   197 
       
   198     @Test
       
   199     public void testIllegalStateIntermixed1() throws IOException {
       
   200         try (DummyWebSocketServer server = new DummyWebSocketServer()) {
       
   201             server.open();
       
   202             WebSocket ws = newHttpClient()
       
   203                     .newWebSocketBuilder()
       
   204                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   205                     .join();
       
   206 
       
   207             ws.sendBinary(ByteBuffer.allocate(16), false).join();
       
   208             assertISE(ws.sendText("text", false));
       
   209             assertISE(ws.sendText("text", true));
       
   210         }
       
   211     }
       
   212 
       
   213     @Test
       
   214     public void testIllegalStateIntermixed2() throws IOException {
       
   215         try (DummyWebSocketServer server = new DummyWebSocketServer()) {
       
   216             server.open();
       
   217             WebSocket ws = newHttpClient()
       
   218                     .newWebSocketBuilder()
       
   219                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   220                     .join();
       
   221 
       
   222             ws.sendText("text", false).join();
       
   223             assertISE(ws.sendBinary(ByteBuffer.allocate(16), false));
       
   224             assertISE(ws.sendBinary(ByteBuffer.allocate(16), true));
       
   225         }
       
   226     }
       
   227 
       
   228     private static String malformedString() {
   137     private static String malformedString() {
   229         return new String(new char[]{0xDC00, 0xD800});
   138         return new String(new char[]{0xDC00, 0xD800});
   230     }
   139     }
   231 
   140 
   232     private static String incompleteString() {
   141     private static String incompleteString() {
   256         String s = sb.toString();
   165         String s = sb.toString();
   257         assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n;
   166         assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n;
   258         return s;
   167         return s;
   259     }
   168     }
   260 
   169 
   261     @Test
       
   262     public void testIllegalStateSendClose() throws IOException {
       
   263         try (DummyWebSocketServer server = new DummyWebSocketServer()) {
       
   264             server.open();
       
   265             WebSocket ws = newHttpClient()
       
   266                     .newWebSocketBuilder()
       
   267                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   268                     .join();
       
   269 
       
   270             ws.sendClose(NORMAL_CLOSURE, "normal close").join();
       
   271 
       
   272             assertISE(ws.sendText("", true));
       
   273             assertISE(ws.sendText("", false));
       
   274             assertISE(ws.sendText("abc", true));
       
   275             assertISE(ws.sendText("abc", false));
       
   276             assertISE(ws.sendBinary(ByteBuffer.allocate(0), true));
       
   277             assertISE(ws.sendBinary(ByteBuffer.allocate(0), false));
       
   278             assertISE(ws.sendBinary(ByteBuffer.allocate(1), true));
       
   279             assertISE(ws.sendBinary(ByteBuffer.allocate(1), false));
       
   280 
       
   281             assertISE(ws.sendPing(ByteBuffer.allocate(125)));
       
   282             assertISE(ws.sendPing(ByteBuffer.allocate(124)));
       
   283             assertISE(ws.sendPing(ByteBuffer.allocate(1)));
       
   284             assertISE(ws.sendPing(ByteBuffer.allocate(0)));
       
   285 
       
   286             assertISE(ws.sendPong(ByteBuffer.allocate(125)));
       
   287             assertISE(ws.sendPong(ByteBuffer.allocate(124)));
       
   288             assertISE(ws.sendPong(ByteBuffer.allocate(1)));
       
   289             assertISE(ws.sendPong(ByteBuffer.allocate(0)));
       
   290         }
       
   291     }
       
   292 
       
   293     private static void assertIAE(CompletableFuture<?> stage) {
   170     private static void assertIAE(CompletableFuture<?> stage) {
   294         assertExceptionally(IAE, stage);
   171         assertExceptionally(IAE, stage);
   295     }
       
   296 
       
   297     private static void assertISE(CompletableFuture<?> stage) {
       
   298         assertExceptionally(IllegalStateException.class, stage);
       
   299     }
   172     }
   300 
   173 
   301     private static void assertExceptionally(Class<? extends Throwable> clazz,
   174     private static void assertExceptionally(Class<? extends Throwable> clazz,
   302                                             CompletableFuture<?> stage) {
   175                                             CompletableFuture<?> stage) {
   303         stage.handle((result, error) -> {
   176         stage.handle((result, error) -> {
   321         }
   194         }
   322         if (!clazz.isInstance(t)) {
   195         if (!clazz.isInstance(t)) {
   323             throw new AssertionError("Expected " + clazz + ", caught " + t);
   196             throw new AssertionError("Expected " + clazz + ", caught " + t);
   324         }
   197         }
   325     }
   198     }
       
   199 
       
   200     @Test
       
   201     public void testIllegalStateOutstanding1() throws Exception {
       
   202         try (DummyWebSocketServer server = notReadingServer()) {
       
   203             server.open();
       
   204             WebSocket ws = newHttpClient()
       
   205                     .newWebSocketBuilder()
       
   206                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   207                     .join();
       
   208 
       
   209             ByteBuffer data = ByteBuffer.allocate(65536);
       
   210             for (int i = 0; ; i++) {
       
   211                 System.out.println("cycle #" + i);
       
   212                 try {
       
   213                     ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
       
   214                     data.clear();
       
   215                 } catch (TimeoutException e) {
       
   216                     break;
       
   217                 }
       
   218             }
       
   219             assertISE(ws.sendBinary(ByteBuffer.allocate(0), true));
       
   220             assertISE(ws.sendText("", true));
       
   221         }
       
   222     }
       
   223 
       
   224     private static DummyWebSocketServer notReadingServer() {
       
   225         return new DummyWebSocketServer() {
       
   226             @Override
       
   227             protected void serve(SocketChannel channel) throws IOException {
       
   228                 try {
       
   229                     Thread.sleep(Long.MAX_VALUE);
       
   230                 } catch (InterruptedException e) {
       
   231                     throw new IOException(e);
       
   232                 }
       
   233             }
       
   234         };
       
   235     }
       
   236 
       
   237     private static void assertISE(CompletableFuture<?> stage) {
       
   238         assertExceptionally(IllegalStateException.class, stage);
       
   239     }
       
   240 
       
   241     @Test
       
   242     public void testIllegalStateOutstanding2() throws Exception {
       
   243         try (DummyWebSocketServer server = notReadingServer()) {
       
   244             server.open();
       
   245             WebSocket ws = newHttpClient()
       
   246                     .newWebSocketBuilder()
       
   247                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   248                     .join();
       
   249 
       
   250             CharBuffer data = CharBuffer.allocate(65536);
       
   251             for (int i = 0; ; i++) {
       
   252                 System.out.println("cycle #" + i);
       
   253                 try {
       
   254                     ws.sendText(data, true).get(10, TimeUnit.SECONDS);
       
   255                     data.clear();
       
   256                 } catch (TimeoutException e) {
       
   257                     break;
       
   258                 }
       
   259             }
       
   260             assertISE(ws.sendText("", true));
       
   261             assertISE(ws.sendBinary(ByteBuffer.allocate(0), true));
       
   262         }
       
   263     }
       
   264 
       
   265     @Test
       
   266     public void testIllegalStateIntermixed1() throws IOException {
       
   267         try (DummyWebSocketServer server = new DummyWebSocketServer()) {
       
   268             server.open();
       
   269             WebSocket ws = newHttpClient()
       
   270                     .newWebSocketBuilder()
       
   271                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   272                     .join();
       
   273 
       
   274             ws.sendBinary(ByteBuffer.allocate(16), false).join();
       
   275             assertISE(ws.sendText("text", false));
       
   276             assertISE(ws.sendText("text", true));
       
   277         }
       
   278     }
       
   279 
       
   280     @Test
       
   281     public void testIllegalStateIntermixed2() throws IOException {
       
   282         try (DummyWebSocketServer server = new DummyWebSocketServer()) {
       
   283             server.open();
       
   284             WebSocket ws = newHttpClient()
       
   285                     .newWebSocketBuilder()
       
   286                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   287                     .join();
       
   288 
       
   289             ws.sendText("text", false).join();
       
   290             assertISE(ws.sendBinary(ByteBuffer.allocate(16), false));
       
   291             assertISE(ws.sendBinary(ByteBuffer.allocate(16), true));
       
   292         }
       
   293     }
       
   294 
       
   295     @Test
       
   296     public void testIllegalStateSendClose() throws IOException {
       
   297         try (DummyWebSocketServer server = new DummyWebSocketServer()) {
       
   298             server.open();
       
   299             WebSocket ws = newHttpClient()
       
   300                     .newWebSocketBuilder()
       
   301                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
       
   302                     .join();
       
   303 
       
   304             ws.sendClose(NORMAL_CLOSURE, "normal close").join();
       
   305 
       
   306             assertISE(ws.sendText("", true));
       
   307             assertISE(ws.sendText("", false));
       
   308             assertISE(ws.sendText("abc", true));
       
   309             assertISE(ws.sendText("abc", false));
       
   310             assertISE(ws.sendBinary(ByteBuffer.allocate(0), true));
       
   311             assertISE(ws.sendBinary(ByteBuffer.allocate(0), false));
       
   312             assertISE(ws.sendBinary(ByteBuffer.allocate(1), true));
       
   313             assertISE(ws.sendBinary(ByteBuffer.allocate(1), false));
       
   314 
       
   315             assertISE(ws.sendPing(ByteBuffer.allocate(125)));
       
   316             assertISE(ws.sendPing(ByteBuffer.allocate(124)));
       
   317             assertISE(ws.sendPing(ByteBuffer.allocate(1)));
       
   318             assertISE(ws.sendPing(ByteBuffer.allocate(0)));
       
   319 
       
   320             assertISE(ws.sendPong(ByteBuffer.allocate(125)));
       
   321             assertISE(ws.sendPong(ByteBuffer.allocate(124)));
       
   322             assertISE(ws.sendPong(ByteBuffer.allocate(1)));
       
   323             assertISE(ws.sendPong(ByteBuffer.allocate(0)));
       
   324         }
       
   325     }
   326 }
   326 }