Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableElementAtTest.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
18 import java.util.*;
19
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import org.junit.Test;
22 import org.reactivestreams.*;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.core.Observable;
26 import io.reactivex.rxjava3.core.Observer;
27 import io.reactivex.rxjava3.exceptions.TestException;
28 import io.reactivex.rxjava3.functions.*;
29 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
31 import io.reactivex.rxjava3.processors.PublishProcessor;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class FlowableElementAtTest extends RxJavaTest {
35
36 @Test
37 public void elementAtFlowable() {
38 assertEquals(2, Flowable.fromArray(1, 2).elementAt(1).toFlowable().blockingSingle()
39 .intValue());
40 }
41
42 @Test(expected = IndexOutOfBoundsException.class)
43 public void elementAtWithMinusIndexFlowable() {
44 Flowable.fromArray(1, 2).elementAt(-1);
45 }
46
47 @Test
48 public void elementAtWithIndexOutOfBoundsFlowable() {
49 assertEquals(-100, Flowable.fromArray(1, 2).elementAt(2).toFlowable().blockingFirst(-100).intValue());
50 }
51
52 @Test
53 public void elementAtOrDefaultFlowable() {
54 assertEquals(2, Flowable.fromArray(1, 2).elementAt(1, 0).toFlowable().blockingSingle().intValue());
55 }
56
57 @Test
58 public void elementAtOrDefaultWithIndexOutOfBoundsFlowable() {
59 assertEquals(0, Flowable.fromArray(1, 2).elementAt(2, 0).toFlowable().blockingSingle().intValue());
60 }
61
62 @Test(expected = IndexOutOfBoundsException.class)
63 public void elementAtOrDefaultWithMinusIndexFlowable() {
64 Flowable.fromArray(1, 2).elementAt(-1, 0);
65 }
66
67 @Test
68 public void elementAt() {
69 assertEquals(2, Flowable.fromArray(1, 2).elementAt(1).blockingGet()
70 .intValue());
71 }
72
73 @Test
74 public void elementAtConstrainsUpstreamRequests() {
75 final List<Long> requests = new ArrayList<>();
76 Flowable.fromArray(1, 2, 3, 4)
77 .doOnRequest(new LongConsumer() {
78 @Override
79 public void accept(long n) throws Throwable {
80 requests.add(n);
81 }
82 })
83 .elementAt(2)
84 .blockingGet()
85 .intValue();
86 assertEquals(Arrays.asList(3L), requests);
87 }
88
89 @Test
90 public void elementAtWithDefaultConstrainsUpstreamRequests() {
91 final List<Long> requests = new ArrayList<>();
92 Flowable.fromArray(1, 2, 3, 4)
93 .doOnRequest(new LongConsumer() {
94 @Override
95 public void accept(long n) throws Throwable {
96 requests.add(n);
97 }
98 })
99 .elementAt(2, 100)
100 .blockingGet()
101 .intValue();
102 assertEquals(Arrays.asList(3L), requests);
103 }
104
105 @Test(expected = IndexOutOfBoundsException.class)
106 public void elementAtWithMinusIndex() {
107 Flowable.fromArray(1, 2).elementAt(-1);
108 }
109
110 @Test
111 public void elementAtWithIndexOutOfBounds() {
112 assertNull(Flowable.fromArray(1, 2).elementAt(2).blockingGet());
113 }
114
115 @Test
116 public void elementAtOrDefault() {
117 assertEquals(2, Flowable.fromArray(1, 2).elementAt(1, 0).blockingGet().intValue());
118 }
119
120 @Test
121 public void elementAtOrDefaultWithIndexOutOfBounds() {
122 assertEquals(0, Flowable.fromArray(1, 2).elementAt(2, 0).blockingGet().intValue());
123 }
124
125 @Test(expected = IndexOutOfBoundsException.class)
126 public void elementAtOrDefaultWithMinusIndex() {
127 Flowable.fromArray(1, 2).elementAt(-1, 0);
128 }
129
130 @Test(expected = IndexOutOfBoundsException.class)
131 public void elementAtOrErrorNegativeIndex() {
132 Flowable.empty()
133 .elementAtOrError(-1);
134 }
135
136 @Test
137 public void elementAtOrErrorNoElement() {
138 Flowable.empty()
139 .elementAtOrError(0)
140 .test()
141 .assertNoValues()
142 .assertError(NoSuchElementException.class);
143 }
144
145 @Test
146 public void elementAtOrErrorOneElement() {
147 Flowable.just(1)
148 .elementAtOrError(0)
149 .test()
150 .assertNoErrors()
151 .assertValue(1);
152 }
153
154 @Test
155 public void elementAtOrErrorMultipleElements() {
156 Flowable.just(1, 2, 3)
157 .elementAtOrError(1)
158 .test()
159 .assertNoErrors()
160 .assertValue(2);
161 }
162
163 @Test
164 public void elementAtOrErrorInvalidIndex() {
165 Flowable.just(1, 2, 3)
166 .elementAtOrError(3)
167 .test()
168 .assertNoValues()
169 .assertError(NoSuchElementException.class);
170 }
171
172 @Test
173 public void elementAtOrErrorError() {
174 Flowable.error(new RuntimeException("error"))
175 .elementAtOrError(0)
176 .to(TestHelper.testConsumer())
177 .assertNoValues()
178 .assertErrorMessage("error")
179 .assertError(RuntimeException.class);
180 }
181
182 @Test
183 public void elementAtIndex0OnEmptySource() {
184 Flowable.empty()
185 .elementAt(0)
186 .test()
187 .assertResult();
188 }
189
190 @Test
191 public void elementAtIndex0WithDefaultOnEmptySource() {
192 Flowable.empty()
193 .elementAt(0, 5)
194 .test()
195 .assertResult(5);
196 }
197
198 @Test
199 public void elementAtIndex1OnEmptySource() {
200 Flowable.empty()
201 .elementAt(1)
202 .test()
203 .assertResult();
204 }
205
206 @Test
207 public void elementAtIndex1WithDefaultOnEmptySource() {
208 Flowable.empty()
209 .elementAt(1, 10)
210 .test()
211 .assertResult(10);
212 }
213
214 @Test
215 public void elementAtOrErrorIndex1OnEmptySource() {
216 Flowable.empty()
217 .elementAtOrError(1)
218 .test()
219 .assertFailure(NoSuchElementException.class);
220 }
221
222 @Test
223 public void doubleOnSubscribe() {
224 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() {
225 @Override
226 public Publisher<Object> apply(Flowable<Object> f) throws Exception {
227 return f.elementAt(0).toFlowable();
228 }
229 });
230
231 TestHelper.checkDoubleOnSubscribeFlowableToMaybe(new Function<Flowable<Object>, Maybe<Object>>() {
232 @Override
233 public Maybe<Object> apply(Flowable<Object> f) throws Exception {
234 return f.elementAt(0);
235 }
236 });
237
238 TestHelper.checkDoubleOnSubscribeFlowableToSingle(new Function<Flowable<Object>, Single<Object>>() {
239 @Override
240 public Single<Object> apply(Flowable<Object> f) throws Exception {
241 return f.elementAt(0, 1);
242 }
243 });
244 }
245
246 @Test
247 public void elementAtIndex1WithDefaultOnEmptySourceObservable() {
248 Flowable.empty()
249 .elementAt(1, 10)
250 .toFlowable()
251 .test()
252 .assertResult(10);
253 }
254
255 @Test
256 public void errorFlowable() {
257 Flowable.error(new TestException())
258 .elementAt(1, 10)
259 .toFlowable()
260 .test()
261 .assertFailure(TestException.class);
262 }
263
264 @Test
265 public void error() {
266 Flowable.error(new TestException())
267 .elementAt(1, 10)
268 .test()
269 .assertFailure(TestException.class);
270
271 Flowable.error(new TestException())
272 .elementAt(1)
273 .test()
274 .assertFailure(TestException.class);
275 }
276
277 @Test
278 public void badSource() {
279 List<Throwable> errors = TestHelper.trackPluginErrors();
280 try {
281 new Flowable<Integer>() {
282 @Override
283 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
284 subscriber.onSubscribe(new BooleanSubscription());
285
286 subscriber.onNext(1);
287 subscriber.onNext(2);
288 subscriber.onError(new TestException());
289 subscriber.onComplete();
290 }
291 }
292 .elementAt(0)
293 .toFlowable()
294 .test()
295 .assertResult(1);
296
297 TestHelper.assertUndeliverable(errors, 0, TestException.class);
298 } finally {
299 RxJavaPlugins.reset();
300 }
301
302 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
303 @Override
304 public Object apply(Flowable<Integer> f) throws Exception {
305 return f.elementAt(0);
306 }
307 }, false, null, 1);
308
309 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
310 @Override
311 public Object apply(Flowable<Integer> f) throws Exception {
312 return f.elementAt(0, 1);
313 }
314 }, false, null, 1, 1);
315
316 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
317 @Override
318 public Object apply(Flowable<Integer> f) throws Exception {
319 return f.elementAt(0).toFlowable();
320 }
321 }, false, null, 1);
322
323 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
324 @Override
325 public Object apply(Flowable<Integer> f) throws Exception {
326 return f.elementAt(0, 1).toFlowable();
327 }
328 }, false, null, 1, 1);
329 }
330
331 @Test
332 public void dispose() {
333 TestHelper.checkDisposed(PublishProcessor.create().elementAt(0).toFlowable());
334 TestHelper.checkDisposed(PublishProcessor.create().elementAt(0, 1).toFlowable());
335
336 TestHelper.checkDisposed(PublishProcessor.create().elementAt(0));
337 TestHelper.checkDisposed(PublishProcessor.create().elementAt(0, 1));
338 }
339
340 @Test
341 public void badSourceObservable() {
342 List<Throwable> errors = TestHelper.trackPluginErrors();
343 try {
344 new Observable<Integer>() {
345 @Override
346 protected void subscribeActual(Observer<? super Integer> observer) {
347 observer.onSubscribe(Disposable.empty());
348
349 observer.onNext(1);
350 observer.onNext(2);
351 observer.onError(new TestException());
352 observer.onComplete();
353 }
354 }
355 .elementAt(0)
356 .toFlowable()
357 .test()
358 .assertResult(1);
359
360 TestHelper.assertUndeliverable(errors, 0, TestException.class);
361 } finally {
362 RxJavaPlugins.reset();
363 }
364 }
365
366 @Test
367 public void badSource2() {
368 List<Throwable> errors = TestHelper.trackPluginErrors();
369 try {
370 new Flowable<Integer>() {
371 @Override
372 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
373 subscriber.onSubscribe(new BooleanSubscription());
374
375 subscriber.onNext(1);
376 subscriber.onNext(2);
377 subscriber.onError(new TestException());
378 subscriber.onComplete();
379 }
380 }
381 .elementAt(0, 1)
382 .test()
383 .assertResult(1);
384
385 TestHelper.assertUndeliverable(errors, 0, TestException.class);
386 } finally {
387 RxJavaPlugins.reset();
388 }
389 }
390 }