Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeTimerTest.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.maybe;
15
16 import static org.junit.Assert.assertTrue;
17
18 import java.util.concurrent.*;
19 import java.util.concurrent.atomic.AtomicBoolean;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.observers.TestObserver;
26 import io.reactivex.rxjava3.schedulers.*;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class MaybeTimerTest extends RxJavaTest {
30
31 @Test
32 public void dispose() {
33 TestHelper.checkDisposed(Maybe.timer(1, TimeUnit.SECONDS, new TestScheduler()));
34 }
35
36 @Test
37 public void timerInterruptible() throws Exception {
38 ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
39 try {
40 for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
41 final AtomicBoolean interrupted = new AtomicBoolean();
42 TestObserver<Long> to = Maybe.timer(1, TimeUnit.MILLISECONDS, s)
43 .map(new Function<Long, Long>() {
44 @Override
45 public Long apply(Long v) throws Exception {
46 try {
47 Thread.sleep(3000);
48 } catch (InterruptedException ex) {
49 interrupted.set(true);
50 }
51 return v;
52 }
53 })
54 .test();
55
56 Thread.sleep(500);
57
58 to.dispose();
59
60 Thread.sleep(500);
61
62 assertTrue(s.getClass().getSimpleName(), interrupted.get());
63 }
64 } finally {
65 exec.shutdown();
66 }
67 }
68
69 }