Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableGenerateTest.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.assertEquals;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.internal.functions.Functions;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class ObservableGenerateTest extends RxJavaTest {
30
31 @Test
32 public void statefulBiconsumer() {
33 Observable.generate(new Supplier<Object>() {
34 @Override
35 public Object get() throws Exception {
36 return 10;
37 }
38 }, new BiConsumer<Object, Emitter<Object>>() {
39 @Override
40 public void accept(Object s, Emitter<Object> e) throws Exception {
41 e.onNext(s);
42 }
43 }, new Consumer<Object>() {
44 @Override
45 public void accept(Object d) throws Exception {
46
47 }
48 })
49 .take(5)
50 .test()
51 .assertResult(10, 10, 10, 10, 10);
52 }
53
54 @Test
55 public void stateSupplierThrows() {
56 Observable.generate(new Supplier<Object>() {
57 @Override
58 public Object get() throws Exception {
59 throw new TestException();
60 }
61 }, new BiConsumer<Object, Emitter<Object>>() {
62 @Override
63 public void accept(Object s, Emitter<Object> e) throws Exception {
64 e.onNext(s);
65 }
66 }, Functions.emptyConsumer())
67 .test()
68 .assertFailure(TestException.class);
69 }
70
71 @Test
72 public void generatorThrows() {
73 Observable.generate(new Supplier<Object>() {
74 @Override
75 public Object get() throws Exception {
76 return 1;
77 }
78 }, new BiConsumer<Object, Emitter<Object>>() {
79 @Override
80 public void accept(Object s, Emitter<Object> e) throws Exception {
81 throw new TestException();
82 }
83 }, Functions.emptyConsumer())
84 .test()
85 .assertFailure(TestException.class);
86 }
87
88 @Test
89 public void disposerThrows() {
90 List<Throwable> errors = TestHelper.trackPluginErrors();
91 try {
92 Observable.generate(new Supplier<Object>() {
93 @Override
94 public Object get() throws Exception {
95 return 1;
96 }
97 }, new BiConsumer<Object, Emitter<Object>>() {
98 @Override
99 public void accept(Object s, Emitter<Object> e) throws Exception {
100 e.onComplete();
101 }
102 }, new Consumer<Object>() {
103 @Override
104 public void accept(Object d) throws Exception {
105 throw new TestException();
106 }
107 })
108 .test()
109 .assertResult();
110
111 TestHelper.assertUndeliverable(errors, 0, TestException.class);
112 } finally {
113 RxJavaPlugins.reset();
114 }
115 }
116
117 @Test
118 public void dispose() {
119 TestHelper.checkDisposed(Observable.generate(new Supplier<Object>() {
120 @Override
121 public Object get() throws Exception {
122 return 1;
123 }
124 }, new BiConsumer<Object, Emitter<Object>>() {
125 @Override
126 public void accept(Object s, Emitter<Object> e) throws Exception {
127 e.onComplete();
128 }
129 }, Functions.emptyConsumer()));
130 }
131
132 @Test
133 public void nullError() {
134 final int[] call = { 0 };
135 Observable.generate(Functions.justSupplier(1),
136 new BiConsumer<Integer, Emitter<Object>>() {
137 @Override
138 public void accept(Integer s, Emitter<Object> e) throws Exception {
139 try {
140 e.onError(null);
141 } catch (NullPointerException ex) {
142 call[0]++;
143 }
144 }
145 }, Functions.emptyConsumer())
146 .test()
147 .assertFailure(NullPointerException.class);
148
149 assertEquals(0, call[0]);
150 }
151
152 @Test
153 public void multipleOnNext() {
154 Observable.generate(new Consumer<Emitter<Object>>() {
155 @Override
156 public void accept(Emitter<Object> e) throws Exception {
157 e.onNext(1);
158 e.onNext(2);
159 }
160 })
161 .test()
162 .assertFailure(IllegalStateException.class, 1);
163 }
164
165 @Test
166 public void multipleOnError() {
167 List<Throwable> errors = TestHelper.trackPluginErrors();
168 try {
169 Observable.generate(new Consumer<Emitter<Object>>() {
170 @Override
171 public void accept(Emitter<Object> e) throws Exception {
172 e.onError(new TestException("First"));
173 e.onError(new TestException("Second"));
174 }
175 })
176 .test()
177 .assertFailure(TestException.class);
178
179 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
180 } finally {
181 RxJavaPlugins.reset();
182 }
183 }
184
185 @Test
186 public void multipleOnComplete() {
187 Observable.generate(new Consumer<Emitter<Object>>() {
188 @Override
189 public void accept(Emitter<Object> e) throws Exception {
190 e.onComplete();
191 e.onComplete();
192 }
193 })
194 .test()
195 .assertResult();
196 }
197
198 @Test
199 public void onNextAfterOnComplete() {
200 Observable.generate(new Consumer<Emitter<Object>>() {
201 @Override
202 public void accept(Emitter<Object> e) throws Exception {
203 e.onComplete();
204 e.onNext(1);
205 }
206 })
207 .test()
208 .assertResult();
209 }
210 }