mercredi 27 juin 2018

Java Stomp WebSocket TimeoutException

I have issues with integration tests with WebSockets and SpringBoot. If I try to connect with StomJS from web page, client gets notification. But in integration test with Java client I get exception.What is a proper way to write these tests? My Config class look like:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends 
AbstractWebSocketMessageBrokerConfigurer  {
@Autowired
private ConnectionInterceptor connectionInterceptor;

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
    registration.interceptors(connectionInterceptor);
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry
        .addEndpoint(ApiConstants.SOCKET_BASE_URL)
        .setAllowedOrigins("*")
        .withSockJS();

}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker( "/topic", "/queue" );
    registry.setApplicationDestinationPrefixes("/app");
    registry.setUserDestinationPrefix("/user");
}

}

My test is:

 @Test
 @WithMockUser(authorities = "ADMIN")
 public void ShouldLogoutGroupOfUsers() throws Exception {
    String url = "ws://127.0.0.1:" + port+ "/socket";
    StompSessionHandler sessionHandler = new MyStompSessionHandler();
    CompletableFuture<SocketMessage> completable = new 
    CompletableFuture<>();

    val stompHeaders = new StompHeaders();
    //add authorization headers
    stompHeaders.add(
            tokenHeaderKey,
    getUserHeaders().getValuesAsList(tokenHeaderKey).get(0)
    );

    val session = getStompClient().connect(
            url,
            new WebSocketHttpHeaders(),
            stompHeaders,
            new StompSessionHandlerAdapter() {
            }
    ).get(1, SECONDS);

    assertTrue(session.isConnected());
    stompHeaders.setDestination("/user/queue/action/logout");

    val subscription = session.subscribe(
            stompHeaders,
            new StompMessageHandler<>(completable, 
            SocketMessage.class)
    );

Now I call controller method that will call :

sender.convertAndSendToUser(registeredUser, DEVICE_LOGOUT_PATH,new 
SocketMessage("hello socket"));

And test fails on this line:

SocketMessage message = completable.get(30, SECONDS);

Aucun commentaire:

Enregistrer un commentaire