Skip to content

Commit

Permalink
make it configurable via property
Browse files Browse the repository at this point in the history
  • Loading branch information
abersnaze committed May 13, 2016
1 parent e0929ee commit e277814
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Worker createWorker() {

/** Worker that schedules tasks on the executor indirectly through a trampoline mechanism. */
static final class ExecutorSchedulerWorker extends Scheduler.Worker implements Runnable {
private final Throwable creationContext = new SchedulerContextException();
private final Throwable creationContext = SchedulerContextException.create();
final Executor executor;
// TODO: use a better performing structure for task tracking
final CompositeSubscription tasks;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/schedulers/NewThreadWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @warn class description missing
*/
public class NewThreadWorker extends Scheduler.Worker implements Subscription {
private final Throwable creationContext = new SchedulerContextException();
private final Throwable creationContext = SchedulerContextException.create();
private final ScheduledExecutorService executor;
private final RxJavaSchedulersHook schedulersHook;
volatile boolean isUnsubscribed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,44 @@
* Used only for providing context around where work was scheduled should an error occur in a different thread.
*/
public class SchedulerContextException extends Exception {
public SchedulerContextException() {
super("Asynchronous work scheduled at");
/**
* Constant to use when disabled
*/
private static final Throwable CONTEXT_MISSING = new SchedulerContextException("Missing context. Enable by "//
+ "setting the system property \"rxjava.captureSchedulerContext=true\" at startup or "
+ "calling \"rx.internal.schedulers.SchedulerContextException.setCaptureSchedulerContext(true)\" at runtime");
/**
* cache of the system property
*/
private static volatile boolean captureSchedulerContext;

static {
CONTEXT_MISSING.setStackTrace(new StackTraceElement[0]);

try {
captureSchedulerContext = System.getProperty("rxjava.captureSchedulerContext", "false").equals("true");
} catch (Exception e) {
captureSchedulerContext = false;
}
}

/**
* Immediately change the behavior to of the {@link SchedulerContextException#create()} method.
* @param captureSchedulerContext
*/
public static void setCaptureSchedulerContext(boolean captureSchedulerContext) {
SchedulerContextException.captureSchedulerContext = captureSchedulerContext;
}

/**
* @return a {@link Throwable} that captures the stack trace or a {@link Throwable} that documents how to enable the feature if needed.
*/
public static Throwable create() {
return captureSchedulerContext ? new SchedulerContextException("Asynchronous work scheduled at") : CONTEXT_MISSING;
}

private SchedulerContextException(String message) {
super(message);
}

private static final long serialVersionUID = 1L;
Expand Down
54 changes: 31 additions & 23 deletions src/test/java/rx/schedulers/ComputationSchedulerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.internal.schedulers.SchedulerContextException;
import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;

Expand Down Expand Up @@ -176,43 +177,50 @@ public void testCancelledTaskRetention() throws InterruptedException {
public void testStackTraceAcrossThreads() throws Throwable {
final AtomicReference<Throwable> exceptionRef = new AtomicReference<Throwable>();
final CountDownLatch done = new CountDownLatch(1);

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
System.setProperty("rxjava.captureSchedulerContext", "true");

try {
getScheduler().createWorker().schedule(new Action0() {

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void call() {
throw new RuntimeException();
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();
try {
getScheduler().createWorker().schedule(new Action0() {
@Override
public void call() {
throw new RuntimeException();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (!(e instanceof SchedulerContextException)) {
e = e.getCause();
}

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (e != null) {
StackTraceElement[] st = e.getStackTrace();
for (StackTraceElement stackTraceElement : st) {
if (stackTraceElement.getClassName().contains(getClass().getName())) {
// pass we found this class in the stack trace.
return;
}
}
e = e.getCause();
}

throw exception;
throw exception;
} finally {
System.setProperty("rxjava.captureSchedulerContext", "false");
}
}
}
54 changes: 31 additions & 23 deletions src/test/java/rx/schedulers/ImmediateSchedulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.internal.schedulers.SchedulerContextException;
import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;

Expand Down Expand Up @@ -112,43 +113,50 @@ public void call(String t) {
public void testStackTraceAcrossThreads() throws Throwable {
final AtomicReference<Throwable> exceptionRef = new AtomicReference<Throwable>();
final CountDownLatch done = new CountDownLatch(1);

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
System.setProperty("rxjava.captureSchedulerContext", "true");

try {
getScheduler().createWorker().schedule(new Action0() {

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void call() {
throw new RuntimeException();
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();
try {
getScheduler().createWorker().schedule(new Action0() {
@Override
public void call() {
throw new RuntimeException();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (!(e instanceof SchedulerContextException)) {
e = e.getCause();
}

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (e != null) {
StackTraceElement[] st = e.getStackTrace();
for (StackTraceElement stackTraceElement : st) {
if (stackTraceElement.getClassName().contains(getClass().getName())) {
// pass we found this class in the stack trace.
return;
}
}
e = e.getCause();
}

throw exception;
throw exception;
} finally {
System.setProperty("rxjava.captureSchedulerContext", "false");
}
}
}
54 changes: 31 additions & 23 deletions src/test/java/rx/schedulers/IoSchedulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import rx.*;
import rx.Scheduler.Worker;
import rx.functions.*;
import rx.internal.schedulers.SchedulerContextException;
import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;

Expand Down Expand Up @@ -92,43 +93,50 @@ public void testCancelledTaskRetention() throws InterruptedException {
public void testStackTraceAcrossThreads() throws Throwable {
final AtomicReference<Throwable> exceptionRef = new AtomicReference<Throwable>();
final CountDownLatch done = new CountDownLatch(1);

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
System.setProperty("rxjava.captureSchedulerContext", "true");

try {
getScheduler().createWorker().schedule(new Action0() {

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void call() {
throw new RuntimeException();
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();
try {
getScheduler().createWorker().schedule(new Action0() {
@Override
public void call() {
throw new RuntimeException();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (!(e instanceof SchedulerContextException)) {
e = e.getCause();
}

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (e != null) {
StackTraceElement[] st = e.getStackTrace();
for (StackTraceElement stackTraceElement : st) {
if (stackTraceElement.getClassName().contains(getClass().getName())) {
// pass we found this class in the stack trace.
return;
}
}
e = e.getCause();
}

throw exception;
throw exception;
} finally {
System.setProperty("rxjava.captureSchedulerContext", "false");
}
}
}
54 changes: 31 additions & 23 deletions src/test/java/rx/schedulers/NewThreadSchedulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import rx.Scheduler;
import rx.functions.Action0;
import rx.internal.schedulers.ScheduledAction;
import rx.internal.schedulers.SchedulerContextException;
import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;
import rx.subscriptions.Subscriptions;
Expand Down Expand Up @@ -90,43 +91,50 @@ public void call() {
public void testStackTraceAcrossThreads() throws Throwable {
final AtomicReference<Throwable> exceptionRef = new AtomicReference<Throwable>();
final CountDownLatch done = new CountDownLatch(1);

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
System.setProperty("rxjava.captureSchedulerContext", "true");

try {
getScheduler().createWorker().schedule(new Action0() {

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
@Override
public void call() {
throw new RuntimeException();
public void handleError(Throwable e) {
exceptionRef.set(e);
done.countDown();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();
try {
getScheduler().createWorker().schedule(new Action0() {
@Override
public void call() {
throw new RuntimeException();
}
});
} catch (Exception e) {
exceptionRef.set(e);
done.countDown();
}

done.await();

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (!(e instanceof SchedulerContextException)) {
e = e.getCause();
}

Throwable exception = exceptionRef.get();
Throwable e = exception;
while (e != null) {
StackTraceElement[] st = e.getStackTrace();
for (StackTraceElement stackTraceElement : st) {
if (stackTraceElement.getClassName().contains(getClass().getName())) {
// pass we found this class in the stack trace.
return;
}
}
e = e.getCause();
}

throw exception;
throw exception;
} finally {
System.setProperty("rxjava.captureSchedulerContext", "false");
}
}
}
Loading

0 comments on commit e277814

Please sign in to comment.