use of org.springframework.messaging.Message in project spring-framework by spring-projects.
the class SimpAnnotationMethodMessageHandlerTests method listenableFutureSuccess.
@Test
@SuppressWarnings("unchecked")
public void listenableFutureSuccess() {
Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
given(this.channel.send(any(Message.class))).willReturn(true);
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
ListenableFutureController controller = new ListenableFutureController();
this.messageHandler.registerHandler(controller);
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
Message<?> message = createMessage("/app1/listenable-future/success");
this.messageHandler.handleMessage(message);
assertNotNull(controller.future);
controller.future.run();
verify(this.converter).toMessage(this.payloadCaptor.capture(), any(MessageHeaders.class));
assertEquals("foo", this.payloadCaptor.getValue());
}
use of org.springframework.messaging.Message in project spring-framework by spring-projects.
the class SimpAnnotationMethodMessageHandlerTests method completableFutureSuccess.
@Test
@SuppressWarnings("unchecked")
public void completableFutureSuccess() {
Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
given(this.channel.send(any(Message.class))).willReturn(true);
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
CompletableFutureController controller = new CompletableFutureController();
this.messageHandler.registerHandler(controller);
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
Message<?> message = createMessage("/app1/completable-future");
this.messageHandler.handleMessage(message);
assertNotNull(controller.future);
controller.future.complete("foo");
verify(this.converter).toMessage(this.payloadCaptor.capture(), any(MessageHeaders.class));
assertEquals("foo", this.payloadCaptor.getValue());
}
use of org.springframework.messaging.Message 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));
}
use of org.springframework.messaging.Message 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));
}
use of org.springframework.messaging.Message 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));
}
Aggregations