use of com.generated.graphql.Human in project graphql-maven-plugin-project by graphql-java-generator.
the class SubscriptionIT method testSubscription.
@Test
@Execution(ExecutionMode.CONCURRENT)
void testSubscription() throws GraphQLRequestExecutionException, InterruptedException, ExecutionException {
// Preparation
CharacterSubscriptionCallback callback = new CharacterSubscriptionCallback();
// Go, go, go
logger.debug("Subscribing to the GraphQL subscription");
SubscriptionClient client = subscriptionType.newCharacter(subscriptionRequest, callback);
// Let's wait 1 seconds max, that the web socket is properly connected
try {
// Wait 1s
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.debug("Got interrupted (1)");
}
logger.debug("Creating the post, for which we should receice the notification");
CompletableFuture<Character> createdPostASync = CompletableFuture.supplyAsync(() -> {
try {
// But we wait as little as possible
for (int i = 0; i < 100; i += 1) {
Thread.sleep(10);
if (callback.connected)
break;
}
return mutationType.createHuman(createHumanRequest, "new name", "new home planet");
} catch (GraphQLRequestExecutionException | InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
});
// (see the callback implementation in the CharacterSubscriptionCallback class)
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
logger.debug("Got interrupted (2)");
}
// Verification
assertNull(callback.lastReceivedClose, "We should have received no close message (" + callback.lastReceivedClose + ")");
assertNull(callback.lastReceivedError, "We should have received no error (" + callback.lastReceivedError + ")");
assertNotNull(callback.lastReceivedMessage, "We should have received a post");
Character createdCharacter = createdPostASync.get();
assertTrue(createdCharacter instanceof Human);
assertEquals(createdCharacter.getId(), callback.lastReceivedMessage.getId(), "Is it 'our' new Character?");
assertEquals("new home planet", ((Human) callback.lastReceivedMessage).getHomePlanet(), "Check of the fragment behavior");
// We must free the server resource at the end
client.unsubscribe();
logger.debug("Stopped listening");
}
Aggregations