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

3.x: Fix Observable.window (size, skip, overlap) dispose behavior #7049

Merged
merged 1 commit into from
Aug 6, 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 @@ -51,18 +51,20 @@ static final class WindowExactObserver<T>
final long count;
final int capacityHint;

final AtomicBoolean cancelled;

long size;

Disposable upstream;

UnicastSubject<T> window;

volatile boolean cancelled;

WindowExactObserver(Observer<? super Observable<T>> actual, long count, int capacityHint) {
this.downstream = actual;
this.count = count;
this.capacityHint = capacityHint;
this.cancelled = new AtomicBoolean();
this.lazySet(1);
}

@Override
Expand All @@ -78,7 +80,9 @@ public void onSubscribe(Disposable d) {
public void onNext(T t) {
UnicastSubject<T> w = window;
ObservableWindowSubscribeIntercept<T> intercept = null;
if (w == null && !cancelled) {
if (w == null && !cancelled.get()) {
getAndIncrement();

w = UnicastSubject.create(capacityHint, this);
window = w;
intercept = new ObservableWindowSubscribeIntercept<>(w);
Expand All @@ -92,15 +96,12 @@ public void onNext(T t) {
size = 0;
window = null;
w.onComplete();
if (cancelled) {
upstream.dispose();
}
}

if (intercept != null && intercept.tryAbandon()) {
window = null;
w.onComplete();
w = null;
window = null;
}
}
}
Expand All @@ -127,23 +128,25 @@ public void onComplete() {

@Override
public void dispose() {
cancelled = true;
if (cancelled.compareAndSet(false, true)) {
run();
}
}

@Override
public boolean isDisposed() {
return cancelled;
return cancelled.get();
}

@Override
public void run() {
if (cancelled) {
if (decrementAndGet() == 0) {
upstream.dispose();
}
}
}

static final class WindowSkipObserver<T> extends AtomicBoolean
static final class WindowSkipObserver<T> extends AtomicInteger
implements Observer<T>, Disposable, Runnable {

private static final long serialVersionUID = 3366976432059579510L;
Expand All @@ -153,23 +156,23 @@ static final class WindowSkipObserver<T> extends AtomicBoolean
final int capacityHint;
final ArrayDeque<UnicastSubject<T>> windows;

long index;
final AtomicBoolean cancelled;

volatile boolean cancelled;
long index;

/** Counts how many elements were emitted to the very first window in windows. */
long firstEmission;

Disposable upstream;

final AtomicInteger wip = new AtomicInteger();

WindowSkipObserver(Observer<? super Observable<T>> actual, long count, long skip, int capacityHint) {
this.downstream = actual;
this.count = count;
this.skip = skip;
this.capacityHint = capacityHint;
this.windows = new ArrayDeque<>();
this.cancelled = new AtomicBoolean();
this.lazySet(1);
}

@Override
Expand All @@ -191,8 +194,8 @@ public void onNext(T t) {

ObservableWindowSubscribeIntercept<T> intercept = null;

if (i % s == 0 && !cancelled) {
wip.getAndIncrement();
if (i % s == 0 && !cancelled.get()) {
getAndIncrement();
UnicastSubject<T> w = UnicastSubject.create(capacityHint, this);
intercept = new ObservableWindowSubscribeIntercept<>(w);
ws.offer(w);
Expand All @@ -207,8 +210,7 @@ public void onNext(T t) {

if (c >= count) {
ws.poll().onComplete();
if (ws.isEmpty() && cancelled) {
this.upstream.dispose();
if (ws.isEmpty() && cancelled.get()) {
return;
}
firstEmission = c - s;
Expand Down Expand Up @@ -243,20 +245,20 @@ public void onComplete() {

@Override
public void dispose() {
cancelled = true;
if (cancelled.compareAndSet(false, true)) {
run();
}
}

@Override
public boolean isDisposed() {
return cancelled;
return cancelled.get();
}

@Override
public void run() {
if (wip.decrementAndGet() == 0) {
if (cancelled) {
upstream.dispose();
}
if (decrementAndGet() == 0) {
upstream.dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.reactivestreams.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
Expand Down Expand Up @@ -721,4 +722,94 @@ public void moreQueuedClean() {
.test(3)
.cancel();
}

@Test
public void cancelWithoutWindowSize() {
PublishProcessor<Integer> pp = PublishProcessor.create();

TestSubscriber<Flowable<Integer>> ts = pp.window(10)
.test();

assertTrue(pp.hasSubscribers());

ts.cancel();

assertFalse("Subject still has subscribers!", pp.hasSubscribers());
}

@Test
public void cancelAfterAbandonmentSize() {
PublishProcessor<Integer> pp = PublishProcessor.create();

TestSubscriber<Flowable<Integer>> ts = pp.window(10)
.test();

assertTrue(pp.hasSubscribers());

pp.onNext(1);

ts.cancel();

assertFalse("Subject still has subscribers!", pp.hasSubscribers());
}

@Test
public void cancelWithoutWindowSkip() {
PublishProcessor<Integer> pp = PublishProcessor.create();

TestSubscriber<Flowable<Integer>> ts = pp.window(10, 15)
.test();

assertTrue(pp.hasSubscribers());

ts.cancel();

assertFalse("Subject still has subscribers!", pp.hasSubscribers());
}

@Test
public void cancelAfterAbandonmentSkip() {
PublishProcessor<Integer> pp = PublishProcessor.create();

TestSubscriber<Flowable<Integer>> ts = pp.window(10, 15)
.test();

assertTrue(pp.hasSubscribers());

pp.onNext(1);

ts.cancel();

assertFalse("Subject still has subscribers!", pp.hasSubscribers());
}

@Test
public void cancelWithoutWindowOverlap() {
PublishProcessor<Integer> pp = PublishProcessor.create();

TestSubscriber<Flowable<Integer>> ts = pp.window(10, 5)
.test();

assertTrue(pp.hasSubscribers());

ts.cancel();

assertFalse("Subject still has subscribers!", pp.hasSubscribers());
}

@Test
public void cancelAfterAbandonmentOverlap() {
PublishProcessor<Integer> pp = PublishProcessor.create();

TestSubscriber<Flowable<Integer>> ts = pp.window(10, 5)
.test();

assertTrue(pp.hasSubscribers());

pp.onNext(1);

ts.cancel();

assertFalse("Subject still has subscribers!", pp.hasSubscribers());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,94 @@ public void accept(Observable<Integer> v) throws Throwable {

inner.get().test().assertResult(1);
}

@Test
public void cancelWithoutWindowSize() {
PublishSubject<Integer> ps = PublishSubject.create();

TestObserver<Observable<Integer>> to = ps.window(10)
.test();

assertTrue(ps.hasObservers());

to.dispose();

assertFalse("Subject still has observers!", ps.hasObservers());
}

@Test
public void cancelAfterAbandonmentSize() {
PublishSubject<Integer> ps = PublishSubject.create();

TestObserver<Observable<Integer>> to = ps.window(10)
.test();

assertTrue(ps.hasObservers());

ps.onNext(1);

to.dispose();

assertFalse("Subject still has observers!", ps.hasObservers());
}

@Test
public void cancelWithoutWindowSkip() {
PublishSubject<Integer> ps = PublishSubject.create();

TestObserver<Observable<Integer>> to = ps.window(10, 15)
.test();

assertTrue(ps.hasObservers());

to.dispose();

assertFalse("Subject still has observers!", ps.hasObservers());
}

@Test
public void cancelAfterAbandonmentSkip() {
PublishSubject<Integer> ps = PublishSubject.create();

TestObserver<Observable<Integer>> to = ps.window(10, 15)
.test();

assertTrue(ps.hasObservers());

ps.onNext(1);

to.dispose();

assertFalse("Subject still has observers!", ps.hasObservers());
}

@Test
public void cancelWithoutWindowOverlap() {
PublishSubject<Integer> ps = PublishSubject.create();

TestObserver<Observable<Integer>> to = ps.window(10, 5)
.test();

assertTrue(ps.hasObservers());

to.dispose();

assertFalse("Subject still has observers!", ps.hasObservers());
}

@Test
public void cancelAfterAbandonmentOverlap() {
PublishSubject<Integer> ps = PublishSubject.create();

TestObserver<Observable<Integer>> to = ps.window(10, 5)
.test();

assertTrue(ps.hasObservers());

ps.onNext(1);

to.dispose();

assertFalse("Subject still has observers!", ps.hasObservers());
}
}