Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableFromCallableTest.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.completable;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.concurrent.*;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import org.junit.Test;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26
27 import io.reactivex.rxjava3.core.*;
28 import io.reactivex.rxjava3.disposables.Disposable;
29 import io.reactivex.rxjava3.exceptions.TestException;
30 import io.reactivex.rxjava3.observers.TestObserver;
31 import io.reactivex.rxjava3.schedulers.Schedulers;
32 import io.reactivex.rxjava3.testsupport.*;
33
34 public class CompletableFromCallableTest extends RxJavaTest {
35
36 @Test
37 public void fromCallable() {
38 final AtomicInteger atomicInteger = new AtomicInteger();
39
40 Completable.fromCallable(new Callable<Object>() {
41 @Override
42 public Object call() throws Exception {
43 atomicInteger.incrementAndGet();
44 return null;
45 }
46 })
47 .test()
48 .assertResult();
49
50 assertEquals(1, atomicInteger.get());
51 }
52
53 @Test
54 public void fromCallableTwice() {
55 final AtomicInteger atomicInteger = new AtomicInteger();
56
57 Callable<Object> callable = new Callable<Object>() {
58 @Override
59 public Object call() throws Exception {
60 atomicInteger.incrementAndGet();
61 return null;
62 }
63 };
64
65 Completable.fromCallable(callable)
66 .test()
67 .assertResult();
68
69 assertEquals(1, atomicInteger.get());
70
71 Completable.fromCallable(callable)
72 .test()
73 .assertResult();
74
75 assertEquals(2, atomicInteger.get());
76 }
77
78 @Test
79 public void fromCallableInvokesLazy() {
80 final AtomicInteger atomicInteger = new AtomicInteger();
81
82 Completable completable = Completable.fromCallable(new Callable<Object>() {
83 @Override
84 public Object call() throws Exception {
85 atomicInteger.incrementAndGet();
86 return null;
87 }
88 });
89
90 assertEquals(0, atomicInteger.get());
91
92 completable
93 .test()
94 .assertResult();
95
96 assertEquals(1, atomicInteger.get());
97 }
98
99 @Test
100 public void fromCallableThrows() {
101 Completable.fromCallable(new Callable<Object>() {
102 @Override
103 public Object call() throws Exception {
104 throw new UnsupportedOperationException();
105 }
106 })
107 .test()
108 .assertFailure(UnsupportedOperationException.class);
109 }
110
111 @SuppressWarnings("unchecked")
112 @Test
113 public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Exception {
114 Callable<String> func = mock(Callable.class);
115
116 final CountDownLatch funcLatch = new CountDownLatch(1);
117 final CountDownLatch observerLatch = new CountDownLatch(1);
118
119 when(func.call()).thenAnswer(new Answer<String>() {
120 @Override
121 public String answer(InvocationOnMock invocation) throws Throwable {
122 observerLatch.countDown();
123
124 try {
125 funcLatch.await();
126 } catch (InterruptedException e) {
127 // It's okay, unsubscription causes Thread interruption
128
129 // Restoring interruption status of the Thread
130 Thread.currentThread().interrupt();
131 }
132
133 return "should_not_be_delivered";
134 }
135 });
136
137 Completable fromCallableObservable = Completable.fromCallable(func);
138
139 Observer<Object> observer = TestHelper.mockObserver();
140
141 TestObserver<String> outer = new TestObserver<>(observer);
142
143 fromCallableObservable
144 .subscribeOn(Schedulers.computation())
145 .subscribe(outer);
146
147 // Wait until func will be invoked
148 observerLatch.await();
149
150 // Unsubscribing before emission
151 outer.dispose();
152
153 // Emitting result
154 funcLatch.countDown();
155
156 // func must be invoked
157 verify(func).call();
158
159 // Observer must not be notified at all
160 verify(observer).onSubscribe(any(Disposable.class));
161 verifyNoMoreInteractions(observer);
162 }
163
164 @Test
165 @SuppressUndeliverable
166 public void fromActionErrorsDisposed() {
167 final AtomicInteger calls = new AtomicInteger();
168 Completable.fromCallable(new Callable<Object>() {
169 @Override
170 public Object call() throws Exception {
171 calls.incrementAndGet();
172 throw new TestException();
173 }
174 })
175 .test(true)
176 .assertEmpty();
177
178 assertEquals(1, calls.get());
179 }
180 }