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

1.x: Schedulers.from() to call RxJavaHooks.onScheduleAction #4311

Merged
merged 1 commit into from
Aug 8, 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
9 changes: 7 additions & 2 deletions src/main/java/rx/internal/schedulers/ExecutorScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public Subscription schedule(Action0 action) {
if (isUnsubscribed()) {
return Subscriptions.unsubscribed();
}

action = RxJavaHooks.onScheduledAction(action);

ScheduledAction ea = new ScheduledAction(action, tasks);
tasks.add(ea);
queue.offer(ea);
Expand Down Expand Up @@ -111,14 +114,16 @@ public void run() {
}

@Override
public Subscription schedule(final Action0 action, long delayTime, TimeUnit unit) {
public Subscription schedule(Action0 action, long delayTime, TimeUnit unit) {
if (delayTime <= 0) {
return schedule(action);
}
if (isUnsubscribed()) {
return Subscriptions.unsubscribed();
}

final Action0 decorated = RxJavaHooks.onScheduledAction(action);

final MultipleAssignmentSubscription first = new MultipleAssignmentSubscription();
final MultipleAssignmentSubscription mas = new MultipleAssignmentSubscription();
mas.set(first);
Expand All @@ -137,7 +142,7 @@ public void call() {
return;
}
// schedule the real action untimed
Subscription s2 = schedule(action);
Subscription s2 = schedule(decorated);
mas.set(s2);
// unless the worker is unsubscribed, we should get a new ScheduledAction
if (s2.getClass() == ScheduledAction.class) {
Expand Down
49 changes: 45 additions & 4 deletions src/test/java/rx/internal/schedulers/ExecutorSchedulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;

import org.junit.*;

import rx.*;
import rx.Scheduler.Worker;
import rx.functions.*;
import rx.internal.schedulers.ExecutorScheduler.ExecutorSchedulerWorker;
import rx.internal.util.RxThreadFactory;
import rx.schedulers.AbstractSchedulerConcurrencyTests;
import rx.schedulers.SchedulerTests;
import rx.schedulers.Schedulers;
import rx.plugins.RxJavaHooks;
import rx.schedulers.*;

public class ExecutorSchedulerTest extends AbstractSchedulerConcurrencyTests {

Expand Down Expand Up @@ -208,4 +209,44 @@ public void call() {

assertFalse(w.tasks.hasSubscriptions());
}

@Test
public void actionHookCalled() throws Exception {
ExecutorService exec = Executors.newSingleThreadExecutor();
try {
final int[] call = { 0 };

RxJavaHooks.setOnScheduleAction(new Func1<Action0, Action0>() {
@Override
public Action0 call(Action0 t) {
call[0]++;
return t;
}
});

Scheduler s = Schedulers.from(exec);

Worker w = s.createWorker();

final CountDownLatch cdl = new CountDownLatch(1);

try {
w.schedule(new Action0() {
@Override
public void call() {
cdl.countDown();
}
});

Assert.assertTrue("Action timed out", cdl.await(5, TimeUnit.SECONDS));
} finally {
w.unsubscribe();
}

Assert.assertEquals("Hook not called!", 1, call[0]);
} finally {
RxJavaHooks.reset();
exec.shutdown();
}
}
}