Skip to content

Commit

Permalink
Merge pull request #2902 from davidmoten/exceptions-test
Browse files Browse the repository at this point in the history
Prevent ExceptionsTest from hanging when testing stack overflow
  • Loading branch information
akarnokd committed Apr 21, 2015
2 parents 9fb5614 + 7a6dd8d commit d7d94ba
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/test/java/rx/exceptions/ExceptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import rx.Observable;
Expand All @@ -39,27 +41,13 @@ public void call(Integer t1) {
});
}

@Test(expected = StackOverflowError.class)
public void testStackOverflowIsThrown() {
@Test
public void testStackOverflowWouldOccur() {
final PublishSubject<Integer> a = PublishSubject.create();
final PublishSubject<Integer> b = PublishSubject.create();
new Observer<Integer>() {

@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
e.printStackTrace();
}

@Override
public void onNext(Integer args) {
System.out.println(args);
}
};
final int MAX_STACK_DEPTH = 1000;
final AtomicInteger depth = new AtomicInteger();

a.subscribe(new Observer<Integer>() {

@Override
Expand All @@ -73,12 +61,11 @@ public void onError(Throwable e) {
}

@Override
public void onNext(Integer args) {
System.out.println(args);
public void onNext(Integer n) {
b.onNext(n + 1);
}
});
b.subscribe();
a.subscribe(new Observer<Integer>() {
b.subscribe(new Observer<Integer>() {

@Override
public void onCompleted() {
Expand All @@ -91,11 +78,20 @@ public void onError(Throwable e) {
}

@Override
public void onNext(Integer args) {
b.onNext(args + 1);
public void onNext(Integer n) {
if (depth.get() < MAX_STACK_DEPTH) {
depth.set(Thread.currentThread().getStackTrace().length);
a.onNext(n + 1);
}
}
});
b.subscribe(new Observer<Integer>() {
a.onNext(1);
assertTrue(depth.get() > MAX_STACK_DEPTH);
}

@Test(expected = StackOverflowError.class)
public void testStackOverflowErrorIsThrown() {
Observable.just(1).subscribe(new Observer<Integer>() {

@Override
public void onCompleted() {
Expand All @@ -108,11 +104,11 @@ public void onError(Throwable e) {
}

@Override
public void onNext(Integer args) {
a.onNext(args + 1);
public void onNext(Integer t) {
throw new StackOverflowError();
}

});
a.onNext(1);
}

@Test(expected = ThreadDeath.class)
Expand Down

0 comments on commit d7d94ba

Please sign in to comment.