use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class StompWebSocketIntegrationTests method sendMessageToBrokerAndReceiveReplyViaTopic.
// SPR-10930
@Test
public void sendMessageToBrokerAndReceiveReplyViaTopic() throws Exception {
TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/foo").build();
TextMessage m2 = create(StompCommand.SEND).headers("destination:/topic/foo").body("5").build();
TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(1, m1, m2);
WebSocketSession session = doHandshake(clientHandler, "/ws").get();
try {
assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS));
String payload = clientHandler.actual.get(0).getPayload();
assertTrue("Expected STOMP MESSAGE, got " + payload, payload.startsWith("MESSAGE\n"));
} finally {
session.close();
}
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class ConcurrentWebSocketSessionDecoratorTests method sendTimeLimitExceeded.
@Test
public void sendTimeLimitExceeded() throws IOException, InterruptedException {
BlockingSession blockingSession = new BlockingSession();
blockingSession.setId("123");
blockingSession.setOpen(true);
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
int sendTimeLimit = 100;
int bufferSizeLimit = 1024;
final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit);
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
TextMessage message = new TextMessage("slow message");
try {
concurrentSession.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
});
assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS));
// ensure some send time elapses
Thread.sleep(sendTimeLimit + 100);
try {
TextMessage payload = new TextMessage("payload");
concurrentSession.sendMessage(payload);
fail("Expected exception");
} catch (SessionLimitExceededException ex) {
String actual = ex.getMessage();
String regex = "Message send time [\\d]+ \\(ms\\) for session '123' exceeded the allowed limit 100";
assertTrue("Unexpected message: " + actual, actual.matches(regex));
assertEquals(CloseStatus.SESSION_NOT_RELIABLE, ex.getStatus());
}
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class ConcurrentWebSocketSessionDecoratorTests method closeStatusChangesToSessionNotReliable.
@Test
public void closeStatusChangesToSessionNotReliable() throws Exception {
BlockingSession blockingSession = new BlockingSession();
blockingSession.setId("123");
blockingSession.setOpen(true);
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
int sendTimeLimit = 100;
int bufferSizeLimit = 1024;
final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit);
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
TextMessage message = new TextMessage("slow message");
try {
concurrentSession.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
});
assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS));
// ensure some send time elapses
Thread.sleep(sendTimeLimit + 100);
concurrentSession.close(CloseStatus.PROTOCOL_ERROR);
assertEquals("CloseStatus should have changed to SESSION_NOT_RELIABLE", CloseStatus.SESSION_NOT_RELIABLE, blockingSession.getCloseStatus());
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class ConcurrentWebSocketSessionDecoratorTests method sendBufferSizeExceeded.
@Test
public void sendBufferSizeExceeded() throws IOException, InterruptedException {
BlockingSession blockingSession = new BlockingSession();
blockingSession.setId("123");
blockingSession.setOpen(true);
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
int sendTimeLimit = 10 * 1000;
int bufferSizeLimit = 1024;
final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit);
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
TextMessage message = new TextMessage("slow message");
try {
concurrentSession.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
});
assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1023; i++) {
sb.append("a");
}
TextMessage message = new TextMessage(sb.toString());
concurrentSession.sendMessage(message);
assertEquals(1023, concurrentSession.getBufferSize());
assertTrue(blockingSession.isOpen());
try {
concurrentSession.sendMessage(message);
fail("Expected exception");
} catch (SessionLimitExceededException ex) {
String actual = ex.getMessage();
String regex = "The send buffer size [\\d]+ bytes for session '123' exceeded the allowed limit 1024";
assertTrue("Unexpected message: " + actual, actual.matches(regex));
assertEquals(CloseStatus.SESSION_NOT_RELIABLE, ex.getStatus());
}
}
use of org.springframework.web.socket.TextMessage in project spring-framework by spring-projects.
the class ConcurrentWebSocketSessionDecoratorTests method sendAfterBlockedSend.
@Test
public void sendAfterBlockedSend() throws IOException, InterruptedException {
BlockingSession blockingSession = new BlockingSession();
blockingSession.setOpen(true);
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, 10 * 1000, 1024);
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
TextMessage message = new TextMessage("slow message");
try {
concurrentSession.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
});
assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS));
// ensure some send time elapses
Thread.sleep(100);
assertTrue(concurrentSession.getTimeSinceSendStarted() > 0);
TextMessage payload = new TextMessage("payload");
for (int i = 0; i < 5; i++) {
concurrentSession.sendMessage(payload);
}
assertTrue(concurrentSession.getTimeSinceSendStarted() > 0);
assertEquals(5 * payload.getPayloadLength(), concurrentSession.getBufferSize());
assertTrue(blockingSession.isOpen());
}
Aggregations