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