Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFromCompletableTest.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 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.internal.fuseable.*;
28 import io.reactivex.rxjava3.operators.QueueFuseable;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.schedulers.Schedulers;
31 import io.reactivex.rxjava3.subscribers.TestSubscriber;
32 import io.reactivex.rxjava3.testsupport.*;
33
34 public class FlowableFromCompletableTest extends RxJavaTest {
35 @Test
36 public void fromCompletable() {
37 final AtomicInteger atomicInteger = new AtomicInteger();
38
39 Flowable.fromCompletable(Completable.fromAction(new Action() {
40 @Override
41 public void run() throws Exception {
42 atomicInteger.incrementAndGet();
43 }
44 }))
45 .test()
46 .assertResult();
47
48 assertEquals(1, atomicInteger.get());
49 }
50
51 @Test
52 public void fromCompletableTwice() {
53 final AtomicInteger atomicInteger = new AtomicInteger();
54
55 Action run = new Action() {
56 @Override
57 public void run() throws Exception {
58 atomicInteger.incrementAndGet();
59 }
60 };
61
62 Flowable.fromCompletable(Completable.fromAction(run))
63 .test()
64 .assertResult();
65
66 assertEquals(1, atomicInteger.get());
67
68 Flowable.fromCompletable(Completable.fromAction(run))
69 .test()
70 .assertResult();
71
72 assertEquals(2, atomicInteger.get());
73 }
74
75 @Test
76 public void fromCompletableInvokesLazy() {
77 final AtomicInteger atomicInteger = new AtomicInteger();
78
79 Flowable<Object> source = Flowable.fromCompletable(Completable.fromAction(new Action() {
80 @Override
81 public void run() throws Exception {
82 atomicInteger.incrementAndGet();
83 }
84 }));
85
86 assertEquals(0, atomicInteger.get());
87
88 source
89 .test()
90 .assertResult();
91
92 assertEquals(1, atomicInteger.get());
93 }
94
95 @Test
96 public void fromCompletableThrows() {
97 Flowable.fromCompletable(Completable.fromAction(new Action() {
98 @Override
99 public void run() throws Exception {
100 throw new UnsupportedOperationException();
101 }
102 }))
103 .test()
104 .assertFailure(UnsupportedOperationException.class);
105 }
106
107 @Test
108 public void noErrorLoss() throws Exception {
109 List<Throwable> errors = TestHelper.trackPluginErrors();
110 try {
111 final CountDownLatch cdl1 = new CountDownLatch(1);
112 final CountDownLatch cdl2 = new CountDownLatch(1);
113
114 TestSubscriber<Object> ts = Flowable.fromCompletable(Completable.fromAction(new Action() {
115 @Override
116 public void run() throws Exception {
117 cdl1.countDown();
118 cdl2.await(5, TimeUnit.SECONDS);
119 }
120 }))
121 .subscribeOn(Schedulers.single()).test();
122
123 assertTrue(cdl1.await(5, TimeUnit.SECONDS));
124
125 ts.cancel();
126
127 int timeout = 10;
128
129 while (timeout-- > 0 && errors.isEmpty()) {
130 Thread.sleep(100);
131 }
132
133 TestHelper.assertUndeliverable(errors, 0, InterruptedException.class);
134 } finally {
135 RxJavaPlugins.reset();
136 }
137 }
138
139 @Test
140 public void disposedUpfront() throws Throwable {
141 Action run = mock(Action.class);
142
143 Flowable.fromCompletable(Completable.fromAction(run))
144 .test(1L, true)
145 .assertEmpty();
146
147 verify(run, never()).run();
148 }
149
150 @Test
151 public void cancelWhileRunning() {
152 final TestSubscriber<Object> ts = new TestSubscriber<>();
153
154 Flowable.fromCompletable(Completable.fromAction(new Action() {
155 @Override
156 public void run() throws Exception {
157 ts.cancel();
158 }
159 }))
160 .subscribeWith(ts)
161 .assertEmpty();
162
163 assertTrue(ts.isCancelled());
164 }
165
166 @Test
167 public void asyncFused() throws Throwable {
168 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
169 ts.setInitialFusionMode(QueueFuseable.ASYNC);
170
171 Action action = mock(Action.class);
172
173 Flowable.fromCompletable(Completable.fromAction(action))
174 .subscribe(ts);
175
176 ts.assertFusionMode(QueueFuseable.ASYNC)
177 .assertResult();
178
179 verify(action).run();
180 }
181
182 @Test
183 public void syncFusedRejected() throws Throwable {
184 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
185 ts.setInitialFusionMode(QueueFuseable.SYNC);
186
187 Action action = mock(Action.class);
188
189 Flowable.fromCompletable(Completable.fromAction(action))
190 .subscribe(ts);
191
192 ts.assertFusionMode(QueueFuseable.NONE)
193 .assertResult();
194
195 verify(action).run();
196 }
197
198 @Test
199 public void upstream() {
200 Flowable<?> f = Flowable.fromCompletable(Completable.never());
201 assertTrue(f instanceof HasUpstreamCompletableSource);
202 assertSame(Completable.never(), ((HasUpstreamCompletableSource)f).source());
203 }
204 }