Debian Codegraph @

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