| 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 |
import java.util.concurrent.ExecutionException; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.*; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 27 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 28 |
import io.reactivex.rxjava3.subscribers.*; |
| 29 |
import io.reactivex.rxjava3.testsupport.*; |
| 30 |
|
| 31 |
public class FlowableMaterializeTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void materialize1() { |
| 35 |
// null will cause onError to be triggered before "three" can be |
| 36 |
// returned |
| 37 |
final TestAsyncErrorObservable o1 = new TestAsyncErrorObservable("one", "two", null, |
| 38 |
"three"); |
| 39 |
|
| 40 |
TestNotificationSubscriber observer = new TestNotificationSubscriber(); |
| 41 |
Flowable<Notification<String>> m = Flowable.unsafeCreate(o1).materialize(); |
| 42 |
m.subscribe(observer); |
| 43 |
|
| 44 |
try { |
| 45 |
o1.t.join(); |
| 46 |
} catch (InterruptedException e) { |
| 47 |
throw new RuntimeException(e); |
| 48 |
} |
| 49 |
|
| 50 |
assertFalse(observer.onError); |
| 51 |
assertTrue(observer.onComplete); |
| 52 |
assertEquals(3, observer.notifications.size()); |
| 53 |
|
| 54 |
assertTrue(observer.notifications.get(0).isOnNext()); |
| 55 |
assertEquals("one", observer.notifications.get(0).getValue()); |
| 56 |
|
| 57 |
assertTrue(observer.notifications.get(1).isOnNext()); |
| 58 |
assertEquals("two", observer.notifications.get(1).getValue()); |
| 59 |
|
| 60 |
assertTrue(observer.notifications.get(2).isOnError()); |
| 61 |
assertEquals(NullPointerException.class, observer.notifications.get(2).getError().getClass()); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void materialize2() { |
| 66 |
final TestAsyncErrorObservable o1 = new TestAsyncErrorObservable("one", "two", "three"); |
| 67 |
|
| 68 |
TestNotificationSubscriber subscriber = new TestNotificationSubscriber(); |
| 69 |
Flowable<Notification<String>> m = Flowable.unsafeCreate(o1).materialize(); |
| 70 |
m.subscribe(subscriber); |
| 71 |
|
| 72 |
try { |
| 73 |
o1.t.join(); |
| 74 |
} catch (InterruptedException e) { |
| 75 |
throw new RuntimeException(e); |
| 76 |
} |
| 77 |
|
| 78 |
assertFalse(subscriber.onError); |
| 79 |
assertTrue(subscriber.onComplete); |
| 80 |
assertEquals(4, subscriber.notifications.size()); |
| 81 |
assertTrue(subscriber.notifications.get(0).isOnNext()); |
| 82 |
assertEquals("one", subscriber.notifications.get(0).getValue()); |
| 83 |
|
| 84 |
assertTrue(subscriber.notifications.get(1).isOnNext()); |
| 85 |
assertEquals("two", subscriber.notifications.get(1).getValue()); |
| 86 |
|
| 87 |
assertTrue(subscriber.notifications.get(2).isOnNext()); |
| 88 |
assertEquals("three", subscriber.notifications.get(2).getValue()); |
| 89 |
|
| 90 |
assertTrue(subscriber.notifications.get(3).isOnComplete()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void multipleSubscribes() throws InterruptedException, ExecutionException { |
| 95 |
final TestAsyncErrorObservable o = new TestAsyncErrorObservable("one", "two", null, "three"); |
| 96 |
|
| 97 |
Flowable<Notification<String>> m = Flowable.unsafeCreate(o).materialize(); |
| 98 |
|
| 99 |
assertEquals(3, m.toList().toFuture().get().size()); |
| 100 |
assertEquals(3, m.toList().toFuture().get().size()); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void backpressureOnEmptyStream() { |
| 105 |
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<>(0L); |
| 106 |
Flowable.<Integer> empty().materialize().subscribe(ts); |
| 107 |
ts.assertNoValues(); |
| 108 |
ts.request(1); |
| 109 |
ts.assertValueCount(1); |
| 110 |
assertTrue(ts.values().get(0).isOnComplete()); |
| 111 |
ts.assertComplete(); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void backpressureNoError() { |
| 116 |
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<>(0L); |
| 117 |
Flowable.just(1, 2, 3).materialize().subscribe(ts); |
| 118 |
ts.assertNoValues(); |
| 119 |
ts.request(1); |
| 120 |
ts.assertValueCount(1); |
| 121 |
ts.request(2); |
| 122 |
ts.assertValueCount(3); |
| 123 |
ts.request(1); |
| 124 |
ts.assertValueCount(4); |
| 125 |
ts.assertComplete(); |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
public void backpressureNoErrorAsync() throws InterruptedException { |
| 130 |
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<>(0L); |
| 131 |
Flowable.just(1, 2, 3) |
| 132 |
.materialize() |
| 133 |
.subscribeOn(Schedulers.computation()) |
| 134 |
.subscribe(ts); |
| 135 |
Thread.sleep(100); |
| 136 |
ts.assertNoValues(); |
| 137 |
ts.request(1); |
| 138 |
Thread.sleep(100); |
| 139 |
ts.assertValueCount(1); |
| 140 |
ts.request(2); |
| 141 |
Thread.sleep(100); |
| 142 |
ts.assertValueCount(3); |
| 143 |
ts.request(1); |
| 144 |
Thread.sleep(100); |
| 145 |
ts.assertValueCount(4); |
| 146 |
ts.assertComplete(); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void backpressureWithError() { |
| 151 |
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<>(0L); |
| 152 |
Flowable.<Integer> error(new IllegalArgumentException()).materialize().subscribe(ts); |
| 153 |
ts.assertNoValues(); |
| 154 |
ts.request(1); |
| 155 |
ts.assertValueCount(1); |
| 156 |
ts.assertComplete(); |
| 157 |
} |
| 158 |
|
| 159 |
@Test |
| 160 |
public void backpressureWithEmissionThenError() { |
| 161 |
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<>(0L); |
| 162 |
IllegalArgumentException ex = new IllegalArgumentException(); |
| 163 |
Flowable.fromIterable(Arrays.asList(1)).concatWith(Flowable.<Integer> error(ex)).materialize() |
| 164 |
.subscribe(ts); |
| 165 |
ts.assertNoValues(); |
| 166 |
ts.request(1); |
| 167 |
ts.assertValueCount(1); |
| 168 |
assertTrue(ts.values().get(0).isOnNext()); |
| 169 |
ts.request(1); |
| 170 |
ts.assertValueCount(2); |
| 171 |
assertTrue(ts.values().get(1).isOnError()); |
| 172 |
assertEquals(ex, ts.values().get(1).getError()); |
| 173 |
ts.assertComplete(); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void withCompletionCausingError() { |
| 178 |
TestSubscriberEx<Notification<Integer>> ts = new TestSubscriberEx<>(); |
| 179 |
final RuntimeException ex = new RuntimeException("boo"); |
| 180 |
Flowable.<Integer>empty().materialize().doOnNext(new Consumer<Object>() { |
| 181 |
@Override |
| 182 |
public void accept(Object t) { |
| 183 |
throw ex; |
| 184 |
} |
| 185 |
}).subscribe(ts); |
| 186 |
ts.assertError(ex); |
| 187 |
ts.assertNoValues(); |
| 188 |
ts.assertTerminated(); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void unsubscribeJustBeforeCompletionNotificationShouldPreventThatNotificationArriving() { |
| 193 |
TestSubscriber<Notification<Integer>> ts = new TestSubscriber<>(0L); |
| 194 |
|
| 195 |
Flowable.<Integer>empty().materialize() |
| 196 |
.subscribe(ts); |
| 197 |
ts.assertNoValues(); |
| 198 |
ts.cancel(); |
| 199 |
ts.request(1); |
| 200 |
ts.assertNoValues(); |
| 201 |
} |
| 202 |
|
| 203 |
private static class TestNotificationSubscriber extends DefaultSubscriber<Notification<String>> { |
| 204 |
|
| 205 |
boolean onComplete; |
| 206 |
boolean onError; |
| 207 |
List<Notification<String>> notifications = new Vector<>(); |
| 208 |
|
| 209 |
@Override |
| 210 |
public void onComplete() { |
| 211 |
this.onComplete = true; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public void onError(Throwable e) { |
| 216 |
this.onError = true; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public void onNext(Notification<String> value) { |
| 221 |
this.notifications.add(value); |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
private static class TestAsyncErrorObservable implements Publisher<String> { |
| 227 |
|
| 228 |
String[] valuesToReturn; |
| 229 |
|
| 230 |
TestAsyncErrorObservable(String... values) { |
| 231 |
valuesToReturn = values; |
| 232 |
} |
| 233 |
|
| 234 |
volatile Thread t; |
| 235 |
|
| 236 |
@Override |
| 237 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 238 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 239 |
t = new Thread(new Runnable() { |
| 240 |
|
| 241 |
@Override |
| 242 |
public void run() { |
| 243 |
for (String s : valuesToReturn) { |
| 244 |
if (s == null) { |
| 245 |
System.out.println("throwing exception"); |
| 246 |
try { |
| 247 |
Thread.sleep(100); |
| 248 |
} catch (Throwable e) { |
| 249 |
|
| 250 |
} |
| 251 |
subscriber.onError(new NullPointerException()); |
| 252 |
return; |
| 253 |
} else { |
| 254 |
subscriber.onNext(s); |
| 255 |
} |
| 256 |
} |
| 257 |
System.out.println("subscription complete"); |
| 258 |
subscriber.onComplete(); |
| 259 |
} |
| 260 |
|
| 261 |
}); |
| 262 |
t.start(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
@Test |
| 267 |
public void backpressure() { |
| 268 |
TestSubscriber<Notification<Integer>> ts = Flowable.range(1, 5).materialize().test(0); |
| 269 |
|
| 270 |
ts.assertEmpty(); |
| 271 |
|
| 272 |
ts.request(5); |
| 273 |
|
| 274 |
ts.assertValueCount(5) |
| 275 |
.assertNoErrors() |
| 276 |
.assertNotComplete(); |
| 277 |
|
| 278 |
ts.request(1); |
| 279 |
|
| 280 |
ts.assertValueCount(6) |
| 281 |
.assertNoErrors() |
| 282 |
.assertComplete(); |
| 283 |
} |
| 284 |
|
| 285 |
@Test |
| 286 |
public void dispose() { |
| 287 |
TestHelper.checkDisposed(Flowable.just(1).materialize()); |
| 288 |
} |
| 289 |
|
| 290 |
@Test |
| 291 |
public void doubleOnSubscribe() { |
| 292 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Notification<Object>>>() { |
| 293 |
@Override |
| 294 |
public Flowable<Notification<Object>> apply(Flowable<Object> f) throws Exception { |
| 295 |
return f.materialize(); |
| 296 |
} |
| 297 |
}); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void badSource() { |
| 302 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() { |
| 303 |
@Override |
| 304 |
public Object apply(Flowable<Object> f) throws Exception { |
| 305 |
return f.materialize(); |
| 306 |
} |
| 307 |
}, false, null, null, Notification.createOnComplete()); |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void badRequest() { |
| 312 |
TestHelper.assertBadRequestReported(Flowable.just(1).materialize()); |
| 313 |
} |
| 314 |
} |