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

Commit 556e866

Browse files
fix: test subscription not waiting the specified amount of time
If numExpectedResponses was 0 or negative, the method did not work according to documentation, it did not wait out the specified amount of time as the condition in await() was fulfilled immediately. This resulted in waitAndExpectNoResponse and awaitAndGetAllResponses not working as specified in the documentation. The test suite was also updated to catch this issue in the future.
1 parent 9dc6d2e commit 556e866

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,17 @@ public List<GraphQLResponse> awaitAndGetNextResponses(
300300
fail("Subscription already stopped. Forgot to call reset after test case?");
301301
}
302302

303-
await()
304-
.atMost(timeout, TimeUnit.MILLISECONDS)
305-
.until(() -> state.getResponses().size() >= numExpectedResponses);
303+
if (numExpectedResponses > 0) {
304+
await()
305+
.atMost(timeout, TimeUnit.MILLISECONDS)
306+
.until(() -> state.getResponses().size() >= numExpectedResponses);
307+
} else {
308+
try {
309+
Thread.sleep(timeout);
310+
} catch (InterruptedException e) {
311+
fail("Unable to wait the specified amount of time.", e);
312+
}
313+
}
306314

307315
if (stopAfter) {
308316
stop();

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

+20-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
import org.junit.jupiter.params.ParameterizedTest;
88
import org.junit.jupiter.params.provider.ValueSource;
99

10+
import java.time.Instant;
11+
1012
@DisplayName("Testing awaitNoResponse methods")
1113
class GraphQLSubscriptionTestAwaitNoAnswerTest extends GraphQLTestSubscriptionTestBase {
1214

1315
@Test
1416
@DisplayName("Should succeed if no responses arrived / default stopAfter.")
1517
void shouldAwaitNoResponseSucceedIfNoResponsesArrivedDefaultStopAfter() {
16-
// WHEN - THEN
18+
// GIVEN
19+
final Instant timeBeforeTestStart = Instant.now();
20+
// WHEN
1721
graphQLTestSubscription
1822
.start(SUBSCRIPTION_THAT_TIMES_OUT_RESOURCE)
1923
.waitAndExpectNoResponse(TIMEOUT);
24+
// THEN
2025
assertThatSubscriptionWasStopped();
26+
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTestStart);
2127
}
2228

2329
@ParameterizedTest
@@ -26,20 +32,25 @@ void shouldAwaitNoResponseSucceedIfNoResponsesArrivedDefaultStopAfter() {
2632
void shouldAwaitNoResponseSucceedIfNoResponsesArrived(
2733
final boolean stopAfter
2834
) {
29-
// WHEN - THEN
35+
// GIVEN
36+
final Instant timeBeforeTestStart = Instant.now();
37+
// WHEN
3038
graphQLTestSubscription
3139
.start(SUBSCRIPTION_THAT_TIMES_OUT_RESOURCE)
3240
.waitAndExpectNoResponse(TIMEOUT, stopAfter);
41+
// THEN
3342
assertThatSubscriptionStoppedStatusIs(stopAfter);
43+
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTestStart);
3444
}
3545

3646
@Test
3747
@DisplayName("Should raise assertion error if any response arrived / default stop after.")
3848
void shouldRaiseAssertionErrorIfResponseArrivedDefaultStopAfter() {
39-
// WHEN - THEN
49+
// WHEN
4050
graphQLTestSubscription.start(TIMER_SUBSCRIPTION_RESOURCE);
41-
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> graphQLTestSubscription
42-
.waitAndExpectNoResponse(TIMEOUT));
51+
// THEN
52+
assertThatExceptionOfType(AssertionError.class)
53+
.isThrownBy(() -> graphQLTestSubscription.waitAndExpectNoResponse(TIMEOUT));
4354
assertThatSubscriptionWasStopped();
4455
}
4556

@@ -49,10 +60,11 @@ void shouldRaiseAssertionErrorIfResponseArrivedDefaultStopAfter() {
4960
void shouldRaiseAssertionErrorIfResponseArrived(
5061
final boolean stopAfter
5162
) {
52-
// WHEN - THEN
63+
// WHEN
5364
graphQLTestSubscription.start(TIMER_SUBSCRIPTION_RESOURCE);
54-
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> graphQLTestSubscription
55-
.waitAndExpectNoResponse(TIMEOUT, stopAfter));
65+
// THEN
66+
assertThatExceptionOfType(AssertionError.class)
67+
.isThrownBy(() -> graphQLTestSubscription.waitAndExpectNoResponse(TIMEOUT, stopAfter));
5668
assertThatSubscriptionStoppedStatusIs(stopAfter);
5769
}
5870
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import java.time.Instant;
56
import java.util.Collections;
67
import java.util.List;
78
import java.util.Map;
@@ -43,6 +44,8 @@ void shouldAwaitAndGetMultipleResponses() {
4344
@Test
4445
@DisplayName("Should await and get all responses / default stopAfter.")
4546
void shouldAwaitAndGetAllResponsesDefaultStopAfter() {
47+
// GIVEN
48+
final Instant timeBeforeTest = Instant.now();
4649
// WHEN
4750
final List<GraphQLResponse> graphQLResponses = graphQLTestSubscription
4851
.start(TIMER_SUBSCRIPTION_RESOURCE)
@@ -52,6 +55,7 @@ void shouldAwaitAndGetAllResponsesDefaultStopAfter() {
5255
.extracting(response -> response.get(DATA_TIMER_FIELD, Long.class))
5356
.containsExactly(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
5457
assertThatSubscriptionWasStopped();
58+
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTest);
5559
}
5660

5761
@ParameterizedTest
@@ -60,6 +64,8 @@ void shouldAwaitAndGetAllResponsesDefaultStopAfter() {
6064
void shouldAwaitAndGetAllResponses(
6165
final boolean stopAfter
6266
) {
67+
// GIVEN
68+
final Instant timeBeforeTest = Instant.now();
6369
// WHEN
6470
final List<GraphQLResponse> graphQLResponses = graphQLTestSubscription
6571
.start(TIMER_SUBSCRIPTION_RESOURCE)
@@ -69,6 +75,7 @@ void shouldAwaitAndGetAllResponses(
6975
.extracting(response -> response.get(DATA_TIMER_FIELD, Long.class))
7076
.containsExactly(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
7177
assertThatSubscriptionStoppedStatusIs(stopAfter);
78+
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTest);
7279
}
7380

7481
@Test

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

+11
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import org.junit.jupiter.api.AfterEach;
77
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Timeout;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.test.context.SpringBootTest;
1011
import org.springframework.core.env.Environment;
1112

13+
import java.time.Duration;
14+
import java.time.Instant;
15+
1216
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
17+
@Timeout(60)
1318
public class GraphQLTestSubscriptionTestBase {
1419

1520
protected static final String TIMER_SUBSCRIPTION_RESOURCE = "timer-subscription-resource.graphql";
@@ -63,4 +68,10 @@ protected void assertThatSubscriptionWasNotStopped() {
6368
.as("Subscription should not be stopped.")
6469
.isFalse();
6570
}
71+
72+
protected void assertThatMinimumRequiredTimeElapsedSince(final Instant timeBeforeTestStart) {
73+
assertThat(Duration.between(timeBeforeTestStart, Instant.now()))
74+
.as("Should wait the specified amount of time")
75+
.isGreaterThanOrEqualTo(Duration.ofMillis(TIMEOUT));
76+
}
6677
}

0 commit comments

Comments
 (0)