Skip to content

Take/Redo Unsubscribe #1793

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

Merged
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
4 changes: 4 additions & 0 deletions src/main/java/rx/internal/operators/OnSubscribeRedo.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public void call(final Subscriber<? super T> child) {
final Action0 subscribeToSource = new Action0() {
@Override
public void call() {
if (child.isUnsubscribed()) {
return;
}

Subscriber<T> terminalDelegatingSubscriber = new Subscriber<T>() {
@Override
public void onCompleted() {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/rx/internal/operators/OperatorTake.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ public void onError(Throwable e) {
@Override
public void onNext(T i) {
if (!isUnsubscribed()) {
child.onNext(i);
if (++count >= limit) {
completed = true;
child.onCompleted();
// unsubscribe before emitting onNext so shutdown happens before possible effects
// of onNext such as product.request(n) calls be sent upstream.
unsubscribe();
}
child.onNext(i);
if (completed) {
child.onCompleted();
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/rx/internal/operators/OperatorRetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,37 @@ public Observable<?> call(Observable<? extends Throwable> t1) {
inOrder.verifyNoMoreInteractions();
}

@Test
public void testSingleSubscriptionOnFirst() throws Exception {
final AtomicInteger inc = new AtomicInteger(0);
Observable.OnSubscribe<Integer> onSubscribe = new OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
final int emit = inc.incrementAndGet();
subscriber.onNext(emit);
subscriber.onCompleted();
}
};

int first = Observable.create(onSubscribe)
.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Throwable> attempt) {
return attempt.zipWith(Observable.just(1), new Func2<Throwable, Integer, Void>() {
@Override
public Void call(Throwable o, Integer integer) {
return null;
}
});
}
})
.toBlocking()
.first();

assertEquals("Observer did not receive the expected output", 1, first);
assertEquals("Subscribe was not called once", 1, inc.get());
}

@Test
public void testOriginFails() {
@SuppressWarnings("unchecked")
Expand Down