Skip to content

1.x: fix Amb sharing the choice among all subscribers #3645

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 1 commit into from
Jan 25, 2016
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: 2 additions & 2 deletions src/main/java/rx/internal/operators/OnSubscribeAmb.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,15 @@ public void unsubscribeOthers(AmbSubscriber<T> notThis) {
//give default access instead of private as a micro-optimization
//for access from anonymous classes below
final Iterable<? extends Observable<? extends T>> sources;
final Selection<T> selection = new Selection<T>();
final AtomicReference<AmbSubscriber<T>> choice = selection.choice;

private OnSubscribeAmb(Iterable<? extends Observable<? extends T>> sources) {
this.sources = sources;
}

@Override
public void call(final Subscriber<? super T> subscriber) {
final Selection<T> selection = new Selection<T>();
final AtomicReference<AmbSubscriber<T>> choice = selection.choice;

//setup unsubscription of all the subscribers to the sources
subscriber.add(Subscriptions.create(new Action0() {
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/rx/internal/operators/OnSubscribeAmbTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,26 @@ public void call(Object t) {
}).ambWith(Observable.just(2)).toBlocking().single();
assertEquals(1, result);
}


@Test(timeout = 1000)
Copy link
Member

Choose a reason for hiding this comment

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

Why not use TestScheduler to avoid using timeout?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't really matter.

Copy link
Member

Choose a reason for hiding this comment

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

okey. not a big deal.

public void testMultipleUse() {
TestSubscriber<Long> ts1 = new TestSubscriber<Long>();
TestSubscriber<Long> ts2 = new TestSubscriber<Long>();

Observable<Long> amb = Observable.timer(100, TimeUnit.MILLISECONDS).ambWith(Observable.timer(200, TimeUnit.MILLISECONDS));

amb.subscribe(ts1);
amb.subscribe(ts2);

ts1.awaitTerminalEvent();
ts2.awaitTerminalEvent();

ts1.assertValue(0L);
ts1.assertCompleted();
ts1.assertNoErrors();

ts2.assertValue(0L);
ts2.assertCompleted();
ts2.assertNoErrors();
}
}