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

[MPRS] Route cancellation to a background thread for the TCK's sake #1608

Merged
merged 2 commits into from
Apr 2, 2020
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 @@ -513,29 +513,27 @@ static <T, R> Flow.Processor<T, R> coupledBuildProcessor(Flow.Subscriber<? super
inlet
.onComplete(() -> complete(subscriberActivity))
.onError(e -> fail(subscriberActivity, e))
.compose(upstream -> new MultiCancelOnExecutor<>(upstream, coupledExecutor))
.takeUntil(Multi.from(publisherActivity, true))
.onCancel(() -> complete(subscriberActivity))
.subscribe(subscriber);

Multi<? extends R> outlet = Multi.from(publisher)
.onComplete(() -> complete(publisherActivity))
.onError(e -> fail(publisherActivity, e))
.compose(upstream -> new MultiCancelOnExecutor<>(upstream, coupledExecutor))
.takeUntil(Multi.from(subscriberActivity, true))
.onCancel(() -> complete(publisherActivity));

return new BridgeProcessor<>(inlet, outlet);
}

static void complete(CompletableFuture<Object> cf) {
coupledExecutor.execute(() -> {
cf.complete(null);
});
cf.complete(null);
}

static void fail(CompletableFuture<Object> cf, Throwable ex) {
coupledExecutor.execute(() -> {
cf.completeExceptionally(ex);
});
cf.completeExceptionally(ex);
}

// Workaround for a TCK bug when calling cancel() from any method named onComplete().
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package io.helidon.microprofile.reactive;

import java.util.concurrent.Executor;
import java.util.concurrent.Flow;

import io.helidon.common.reactive.Multi;

/**
* Routes the downstream cancel() call through the given executor.
* @param <T> the element type of the sequence
*/
final class MultiCancelOnExecutor<T> implements Multi<T> {

private final Multi<T> source;

private final Executor executor;

MultiCancelOnExecutor(Multi<T> source, Executor executor) {
this.source = source;
this.executor = executor;
}

@Override
public void subscribe(Flow.Subscriber<? super T> subscriber) {
source.subscribe(new CancelOnExecutorSubscriber<>(subscriber, executor));
}

static final class CancelOnExecutorSubscriber<T>
implements Flow.Subscriber<T>, Flow.Subscription, Runnable {

private final Flow.Subscriber<? super T> downstream;

private final Executor executor;

private volatile boolean canceled;

private Flow.Subscription upstream;

CancelOnExecutorSubscriber(Flow.Subscriber<? super T> downstream, Executor executor) {
this.downstream = downstream;
this.executor = executor;
}

@Override
public void run() {
upstream.cancel();
upstream = SubscriptionHelper.CANCELED;
}

@Override
public void onSubscribe(Flow.Subscription subscription) {
SubscriptionHelper.validate(upstream, subscription);
upstream = subscription;
downstream.onSubscribe(this);
}

@Override
public void onNext(T item) {
if (!canceled) {
downstream.onNext(item);
}
}

@Override
public void onError(Throwable throwable) {
if (!canceled) {
downstream.onError(throwable);
}
}

@Override
public void onComplete() {
if (!canceled) {
downstream.onComplete();
}
}

@Override
public void request(long n) {
upstream.request(n);
}

@Override
public void cancel() {
if (!canceled) {
canceled = true;
executor.execute(this);
}
}
}
}