Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableForEachTest.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.observable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.Observable;
23 import io.reactivex.rxjava3.core.RxJavaTest;
24 import io.reactivex.rxjava3.disposables.Disposable;
25 import io.reactivex.rxjava3.exceptions.*;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.internal.functions.Functions;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.subjects.PublishSubject;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class ObservableForEachTest extends RxJavaTest {
33
34 @Test
35 public void forEachWile() {
36 final List<Object> list = new ArrayList<>();
37
38 Observable.range(1, 5)
39 .doOnNext(new Consumer<Integer>() {
40 @Override
41 public void accept(Integer v) throws Exception {
42 list.add(v);
43 }
44 })
45 .forEachWhile(new Predicate<Integer>() {
46 @Override
47 public boolean test(Integer v) throws Exception {
48 return v < 3;
49 }
50 });
51
52 assertEquals(Arrays.asList(1, 2, 3), list);
53 }
54
55 @Test
56 public void forEachWileWithError() {
57 final List<Object> list = new ArrayList<>();
58
59 Observable.range(1, 5).concatWith(Observable.<Integer>error(new TestException()))
60 .doOnNext(new Consumer<Integer>() {
61 @Override
62 public void accept(Integer v) throws Exception {
63 list.add(v);
64 }
65 })
66 .forEachWhile(new Predicate<Integer>() {
67 @Override
68 public boolean test(Integer v) throws Exception {
69 return true;
70 }
71 }, new Consumer<Throwable>() {
72 @Override
73 public void accept(Throwable e) throws Exception {
74 list.add(100);
75 }
76 });
77
78 assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list);
79 }
80
81 @Test
82 public void badSource() {
83 TestHelper.checkBadSourceObservable(new Function<Observable<Integer>, Object>() {
84 @Override
85 public Object apply(Observable<Integer> f) throws Exception {
86 return f.forEachWhile(Functions.alwaysTrue());
87 }
88 }, false, 1, 1, (Object[])null);
89 }
90
91 @Test
92 public void dispose() {
93 PublishSubject<Integer> ps = PublishSubject.create();
94
95 Disposable d = ps.forEachWhile(Functions.alwaysTrue());
96
97 assertFalse(d.isDisposed());
98
99 d.dispose();
100
101 assertTrue(d.isDisposed());
102 }
103
104 @Test
105 public void whilePredicateThrows() {
106 List<Throwable> errors = TestHelper.trackPluginErrors();
107 try {
108 Observable.just(1).forEachWhile(new Predicate<Integer>() {
109 @Override
110 public boolean test(Integer v) throws Exception {
111 throw new TestException();
112 }
113 });
114
115 TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
116 Throwable c = errors.get(0).getCause();
117 assertTrue("" + c, c instanceof TestException);
118 } finally {
119 RxJavaPlugins.reset();
120 }
121 }
122
123 @Test
124 public void whileErrorThrows() {
125 List<Throwable> errors = TestHelper.trackPluginErrors();
126 try {
127 Observable.<Integer>error(new TestException("Outer"))
128 .forEachWhile(Functions.alwaysTrue(), new Consumer<Throwable>() {
129 @Override
130 public void accept(Throwable v) throws Exception {
131 throw new TestException("Inner");
132 }
133 });
134
135 TestHelper.assertError(errors, 0, CompositeException.class);
136
137 List<Throwable> ce = TestHelper.compositeList(errors.get(0));
138
139 TestHelper.assertError(ce, 0, TestException.class, "Outer");
140 TestHelper.assertError(ce, 1, TestException.class, "Inner");
141 } finally {
142 RxJavaPlugins.reset();
143 }
144 }
145
146 @Test
147 public void whileCompleteThrows() {
148 List<Throwable> errors = TestHelper.trackPluginErrors();
149 try {
150 Observable.just(1).forEachWhile(Functions.alwaysTrue(), Functions.emptyConsumer(),
151 new Action() {
152 @Override
153 public void run() throws Exception {
154 throw new TestException();
155 }
156 });
157
158 TestHelper.assertUndeliverable(errors, 0, TestException.class);
159 } finally {
160 RxJavaPlugins.reset();
161 }
162 }
163
164 }