Search in sources :

Example 21 with TextMessage

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();
    }
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 22 with TextMessage

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());
    }
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 23 with TextMessage

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());
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 24 with TextMessage

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());
    }
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 25 with TextMessage

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());
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Aggregations

TextMessage (org.springframework.web.socket.TextMessage)65 Test (org.junit.Test)48 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)16 WebSocketSession (org.springframework.web.socket.WebSocketSession)10 BinaryMessage (org.springframework.web.socket.BinaryMessage)8 IOException (java.io.IOException)7 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)7 Message (org.springframework.messaging.Message)6 URI (java.net.URI)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)4 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 MessageChannel (org.springframework.messaging.MessageChannel)3 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)3 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)3 CloseStatus (org.springframework.web.socket.CloseStatus)3 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)3 SockJsMessageDeliveryException (org.springframework.web.socket.sockjs.SockJsMessageDeliveryException)3