Search in sources :

Example 1 with TestPrincipal

use of org.springframework.messaging.simp.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method writeInactivity.

@Test
public void writeInactivity() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 1, 0 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertNotNull(heartbeatTask);
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 0, 1 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutboundChannel, times(2)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertEquals(2, messages.size());
    MessageHeaders headers = messages.get(0).getHeaders();
    assertEquals(SimpMessageType.CONNECT_ACK, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    headers = messages.get(1).getHeaders();
    assertEquals(SimpMessageType.HEARTBEAT, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    assertEquals(id, headers.get(SimpMessageHeaderAccessor.SESSION_ID_HEADER));
    assertEquals(user, headers.get(SimpMessageHeaderAccessor.USER_HEADER));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 2 with TestPrincipal

use of org.springframework.messaging.simp.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method subcribeDisconnectPublish.

@Test
public void subcribeDisconnectPublish() {
    String sess1 = "sess1";
    String sess2 = "sess2";
    this.messageHandler.start();
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub1", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub2", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub3", "/bar"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub1", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub2", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub3", "/bar"));
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
    headers.setSessionId(sess1);
    headers.setUser(new TestPrincipal("joe"));
    Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
    this.messageHandler.handleMessage(message);
    this.messageHandler.handleMessage(createMessage("/foo", "message1"));
    this.messageHandler.handleMessage(createMessage("/bar", "message2"));
    verify(this.clientOutboundChannel, times(4)).send(this.messageCaptor.capture());
    Message<?> captured = this.messageCaptor.getAllValues().get(0);
    assertEquals(SimpMessageType.DISCONNECT_ACK, SimpMessageHeaderAccessor.getMessageType(captured.getHeaders()));
    assertSame(message, captured.getHeaders().get(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER));
    assertEquals(sess1, SimpMessageHeaderAccessor.getSessionId(captured.getHeaders()));
    assertEquals("joe", SimpMessageHeaderAccessor.getUser(captured.getHeaders()).getName());
    assertTrue(messageCaptured(sess2, "sub1", "/foo"));
    assertTrue(messageCaptured(sess2, "sub2", "/foo"));
    assertTrue(messageCaptured(sess2, "sub3", "/bar"));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 3 with TestPrincipal

use of org.springframework.messaging.simp.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method connect.

@Test
public void connect() {
    this.messageHandler.start();
    String id = "sess1";
    Message<String> connectMessage = createConnectMessage(id, new TestPrincipal("joe"), null);
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.handleMessage(connectMessage);
    verify(this.clientOutboundChannel, times(1)).send(this.messageCaptor.capture());
    Message<?> connectAckMessage = this.messageCaptor.getValue();
    SimpMessageHeaderAccessor connectAckHeaders = SimpMessageHeaderAccessor.wrap(connectAckMessage);
    assertEquals(connectMessage, connectAckHeaders.getHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER));
    assertEquals(id, connectAckHeaders.getSessionId());
    assertEquals("joe", connectAckHeaders.getUser().getName());
    assertArrayEquals(new long[] { 10000, 10000 }, SimpMessageHeaderAccessor.getHeartbeat(connectAckHeaders.getMessageHeaders()));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 4 with TestPrincipal

use of org.springframework.messaging.simp.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method readInactivity.

@Test
public void readInactivity() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 0, 1 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertNotNull(heartbeatTask);
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 1, 0 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutboundChannel, atLeast(2)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertEquals(2, messages.size());
    MessageHeaders headers = messages.get(0).getHeaders();
    assertEquals(SimpMessageType.CONNECT_ACK, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    headers = messages.get(1).getHeaders();
    assertEquals(SimpMessageType.DISCONNECT_ACK, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    assertEquals(id, headers.get(SimpMessageHeaderAccessor.SESSION_ID_HEADER));
    assertEquals(user, headers.get(SimpMessageHeaderAccessor.USER_HEADER));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 5 with TestPrincipal

use of org.springframework.messaging.simp.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method readWriteIntervalCalculation.

@Test
public void readWriteIntervalCalculation() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 1, 1 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertNotNull(heartbeatTask);
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 10000, 10000 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutboundChannel, times(1)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertEquals(1, messages.size());
    assertEquals(SimpMessageType.CONNECT_ACK, messages.get(0).getHeaders().get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) Message(org.springframework.messaging.Message) Test(org.junit.Test)

Aggregations

TestPrincipal (org.springframework.messaging.simp.TestPrincipal)18 Test (org.junit.Test)15 Message (org.springframework.messaging.Message)4 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)3 MessageHeaders (org.springframework.messaging.MessageHeaders)2 AntPathMatcher (org.springframework.util.AntPathMatcher)2 TextMessage (org.springframework.web.socket.TextMessage)2 Before (org.junit.Before)1 MessageChannel (org.springframework.messaging.MessageChannel)1 BinaryMessage (org.springframework.web.socket.BinaryMessage)1 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)1 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)1