use of org.springframework.messaging.simp.stomp.StompSession in project spring-integration by spring-projects.
the class StompSessionManagerTests method testDoConnectFailure.
@Test
public void testDoConnectFailure() throws Exception {
StompClientSupport stompClient = mock(StompClientSupport.class);
stompClient.setTaskScheduler(new ConcurrentTaskScheduler());
AbstractStompSessionManager sessionManager = new AbstractStompSessionManager(stompClient) {
private final AtomicBoolean thrown = new AtomicBoolean();
@Override
protected ListenableFuture<StompSession> doConnect(StompSessionHandler handler) {
if (!this.thrown.getAndSet(true)) {
throw new RuntimeException("intentional");
} else {
SettableListenableFuture<StompSession> future = new SettableListenableFuture<StompSession>();
StompSession stompSession = mock(StompSession.class);
future.set(stompSession);
handler.afterConnected(stompSession, getConnectHeaders());
return future;
}
}
};
sessionManager.start();
final SettableListenableFuture<StompSession> stompSessionFuture = new SettableListenableFuture<StompSession>();
sessionManager.connect(new StompSessionHandlerAdapter() {
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
stompSessionFuture.set(session);
}
});
assertNotNull(stompSessionFuture.get(10, TimeUnit.SECONDS));
sessionManager.stop();
}
use of org.springframework.messaging.simp.stomp.StompSession in project elastest-torm by elastest.
the class StompTestUtils method connectToRabbitMQ.
public static StompSession connectToRabbitMQ(int serverPort) throws InterruptedException, ExecutionException, TimeoutException {
WebSocketContainer cont = ContainerProvider.getWebSocketContainer();
cont.setDefaultMaxTextMessageBufferSize(65500);
WebSocketClient webSocketClient = new StandardWebSocketClient(cont);
WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
stompClient.setMessageConverter(new StringMessageConverter());
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
// for heartbeats
stompClient.setTaskScheduler(taskScheduler);
stompClient.setDefaultHeartbeat(new long[] { 10000, 10000 });
String url = "ws://localhost:" + serverPort + "/rabbitMq";
StompSessionHandler sessionHandler = new LogConnectedSessionHandler();
final int MAX_RETRIES = 5;
int retry = 0;
while (true) {
try {
StompSession stompSession = stompClient.connect(url, sessionHandler).get(10, TimeUnit.SECONDS);
log.info("Test connected to RabbitMQ in URL '{}'", url);
return stompSession;
} catch (Exception e) {
if (retry < MAX_RETRIES) {
retry++;
log.warn("Exception trying to connect to RabbitMQ: {}:{}", e.getClass().getName(), e.getMessage());
log.info("Retrying {}/{} in 5 second", retry, MAX_RETRIES);
Thread.sleep(5000);
} else {
throw e;
}
}
}
}
use of org.springframework.messaging.simp.stomp.StompSession in project spring-boot by spring-projects.
the class WebSocketMessagingAutoConfigurationTests method performStompSubscription.
private Object performStompSubscription(String topic) throws Throwable {
TestPropertyValues.of("server.port:0", "spring.jackson.serialization.indent-output:true").applyTo(this.context);
this.context.register(WebSocketMessagingConfiguration.class);
new ServerPortInfoApplicationContextInitializer().initialize(this.context);
this.context.refresh();
WebSocketStompClient stompClient = new WebSocketStompClient(this.sockJsClient);
final AtomicReference<Throwable> failure = new AtomicReference<>();
final AtomicReference<Object> result = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
StompSessionHandler handler = new StompSessionHandlerAdapter() {
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
session.subscribe(topic, new StompFrameHandler() {
@Override
public void handleFrame(StompHeaders headers, Object payload) {
result.set(payload);
latch.countDown();
}
@Override
public Type getPayloadType(StompHeaders headers) {
return Object.class;
}
});
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
latch.countDown();
}
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
failure.set(exception);
latch.countDown();
}
@Override
public void handleTransportError(StompSession session, Throwable exception) {
failure.set(exception);
latch.countDown();
}
};
stompClient.setMessageConverter(new SimpleMessageConverter());
stompClient.connect("ws://localhost:{port}/messaging", handler, this.context.getEnvironment().getProperty("local.server.port"));
if (!latch.await(30, TimeUnit.SECONDS)) {
if (failure.get() != null) {
throw failure.get();
}
fail("Response was not received within 30 seconds");
}
return result.get();
}
use of org.springframework.messaging.simp.stomp.StompSession in project spring-boot by spring-projects.
the class WebSocketMessagingAutoConfigurationTests method performStompSubscription.
private Object performStompSubscription(final String topic) throws Throwable {
EnvironmentTestUtils.addEnvironment(this.context, "server.port:0", "spring.jackson.serialization.indent-output:true");
this.context.register(WebSocketMessagingConfiguration.class);
new ServerPortInfoApplicationContextInitializer().initialize(this.context);
this.context.refresh();
WebSocketStompClient stompClient = new WebSocketStompClient(this.sockJsClient);
final AtomicReference<Throwable> failure = new AtomicReference<>();
final AtomicReference<Object> result = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
StompSessionHandler handler = new StompSessionHandlerAdapter() {
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
session.subscribe(topic, new StompFrameHandler() {
@Override
public void handleFrame(StompHeaders headers, Object payload) {
result.set(payload);
latch.countDown();
}
@Override
public Type getPayloadType(StompHeaders headers) {
return Object.class;
}
});
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
latch.countDown();
}
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
failure.set(exception);
latch.countDown();
}
@Override
public void handleTransportError(StompSession session, Throwable exception) {
failure.set(exception);
latch.countDown();
}
};
stompClient.setMessageConverter(new SimpleMessageConverter());
stompClient.connect("ws://localhost:{port}/messaging", handler, this.context.getEnvironment().getProperty("local.server.port"));
if (!latch.await(30000, TimeUnit.SECONDS)) {
if (failure.get() != null) {
throw failure.get();
}
fail("Response was not received within 30 seconds");
}
return result.get();
}
use of org.springframework.messaging.simp.stomp.StompSession in project elastest-torm by elastest.
the class TJobExecutionApiItTest method testExecuteTJob.
private void testExecuteTJob(boolean withSut) throws InterruptedException, ExecutionException, TimeoutException, MultipleFailuresError {
log.info("Start the test testExecuteTJob " + (withSut ? "with" : "without") + " SuT");
TJob tJob;
if (withSut) {
Long sutId = createSut(projectId).getId();
tJob = createTJob(projectId, sutId);
} else {
tJob = createTJob(projectId);
}
StompSession stompSession = connectToRabbitMQ(serverPort);
log.info("POST /api/tjob/{tjobId}/exec");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String body = "{\"tJobParams\" : [{\"Param1\":\"NewValue1\"}], \"sutParams\" : [{\"Param1\":\"NewValue1\"}]}";
HttpEntity<String> entity = new HttpEntity<>(body, headers);
Map<String, Object> urlParams = new HashMap<>();
urlParams.put("tjobId", tJob.getId());
ResponseEntity<TJobExecution> response = httpClient.postForEntity("/api/tjob/{tjobId}/exec", entity, TJobExecution.class, urlParams);
TJobExecution exec = response.getBody();
log.info("TJobExecution creation response: " + response);
if (withSut) {
String queueToSuscribe = "/topic/" + "sut.default_log." + exec.getId() + ".log";
log.info("Sut log queue '" + queueToSuscribe + "'");
WaitForMessagesHandler handler = new WaitForMessagesHandler();
stompSession.subscribe(queueToSuscribe, handler);
handler.waitForCompletion(5, TimeUnit.SECONDS);
log.info("Sut log queue received a message");
}
String queueToSuscribe = "/topic/" + "test.default_log." + exec.getId() + ".log";
log.info("TJob log queue '" + queueToSuscribe + "'");
WaitForMessagesHandler handler = new WaitForMessagesHandler(msg -> msg.contains("BUILD SUCCESS") || msg.contains("BUILD FAILURE"));
stompSession.subscribe(queueToSuscribe, handler);
handler.waitForCompletion(180, TimeUnit.SECONDS);
assertAll("Validating TJobExecution Properties", () -> assertNotNull(response.getBody()), () -> assertNotNull(response.getBody().getId()), () -> assertTrue(response.getBody().getTjob().getId().equals(urlParams.get("tjobId"))));
while (true) {
exec = getTJobExecutionById(exec.getId(), tJob.getId()).getBody();
log.info("TJobExecution: " + exec);
if (exec.getResult() != ResultEnum.IN_PROGRESS) {
log.info("Test results:" + exec.getTestSuites());
break;
}
sleep(500);
}
deleteTJobExecution(exec.getId(), tJob.getId());
deleteTJob(tJob.getId());
log.info("Finished.");
}
Aggregations