Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableUnsubscribeOnTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.AtomicReference;
21
22 import org.junit.Test;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.annotations.NonNull;
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.exceptions.TestException;
28 import io.reactivex.rxjava3.functions.Action;
29 import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler;
30 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
31 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
32 import io.reactivex.rxjava3.schedulers.Schedulers;
33 import io.reactivex.rxjava3.testsupport.*;
34
35 public class FlowableUnsubscribeOnTest extends RxJavaTest {
36
37 @Test
38 public void unsubscribeWhenSubscribeOnAndUnsubscribeOnAreOnSameThread() throws InterruptedException {
39 UIEventLoopScheduler uiEventLoop = new UIEventLoopScheduler();
40 try {
41 final ThreadSubscription subscription = new ThreadSubscription();
42 final AtomicReference<Thread> subscribeThread = new AtomicReference<>();
43 Flowable<Integer> w = Flowable.unsafeCreate(new Publisher<Integer>() {
44
45 @Override
46 public void subscribe(Subscriber<? super Integer> t1) {
47 subscribeThread.set(Thread.currentThread());
48 t1.onSubscribe(subscription);
49 t1.onNext(1);
50 t1.onNext(2);
51 // observeOn will prevent canceling the upstream upon its termination now
52 // this call is racing for that state in this test
53 // not doing it will make sure the unsubscribeOn always gets through
54 // t1.onComplete();
55 }
56 });
57
58 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
59 w.subscribeOn(uiEventLoop).observeOn(Schedulers.computation())
60 .unsubscribeOn(uiEventLoop)
61 .take(2)
62 .subscribe(ts);
63
64 ts.awaitDone(1, TimeUnit.SECONDS);
65
66 Thread unsubscribeThread = subscription.getThread();
67
68 assertNotNull(unsubscribeThread);
69 assertNotSame(Thread.currentThread(), unsubscribeThread);
70
71 assertNotNull(subscribeThread.get());
72 assertNotSame(Thread.currentThread(), subscribeThread.get());
73 // True for Schedulers.newThread()
74
75 System.out.println("unsubscribeThread: " + unsubscribeThread);
76 System.out.println("subscribeThread.get(): " + subscribeThread.get());
77 assertSame(unsubscribeThread, uiEventLoop.getThread());
78
79 ts.assertValues(1, 2);
80 ts.assertTerminated();
81 } finally {
82 uiEventLoop.shutdown();
83 }
84 }
85
86 @Test
87 public void unsubscribeWhenSubscribeOnAndUnsubscribeOnAreOnDifferentThreads() throws InterruptedException {
88 UIEventLoopScheduler uiEventLoop = new UIEventLoopScheduler();
89 try {
90 final ThreadSubscription subscription = new ThreadSubscription();
91 final AtomicReference<Thread> subscribeThread = new AtomicReference<>();
92 Flowable<Integer> w = Flowable.unsafeCreate(new Publisher<Integer>() {
93
94 @Override
95 public void subscribe(Subscriber<? super Integer> t1) {
96 subscribeThread.set(Thread.currentThread());
97 t1.onSubscribe(subscription);
98 t1.onNext(1);
99 t1.onNext(2);
100 // observeOn will prevent canceling the upstream upon its termination now
101 // this call is racing for that state in this test
102 // not doing it will make sure the unsubscribeOn always gets through
103 // t1.onComplete();
104 }
105 });
106
107 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
108 w.subscribeOn(Schedulers.newThread()).observeOn(Schedulers.computation())
109 .unsubscribeOn(uiEventLoop)
110 .take(2)
111 .subscribe(ts);
112
113 ts.awaitDone(1, TimeUnit.SECONDS);
114
115 Thread unsubscribeThread = subscription.getThread();
116
117 assertNotNull(unsubscribeThread);
118 assertNotSame(Thread.currentThread(), unsubscribeThread);
119
120 assertNotNull(subscribeThread.get());
121 assertNotSame(Thread.currentThread(), subscribeThread.get());
122 // True for Schedulers.newThread()
123
124 System.out.println("UI Thread: " + uiEventLoop.getThread());
125 System.out.println("unsubscribeThread: " + unsubscribeThread);
126 System.out.println("subscribeThread.get(): " + subscribeThread.get());
127 assertSame(unsubscribeThread, uiEventLoop.getThread());
128
129 ts.assertValues(1, 2);
130 ts.assertTerminated();
131 } finally {
132 uiEventLoop.shutdown();
133 }
134 }
135
136 private static class ThreadSubscription implements Subscription {
137 private volatile Thread thread;
138
139 private final CountDownLatch latch = new CountDownLatch(1);
140
141 @Override
142 public void cancel() {
143 System.out.println("unsubscribe invoked: " + Thread.currentThread());
144 thread = Thread.currentThread();
145 latch.countDown();
146 }
147
148 public Thread getThread() throws InterruptedException {
149 latch.await();
150 return thread;
151 }
152
153 @Override
154 public void request(long n) {
155
156 }
157 }
158
159 public static class UIEventLoopScheduler extends Scheduler {
160
161 private final Scheduler eventLoop;
162 private volatile Thread t;
163
164 public UIEventLoopScheduler() {
165
166 eventLoop = Schedulers.single();
167
168 /*
169 * DON'T DO THIS IN PRODUCTION CODE
170 */
171 final CountDownLatch latch = new CountDownLatch(1);
172 eventLoop.scheduleDirect(new Runnable() {
173
174 @Override
175 public void run() {
176 t = Thread.currentThread();
177 latch.countDown();
178 }
179
180 });
181 try {
182 latch.await();
183 } catch (InterruptedException e) {
184 throw new RuntimeException("failed to initialize and get inner thread");
185 }
186 }
187
188 @NonNull
189 @Override
190 public Worker createWorker() {
191 return eventLoop.createWorker();
192 }
193
194 public Thread getThread() {
195 return t;
196 }
197
198 }
199
200 @Test
201 public void takeHalf() {
202 int elements = 1024;
203 Flowable.range(0, elements * 2).unsubscribeOn(Schedulers.single())
204 .take(elements)
205 .to(TestHelper.<Integer>testConsumer())
206 .awaitDone(5, TimeUnit.SECONDS)
207 .assertValueCount(elements)
208 .assertComplete()
209 .assertNoErrors()
210 .assertSubscribed();
211 }
212
213 @Test
214 public void dispose() {
215 TestHelper.checkDisposed(Flowable.just(1).unsubscribeOn(Schedulers.single()));
216 }
217
218 @Test
219 public void normal() {
220 final int[] calls = { 0 };
221
222 Flowable.just(1)
223 .doOnCancel(new Action() {
224 @Override
225 public void run() throws Exception {
226 calls[0]++;
227 }
228 })
229 .unsubscribeOn(Schedulers.single())
230 .test()
231 .assertResult(1);
232
233 assertEquals(0, calls[0]);
234 }
235
236 @Test
237 public void error() {
238 final int[] calls = { 0 };
239
240 Flowable.error(new TestException())
241 .doOnCancel(new Action() {
242 @Override
243 public void run() throws Exception {
244 calls[0]++;
245 }
246 })
247 .unsubscribeOn(Schedulers.single())
248 .test()
249 .assertFailure(TestException.class);
250
251 assertEquals(0, calls[0]);
252 }
253
254 @Test
255 public void signalAfterDispose() {
256 List<Throwable> errors = TestHelper.trackPluginErrors();
257 try {
258 new Flowable<Integer>() {
259 @Override
260 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
261 subscriber.onSubscribe(new BooleanSubscription());
262 subscriber.onNext(1);
263 subscriber.onNext(2);
264 subscriber.onError(new TestException());
265 subscriber.onComplete();
266 }
267 }
268 .unsubscribeOn(Schedulers.single())
269 .take(1)
270 .test()
271 .assertResult(1);
272
273 TestHelper.assertUndeliverable(errors, 0, TestException.class);
274 } finally {
275 RxJavaPlugins.reset();
276 }
277 }
278
279 @Test
280 public void doubleOnSubscribe() {
281 TestHelper.checkDoubleOnSubscribeFlowable(f -> f.unsubscribeOn(ImmediateThinScheduler.INSTANCE));
282 }
283 }