Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

fix: GraphQLTestSubscription test failing #436

Merged
merged 1 commit into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -97,6 +98,7 @@ public GraphQLTestSubscription init(@Nullable final Object payload) {
message.set("payload", getFinalPayload(payload));
sendMessage(message);
initialized = true;
awaitAcknowledgement();
return this;
}

Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -177,7 +179,10 @@ public void reset() {
started = false;
stopped = false;
acknowledged = false;
responses.clear();
session = null;
synchronized (responses) {
responses.clear();
}
}

/**
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<String> {
@Override
public void onMessage(final String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down