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

Commit e30b0d3

Browse files
authored
Merge pull request #436 from BlasiusSecundus/master
fix: GraphQLTestSubscription test failing
2 parents 5e198a7 + 319f7ee commit e30b0d3

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestSubscription.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
public class GraphQLTestSubscription {
4848

4949
private static final int SLEEP_INTERVAL_MS = 100;
50+
private static final int ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT = 6000000;
5051
private static final AtomicInteger ID_COUNTER = new AtomicInteger(1);
5152
private static final UriBuilderFactory URI_BUILDER_FACTORY = new DefaultUriBuilderFactory();
5253

@@ -97,6 +98,7 @@ public GraphQLTestSubscription init(@Nullable final Object payload) {
9798
message.set("payload", getFinalPayload(payload));
9899
sendMessage(message);
99100
initialized = true;
101+
awaitAcknowledgement();
100102
return this;
101103
}
102104

@@ -141,8 +143,8 @@ public GraphQLTestSubscription start(@NonNull final String graphGLResource, @Nul
141143
* @return self reference
142144
*/
143145
public GraphQLTestSubscription stop() {
144-
if (!started) {
145-
fail("Subscription not yet started.");
146+
if (!initialized) {
147+
fail("Subscription not yet initialized.");
146148
}
147149
if (stopped) {
148150
fail("Subscription already stopped.");
@@ -177,7 +179,10 @@ public void reset() {
177179
started = false;
178180
stopped = false;
179181
acknowledged = false;
180-
responses.clear();
182+
session = null;
183+
synchronized (responses) {
184+
responses.clear();
185+
}
181186
}
182187

183188
/**
@@ -348,6 +353,8 @@ private void initClient() throws Exception {
348353
final ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create()
349354
.configurator(new TestWebSocketClientConfigurator())
350355
.build();
356+
clientEndpointConfig.getUserProperties().put("org.apache.tomcat.websocket.IO_TIMEOUT_MS",
357+
String.valueOf(ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT));
351358
session = webSocketContainer.connectToServer(TestWebSocketClient.class, clientEndpointConfig, uri);
352359
session.addMessageHandler(new TestMessageHandler());
353360
}
@@ -376,6 +383,22 @@ private void sendMessage(final Object message) {
376383
}
377384
}
378385

386+
private void awaitAcknowledgement() {
387+
int elapsedTime = 0;
388+
while(!acknowledged && elapsedTime < ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT) {
389+
try {
390+
Thread.sleep(SLEEP_INTERVAL_MS);
391+
elapsedTime += SLEEP_INTERVAL_MS;
392+
} catch (InterruptedException e) {
393+
fail("Test execution error - Thread.sleep failed.", e);
394+
}
395+
}
396+
397+
if (!acknowledged) {
398+
fail("Timeout: Connection was not acknowledged by the GraphQL server.");
399+
}
400+
}
401+
379402
class TestMessageHandler implements MessageHandler.Whole<String> {
380403
@Override
381404
public void onMessage(final String message) {

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestSubscriptionTestBase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.graphql.spring.boot.test;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.jupiter.api.AfterEach;
45
import org.junit.jupiter.api.BeforeEach;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.test.context.SpringBootTest;
@@ -37,6 +38,11 @@ protected void setUp() {
3738
graphQLTestSubscription = new GraphQLTestSubscription(environment, objectMapper, "subscriptions");
3839
}
3940

41+
@AfterEach
42+
protected void tearDown() {
43+
graphQLTestSubscription.reset();
44+
}
45+
4046
protected void assertThatSubscriptionStoppedStatusIs(boolean isStopped) {
4147
if (isStopped) {
4248
assertThatSubscriptionWasStopped();

0 commit comments

Comments
 (0)