diff --git a/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestSubscription.java b/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestSubscription.java index f52023f4..05b1d02f 100644 --- a/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestSubscription.java +++ b/graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestSubscription.java @@ -47,6 +47,7 @@ public class GraphQLTestSubscription { private static final int SLEEP_INTERVAL_MS = 100; + private static final int ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT = 6000000; private static final AtomicInteger ID_COUNTER = new AtomicInteger(1); private static final UriBuilderFactory URI_BUILDER_FACTORY = new DefaultUriBuilderFactory(); @@ -97,6 +98,7 @@ public GraphQLTestSubscription init(@Nullable final Object payload) { message.set("payload", getFinalPayload(payload)); sendMessage(message); initialized = true; + awaitAcknowledgement(); return this; } @@ -141,8 +143,8 @@ public GraphQLTestSubscription start(@NonNull final String graphGLResource, @Nul * @return self reference */ public GraphQLTestSubscription stop() { - if (!started) { - fail("Subscription not yet started."); + if (!initialized) { + fail("Subscription not yet initialized."); } if (stopped) { fail("Subscription already stopped."); @@ -177,7 +179,10 @@ public void reset() { started = false; stopped = false; acknowledged = false; - responses.clear(); + session = null; + synchronized (responses) { + responses.clear(); + } } /** @@ -348,6 +353,8 @@ private void initClient() throws Exception { final ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create() .configurator(new TestWebSocketClientConfigurator()) .build(); + clientEndpointConfig.getUserProperties().put("org.apache.tomcat.websocket.IO_TIMEOUT_MS", + String.valueOf(ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT)); session = webSocketContainer.connectToServer(TestWebSocketClient.class, clientEndpointConfig, uri); session.addMessageHandler(new TestMessageHandler()); } @@ -376,6 +383,22 @@ private void sendMessage(final Object message) { } } + private void awaitAcknowledgement() { + int elapsedTime = 0; + while(!acknowledged && elapsedTime < ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT) { + try { + Thread.sleep(SLEEP_INTERVAL_MS); + elapsedTime += SLEEP_INTERVAL_MS; + } catch (InterruptedException e) { + fail("Test execution error - Thread.sleep failed.", e); + } + } + + if (!acknowledged) { + fail("Timeout: Connection was not acknowledged by the GraphQL server."); + } + } + class TestMessageHandler implements MessageHandler.Whole { @Override public void onMessage(final String message) { diff --git a/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestSubscriptionTestBase.java b/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestSubscriptionTestBase.java index 9d5a049b..5320a7a8 100644 --- a/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestSubscriptionTestBase.java +++ b/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestSubscriptionTestBase.java @@ -1,6 +1,7 @@ package com.graphql.spring.boot.test; import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -37,6 +38,11 @@ protected void setUp() { graphQLTestSubscription = new GraphQLTestSubscription(environment, objectMapper, "subscriptions"); } + @AfterEach + protected void tearDown() { + graphQLTestSubscription.reset(); + } + protected void assertThatSubscriptionStoppedStatusIs(boolean isStopped) { if (isStopped) { assertThatSubscriptionWasStopped();