Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTimerTest.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.assertTrue;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19
20 import java.util.List;
21 import java.util.concurrent.*;
22 import java.util.concurrent.atomic.AtomicBoolean;
23
24 import org.junit.*;
25 import org.mockito.*;
26 import org.reactivestreams.Subscriber;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.exceptions.*;
30 import io.reactivex.rxjava3.flowables.ConnectableFlowable;
31 import io.reactivex.rxjava3.functions.Function;
32 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
33 import io.reactivex.rxjava3.schedulers.*;
34 import io.reactivex.rxjava3.subscribers.*;
35 import io.reactivex.rxjava3.testsupport.TestHelper;
36
37 public class FlowableTimerTest extends RxJavaTest {
38 @Mock
39 Subscriber<Object> subscriber;
40 @Mock
41 Subscriber<Long> subscriber2;
42
43 TestScheduler scheduler;
44
45 @Before
46 public void before() {
47 subscriber = TestHelper.mockSubscriber();
48
49 subscriber2 = TestHelper.mockSubscriber();
50
51 scheduler = new TestScheduler();
52 }
53
54 @Test
55 public void timerOnce() {
56 Flowable.timer(100, TimeUnit.MILLISECONDS, scheduler).subscribe(subscriber);
57 scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
58
59 verify(subscriber, times(1)).onNext(0L);
60 verify(subscriber, times(1)).onComplete();
61 verify(subscriber, never()).onError(any(Throwable.class));
62 }
63
64 @Test
65 public void timerPeriodically() {
66 TestSubscriber<Long> ts = new TestSubscriber<>();
67
68 Flowable.interval(100, 100, TimeUnit.MILLISECONDS, scheduler).subscribe(ts);
69
70 scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
71
72 ts.assertValue(0L);
73
74 scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
75 ts.assertValues(0L, 1L);
76
77 scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
78 ts.assertValues(0L, 1L, 2L);
79
80 scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
81 ts.assertValues(0L, 1L, 2L, 3L);
82
83 ts.cancel();
84 scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
85 ts.assertValues(0L, 1L, 2L, 3L);
86
87 ts.assertNotComplete();
88 ts.assertNoErrors();
89 }
90
91 @Test
92 public void interval() {
93 Flowable<Long> w = Flowable.interval(1, TimeUnit.SECONDS, scheduler);
94 TestSubscriber<Long> ts = new TestSubscriber<>();
95 w.subscribe(ts);
96
97 ts.assertNoValues();
98 ts.assertNoErrors();
99 ts.assertNotComplete();
100
101 scheduler.advanceTimeTo(2, TimeUnit.SECONDS);
102
103 ts.assertValues(0L, 1L);
104 ts.assertNoErrors();
105 ts.assertNotComplete();
106
107 ts.cancel();
108
109 scheduler.advanceTimeTo(4, TimeUnit.SECONDS);
110 ts.assertValues(0L, 1L);
111 ts.assertNoErrors();
112 ts.assertNotComplete();
113 }
114
115 @Test
116 public void withMultipleSubscribersStartingAtSameTime() {
117 Flowable<Long> w = Flowable.interval(1, TimeUnit.SECONDS, scheduler);
118
119 TestSubscriber<Long> ts1 = new TestSubscriber<>();
120 TestSubscriber<Long> ts2 = new TestSubscriber<>();
121
122 w.subscribe(ts1);
123 w.subscribe(ts2);
124
125 ts1.assertNoValues();
126 ts2.assertNoValues();
127
128 scheduler.advanceTimeTo(2, TimeUnit.SECONDS);
129
130 ts1.assertValues(0L, 1L);
131 ts1.assertNoErrors();
132 ts1.assertNotComplete();
133
134 ts2.assertValues(0L, 1L);
135 ts2.assertNoErrors();
136 ts2.assertNotComplete();
137
138 ts1.cancel();
139 ts2.cancel();
140
141 scheduler.advanceTimeTo(4, TimeUnit.SECONDS);
142
143 ts1.assertValues(0L, 1L);
144 ts1.assertNoErrors();
145 ts1.assertNotComplete();
146
147 ts2.assertValues(0L, 1L);
148 ts2.assertNoErrors();
149 ts2.assertNotComplete();
150 }
151
152 @Test
153 public void withMultipleStaggeredSubscribers() {
154 Flowable<Long> w = Flowable.interval(1, TimeUnit.SECONDS, scheduler);
155
156 TestSubscriber<Long> ts1 = new TestSubscriber<>();
157
158 w.subscribe(ts1);
159
160 ts1.assertNoErrors();
161
162 scheduler.advanceTimeTo(2, TimeUnit.SECONDS);
163
164 TestSubscriber<Long> ts2 = new TestSubscriber<>();
165
166 w.subscribe(ts2);
167
168 ts1.assertValues(0L, 1L);
169 ts1.assertNoErrors();
170 ts1.assertNotComplete();
171
172 ts2.assertNoValues();
173
174 scheduler.advanceTimeTo(4, TimeUnit.SECONDS);
175
176 ts1.assertValues(0L, 1L, 2L, 3L);
177
178 ts2.assertValues(0L, 1L);
179
180 ts1.cancel();
181 ts2.cancel();
182
183 ts1.assertValues(0L, 1L, 2L, 3L);
184 ts1.assertNoErrors();
185 ts1.assertNotComplete();
186
187 ts2.assertValues(0L, 1L);
188 ts2.assertNoErrors();
189 ts2.assertNotComplete();
190 }
191
192 @Test
193 public void withMultipleStaggeredSubscribersAndPublish() {
194 ConnectableFlowable<Long> w = Flowable.interval(1, TimeUnit.SECONDS, scheduler).publish();
195
196 TestSubscriber<Long> ts1 = new TestSubscriber<>();
197
198 w.subscribe(ts1);
199 w.connect();
200
201 ts1.assertNoValues();
202
203 scheduler.advanceTimeTo(2, TimeUnit.SECONDS);
204
205 TestSubscriber<Long> ts2 = new TestSubscriber<>();
206 w.subscribe(ts2);
207
208 ts1.assertValues(0L, 1L);
209 ts1.assertNoErrors();
210 ts1.assertNotComplete();
211
212 ts2.assertNoValues();
213
214 scheduler.advanceTimeTo(4, TimeUnit.SECONDS);
215
216 ts1.assertValues(0L, 1L, 2L, 3L);
217
218 ts2.assertValues(2L, 3L);
219
220 ts1.cancel();
221 ts2.cancel();
222
223 ts1.assertValues(0L, 1L, 2L, 3L);
224 ts1.assertNoErrors();
225 ts1.assertNotComplete();
226
227 ts2.assertValues(2L, 3L);
228 ts2.assertNoErrors();
229 ts2.assertNotComplete();
230 }
231
232 @Test
233 public void onceObserverThrows() {
234 Flowable<Long> source = Flowable.timer(100, TimeUnit.MILLISECONDS, scheduler);
235
236 source.safeSubscribe(new DefaultSubscriber<Long>() {
237
238 @Override
239 public void onNext(Long t) {
240 throw new TestException();
241 }
242
243 @Override
244 public void onError(Throwable e) {
245 subscriber.onError(e);
246 }
247
248 @Override
249 public void onComplete() {
250 subscriber.onComplete();
251 }
252 });
253
254 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
255
256 verify(subscriber).onError(any(TestException.class));
257 verify(subscriber, never()).onNext(anyLong());
258 verify(subscriber, never()).onComplete();
259 }
260
261 @Test
262 public void periodicObserverThrows() {
263 Flowable<Long> source = Flowable.interval(100, 100, TimeUnit.MILLISECONDS, scheduler);
264
265 InOrder inOrder = inOrder(subscriber);
266
267 source.safeSubscribe(new DefaultSubscriber<Long>() {
268
269 @Override
270 public void onNext(Long t) {
271 if (t > 0) {
272 throw new TestException();
273 }
274 subscriber.onNext(t);
275 }
276
277 @Override
278 public void onError(Throwable e) {
279 subscriber.onError(e);
280 }
281
282 @Override
283 public void onComplete() {
284 subscriber.onComplete();
285 }
286 });
287
288 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
289
290 inOrder.verify(subscriber).onNext(0L);
291 inOrder.verify(subscriber).onError(any(TestException.class));
292 inOrder.verifyNoMoreInteractions();
293 verify(subscriber, never()).onComplete();
294 }
295
296 @Test
297 public void disposed() {
298 TestHelper.checkDisposed(Flowable.timer(1, TimeUnit.DAYS));
299 }
300
301 @Test
302 public void backpressureNotReady() {
303 Flowable.timer(1, TimeUnit.MILLISECONDS)
304 .test(0L)
305 .awaitDone(5, TimeUnit.SECONDS)
306 .assertFailure(MissingBackpressureException.class);
307 }
308
309 @Test
310 public void timerCancelRace() {
311 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
312 final TestSubscriber<Long> ts = new TestSubscriber<>();
313
314 final TestScheduler scheduler = new TestScheduler();
315
316 Flowable.timer(1, TimeUnit.SECONDS, scheduler)
317 .subscribe(ts);
318
319 Runnable r1 = new Runnable() {
320 @Override
321 public void run() {
322 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
323 }
324 };
325
326 Runnable r2 = new Runnable() {
327 @Override
328 public void run() {
329 ts.cancel();
330 }
331 };
332
333 TestHelper.race(r1, r2);
334 }
335 }
336
337 @Test
338 public void timerDelayZero() {
339 List<Throwable> errors = TestHelper.trackPluginErrors();
340 try {
341 for (int i = 0; i < 1000; i++) {
342 Flowable.timer(0, TimeUnit.MILLISECONDS).blockingFirst();
343 }
344
345 assertTrue(errors.toString(), errors.isEmpty());
346 } finally {
347 RxJavaPlugins.reset();
348 }
349 }
350
351 @Test
352 public void timerInterruptible() throws Exception {
353 ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
354 try {
355 for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
356 final AtomicBoolean interrupted = new AtomicBoolean();
357 TestSubscriber<Long> ts = Flowable.timer(1, TimeUnit.MILLISECONDS, s)
358 .map(new Function<Long, Long>() {
359 @Override
360 public Long apply(Long v) throws Exception {
361 try {
362 Thread.sleep(3000);
363 } catch (InterruptedException ex) {
364 interrupted.set(true);
365 }
366 return v;
367 }
368 })
369 .test();
370
371 Thread.sleep(500);
372
373 ts.cancel();
374
375 Thread.sleep(500);
376
377 assertTrue(s.getClass().getSimpleName(), interrupted.get());
378 }
379 } finally {
380 exec.shutdown();
381 }
382 }
383
384 @Test
385 public void badRequest() {
386 TestHelper.assertBadRequestReported(Flowable.timer(1, TimeUnit.MINUTES));
387 }
388 }