46
46
47
47
public class OperatorSubscribeOnTest {
48
48
49
- private class ThreadSubscription implements Subscription {
49
+ private static class ThreadSubscription implements Subscription {
50
50
private volatile Thread thread ;
51
51
52
52
private final CountDownLatch latch = new CountDownLatch (1 );
@@ -111,9 +111,10 @@ public void call(Subscriber<? super Integer> t1) {
111
111
observer .assertTerminalEvent ();
112
112
}
113
113
114
- @ Test
114
+ @ Test ( timeout = 2000 )
115
115
public void testIssue813 () throws InterruptedException {
116
116
// https://github.com/Netflix/RxJava/issues/813
117
+ final CountDownLatch scheduled = new CountDownLatch (1 );
117
118
final CountDownLatch latch = new CountDownLatch (1 );
118
119
final CountDownLatch doneLatch = new CountDownLatch (1 );
119
120
@@ -126,16 +127,14 @@ public void testIssue813() throws InterruptedException {
126
127
public void call (
127
128
final Subscriber <? super Integer > subscriber ) {
128
129
subscriber .add (s );
130
+ scheduled .countDown ();
129
131
try {
130
132
latch .await ();
131
- // Already called "unsubscribe", "isUnsubscribed"
132
- // shouble be true
133
- if (!subscriber .isUnsubscribed ()) {
134
- throw new IllegalStateException (
135
- "subscriber.isUnsubscribed should be true" );
136
- }
133
+
134
+ // this should not run because the await above will be interrupted by the unsubscribe
137
135
subscriber .onCompleted ();
138
136
} catch (InterruptedException e ) {
137
+ System .out .println ("Interrupted because it is unsubscribed" );
139
138
Thread .currentThread ().interrupt ();
140
139
} catch (Throwable e ) {
141
140
subscriber .onError (e );
@@ -145,13 +144,17 @@ public void call(
145
144
}
146
145
}).subscribeOn (Schedulers .computation ()).subscribe (observer );
147
146
147
+ // wait for scheduling
148
+ scheduled .await ();
149
+ // trigger unsubscribe
148
150
subscription .unsubscribe ();
149
151
// As unsubscribe is called in other thread, we need to wait for it.
150
152
s .getThread ();
151
153
latch .countDown ();
152
154
doneLatch .await ();
153
155
assertEquals (0 , observer .getOnErrorEvents ().size ());
154
- assertEquals (1 , observer .getOnCompletedEvents ().size ());
156
+ // 0 because the unsubscribe interrupts and prevents onCompleted from being executed
157
+ assertEquals (0 , observer .getOnCompletedEvents ().size ());
155
158
}
156
159
157
160
public static class SlowScheduler extends Scheduler {
@@ -395,4 +398,5 @@ public void call(Subscriber<? super Integer> sub) {
395
398
ts .assertReceivedOnNext (Arrays .asList (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ));
396
399
assertEquals (10 , count .get ());
397
400
}
401
+
398
402
}
0 commit comments