use of com.graphql_java_generator.client.SubscriptionClient in project graphql-maven-plugin-project by graphql-java-generator.
the class SubscriptionRequests method execSubscription.
public void execSubscription() throws GraphQLRequestPreparationException, GraphQLRequestExecutionException, IOException {
// Preparation
GraphQLRequest subscriptionRequest = subscriptionTypeExecutor.getSubscribeToNewPostGraphQLRequest("{id date author publiclyAvailable title content}");
GraphQLRequest createPostRequest = mutationTypeExecutor.getCreatePostGraphQLRequest("{id date author{id} title content publiclyAvailable}");
PostSubscriptionCallback postSubscriptionCallback = new PostSubscriptionCallback();
Member author = new Member();
author.setId("12");
PostInput postInput = new PostInput();
postInput.setTopicId("22");
postInput.setInput(getTopicPostInput(author, "Some other content", new GregorianCalendar(2020, 11 - 1, 21).getTime(), false, "The good title for a post"));
// Go, go, go
System.out.println("Submitting the 'subscribeToNewPostWithBindValues' GraphQL subscription");
SubscriptionClient subscriptionClient = subscriptionTypeExecutor.subscribeToNewPost(subscriptionRequest, postSubscriptionCallback, "Board name 1");
// For this test, we need to be sure that the subscription is active, before creating the post (that we will
// receive a notification about). 3s: that's long, but my PC is so slow from time to time... :(
final int TIMEOUT1 = 3000;
for (int i = 0; i < TIMEOUT1 / 10; i += 1) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
if (postSubscriptionCallback.connected) {
break;
}
}
// Let's check that everything is ready
if (!postSubscriptionCallback.connected) {
throw new RuntimeException("The subscription should be active");
}
System.out.println("Creating a post (for which we expect a notification) from this postInput: " + postInput.toString());
mutationTypeExecutor.createPost(createPostRequest, postInput);
// ////////////////////////////////////////////////////////////////////////////////////
// Let's check that we've received the expected notification, for this post creation
// Let's wait 10 seconds max for the post creation notification (from the subscription). We'll get interrupted
// before, as soon as we receive the notification
// (see the callback implementation in the PostSubscriptionCallback class)
Post notifiedPost = null;
// Let's wait 10s max, until the connection is active
final int TIMEOUT2 = 10;
for (int i = 0; i < TIMEOUT2 * 1000 / 10; i += 1) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
if ((notifiedPost = postSubscriptionCallback.lastReceivedMessage) != null) {
// Ok, we're connected. We're done
break;
}
}
if (notifiedPost == null) {
throw new RuntimeException("The notification for the post creation was not received");
}
// We need to free the server resources, at the end
subscriptionClient.unsubscribe();
}
use of com.graphql_java_generator.client.SubscriptionClient in project graphql-maven-plugin-project by graphql-java-generator.
the class GraphQLVariablesIT method test_GraphQLVariables_subscribeToADate.
@Execution(ExecutionMode.CONCURRENT)
@Test
public void test_GraphQLVariables_subscribeToADate() throws GraphQLRequestExecutionException, GraphQLRequestPreparationException, InterruptedException {
// Preparation
SubscriptionCallbackToADate callback = new SubscriptionCallbackToADate("test_GraphQLVariables_subscribeToAList");
Date date = new GregorianCalendar(2021, 4 - 1, 15).getTime();
// Go, go, go
GraphQLRequest subscription = subscriptionExecutor.getGraphQLRequest("subscription sub($aCustomScalarParam: Date!) {issue53(date: $aCustomScalarParam){}}");
SubscriptionClient sub = subscription.execSubscription(callback, Date.class, "aCustomScalarParam", date);
// Let's wait a max of 10 second, until we receive some notifications (my PC is really slow, especially when the
// antivirus consumes 98% of my CPU!
callback.latchForMessageReception.await(10, TimeUnit.SECONDS);
// Let's disconnect from the subscription
sub.unsubscribe();
// Verification
assertNull(callback.lastReceivedError, "expected no error, but received " + callback.lastReceivedError);
assertNotNull(callback.lastReceivedMessage, "The subscription should have received a message");
}
use of com.graphql_java_generator.client.SubscriptionClient in project graphql-maven-plugin-project by graphql-java-generator.
the class AliasesIT method test_subscription.
@Execution(ExecutionMode.CONCURRENT)
@Test
public void test_subscription() throws GraphQLRequestExecutionException, GraphQLRequestPreparationException, InterruptedException {
// Preparation
HumanSubscriptionCallback callback = new HumanSubscriptionCallback();
// Go, go, go
SubscriptionClient sub = subscriptionExecutor.subscribeNewHumanForEpisode(//
"{aliasId:id id aliasName:name name aliasHomePlanet:homePlanet homePlanet}", callback, Episode.JEDI);
// Let's wait a max of 10 second, until we receive some notifications
waitForEvent(100, () -> {
return callback.lastReceivedMessage != null || callback.lastError != null;
}, "Waiting for the subscription to receive the notification");
// Let's disconnect from the subscription
sub.unsubscribe();
// Verification
if (callback.lastError != null) {
fail("The subsccription raised this error: " + callback.lastError);
}
assertNotNull(callback.lastReceivedMessage);
assertTrue(callback.lastReceivedMessage instanceof Human);
Human verif = callback.lastReceivedMessage;
assertEquals(verif.getId(), verif.getAliasValue("aliasId"));
assertEquals(verif.getName(), verif.getAliasValue("aliasName"));
assertEquals(verif.getHomePlanet(), verif.getAliasValue("aliasHomePlanet"));
}
use of com.graphql_java_generator.client.SubscriptionClient in project graphql-maven-plugin-project by graphql-java-generator.
the class SubscriptionRequests method execSubscription.
public void execSubscription() throws GraphQLRequestPreparationException, GraphQLRequestExecutionException, IOException {
// Preparation
GraphQLRequest subscriptionRequest = subscriptionTypeExecutor.getNewCharacterGraphQLRequest("{id name __typename}");
GraphQLRequest createHumanRequest = mutationTypeExecutor.getCreateHumanGraphQLRequest("{id name __typename}");
NewCharacterSubscriptionCallback newCharacterSubscriptionCallback = new NewCharacterSubscriptionCallback();
String humaneName = "Anakin";
String homePlanet = "Tatooine";
// Go, go, go
System.out.println("Submitting the 'subscribeToNewPostWithBindValues' GraphQL subscription");
SubscriptionClient subscriptionClient = subscriptionTypeExecutor.newCharacter(subscriptionRequest, newCharacterSubscriptionCallback);
// (the connection is done in a parallel thread, as it's a reactive stuff)
try {
// Wait 1s
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.debug("Got interrupted (1)");
}
// Let's check that everything is ready
if (!newCharacterSubscriptionCallback.connected) {
throw new RuntimeException("The subscription should be active");
}
System.out.println("Creating a Human (for which we expect a notification) from this name: '" + humaneName + "' and this homePlanet: '" + homePlanet + "'");
mutationTypeExecutor.createHuman(createHumanRequest, humaneName, homePlanet);
// ////////////////////////////////////////////////////////////////////////////////////
// Let's check that we've received the expected notification, for this post creation
// Let's wait 10 seconds max for the post creation notification (from the subscription). We'll get interrupted
// before, as soon as we receive the notification
// (see the callback implementation in the PostSubscriptionCallback class)
com.generated.graphql.Character notifiedPost = null;
// Let's wait 10s max, until the connection is active
final int TIMEOUT = 10;
for (int i = 0; i < TIMEOUT * 1000 / 10; i += 1) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
if ((notifiedPost = newCharacterSubscriptionCallback.lastReceivedMessage) != null) {
// Ok, we're connected. We're done
break;
}
}
if (notifiedPost == null) {
throw new RuntimeException("The notification for the post creation was not received");
}
// We need to free the server resources, at the end
subscriptionClient.unsubscribe();
}
use of com.graphql_java_generator.client.SubscriptionClient in project graphql-maven-plugin-project by graphql-java-generator.
the class ExecSubscriptionIT method test_subscribeToADate_clientComplete.
/**
* Tests that the graphql-transport-ws 'complete' message is properly propagated from the client to the server, if
* it occurs
*
* @throws GraphQLRequestPreparationException
* @throws GraphQLRequestExecutionException
* @throws InterruptedException
*/
@Test
@Execution(ExecutionMode.CONCURRENT)
public void test_subscribeToADate_clientComplete() throws GraphQLRequestExecutionException, GraphQLRequestPreparationException, InterruptedException {
logger.info("------------------------------------------------------------------------------------------------");
logger.info("Starting test_subscribeToADate_clientComplete");
SubscriptionTestParam param = getSubscriptionTestParam();
SubscriptionCallbackString callback = new SubscriptionCallbackString("test_subscribeToADate_clientComplete");
SubscriptionClient sub = subscriptionExecutor.subscriptionTest("", callback, param);
// Let's wait a max of 20 second, until we receive some notifications
// (20s will never occur... unless using the debugger to undebug some stuff)
boolean success = callback.latchForMessageReception.await(20, TimeUnit.SECONDS);
// Let's wait for our first message
logger.debug("latchForMessageReception has been freed for client with status {}", success);
assertNotNull(callback.lastReceivedMessage, "we must have received a message");
// Let's unsubscribe from this subscription
sub.unsubscribe();
// Now, we wait for 1s to be sure that the server has executed this unsubscription
Thread.sleep(1000);
callback.lastReceivedMessage = null;
// /////////////// Now, we wait another second: no message should be sent by the server
Thread.sleep(1000);
assertNull(callback.lastReceivedMessage, "no more message should be sent by the server");
}
Aggregations