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

2.x: make sure interval+trampoline can be stopped #5367

Merged
merged 1 commit into from May 28, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,9 +19,11 @@
import org.reactivestreams.*;

import io.reactivex.*;
import io.reactivex.Scheduler.Worker;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.MissingBackpressureException;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.internal.schedulers.TrampolineScheduler;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.internal.util.BackpressureHelper;

Expand All @@ -43,9 +45,16 @@ public void subscribeActual(Subscriber<? super Long> s) {
IntervalSubscriber is = new IntervalSubscriber(s);
s.onSubscribe(is);

Disposable d = scheduler.schedulePeriodicallyDirect(is, initialDelay, period, unit);
Scheduler sch = scheduler;

is.setResource(d);
if (sch instanceof TrampolineScheduler) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this fix be made in TrampolineScheduler itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the schedulePeriodic has to return a Disposable in order to allow it to be stopped but the first task stays in the trampoline loop and stays there never returning from the call. The disposability has to happen before that. This is a similar case to synchronous cancellation with streams that have to send a Disposable/Subscription first to allow cancellation before going into some form of loop that would otherwise not return. The classic Rx.NET and early RxJava suffered from this on a grand scale. This issue only affects interval and trampoline scheduler together.

Worker worker = sch.createWorker();
is.setResource(worker);
worker.schedulePeriodically(is, initialDelay, period, unit);
} else {
Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit);
is.setResource(d);
}
}

static final class IntervalSubscriber extends AtomicLong
Expand Down
Expand Up @@ -19,9 +19,11 @@
import org.reactivestreams.*;

import io.reactivex.*;
import io.reactivex.Scheduler.Worker;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.MissingBackpressureException;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.internal.schedulers.TrampolineScheduler;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.internal.util.BackpressureHelper;

Expand All @@ -47,9 +49,16 @@ public void subscribeActual(Subscriber<? super Long> s) {
IntervalRangeSubscriber is = new IntervalRangeSubscriber(s, start, end);
s.onSubscribe(is);

Disposable d = scheduler.schedulePeriodicallyDirect(is, initialDelay, period, unit);
Scheduler sch = scheduler;

is.setResource(d);
if (sch instanceof TrampolineScheduler) {
Worker worker = sch.createWorker();
is.setResource(worker);
worker.schedulePeriodically(is, initialDelay, period, unit);
} else {
Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit);
is.setResource(d);
}
}

static final class IntervalRangeSubscriber extends AtomicLong
Expand Down
Expand Up @@ -17,8 +17,10 @@
import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.*;
import io.reactivex.Scheduler.Worker;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.*;
import io.reactivex.internal.schedulers.TrampolineScheduler;

public final class ObservableInterval extends Observable<Long> {
final Scheduler scheduler;
Expand All @@ -38,9 +40,16 @@ public void subscribeActual(Observer<? super Long> s) {
IntervalObserver is = new IntervalObserver(s);
s.onSubscribe(is);

Disposable d = scheduler.schedulePeriodicallyDirect(is, initialDelay, period, unit);
Scheduler sch = scheduler;

is.setResource(d);
if (sch instanceof TrampolineScheduler) {
Worker worker = sch.createWorker();
is.setResource(worker);
worker.schedulePeriodically(is, initialDelay, period, unit);
} else {
Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit);
is.setResource(d);
}
}

static final class IntervalObserver
Expand Down
Expand Up @@ -17,8 +17,10 @@
import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.*;
import io.reactivex.Scheduler.Worker;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.*;
import io.reactivex.internal.schedulers.TrampolineScheduler;

public final class ObservableIntervalRange extends Observable<Long> {
final Scheduler scheduler;
Expand All @@ -42,9 +44,16 @@ public void subscribeActual(Observer<? super Long> s) {
IntervalRangeObserver is = new IntervalRangeObserver(s, start, end);
s.onSubscribe(is);

Disposable d = scheduler.schedulePeriodicallyDirect(is, initialDelay, period, unit);
Scheduler sch = scheduler;

is.setResource(d);
if (sch instanceof TrampolineScheduler) {
Worker worker = sch.createWorker();
is.setResource(worker);
worker.schedulePeriodically(is, initialDelay, period, unit);
} else {
Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit);
is.setResource(d);
}
}

static final class IntervalRangeObserver
Expand Down
Expand Up @@ -100,6 +100,10 @@ Disposable enqueue(Runnable action, long execTime) {
int missed = 1;
for (;;) {
for (;;) {
if (disposed) {
queue.clear();
return EmptyDisposable.INSTANCE;
}
final TimedRunnable polled = queue.poll();
if (polled == null) {
break;
Expand Down
Expand Up @@ -109,4 +109,12 @@ public void take() {
.awaitDone(5, TimeUnit.SECONDS)
.assertResult(1L);
}

@Test(timeout = 2000)
public void cancel() {
Flowable.intervalRange(0, 20, 1, 1, TimeUnit.MILLISECONDS, Schedulers.trampoline())
.take(10)
.test()
.assertResult(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
}
}
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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.reactivex.internal.operators.flowable;

import java.util.concurrent.TimeUnit;

import org.junit.Test;

import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;

public class FlowableIntervalTest {

@Test(timeout = 2000)
public void cancel() {
Flowable.interval(1, TimeUnit.MILLISECONDS, Schedulers.trampoline())
.take(10)
.test()
.assertResult(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
}
}
Expand Up @@ -76,4 +76,12 @@ public void longOverflow() {
public void dispose() {
TestHelper.checkDisposed(Observable.intervalRange(1, 2, 1, 1, TimeUnit.MILLISECONDS));
}

@Test(timeout = 2000)
public void cancel() {
Observable.intervalRange(0, 20, 1, 1, TimeUnit.MILLISECONDS, Schedulers.trampoline())
.take(10)
.test()
.assertResult(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
}
}
Expand Up @@ -18,12 +18,20 @@
import org.junit.Test;

import io.reactivex.*;
import io.reactivex.schedulers.TestScheduler;
import io.reactivex.schedulers.*;

public class ObservableIntervalTest {

@Test
public void dispose() {
TestHelper.checkDisposed(Observable.interval(1, TimeUnit.MILLISECONDS, new TestScheduler()));
}

@Test(timeout = 2000)
public void cancel() {
Observable.interval(1, TimeUnit.MILLISECONDS, Schedulers.trampoline())
.take(10)
.test()
.assertResult(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
}
}