forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BugFix: Unsubscribing does not work when using subscribeOn(Schedulers…
….newThread()) I believe this fixes ReactiveX#431
- Loading branch information
1 parent
2e2ab16
commit ac74a79
Showing
3 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
rxjava-core/src/test/java/rx/concurrency/SchedulerUnsubscribeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package rx.concurrency; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Scheduler; | ||
import rx.operators.SafeObservableSubscription; | ||
import rx.util.functions.Func1; | ||
|
||
public class SchedulerUnsubscribeTest { | ||
|
||
/** | ||
* Bug report: https://github.com/Netflix/RxJava/issues/431 | ||
*/ | ||
@Test | ||
public void testUnsubscribeOfNewThread() throws InterruptedException { | ||
testUnSubscribeForScheduler(Schedulers.newThread()); | ||
} | ||
|
||
@Test | ||
public void testUnsubscribeOfThreadPoolForIO() throws InterruptedException { | ||
testUnSubscribeForScheduler(Schedulers.threadPoolForIO()); | ||
} | ||
|
||
@Test | ||
public void testUnsubscribeOfThreadPoolForComputation() throws InterruptedException { | ||
testUnSubscribeForScheduler(Schedulers.threadPoolForComputation()); | ||
} | ||
|
||
@Test | ||
public void testUnsubscribeOfCurrentThread() throws InterruptedException { | ||
testUnSubscribeForScheduler(Schedulers.currentThread()); | ||
} | ||
|
||
public void testUnSubscribeForScheduler(Scheduler scheduler) throws InterruptedException { | ||
|
||
final AtomicInteger countReceived = new AtomicInteger(); | ||
final AtomicInteger countGenerated = new AtomicInteger(); | ||
final SafeObservableSubscription s = new SafeObservableSubscription(); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
|
||
s.wrap(Observable.interval(50, TimeUnit.MILLISECONDS) | ||
.map(new Func1<Long, Long>() { | ||
@Override | ||
public Long call(Long aLong) { | ||
System.out.println("generated " + aLong); | ||
countGenerated.incrementAndGet(); | ||
return aLong; | ||
} | ||
}) | ||
.subscribeOn(scheduler) | ||
.observeOn(Schedulers.currentThread()) | ||
.subscribe(new Observer<Long>() { | ||
@Override | ||
public void onCompleted() { | ||
System.out.println("--- completed"); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
System.out.println("--- onError"); | ||
} | ||
|
||
@Override | ||
public void onNext(Long args) { | ||
if (countReceived.incrementAndGet() == 2) { | ||
s.unsubscribe(); | ||
latch.countDown(); | ||
} | ||
System.out.println("==> Received " + args); | ||
} | ||
})); | ||
|
||
latch.await(1000, TimeUnit.MILLISECONDS); | ||
|
||
System.out.println("----------- it thinks it is finished ------------------ "); | ||
Thread.sleep(100); | ||
|
||
assertEquals(2, countGenerated.get()); | ||
} | ||
} |