Skip to content
Open
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 @@ -44,7 +44,7 @@ private void emptyInFlightQueueIfWeCan() {
//
// from the top of the in flight queue, take all the CFs that have
// completed... but stop if they are not done
while (!inFlightDataQ.isEmpty()) {
while (!inFlightDataQ.isEmpty() && !isTerminal()) {
CompletionStage<?> cs = inFlightDataQ.peek();
if (cs != null) {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,24 @@ protected void whenNextFinished(CompletionStage<D> completionStage, D d, Throwab
if (throwable != null) {
handleThrowableDuringMapping(throwable);
} else {
downstreamSubscriber.onNext(d);
onNextDownstream(d);
}
} finally {
removeFromInFlightQ(completionStage);
finallyAfterEachPromiseFinishes();
}
}

private void onNextDownstream(D d) {
// promises can complete on different threads, but the downstream signals must not overlap
// (reactive streams rule 1.3) and no onNext may follow onError
lock.runLocked(() -> {
if (!isTerminal()) {
downstreamSubscriber.onNext(d);
}
});
}

protected void finallyAfterEachPromiseFinishes() {
Runnable runOnCompleteOrErrorRun = lock.callLocked(() -> {
if (inFlightDataQ.isEmpty()) {
Expand All @@ -115,7 +125,7 @@ protected void finallyAfterEachPromiseFinishes() {
protected void handleThrowableDuringMapping(Throwable throwable) {
// only do this once
if (isTerminal.compareAndSet(false, true)) {
downstreamSubscriber.onError(throwable);
lock.runLocked(() -> downstreamSubscriber.onError(throwable));
//
// Reactive semantics say that IF an exception happens on a publisher,
// then onError is called and no more messages flow. But since the exception happened
Expand All @@ -133,7 +143,7 @@ protected void handleThrowableDuringMapping(Throwable throwable) {
public void onError(Throwable t) {
// we immediately terminate - we don't wait for any promises to complete
if (isTerminal.compareAndSet(false, true)) {
downstreamSubscriber.onError(t);
lock.runLocked(() -> downstreamSubscriber.onError(t));
cancelInFlightFutures();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package graphql.execution.reactive

import graphql.execution.pubsub.CapturingSubscriber
import graphql.execution.pubsub.CapturingSubscription
import org.awaitility.Awaitility
import org.reactivestreams.Subscriber
import spock.lang.Specification

import java.time.Duration
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import java.util.function.Function

/**
Expand Down Expand Up @@ -209,6 +214,83 @@ class CompletionStageSubscriberTest extends Specification {
capturingSubscriber.events == expectedOrderingOfAsyncCompletion()
}

def "onNext is not signalled concurrently when promises complete on different threads"() {
def active = new AtomicInteger()
def maxActive = new AtomicInteger()
def bothEntered = new CountDownLatch(2)
def capturingSubscriber = new CapturingSubscriber<Object>() {
@Override
void onNext(Object o) {
maxActive.accumulateAndGet(active.incrementAndGet(), { a, b -> Math.max(a, b) })
bothEntered.countDown()
// if the signals can overlap, the other thread enters onNext while this one waits
bothEntered.await(500, TimeUnit.MILLISECONDS)
active.decrementAndGet()
super.onNext(o)
}
}
def subscription = new CapturingSubscription()
List<Runnable> promises = []
Function<Integer, CompletionStage<String>> mapper = mapperThatDoesNotComplete(promises)

Subscriber<Integer> completionStageSubscriber = createSubscriber(mapper, capturingSubscriber)

when:
completionStageSubscriber.onSubscribe(subscription)
completionStageSubscriber.onNext(0)
completionStageSubscriber.onNext(1)
def threads = promises.collect { promise -> Thread.start { promise.run() } }
threads.each { it.join() }

then:
maxActive.get() == 1
capturingSubscriber.events.sort() == ["0", "1"]
}

def "onNext is not signalled after onError when a promise completes while another is being delivered"() {
def deliveringZero = new CountDownLatch(1)
def release = new CountDownLatch(1)
def capturingSubscriber = new CapturingSubscriber<Object>() {
@Override
void onNext(Object o) {
if (o == "0") {
deliveringZero.countDown()
release.await(5, TimeUnit.SECONDS)
}
super.onNext(o)
}
}
def subscription = new CapturingSubscription()
List<Runnable> promises = []
Function<Integer, CompletionStage<String>> mapper = mapperThatDoesNotComplete(promises)

def completionStageSubscriber = createSubscriber(mapper, capturingSubscriber)

when:
completionStageSubscriber.onSubscribe(subscription)
completionStageSubscriber.onNext(0)
completionStageSubscriber.onNext(1)
def completeZero = Thread.start { promises[0].run() }
deliveringZero.await(5, TimeUnit.SECONDS)
def completeOne = Thread.start { promises[1].run() }
waitUntilWaitingOrDone(completeOne)
def fail = Thread.start { completionStageSubscriber.onError(new RuntimeException("Bang")) }
Awaitility.await().atMost(Duration.ofSeconds(5)).until { completionStageSubscriber.isTerminal() }
waitUntilWaitingOrDone(fail)
release.countDown()
[completeZero, completeOne, fail].each { it.join() }

then:
capturingSubscriber.events == ["0"]
capturingSubscriber.throwable.getMessage() == "Bang"
}

static void waitUntilWaitingOrDone(Thread thread) {
Awaitility.await().atMost(Duration.ofSeconds(5)).until {
thread.state == Thread.State.WAITING || thread.state == Thread.State.TERMINATED
}
}


static Function<Integer, CompletionStage<String>> mapperThatDoesNotComplete(List<Runnable> runToComplete, List<CompletableFuture> promises = []) {
def mapper = new Function<Integer, CompletionStage<String>>() {
Expand Down
Loading