Repository navigation
Expand file tree
/
Copy pathFlowableMaterializeTest.java
More file actions
266 lines (221 loc) · 9.01 KB
/
Copy pathFlowableMaterializeTest.java
File metadata and controls
266 lines (221 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.internal.operators.flowable;
import static org.junit.Assert.*;
import java.util.*;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.functions.Consumer;
import io.reactivex.internal.subscriptions.BooleanSubscription;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.*;
public class FlowableMaterializeTest {
@Test
public void testMaterialize1() {
// null will cause onError to be triggered before "three" can be
// returned
final TestAsyncErrorObservable o1 = new TestAsyncErrorObservable("one", "two", null,
"three");
TestNotificationSubscriber observer = new TestNotificationSubscriber();
Flowable<Notification<String>> m = Flowable.unsafeCreate(o1).materialize();
m.subscribe(observer);
try {
o1.t.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertFalse(observer.onError);
assertTrue(observer.onCompleted);
assertEquals(3, observer.notifications.size());
assertTrue(observer.notifications.get(0).isOnNext());
assertEquals("one", observer.notifications.get(0).getValue());
assertTrue(observer.notifications.get(1).isOnNext());
assertEquals("two", observer.notifications.get(1).getValue());
assertTrue(observer.notifications.get(2).isOnError());
assertEquals(NullPointerException.class, observer.notifications.get(2).getError().getClass());
}
@Test
public void testMaterialize2() {
final TestAsyncErrorObservable o1 = new TestAsyncErrorObservable("one", "two", "three");
TestNotificationSubscriber subscriber = new TestNotificationSubscriber();
Flowable<Notification<String>> m = Flowable.unsafeCreate(o1).materialize();
m.subscribe(subscriber);
try {
o1.t.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertFalse(subscriber.onError);
assertTrue(subscriber.onCompleted);
assertEquals(4, subscriber.notifications.size());
assertTrue(subscriber.notifications.get(0).isOnNext());
assertEquals("one", subscriber.notifications.get(0).getValue());
assertTrue(subscriber.notifications.get(1).isOnNext());
assertEquals("two", subscriber.notifications.get(1).getValue());
assertTrue(subscriber.notifications.get(2).isOnNext());
assertEquals("three", subscriber.notifications.get(2).getValue());
assertTrue(subscriber.notifications.get(3).isOnComplete());
}
@Test
public void testMultipleSubscribes() throws InterruptedException, ExecutionException {
final TestAsyncErrorObservable o = new TestAsyncErrorObservable("one", "two", null, "three");
Flowable<Notification<String>> m = Flowable.unsafeCreate(o).materialize();
assertEquals(3, m.toList().toFuture().get().size());
assertEquals(3, m.toList().toFuture().get().size());
}
@Test
public void testBackpressureOnEmptyStream() {
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<Notification<Integer>>(0L);
Flowable.<Integer> empty().materialize().subscribe(ts);
ts.assertNoValues();
ts.request(1);
ts.assertValueCount(1);
assertTrue(ts.values().get(0).isOnComplete());
ts.assertComplete();
}
@Test
public void testBackpressureNoError() {
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<Notification<Integer>>(0L);
Flowable.just(1, 2, 3).materialize().subscribe(ts);
ts.assertNoValues();
ts.request(1);
ts.assertValueCount(1);
ts.request(2);
ts.assertValueCount(3);
ts.request(1);
ts.assertValueCount(4);
ts.assertComplete();
}
@Test
public void testBackpressureNoErrorAsync() throws InterruptedException {
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<Notification<Integer>>(0L);
Flowable.just(1, 2, 3)
.materialize()
.subscribeOn(Schedulers.computation())
.subscribe(ts);
Thread.sleep(100);
ts.assertNoValues();
ts.request(1);
Thread.sleep(100);
ts.assertValueCount(1);
ts.request(2);
Thread.sleep(100);
ts.assertValueCount(3);
ts.request(1);
Thread.sleep(100);
ts.assertValueCount(4);
ts.assertComplete();
}
@Test
public void testBackpressureWithError() {
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<Notification<Integer>>(0L);
Flowable.<Integer> error(new IllegalArgumentException()).materialize().subscribe(ts);
ts.assertNoValues();
ts.request(1);
ts.assertValueCount(1);
ts.assertComplete();
}
@Test
public void testBackpressureWithEmissionThenError() {
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<Notification<Integer>>(0L);
IllegalArgumentException ex = new IllegalArgumentException();
Flowable.fromIterable(Arrays.asList(1)).concatWith(Flowable.<Integer> error(ex)).materialize()
.subscribe(ts);
ts.assertNoValues();
ts.request(1);
ts.assertValueCount(1);
assertTrue(ts.values().get(0).isOnNext());
ts.request(1);
ts.assertValueCount(2);
assertTrue(ts.values().get(1).isOnError());
assertEquals(ex, ts.values().get(1).getError());
ts.assertComplete();
}
@Test
public void testWithCompletionCausingError() {
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<Notification<Integer>>();
final RuntimeException ex = new RuntimeException("boo");
Flowable.<Integer>empty().materialize().doOnNext(new Consumer<Object>() {
@Override
public void accept(Object t) {
throw ex;
}
}).subscribe(ts);
ts.assertError(ex);
ts.assertNoValues();
ts.assertTerminated();
}
@Test
public void testUnsubscribeJustBeforeCompletionNotificationShouldPreventThatNotificationArriving() {
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<Notification<Integer>>(0L);
Flowable.<Integer>empty().materialize()
.subscribe(ts);
ts.assertNoValues();
ts.dispose();
ts.request(1);
ts.assertNoValues();
// FIXME no longer assertable
// ts.assertUnsubscribed();
}
private static class TestNotificationSubscriber extends DefaultSubscriber<Notification<String>> {
boolean onCompleted = false;
boolean onError = false;
List<Notification<String>> notifications = new Vector<Notification<String>>();
@Override
public void onComplete() {
this.onCompleted = true;
}
@Override
public void onError(Throwable e) {
this.onError = true;
}
@Override
public void onNext(Notification<String> value) {
this.notifications.add(value);
}
}
private static class TestAsyncErrorObservable implements Publisher<String> {
String[] valuesToReturn;
TestAsyncErrorObservable(String... values) {
valuesToReturn = values;
}
volatile Thread t;
@Override
public void subscribe(final Subscriber<? super String> observer) {
observer.onSubscribe(new BooleanSubscription());
t = new Thread(new Runnable() {
@Override
public void run() {
for (String s : valuesToReturn) {
if (s == null) {
System.out.println("throwing exception");
try {
Thread.sleep(100);
} catch (Throwable e) {
}
observer.onError(new NullPointerException());
return;
} else {
observer.onNext(s);
}
}
System.out.println("subscription complete");
observer.onComplete();
}
});
t.start();
}
}
}