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

TrampolineScheduler & Unsubscribe #1324

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 @@ -83,9 +83,6 @@ private Subscription enqueue(Action0 action, long execTime) {

if (exec) {
while (!queue.isEmpty()) {
if (innerSubscription.isUnsubscribed()) {
return Subscriptions.empty();
}
queue.poll().action.call();
}

Expand All @@ -108,7 +105,6 @@ public void call() {

@Override
public void unsubscribe() {
QUEUE.set(null); // this assumes we are calling unsubscribe from the same thread
innerSubscription.unsubscribe();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
*/
package rx.schedulers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;

import org.junit.Test;

import rx.Observable;
import rx.Scheduler;
import rx.Scheduler.Worker;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;

Expand Down Expand Up @@ -55,4 +61,62 @@ public void call(String t) {
}
});
}

@Test
public void testNestedTrampolineWithUnsubscribe() {
final ArrayList<String> workDone = new ArrayList<String>();
Worker worker = Schedulers.trampoline().createWorker();
worker.schedule(new Action0() {

@Override
public void call() {
doWorkOnNewTrampoline("A", workDone);
}

});

final Worker worker2 = Schedulers.trampoline().createWorker();
worker2.schedule(new Action0() {

@Override
public void call() {
doWorkOnNewTrampoline("B", workDone);
// we unsubscribe worker2 ... it should not affect work scheduled on a separate Trampline.Worker
worker2.unsubscribe();
}

});

assertEquals(6, workDone.size());
assertEquals(Arrays.asList("A.1", "A.B.1", "A.B.2", "B.1", "B.B.1", "B.B.2"), workDone);
}

private static void doWorkOnNewTrampoline(final String key, final ArrayList<String> workDone) {
Worker worker = Schedulers.trampoline().createWorker();
worker.schedule(new Action0() {

@Override
public void call() {
String msg = key + ".1";
workDone.add(msg);
System.out.println(msg);
Worker worker3 = Schedulers.trampoline().createWorker();
worker3.schedule(createPrintAction(key + ".B.1", workDone));
worker3.schedule(createPrintAction(key + ".B.2", workDone));
}

});
}

private static Action0 createPrintAction(final String message, final ArrayList<String> workDone) {
return new Action0() {

@Override
public void call() {
System.out.println(message);
workDone.add(message);
}

};
}
}