use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method throwingOnFailLogs.
@Disabled("AsyncCall currently lets runtime exceptions propagate.")
@Test
public void throwingOnFailLogs() throws Exception {
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("Body"));
final RuntimeException e = new RuntimeException("boom");
clientListener.setNextEventDelegate(new WebSocketListener() {
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
throw e;
}
});
newWebSocket();
assertThat(testLogHandler.take()).isEqualTo("INFO: [WS client] onFailure");
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method unacknowledgedPingFailsConnection.
/**
* Configure the websocket to send pings every 500 ms. Artificially prevent the server from
* responding to pings. The client should give up when attempting to send its 2nd ping, at about
* 1000 ms.
*/
@Test
public void unacknowledgedPingFailsConnection() {
TestUtil.assumeNotWindows();
client = client.newBuilder().pingInterval(Duration.ofMillis(500)).build();
// Stall in onOpen to prevent pongs from being sent.
final CountDownLatch latch = new CountDownLatch(1);
webServer.enqueue(new MockResponse().withWebSocketUpgrade(new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
try {
// The server can't respond to pings!
latch.await();
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
}));
long openAtNanos = System.nanoTime();
newWebSocket();
clientListener.assertOpen();
clientListener.assertFailure(SocketTimeoutException.class, "sent ping but didn't receive pong within 500ms (after 0 successful ping/pongs)");
latch.countDown();
long elapsedUntilFailure = System.nanoTime() - openAtNanos;
assertThat(TimeUnit.NANOSECONDS.toMillis(elapsedUntilFailure)).isCloseTo(1000L, offset(250L));
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method binaryMessage.
@Test
public void binaryMessage() {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
webSocket.send(ByteString.encodeUtf8("Hello!"));
serverListener.assertBinaryMessage(ByteString.of(new byte[] { 'H', 'e', 'l', 'l', 'o', '!' }));
closeWebSockets(webSocket, server);
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method clientCancelsIfCloseIsNotAcknowledged.
/**
* https://github.com/square/okhttp/issues/2788
*/
@Test
public void clientCancelsIfCloseIsNotAcknowledged() {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
RealWebSocket webSocket = newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
// Initiate a close on the client, which will schedule a hard cancel in 500 ms.
long closeAtNanos = System.nanoTime();
webSocket.close(1000, "goodbye", 500L);
serverListener.assertClosing(1000, "goodbye");
// Confirm that the hard cancel occurred after 500 ms.
clientListener.assertFailure();
long elapsedUntilFailure = System.nanoTime() - closeAtNanos;
assertThat(TimeUnit.NANOSECONDS.toMillis(elapsedUntilFailure)).isCloseTo(500L, offset(250L));
// Close the server and confirm it saw what we expected.
server.close(1000, null);
serverListener.assertClosed(1000, "goodbye");
}
use of okhttp3.WebSocket in project okhttp by square.
the class WebSocketHttpTest method nullByteStringThrows.
@Test
public void nullByteStringThrows() {
TestUtil.assumeNotWindows();
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
try {
webSocket.send((ByteString) null);
fail();
} catch (NullPointerException expected) {
}
closeWebSockets(webSocket, server);
}
Aggregations