Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFromRunnableTest.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.*;
17 import static org.mockito.Mockito.*;
18
19 import java.util.List;
20 import java.util.concurrent.*;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import org.junit.Test;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.functions.Supplier;
27 import io.reactivex.rxjava3.observers.TestObserver;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.schedulers.Schedulers;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class MaybeFromRunnableTest extends RxJavaTest {
33 @Test
34 public void fromRunnable() {
35 final AtomicInteger atomicInteger = new AtomicInteger();
36
37 Maybe.fromRunnable(new Runnable() {
38 @Override
39 public void run() {
40 atomicInteger.incrementAndGet();
41 }
42 })
43 .test()
44 .assertResult();
45
46 assertEquals(1, atomicInteger.get());
47 }
48
49 @Test
50 public void fromRunnableTwice() {
51 final AtomicInteger atomicInteger = new AtomicInteger();
52
53 Runnable run = new Runnable() {
54 @Override
55 public void run() {
56 atomicInteger.incrementAndGet();
57 }
58 };
59
60 Maybe.fromRunnable(run)
61 .test()
62 .assertResult();
63
64 assertEquals(1, atomicInteger.get());
65
66 Maybe.fromRunnable(run)
67 .test()
68 .assertResult();
69
70 assertEquals(2, atomicInteger.get());
71 }
72
73 @Test
74 public void fromRunnableInvokesLazy() {
75 final AtomicInteger atomicInteger = new AtomicInteger();
76
77 final Maybe<Object> maybe = Maybe.fromRunnable(new Runnable() {
78 @Override public void run() {
79 atomicInteger.incrementAndGet();
80 }
81 });
82
83 assertEquals(0, atomicInteger.get());
84
85 maybe
86 .test()
87 .assertResult();
88
89 assertEquals(1, atomicInteger.get());
90 }
91
92 @Test
93 public void fromRunnableThrows() {
94 Maybe.fromRunnable(new Runnable() {
95 @Override
96 public void run() {
97 throw new UnsupportedOperationException();
98 }
99 })
100 .test()
101 .assertFailure(UnsupportedOperationException.class);
102 }
103
104 @SuppressWarnings("unchecked")
105 @Test
106 public void callable() throws Throwable {
107 final int[] counter = { 0 };
108
109 Maybe<Void> m = Maybe.fromRunnable(new Runnable() {
110 @Override
111 public void run() {
112 counter[0]++;
113 }
114 });
115
116 assertTrue(m.getClass().toString(), m instanceof Supplier);
117
118 assertNull(((Supplier<Void>)m).get());
119
120 assertEquals(1, counter[0]);
121 }
122
123 @Test
124 public void noErrorLoss() throws Exception {
125 List<Throwable> errors = TestHelper.trackPluginErrors();
126 try {
127 final CountDownLatch cdl1 = new CountDownLatch(1);
128 final CountDownLatch cdl2 = new CountDownLatch(1);
129
130 TestObserver<Object> to = Maybe.fromRunnable(new Runnable() {
131 @Override
132 public void run() {
133 cdl1.countDown();
134 try {
135 cdl2.await(5, TimeUnit.SECONDS);
136 } catch (InterruptedException ex) {
137 throw new RuntimeException(ex);
138 }
139 }
140 }).subscribeOn(Schedulers.single()).test();
141
142 assertTrue(cdl1.await(5, TimeUnit.SECONDS));
143
144 to.dispose();
145
146 int timeout = 10;
147
148 while (timeout-- > 0 && errors.isEmpty()) {
149 Thread.sleep(100);
150 }
151
152 TestHelper.assertUndeliverable(errors, 0, RuntimeException.class);
153
154 assertTrue(errors.get(0).toString(), errors.get(0).getCause().getCause() instanceof InterruptedException);
155 } finally {
156 RxJavaPlugins.reset();
157 }
158 }
159
160 @Test
161 public void disposedUpfront() {
162 Runnable run = mock(Runnable.class);
163
164 Maybe.fromRunnable(run)
165 .test(true)
166 .assertEmpty();
167
168 verify(run, never()).run();
169 }
170
171 @Test
172 public void cancelWhileRunning() {
173 final TestObserver<Object> to = new TestObserver<>();
174
175 Maybe.fromRunnable(new Runnable() {
176 @Override
177 public void run() {
178 to.dispose();
179 }
180 })
181 .subscribeWith(to)
182 .assertEmpty();
183
184 assertTrue(to.isDisposed());
185 }
186 }