Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix randomly failing SubscriptionHasToBeCancelledOnWebSocketCloseTestCase #1561

Merged
merged 1 commit into from
Sep 19, 2022
Merged
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
@@ -1,10 +1,12 @@
package io.smallrye.graphql.tests.subscription;

import static org.junit.Assert.assertTrue;

import java.lang.reflect.Field;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;
Expand All @@ -14,7 +16,6 @@
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -49,7 +50,7 @@ public static WebArchive deployment() {
@GraphQLApi
public static class Foo {

static AtomicBoolean CANCELLED = new AtomicBoolean(false);
static CountDownLatch CANCELLED = new CountDownLatch(1);

@Subscription
public Multi<Long> counting() {
Expand All @@ -58,7 +59,7 @@ public Multi<Long> counting() {
.every(Duration.ofSeconds(1L))
.onCancellation().invoke(() -> {
// this has to be called on the internal subscriber when a client's websocket connection ends
CANCELLED.set(true);
CANCELLED.countDown();
});
}

Expand All @@ -75,23 +76,24 @@ public void testSubscriptionCancel() throws InterruptedException, NoSuchFieldExc
.url(testingURL.toString() + "graphql")
.build();
Multi<Response> multi = client.subscription("subscription {counting}");
CountDownLatch items = new CountDownLatch(1);
multi.subscribe().with(response -> {
items.countDown();
System.out.println(response);
}, error -> {
error.printStackTrace();
throw new AssertionError("Subscription failed", error);
});

// wait until some items are produced
TimeUnit.SECONDS.sleep(3);
assertTrue(items.await(10, TimeUnit.SECONDS));

System.out.println("Forcibly closing the websocket....");
closeUnderlyingHttpClient(client);

TimeUnit.SECONDS.sleep(1);
// assert that the internal subscriber of the Multi on the server side was closed
Assert.assertTrue("The server-side Multi's internal subscriber has to be cancelled " +
assertTrue("The server-side Multi's internal subscriber has to be cancelled " +
"after the client websocket connection is closed",
Foo.CANCELLED.get());
Foo.CANCELLED.await(10, TimeUnit.SECONDS));
}

private void closeUnderlyingHttpClient(DynamicGraphQLClient client) throws NoSuchFieldException, IllegalAccessException {
Expand Down