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

fix: test subscription not waiting the specified amount of time #570

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 @@ -300,9 +300,17 @@ public List<GraphQLResponse> awaitAndGetNextResponses(
fail("Subscription already stopped. Forgot to call reset after test case?");
}

await()
.atMost(timeout, TimeUnit.MILLISECONDS)
.until(() -> state.getResponses().size() >= numExpectedResponses);
if (numExpectedResponses > 0) {
await()
.atMost(timeout, TimeUnit.MILLISECONDS)
.until(() -> state.getResponses().size() >= numExpectedResponses);
} else {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
fail("Unable to wait the specified amount of time.", e);
}
}

if (stopAfter) {
stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.time.Instant;

@DisplayName("Testing awaitNoResponse methods")
class GraphQLSubscriptionTestAwaitNoAnswerTest extends GraphQLTestSubscriptionTestBase {

@Test
@DisplayName("Should succeed if no responses arrived / default stopAfter.")
void shouldAwaitNoResponseSucceedIfNoResponsesArrivedDefaultStopAfter() {
// WHEN - THEN
// GIVEN
final Instant timeBeforeTestStart = Instant.now();
// WHEN
graphQLTestSubscription
.start(SUBSCRIPTION_THAT_TIMES_OUT_RESOURCE)
.waitAndExpectNoResponse(TIMEOUT);
// THEN
assertThatSubscriptionWasStopped();
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTestStart);
}

@ParameterizedTest
Expand All @@ -26,20 +32,25 @@ void shouldAwaitNoResponseSucceedIfNoResponsesArrivedDefaultStopAfter() {
void shouldAwaitNoResponseSucceedIfNoResponsesArrived(
final boolean stopAfter
) {
// WHEN - THEN
// GIVEN
final Instant timeBeforeTestStart = Instant.now();
// WHEN
graphQLTestSubscription
.start(SUBSCRIPTION_THAT_TIMES_OUT_RESOURCE)
.waitAndExpectNoResponse(TIMEOUT, stopAfter);
// THEN
assertThatSubscriptionStoppedStatusIs(stopAfter);
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTestStart);
}

@Test
@DisplayName("Should raise assertion error if any response arrived / default stop after.")
void shouldRaiseAssertionErrorIfResponseArrivedDefaultStopAfter() {
// WHEN - THEN
// WHEN
graphQLTestSubscription.start(TIMER_SUBSCRIPTION_RESOURCE);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> graphQLTestSubscription
.waitAndExpectNoResponse(TIMEOUT));
// THEN
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> graphQLTestSubscription.waitAndExpectNoResponse(TIMEOUT));
assertThatSubscriptionWasStopped();
}

Expand All @@ -49,10 +60,11 @@ void shouldRaiseAssertionErrorIfResponseArrivedDefaultStopAfter() {
void shouldRaiseAssertionErrorIfResponseArrived(
final boolean stopAfter
) {
// WHEN - THEN
// WHEN
graphQLTestSubscription.start(TIMER_SUBSCRIPTION_RESOURCE);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> graphQLTestSubscription
.waitAndExpectNoResponse(TIMEOUT, stopAfter));
// THEN
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> graphQLTestSubscription.waitAndExpectNoResponse(TIMEOUT, stopAfter));
assertThatSubscriptionStoppedStatusIs(stopAfter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -43,6 +44,8 @@ void shouldAwaitAndGetMultipleResponses() {
@Test
@DisplayName("Should await and get all responses / default stopAfter.")
void shouldAwaitAndGetAllResponsesDefaultStopAfter() {
// GIVEN
final Instant timeBeforeTest = Instant.now();
// WHEN
final List<GraphQLResponse> graphQLResponses = graphQLTestSubscription
.start(TIMER_SUBSCRIPTION_RESOURCE)
Expand All @@ -52,6 +55,7 @@ void shouldAwaitAndGetAllResponsesDefaultStopAfter() {
.extracting(response -> response.get(DATA_TIMER_FIELD, Long.class))
.containsExactly(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
assertThatSubscriptionWasStopped();
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTest);
}

@ParameterizedTest
Expand All @@ -60,6 +64,8 @@ void shouldAwaitAndGetAllResponsesDefaultStopAfter() {
void shouldAwaitAndGetAllResponses(
final boolean stopAfter
) {
// GIVEN
final Instant timeBeforeTest = Instant.now();
// WHEN
final List<GraphQLResponse> graphQLResponses = graphQLTestSubscription
.start(TIMER_SUBSCRIPTION_RESOURCE)
Expand All @@ -69,6 +75,7 @@ void shouldAwaitAndGetAllResponses(
.extracting(response -> response.get(DATA_TIMER_FIELD, Long.class))
.containsExactly(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
assertThatSubscriptionStoppedStatusIs(stopAfter);
assertThatMinimumRequiredTimeElapsedSince(timeBeforeTest);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;

import java.time.Duration;
import java.time.Instant;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Timeout(60)
public class GraphQLTestSubscriptionTestBase {

protected static final String TIMER_SUBSCRIPTION_RESOURCE = "timer-subscription-resource.graphql";
Expand Down Expand Up @@ -63,4 +68,10 @@ protected void assertThatSubscriptionWasNotStopped() {
.as("Subscription should not be stopped.")
.isFalse();
}

protected void assertThatMinimumRequiredTimeElapsedSince(final Instant timeBeforeTestStart) {
assertThat(Duration.between(timeBeforeTestStart, Instant.now()))
.as("Should wait the specified amount of time")
.isGreaterThanOrEqualTo(Duration.ofMillis(TIMEOUT));
}
}