Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleZipArrayTest.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.single;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19 import java.util.concurrent.atomic.AtomicReference;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.Disposable;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.internal.functions.Functions;
28 import io.reactivex.rxjava3.observers.TestObserver;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.PublishProcessor;
31 import io.reactivex.rxjava3.subjects.SingleSubject;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class SingleZipArrayTest extends RxJavaTest {
35
36 final BiFunction<Object, Object, Object> addString = new BiFunction<Object, Object, Object>() {
37 @Override
38 public Object apply(Object a, Object b) throws Exception {
39 return "" + a + b;
40 }
41 };
42
43 final Function3<Object, Object, Object, Object> addString3 = new Function3<Object, Object, Object, Object>() {
44 @Override
45 public Object apply(Object a, Object b, Object c) throws Exception {
46 return "" + a + b + c;
47 }
48 };
49
50 @Test
51 public void firstError() {
52 Single.zip(Single.error(new TestException()), Single.just(1), addString)
53 .test()
54 .assertFailure(TestException.class);
55 }
56
57 @Test
58 public void secondError() {
59 Single.zip(Single.just(1), Single.<Integer>error(new TestException()), addString)
60 .test()
61 .assertFailure(TestException.class);
62 }
63
64 @Test
65 public void dispose() {
66 PublishProcessor<Integer> pp = PublishProcessor.create();
67
68 TestObserver<Object> to = Single.zip(pp.single(0), pp.single(0), addString)
69 .test();
70
71 assertTrue(pp.hasSubscribers());
72
73 to.dispose();
74
75 assertFalse(pp.hasSubscribers());
76 }
77
78 @Test
79 public void zipperThrows() {
80 Single.zip(Single.just(1), Single.just(2), new BiFunction<Integer, Integer, Object>() {
81 @Override
82 public Object apply(Integer a, Integer b) throws Exception {
83 throw new TestException();
84 }
85 })
86 .test()
87 .assertFailure(TestException.class);
88 }
89
90 @Test
91 public void zipperReturnsNull() {
92 Single.zip(Single.just(1), Single.just(2), new BiFunction<Integer, Integer, Object>() {
93 @Override
94 public Object apply(Integer a, Integer b) throws Exception {
95 return null;
96 }
97 })
98 .test()
99 .assertFailure(NullPointerException.class);
100 }
101
102 @Test
103 public void middleError() {
104 PublishProcessor<Integer> pp0 = PublishProcessor.create();
105 PublishProcessor<Integer> pp1 = PublishProcessor.create();
106
107 TestObserver<Object> to = Single.zip(pp0.single(0), pp1.single(0), pp0.single(0), addString3)
108 .test();
109
110 pp1.onError(new TestException());
111
112 assertFalse(pp0.hasSubscribers());
113
114 to.assertFailure(TestException.class);
115 }
116
117 @Test
118 public void innerErrorRace() {
119 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
120 List<Throwable> errors = TestHelper.trackPluginErrors();
121 try {
122 final PublishProcessor<Integer> pp0 = PublishProcessor.create();
123 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
124
125 final TestObserver<Object> to = Single.zip(pp0.single(0), pp1.single(0), addString)
126 .test();
127
128 final TestException ex = new TestException();
129
130 Runnable r1 = new Runnable() {
131 @Override
132 public void run() {
133 pp0.onError(ex);
134 }
135 };
136
137 Runnable r2 = new Runnable() {
138 @Override
139 public void run() {
140 pp1.onError(ex);
141 }
142 };
143
144 TestHelper.race(r1, r2);
145
146 to.assertFailure(TestException.class);
147
148 if (!errors.isEmpty()) {
149 TestHelper.assertUndeliverable(errors, 0, TestException.class);
150 }
151 } finally {
152 RxJavaPlugins.reset();
153 }
154 }
155 }
156
157 @Test(expected = NullPointerException.class)
158 public void zipArrayOneIsNull() {
159 Single.zipArray(new Function<Object[], Object>() {
160 @Override
161 public Object apply(Object[] v) {
162 return 1;
163 }
164 }, Single.just(1), null)
165 .blockingGet();
166 }
167
168 @SuppressWarnings("unchecked")
169 @Test
170 public void emptyArray() {
171 Single.zipArray(new Function<Object[], Object[]>() {
172 @Override
173 public Object[] apply(Object[] a) throws Exception {
174 return a;
175 }
176 }, new SingleSource[0])
177 .test()
178 .assertFailure(NoSuchElementException.class);
179 }
180
181 @Test
182 public void oneArray() {
183 Single.zipArray(new Function<Object[], Object>() {
184 @Override
185 public Object apply(Object[] a) throws Exception {
186 return (Integer)a[0] + 1;
187 }
188 }, Single.just(1))
189 .test()
190 .assertResult(2);
191 }
192
193 @Test
194 public void singleSourceZipperReturnsNull() {
195 Single.zipArray(Functions.justFunction(null), Single.just(1))
196 .to(TestHelper.<Object>testConsumer())
197 .assertFailureAndMessage(NullPointerException.class, "The zipper returned a null value");
198 }
199
200 @Test
201 public void singleSourceZipperReturnsNull2() {
202 Single.zipArray(Functions.justFunction(null), Single.just(1), Single.just(2))
203 .to(TestHelper.<Object>testConsumer())
204 .assertFailureAndMessage(NullPointerException.class, "The zipper returned a null value");
205 }
206
207 @Test
208 public void dispose2() {
209 TestHelper.checkDisposed(Single.zipArray(Functions.justFunction(1), SingleSubject.create(), SingleSubject.create()));
210 }
211
212 @Test
213 public void bothSucceed() {
214 Single.zipArray(a -> Arrays.asList(a), Single.just(1), Single.just(2))
215 .test()
216 .assertResult(Arrays.asList(1, 2));
217 }
218
219 @Test
220 public void onSuccessAfterDispose() {
221 AtomicReference<SingleObserver<? super Integer>> emitter = new AtomicReference<>();
222
223 TestObserver<List<Object>> to = Single.zipArray(Arrays::asList,
224 (SingleSource<Integer>)o -> emitter.set(o), Single.<Integer>never())
225 .test();
226
227 emitter.get().onSubscribe(Disposable.empty());
228
229 to.dispose();
230
231 emitter.get().onSuccess(1);
232
233 to.assertEmpty();
234 }
235 }