use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.
the class BasicAppTest method testWebSocketEcho.
@Test
public void testWebSocketEcho() throws Exception {
WebSocketClient client = new WebSocketClient();
try {
client.start();
CheckSocket socket = new CheckSocket();
client.connect(socket, serverWebsocketURI.resolve("/echo"));
socket.awaitOpen(2, TimeUnit.SECONDS);
socket.sendText("Hello World");
socket.close(StatusCode.NORMAL, "Test complete");
socket.awaitClose(2, TimeUnit.SECONDS);
assertThat("Messages received", socket.getTextMessages().size(), is(1));
String response = socket.getTextMessages().poll();
System.err.println(response);
assertThat("Message[0]", response, is("Hello World"));
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.
the class CdiAppTest method testWebSocket_Info_FieldPresence.
@Test
public void testWebSocket_Info_FieldPresence() throws Exception {
WebSocketClient client = new WebSocketClient();
try {
client.start();
CheckSocket socket = new CheckSocket();
client.connect(socket, serverWebsocketURI.resolve("/cdi-info"));
socket.awaitOpen(2, TimeUnit.SECONDS);
socket.sendText("info");
socket.close(StatusCode.NORMAL, "Test complete");
socket.awaitClose(2, TimeUnit.SECONDS);
assertThat("Messages received", socket.getTextMessages().size(), is(1));
String response = socket.getTextMessages().poll();
System.err.println(response);
assertThat("Message[0]", response, allOf(containsString("websocketSession is PRESENT"), containsString("httpSession is PRESENT"), containsString("servletContext is PRESENT")));
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project spark by perwendel.
the class GenericIntegrationTest method testWebSocketConversation.
@Test
public void testWebSocketConversation() throws Exception {
String uri = "ws://localhost:4567/ws";
WebSocketClient client = new WebSocketClient();
WebSocketTestClient ws = new WebSocketTestClient();
try {
client.start();
client.connect(ws, URI.create(uri), new ClientUpgradeRequest());
ws.awaitClose(30, TimeUnit.SECONDS);
} finally {
client.stop();
}
List<String> events = WebSocketTestHandler.events;
Assert.assertEquals(3, events.size(), 3);
Assert.assertEquals("onConnect", events.get(0));
Assert.assertEquals("onMessage: Hi Spark!", events.get(1));
Assert.assertEquals("onClose: 1000 Bye!", events.get(2));
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project neo4j by neo4j.
the class WebSocketConnectionTest method shouldNotThrowAnyExceptionWhenDataReceivedBeforeClose.
@Test
public void shouldNotThrowAnyExceptionWhenDataReceivedBeforeClose() throws Throwable {
// Given
WebSocketClient client = mock(WebSocketClient.class);
WebSocketConnection conn = new WebSocketConnection(client);
when(client.isStopped()).thenReturn(true);
byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// When
conn.onWebSocketBinary(data, 0, 10);
conn.recv(10);
// Then
// no exception
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project pulsar by yahoo.
the class ProxyAuthenticationTest method socketTest.
@Test
public void socketTest() throws InterruptedException {
URI consumeUri = URI.create(CONSUME_URI);
URI produceUri = URI.create(PRODUCE_URI);
WebSocketClient consumeClient = new WebSocketClient();
SimpleConsumerSocket consumeSocket = new SimpleConsumerSocket();
WebSocketClient produceClient = new WebSocketClient();
SimpleProducerSocket produceSocket = new SimpleProducerSocket();
try {
consumeClient.start();
ClientUpgradeRequest consumeRequest = new ClientUpgradeRequest();
Future<Session> consumerFuture = consumeClient.connect(consumeSocket, consumeUri, consumeRequest);
log.info("Connecting to : {}", consumeUri);
ClientUpgradeRequest produceRequest = new ClientUpgradeRequest();
produceClient.start();
Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest);
// let it connect
Thread.sleep(1000);
Assert.assertTrue(consumerFuture.get().isOpen());
Assert.assertTrue(producerFuture.get().isOpen());
consumeSocket.awaitClose(1, TimeUnit.SECONDS);
produceSocket.awaitClose(1, TimeUnit.SECONDS);
Assert.assertTrue(produceSocket.getBuffer().size() > 0);
Assert.assertEquals(produceSocket.getBuffer(), consumeSocket.getBuffer());
} catch (Throwable t) {
log.error(t.getMessage());
} finally {
ExecutorService executor = newFixedThreadPool(1);
try {
executor.submit(() -> {
try {
consumeClient.stop();
produceClient.stop();
log.info("proxy clients are stopped successfully");
} catch (Exception e) {
log.error(e.getMessage());
}
}).get(2, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("failed to close clients ", e);
}
executor.shutdownNow();
}
}
Aggregations