|
47 | 47 | public class GraphQLTestSubscription { |
48 | 48 |
|
49 | 49 | private static final int SLEEP_INTERVAL_MS = 100; |
| 50 | + private static final int ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT = 6000000; |
50 | 51 | private static final AtomicInteger ID_COUNTER = new AtomicInteger(1); |
51 | 52 | private static final UriBuilderFactory URI_BUILDER_FACTORY = new DefaultUriBuilderFactory(); |
52 | 53 |
|
@@ -97,6 +98,7 @@ public GraphQLTestSubscription init(@Nullable final Object payload) { |
97 | 98 | message.set("payload", getFinalPayload(payload)); |
98 | 99 | sendMessage(message); |
99 | 100 | initialized = true; |
| 101 | + awaitAcknowledgement(); |
100 | 102 | return this; |
101 | 103 | } |
102 | 104 |
|
@@ -141,8 +143,8 @@ public GraphQLTestSubscription start(@NonNull final String graphGLResource, @Nul |
141 | 143 | * @return self reference |
142 | 144 | */ |
143 | 145 | public GraphQLTestSubscription stop() { |
144 | | - if (!started) { |
145 | | - fail("Subscription not yet started."); |
| 146 | + if (!initialized) { |
| 147 | + fail("Subscription not yet initialized."); |
146 | 148 | } |
147 | 149 | if (stopped) { |
148 | 150 | fail("Subscription already stopped."); |
@@ -177,7 +179,10 @@ public void reset() { |
177 | 179 | started = false; |
178 | 180 | stopped = false; |
179 | 181 | acknowledged = false; |
180 | | - responses.clear(); |
| 182 | + session = null; |
| 183 | + synchronized (responses) { |
| 184 | + responses.clear(); |
| 185 | + } |
181 | 186 | } |
182 | 187 |
|
183 | 188 | /** |
@@ -348,6 +353,8 @@ private void initClient() throws Exception { |
348 | 353 | final ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create() |
349 | 354 | .configurator(new TestWebSocketClientConfigurator()) |
350 | 355 | .build(); |
| 356 | + clientEndpointConfig.getUserProperties().put("org.apache.tomcat.websocket.IO_TIMEOUT_MS", |
| 357 | + String.valueOf(ACKNOWLEDGEMENT_AND_CONNECTION_TIMEOUT)); |
351 | 358 | session = webSocketContainer.connectToServer(TestWebSocketClient.class, clientEndpointConfig, uri); |
352 | 359 | session.addMessageHandler(new TestMessageHandler()); |
353 | 360 | } |
@@ -376,6 +383,22 @@ private void sendMessage(final Object message) { |
376 | 383 | } |
377 | 384 | } |
378 | 385 |
|
| 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 | + |
379 | 402 | class TestMessageHandler implements MessageHandler.Whole<String> { |
380 | 403 | @Override |
381 | 404 | public void onMessage(final String message) { |
|
0 commit comments