| 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 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.*; |
| 25 |
import org.mockito.InOrder; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.core.*; |
| 29 |
import io.reactivex.rxjava3.exceptions.*; |
| 30 |
import io.reactivex.rxjava3.functions.*; |
| 31 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 32 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 33 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 34 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 35 |
import io.reactivex.rxjava3.processors.*; |
| 36 |
import io.reactivex.rxjava3.schedulers.*; |
| 37 |
import io.reactivex.rxjava3.subscribers.*; |
| 38 |
import io.reactivex.rxjava3.testsupport.*; |
| 39 |
|
| 40 |
public class FlowableSwitchTest extends RxJavaTest { |
| 41 |
|
| 42 |
private TestScheduler scheduler; |
| 43 |
private Scheduler.Worker innerScheduler; |
| 44 |
private Subscriber<String> subscriber; |
| 45 |
|
| 46 |
@Before |
| 47 |
public void before() { |
| 48 |
scheduler = new TestScheduler(); |
| 49 |
innerScheduler = scheduler.createWorker(); |
| 50 |
subscriber = TestHelper.mockSubscriber(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void switchWhenOuterCompleteBeforeInner() { |
| 55 |
Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 56 |
@Override |
| 57 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 58 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 59 |
publishNext(subscriber, 50, Flowable.unsafeCreate(new Publisher<String>() { |
| 60 |
@Override |
| 61 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 62 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 63 |
publishNext(subscriber, 70, "one"); |
| 64 |
publishNext(subscriber, 100, "two"); |
| 65 |
publishCompleted(subscriber, 200); |
| 66 |
} |
| 67 |
})); |
| 68 |
publishCompleted(subscriber, 60); |
| 69 |
} |
| 70 |
}); |
| 71 |
|
| 72 |
Flowable<String> sampled = Flowable.switchOnNext(source); |
| 73 |
sampled.subscribe(subscriber); |
| 74 |
|
| 75 |
InOrder inOrder = inOrder(subscriber); |
| 76 |
|
| 77 |
scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS); |
| 78 |
inOrder.verify(subscriber, times(2)).onNext(anyString()); |
| 79 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void switchWhenInnerCompleteBeforeOuter() { |
| 84 |
Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 85 |
@Override |
| 86 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 87 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 88 |
publishNext(subscriber, 10, Flowable.unsafeCreate(new Publisher<String>() { |
| 89 |
@Override |
| 90 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 91 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 92 |
publishNext(subscriber, 0, "one"); |
| 93 |
publishNext(subscriber, 10, "two"); |
| 94 |
publishCompleted(subscriber, 20); |
| 95 |
} |
| 96 |
})); |
| 97 |
|
| 98 |
publishNext(subscriber, 100, Flowable.unsafeCreate(new Publisher<String>() { |
| 99 |
@Override |
| 100 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 101 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 102 |
publishNext(subscriber, 0, "three"); |
| 103 |
publishNext(subscriber, 10, "four"); |
| 104 |
publishCompleted(subscriber, 20); |
| 105 |
} |
| 106 |
})); |
| 107 |
publishCompleted(subscriber, 200); |
| 108 |
} |
| 109 |
}); |
| 110 |
|
| 111 |
Flowable<String> sampled = Flowable.switchOnNext(source); |
| 112 |
sampled.subscribe(subscriber); |
| 113 |
|
| 114 |
InOrder inOrder = inOrder(subscriber); |
| 115 |
|
| 116 |
scheduler.advanceTimeTo(150, TimeUnit.MILLISECONDS); |
| 117 |
inOrder.verify(subscriber, never()).onComplete(); |
| 118 |
inOrder.verify(subscriber, times(1)).onNext("one"); |
| 119 |
inOrder.verify(subscriber, times(1)).onNext("two"); |
| 120 |
inOrder.verify(subscriber, times(1)).onNext("three"); |
| 121 |
inOrder.verify(subscriber, times(1)).onNext("four"); |
| 122 |
|
| 123 |
scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS); |
| 124 |
inOrder.verify(subscriber, never()).onNext(anyString()); |
| 125 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
public void switchWithComplete() { |
| 130 |
Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 131 |
@Override |
| 132 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 133 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 134 |
publishNext(subscriber, 50, Flowable.unsafeCreate(new Publisher<String>() { |
| 135 |
@Override |
| 136 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 137 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 138 |
publishNext(subscriber, 60, "one"); |
| 139 |
publishNext(subscriber, 100, "two"); |
| 140 |
} |
| 141 |
})); |
| 142 |
|
| 143 |
publishNext(subscriber, 200, Flowable.unsafeCreate(new Publisher<String>() { |
| 144 |
@Override |
| 145 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 146 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 147 |
publishNext(subscriber, 0, "three"); |
| 148 |
publishNext(subscriber, 100, "four"); |
| 149 |
} |
| 150 |
})); |
| 151 |
|
| 152 |
publishCompleted(subscriber, 250); |
| 153 |
} |
| 154 |
}); |
| 155 |
|
| 156 |
Flowable<String> sampled = Flowable.switchOnNext(source); |
| 157 |
sampled.subscribe(subscriber); |
| 158 |
|
| 159 |
InOrder inOrder = inOrder(subscriber); |
| 160 |
|
| 161 |
scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS); |
| 162 |
inOrder.verify(subscriber, never()).onNext(anyString()); |
| 163 |
verify(subscriber, never()).onComplete(); |
| 164 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 165 |
|
| 166 |
scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS); |
| 167 |
inOrder.verify(subscriber, times(1)).onNext("one"); |
| 168 |
verify(subscriber, never()).onComplete(); |
| 169 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 170 |
|
| 171 |
scheduler.advanceTimeTo(175, TimeUnit.MILLISECONDS); |
| 172 |
inOrder.verify(subscriber, times(1)).onNext("two"); |
| 173 |
verify(subscriber, never()).onComplete(); |
| 174 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 175 |
|
| 176 |
scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS); |
| 177 |
inOrder.verify(subscriber, times(1)).onNext("three"); |
| 178 |
verify(subscriber, never()).onComplete(); |
| 179 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 180 |
|
| 181 |
scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS); |
| 182 |
inOrder.verify(subscriber, times(1)).onNext("four"); |
| 183 |
verify(subscriber, never()).onComplete(); |
| 184 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void switchWithError() { |
| 189 |
Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 190 |
@Override |
| 191 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 192 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 193 |
publishNext(subscriber, 50, Flowable.unsafeCreate(new Publisher<String>() { |
| 194 |
@Override |
| 195 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 196 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 197 |
publishNext(subscriber, 50, "one"); |
| 198 |
publishNext(subscriber, 100, "two"); |
| 199 |
} |
| 200 |
})); |
| 201 |
|
| 202 |
publishNext(subscriber, 200, Flowable.unsafeCreate(new Publisher<String>() { |
| 203 |
@Override |
| 204 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 205 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 206 |
publishNext(subscriber, 0, "three"); |
| 207 |
publishNext(subscriber, 100, "four"); |
| 208 |
} |
| 209 |
})); |
| 210 |
|
| 211 |
publishError(subscriber, 250, new TestException()); |
| 212 |
} |
| 213 |
}); |
| 214 |
|
| 215 |
Flowable<String> sampled = Flowable.switchOnNext(source); |
| 216 |
sampled.subscribe(subscriber); |
| 217 |
|
| 218 |
InOrder inOrder = inOrder(subscriber); |
| 219 |
|
| 220 |
scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS); |
| 221 |
inOrder.verify(subscriber, never()).onNext(anyString()); |
| 222 |
verify(subscriber, never()).onComplete(); |
| 223 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 224 |
|
| 225 |
scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS); |
| 226 |
inOrder.verify(subscriber, times(1)).onNext("one"); |
| 227 |
verify(subscriber, never()).onComplete(); |
| 228 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 229 |
|
| 230 |
scheduler.advanceTimeTo(175, TimeUnit.MILLISECONDS); |
| 231 |
inOrder.verify(subscriber, times(1)).onNext("two"); |
| 232 |
verify(subscriber, never()).onComplete(); |
| 233 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 234 |
|
| 235 |
scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS); |
| 236 |
inOrder.verify(subscriber, times(1)).onNext("three"); |
| 237 |
verify(subscriber, never()).onComplete(); |
| 238 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 239 |
|
| 240 |
scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS); |
| 241 |
inOrder.verify(subscriber, never()).onNext(anyString()); |
| 242 |
verify(subscriber, never()).onComplete(); |
| 243 |
verify(subscriber, times(1)).onError(any(TestException.class)); |
| 244 |
} |
| 245 |
|
| 246 |
@Test |
| 247 |
public void switchWithSubsequenceComplete() { |
| 248 |
Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 249 |
@Override |
| 250 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 251 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 252 |
publishNext(subscriber, 50, Flowable.unsafeCreate(new Publisher<String>() { |
| 253 |
@Override |
| 254 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 255 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 256 |
publishNext(subscriber, 50, "one"); |
| 257 |
publishNext(subscriber, 100, "two"); |
| 258 |
} |
| 259 |
})); |
| 260 |
|
| 261 |
publishNext(subscriber, 130, Flowable.unsafeCreate(new Publisher<String>() { |
| 262 |
@Override |
| 263 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 264 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 265 |
publishCompleted(subscriber, 0); |
| 266 |
} |
| 267 |
})); |
| 268 |
|
| 269 |
publishNext(subscriber, 150, Flowable.unsafeCreate(new Publisher<String>() { |
| 270 |
@Override |
| 271 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 272 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 273 |
publishNext(subscriber, 50, "three"); |
| 274 |
} |
| 275 |
})); |
| 276 |
} |
| 277 |
}); |
| 278 |
|
| 279 |
Flowable<String> sampled = Flowable.switchOnNext(source); |
| 280 |
sampled.subscribe(subscriber); |
| 281 |
|
| 282 |
InOrder inOrder = inOrder(subscriber); |
| 283 |
|
| 284 |
scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS); |
| 285 |
inOrder.verify(subscriber, never()).onNext(anyString()); |
| 286 |
verify(subscriber, never()).onComplete(); |
| 287 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 288 |
|
| 289 |
scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS); |
| 290 |
inOrder.verify(subscriber, times(1)).onNext("one"); |
| 291 |
verify(subscriber, never()).onComplete(); |
| 292 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 293 |
|
| 294 |
scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS); |
| 295 |
inOrder.verify(subscriber, times(1)).onNext("three"); |
| 296 |
verify(subscriber, never()).onComplete(); |
| 297 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void switchWithSubsequenceError() { |
| 302 |
Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 303 |
@Override |
| 304 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 305 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 306 |
publishNext(subscriber, 50, Flowable.unsafeCreate(new Publisher<String>() { |
| 307 |
@Override |
| 308 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 309 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 310 |
publishNext(subscriber, 50, "one"); |
| 311 |
publishNext(subscriber, 100, "two"); |
| 312 |
} |
| 313 |
})); |
| 314 |
|
| 315 |
publishNext(subscriber, 130, Flowable.unsafeCreate(new Publisher<String>() { |
| 316 |
@Override |
| 317 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 318 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 319 |
publishError(subscriber, 0, new TestException()); |
| 320 |
} |
| 321 |
})); |
| 322 |
|
| 323 |
publishNext(subscriber, 150, Flowable.unsafeCreate(new Publisher<String>() { |
| 324 |
@Override |
| 325 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 326 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 327 |
publishNext(subscriber, 50, "three"); |
| 328 |
} |
| 329 |
})); |
| 330 |
|
| 331 |
} |
| 332 |
}); |
| 333 |
|
| 334 |
Flowable<String> sampled = Flowable.switchOnNext(source); |
| 335 |
sampled.subscribe(subscriber); |
| 336 |
|
| 337 |
InOrder inOrder = inOrder(subscriber); |
| 338 |
|
| 339 |
scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS); |
| 340 |
inOrder.verify(subscriber, never()).onNext(anyString()); |
| 341 |
verify(subscriber, never()).onComplete(); |
| 342 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 343 |
|
| 344 |
scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS); |
| 345 |
inOrder.verify(subscriber, times(1)).onNext("one"); |
| 346 |
verify(subscriber, never()).onComplete(); |
| 347 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 348 |
|
| 349 |
scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS); |
| 350 |
inOrder.verify(subscriber, never()).onNext("three"); |
| 351 |
verify(subscriber, never()).onComplete(); |
| 352 |
verify(subscriber, times(1)).onError(any(TestException.class)); |
| 353 |
} |
| 354 |
|
| 355 |
private <T> void publishCompleted(final Subscriber<T> subscriber, long delay) { |
| 356 |
innerScheduler.schedule(new Runnable() { |
| 357 |
@Override |
| 358 |
public void run() { |
| 359 |
subscriber.onComplete(); |
| 360 |
} |
| 361 |
}, delay, TimeUnit.MILLISECONDS); |
| 362 |
} |
| 363 |
|
| 364 |
private <T> void publishError(final Subscriber<T> subscriber, long delay, final Throwable error) { |
| 365 |
innerScheduler.schedule(new Runnable() { |
| 366 |
@Override |
| 367 |
public void run() { |
| 368 |
subscriber.onError(error); |
| 369 |
} |
| 370 |
}, delay, TimeUnit.MILLISECONDS); |
| 371 |
} |
| 372 |
|
| 373 |
private <T> void publishNext(final Subscriber<T> subscriber, long delay, final T value) { |
| 374 |
innerScheduler.schedule(new Runnable() { |
| 375 |
@Override |
| 376 |
public void run() { |
| 377 |
subscriber.onNext(value); |
| 378 |
} |
| 379 |
}, delay, TimeUnit.MILLISECONDS); |
| 380 |
} |
| 381 |
|
| 382 |
@Test |
| 383 |
public void switchIssue737() { |
| 384 |
// https://github.com/ReactiveX/RxJava/issues/737 |
| 385 |
Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { |
| 386 |
@Override |
| 387 |
public void subscribe(Subscriber<? super Flowable<String>> subscriber) { |
| 388 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 389 |
publishNext(subscriber, 0, Flowable.unsafeCreate(new Publisher<String>() { |
| 390 |
@Override |
| 391 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 392 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 393 |
publishNext(subscriber, 10, "1-one"); |
| 394 |
publishNext(subscriber, 20, "1-two"); |
| 395 |
// The following events will be ignored |
| 396 |
publishNext(subscriber, 30, "1-three"); |
| 397 |
publishCompleted(subscriber, 40); |
| 398 |
} |
| 399 |
})); |
| 400 |
publishNext(subscriber, 25, Flowable.unsafeCreate(new Publisher<String>() { |
| 401 |
@Override |
| 402 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 403 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 404 |
publishNext(subscriber, 10, "2-one"); |
| 405 |
publishNext(subscriber, 20, "2-two"); |
| 406 |
publishNext(subscriber, 30, "2-three"); |
| 407 |
publishCompleted(subscriber, 40); |
| 408 |
} |
| 409 |
})); |
| 410 |
publishCompleted(subscriber, 30); |
| 411 |
} |
| 412 |
}); |
| 413 |
|
| 414 |
Flowable<String> sampled = Flowable.switchOnNext(source); |
| 415 |
sampled.subscribe(subscriber); |
| 416 |
|
| 417 |
scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS); |
| 418 |
|
| 419 |
InOrder inOrder = inOrder(subscriber); |
| 420 |
inOrder.verify(subscriber, times(1)).onNext("1-one"); |
| 421 |
inOrder.verify(subscriber, times(1)).onNext("1-two"); |
| 422 |
inOrder.verify(subscriber, times(1)).onNext("2-one"); |
| 423 |
inOrder.verify(subscriber, times(1)).onNext("2-two"); |
| 424 |
inOrder.verify(subscriber, times(1)).onNext("2-three"); |
| 425 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 426 |
inOrder.verifyNoMoreInteractions(); |
| 427 |
} |
| 428 |
|
| 429 |
@Test |
| 430 |
public void backpressure() { |
| 431 |
|
| 432 |
PublishProcessor<String> o1 = PublishProcessor.create(); |
| 433 |
PublishProcessor<String> o2 = PublishProcessor.create(); |
| 434 |
PublishProcessor<String> o3 = PublishProcessor.create(); |
| 435 |
|
| 436 |
PublishProcessor<PublishProcessor<String>> o = PublishProcessor.create(); |
| 437 |
|
| 438 |
publishNext(o, 0, o1); |
| 439 |
publishNext(o, 5, o2); |
| 440 |
publishNext(o, 10, o3); |
| 441 |
publishCompleted(o, 15); |
| 442 |
|
| 443 |
for (int i = 0; i < 10; i++) { |
| 444 |
publishNext(o1, i * 5, "a" + (i + 1)); |
| 445 |
publishNext(o2, 5 + i * 5, "b" + (i + 1)); |
| 446 |
publishNext(o3, 10 + i * 5, "c" + (i + 1)); |
| 447 |
} |
| 448 |
|
| 449 |
publishCompleted(o1, 45); |
| 450 |
publishCompleted(o2, 50); |
| 451 |
publishCompleted(o3, 55); |
| 452 |
|
| 453 |
final TestSubscriberEx<String> testSubscriber = new TestSubscriberEx<>(); |
| 454 |
Flowable.switchOnNext(o).subscribe(new DefaultSubscriber<String>() { |
| 455 |
|
| 456 |
private int requested; |
| 457 |
|
| 458 |
@Override |
| 459 |
public void onStart() { |
| 460 |
requested = 3; |
| 461 |
request(3); |
| 462 |
testSubscriber.onSubscribe(new BooleanSubscription()); |
| 463 |
} |
| 464 |
|
| 465 |
@Override |
| 466 |
public void onComplete() { |
| 467 |
testSubscriber.onComplete(); |
| 468 |
} |
| 469 |
|
| 470 |
@Override |
| 471 |
public void onError(Throwable e) { |
| 472 |
testSubscriber.onError(e); |
| 473 |
} |
| 474 |
|
| 475 |
@Override |
| 476 |
public void onNext(String s) { |
| 477 |
testSubscriber.onNext(s); |
| 478 |
requested--; |
| 479 |
if (requested == 0) { |
| 480 |
requested = 3; |
| 481 |
request(3); |
| 482 |
} |
| 483 |
} |
| 484 |
}); |
| 485 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 486 |
testSubscriber.assertValues("a1", "b1", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10"); |
| 487 |
testSubscriber.assertNoErrors(); |
| 488 |
testSubscriber.assertTerminated(); |
| 489 |
} |
| 490 |
|
| 491 |
@Test |
| 492 |
public void unsubscribe() { |
| 493 |
final AtomicBoolean isUnsubscribed = new AtomicBoolean(); |
| 494 |
Flowable.switchOnNext( |
| 495 |
Flowable.unsafeCreate(new Publisher<Flowable<Integer>>() { |
| 496 |
@Override |
| 497 |
public void subscribe(final Subscriber<? super Flowable<Integer>> subscriber) { |
| 498 |
BooleanSubscription bs = new BooleanSubscription(); |
| 499 |
subscriber.onSubscribe(bs); |
| 500 |
subscriber.onNext(Flowable.just(1)); |
| 501 |
isUnsubscribed.set(bs.isCancelled()); |
| 502 |
} |
| 503 |
}) |
| 504 |
).take(1).subscribe(); |
| 505 |
assertTrue("Switch doesn't propagate 'unsubscribe'", isUnsubscribed.get()); |
| 506 |
} |
| 507 |
/** The upstream producer hijacked the switch producer stopping the requests aimed at the inner observables. */ |
| 508 |
@Test |
| 509 |
public void issue2654() { |
| 510 |
Flowable<String> oneItem = Flowable.just("Hello").mergeWith(Flowable.<String>never()); |
| 511 |
|
| 512 |
Flowable<String> src = oneItem.switchMap(new Function<String, Flowable<String>>() { |
| 513 |
@Override |
| 514 |
public Flowable<String> apply(final String s) { |
| 515 |
return Flowable.just(s) |
| 516 |
.mergeWith(Flowable.interval(10, TimeUnit.MILLISECONDS) |
| 517 |
.map(new Function<Long, String>() { |
| 518 |
@Override |
| 519 |
public String apply(Long i) { |
| 520 |
return s + " " + i; |
| 521 |
} |
| 522 |
})).take(250); |
| 523 |
} |
| 524 |
}) |
| 525 |
.share() |
| 526 |
; |
| 527 |
|
| 528 |
TestSubscriberEx<String> ts = new TestSubscriberEx<String>() { |
| 529 |
@Override |
| 530 |
public void onNext(String t) { |
| 531 |
super.onNext(t); |
| 532 |
if (values().size() == 250) { |
| 533 |
onComplete(); |
| 534 |
dispose(); |
| 535 |
} |
| 536 |
} |
| 537 |
}; |
| 538 |
src.subscribe(ts); |
| 539 |
|
| 540 |
ts.awaitDone(10, TimeUnit.SECONDS); |
| 541 |
|
| 542 |
System.out.println("> testIssue2654: " + ts.values().size()); |
| 543 |
|
| 544 |
ts.assertTerminated(); |
| 545 |
ts.assertNoErrors(); |
| 546 |
|
| 547 |
Assert.assertEquals(250, ts.values().size()); |
| 548 |
} |
| 549 |
|
| 550 |
@Test |
| 551 |
public void initialRequestsAreAdditive() { |
| 552 |
TestSubscriber<Long> ts = new TestSubscriber<>(0L); |
| 553 |
Flowable.switchOnNext( |
| 554 |
Flowable.interval(100, TimeUnit.MILLISECONDS) |
| 555 |
.map( |
| 556 |
new Function<Long, Flowable<Long>>() { |
| 557 |
@Override |
| 558 |
public Flowable<Long> apply(Long t) { |
| 559 |
return Flowable.just(1L, 2L, 3L); |
| 560 |
} |
| 561 |
} |
| 562 |
).take(3)) |
| 563 |
.subscribe(ts); |
| 564 |
ts.request(Long.MAX_VALUE - 100); |
| 565 |
ts.request(1); |
| 566 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 567 |
} |
| 568 |
|
| 569 |
@Test |
| 570 |
public void initialRequestsDontOverflow() { |
| 571 |
TestSubscriber<Long> ts = new TestSubscriber<>(0L); |
| 572 |
Flowable.switchOnNext( |
| 573 |
Flowable.interval(100, TimeUnit.MILLISECONDS) |
| 574 |
.map(new Function<Long, Flowable<Long>>() { |
| 575 |
@Override |
| 576 |
public Flowable<Long> apply(Long t) { |
| 577 |
return Flowable.fromIterable(Arrays.asList(1L, 2L, 3L)).hide(); |
| 578 |
} |
| 579 |
}).take(3)).subscribe(ts); |
| 580 |
ts.request(Long.MAX_VALUE - 1); |
| 581 |
ts.request(2); |
| 582 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 583 |
assertTrue(ts.values().size() > 0); |
| 584 |
} |
| 585 |
|
| 586 |
@Test |
| 587 |
public void secondaryRequestsDontOverflow() throws InterruptedException { |
| 588 |
TestSubscriber<Long> ts = new TestSubscriber<>(0L); |
| 589 |
Flowable.switchOnNext( |
| 590 |
Flowable.interval(100, TimeUnit.MILLISECONDS) |
| 591 |
.map(new Function<Long, Flowable<Long>>() { |
| 592 |
@Override |
| 593 |
public Flowable<Long> apply(Long t) { |
| 594 |
return Flowable.fromIterable(Arrays.asList(1L, 2L, 3L)).hide(); |
| 595 |
} |
| 596 |
}).take(3)).subscribe(ts); |
| 597 |
ts.request(1); |
| 598 |
//we will miss two of the first observable |
| 599 |
Thread.sleep(250); |
| 600 |
ts.request(Long.MAX_VALUE - 1); |
| 601 |
ts.request(Long.MAX_VALUE - 1); |
| 602 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 603 |
ts.assertValueCount(7); |
| 604 |
} |
| 605 |
|
| 606 |
@Test |
| 607 |
public void delayErrors() { |
| 608 |
PublishProcessor<Publisher<Integer>> source = PublishProcessor.create(); |
| 609 |
|
| 610 |
TestSubscriberEx<Integer> ts = source.switchMapDelayError(Functions.<Publisher<Integer>>identity()) |
| 611 |
.to(TestHelper.<Integer>testConsumer()); |
| 612 |
|
| 613 |
ts.assertNoValues() |
| 614 |
.assertNoErrors() |
| 615 |
.assertNotComplete(); |
| 616 |
|
| 617 |
source.onNext(Flowable.just(1)); |
| 618 |
|
| 619 |
source.onNext(Flowable.<Integer>error(new TestException("Forced failure 1"))); |
| 620 |
|
| 621 |
source.onNext(Flowable.just(2, 3, 4)); |
| 622 |
|
| 623 |
source.onNext(Flowable.<Integer>error(new TestException("Forced failure 2"))); |
| 624 |
|
| 625 |
source.onNext(Flowable.just(5)); |
| 626 |
|
| 627 |
source.onError(new TestException("Forced failure 3")); |
| 628 |
|
| 629 |
ts.assertValues(1, 2, 3, 4, 5) |
| 630 |
.assertNotComplete() |
| 631 |
.assertError(CompositeException.class); |
| 632 |
|
| 633 |
List<Throwable> errors = ExceptionHelper.flatten(ts.errors().get(0)); |
| 634 |
|
| 635 |
TestHelper.assertError(errors, 0, TestException.class, "Forced failure 1"); |
| 636 |
TestHelper.assertError(errors, 1, TestException.class, "Forced failure 2"); |
| 637 |
TestHelper.assertError(errors, 2, TestException.class, "Forced failure 3"); |
| 638 |
} |
| 639 |
|
| 640 |
@Test |
| 641 |
public void switchOnNextPrefetch() { |
| 642 |
final List<Integer> list = new ArrayList<>(); |
| 643 |
|
| 644 |
Flowable<Integer> source = Flowable.range(1, 10).hide().doOnNext(new Consumer<Integer>() { |
| 645 |
@Override |
| 646 |
public void accept(Integer v) throws Exception { |
| 647 |
list.add(v); |
| 648 |
} |
| 649 |
}); |
| 650 |
|
| 651 |
Flowable.switchOnNext(Flowable.just(source).hide(), 2) |
| 652 |
.test(1); |
| 653 |
|
| 654 |
assertEquals(Arrays.asList(1, 2, 3), list); |
| 655 |
} |
| 656 |
|
| 657 |
@Test |
| 658 |
public void switchOnNextDelayError() { |
| 659 |
final List<Integer> list = new ArrayList<>(); |
| 660 |
|
| 661 |
Flowable<Integer> source = Flowable.range(1, 10).hide().doOnNext(new Consumer<Integer>() { |
| 662 |
@Override |
| 663 |
public void accept(Integer v) throws Exception { |
| 664 |
list.add(v); |
| 665 |
} |
| 666 |
}); |
| 667 |
|
| 668 |
Flowable.switchOnNextDelayError(Flowable.just(source).hide()) |
| 669 |
.test(1); |
| 670 |
|
| 671 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), list); |
| 672 |
} |
| 673 |
|
| 674 |
@Test |
| 675 |
public void switchOnNextDelayErrorPrefetch() { |
| 676 |
final List<Integer> list = new ArrayList<>(); |
| 677 |
|
| 678 |
Flowable<Integer> source = Flowable.range(1, 10).hide().doOnNext(new Consumer<Integer>() { |
| 679 |
@Override |
| 680 |
public void accept(Integer v) throws Exception { |
| 681 |
list.add(v); |
| 682 |
} |
| 683 |
}); |
| 684 |
|
| 685 |
Flowable.switchOnNextDelayError(Flowable.just(source).hide(), 2) |
| 686 |
.test(1); |
| 687 |
|
| 688 |
assertEquals(Arrays.asList(1, 2, 3), list); |
| 689 |
} |
| 690 |
|
| 691 |
@Test |
| 692 |
public void switchOnNextDelayErrorWithError() { |
| 693 |
PublishProcessor<Flowable<Integer>> pp = PublishProcessor.create(); |
| 694 |
|
| 695 |
TestSubscriber<Integer> ts = Flowable.switchOnNextDelayError(pp).test(); |
| 696 |
|
| 697 |
pp.onNext(Flowable.just(1)); |
| 698 |
pp.onNext(Flowable.<Integer>error(new TestException())); |
| 699 |
pp.onNext(Flowable.range(2, 4)); |
| 700 |
pp.onComplete(); |
| 701 |
|
| 702 |
ts.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 703 |
} |
| 704 |
|
| 705 |
@Test |
| 706 |
public void switchOnNextDelayErrorBufferSize() { |
| 707 |
PublishProcessor<Flowable<Integer>> pp = PublishProcessor.create(); |
| 708 |
|
| 709 |
TestSubscriber<Integer> ts = Flowable.switchOnNextDelayError(pp, 2).test(); |
| 710 |
|
| 711 |
pp.onNext(Flowable.just(1)); |
| 712 |
pp.onNext(Flowable.range(2, 4)); |
| 713 |
pp.onComplete(); |
| 714 |
|
| 715 |
ts.assertResult(1, 2, 3, 4, 5); |
| 716 |
} |
| 717 |
|
| 718 |
@Test |
| 719 |
public void switchMapDelayErrorEmptySource() { |
| 720 |
assertSame(Flowable.empty(), Flowable.<Object>empty() |
| 721 |
.switchMapDelayError(new Function<Object, Publisher<Integer>>() { |
| 722 |
@Override |
| 723 |
public Publisher<Integer> apply(Object v) throws Exception { |
| 724 |
return Flowable.just(1); |
| 725 |
} |
| 726 |
}, 16)); |
| 727 |
} |
| 728 |
|
| 729 |
@Test |
| 730 |
public void switchMapDelayErrorJustSource() { |
| 731 |
Flowable.just(0) |
| 732 |
.switchMapDelayError(new Function<Object, Publisher<Integer>>() { |
| 733 |
@Override |
| 734 |
public Publisher<Integer> apply(Object v) throws Exception { |
| 735 |
return Flowable.just(1); |
| 736 |
} |
| 737 |
}, 16) |
| 738 |
.test() |
| 739 |
.assertResult(1); |
| 740 |
|
| 741 |
} |
| 742 |
|
| 743 |
@Test |
| 744 |
public void switchMapErrorEmptySource() { |
| 745 |
assertSame(Flowable.empty(), Flowable.<Object>empty() |
| 746 |
.switchMap(new Function<Object, Publisher<Integer>>() { |
| 747 |
@Override |
| 748 |
public Publisher<Integer> apply(Object v) throws Exception { |
| 749 |
return Flowable.just(1); |
| 750 |
} |
| 751 |
}, 16)); |
| 752 |
} |
| 753 |
|
| 754 |
@Test |
| 755 |
public void switchMapJustSource() { |
| 756 |
Flowable.just(0) |
| 757 |
.switchMap(new Function<Object, Publisher<Integer>>() { |
| 758 |
@Override |
| 759 |
public Publisher<Integer> apply(Object v) throws Exception { |
| 760 |
return Flowable.just(1); |
| 761 |
} |
| 762 |
}, 16) |
| 763 |
.test() |
| 764 |
.assertResult(1); |
| 765 |
|
| 766 |
} |
| 767 |
|
| 768 |
@Test |
| 769 |
public void switchMapInnerCancelled() { |
| 770 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 771 |
|
| 772 |
TestSubscriber<Integer> ts = Flowable.just(1) |
| 773 |
.switchMap(Functions.justFunction(pp)) |
| 774 |
.test(); |
| 775 |
|
| 776 |
assertTrue(pp.hasSubscribers()); |
| 777 |
|
| 778 |
ts.cancel(); |
| 779 |
|
| 780 |
assertFalse(pp.hasSubscribers()); |
| 781 |
} |
| 782 |
|
| 783 |
@Test |
| 784 |
public void dispose() { |
| 785 |
TestHelper.checkDisposed(Flowable.switchOnNext( |
| 786 |
Flowable.just(Flowable.just(1)).hide())); |
| 787 |
} |
| 788 |
|
| 789 |
@Test |
| 790 |
public void nextSourceErrorRace() { |
| 791 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 792 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 793 |
try { |
| 794 |
|
| 795 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 796 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 797 |
|
| 798 |
pp1.switchMap(new Function<Integer, Flowable<Integer>>() { |
| 799 |
@Override |
| 800 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 801 |
if (v == 1) { |
| 802 |
return pp2; |
| 803 |
} |
| 804 |
return Flowable.never(); |
| 805 |
} |
| 806 |
}) |
| 807 |
.test(); |
| 808 |
|
| 809 |
Runnable r1 = new Runnable() { |
| 810 |
@Override |
| 811 |
public void run() { |
| 812 |
pp1.onNext(2); |
| 813 |
} |
| 814 |
}; |
| 815 |
|
| 816 |
final TestException ex = new TestException(); |
| 817 |
|
| 818 |
Runnable r2 = new Runnable() { |
| 819 |
@Override |
| 820 |
public void run() { |
| 821 |
pp2.onError(ex); |
| 822 |
} |
| 823 |
}; |
| 824 |
|
| 825 |
TestHelper.race(r1, r2); |
| 826 |
|
| 827 |
for (Throwable e : errors) { |
| 828 |
assertTrue(e.toString(), e instanceof TestException); |
| 829 |
} |
| 830 |
} finally { |
| 831 |
RxJavaPlugins.reset(); |
| 832 |
} |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
@Test |
| 837 |
public void outerInnerErrorRace() { |
| 838 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 839 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 840 |
try { |
| 841 |
|
| 842 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 843 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 844 |
|
| 845 |
pp1.switchMap(new Function<Integer, Flowable<Integer>>() { |
| 846 |
@Override |
| 847 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 848 |
if (v == 1) { |
| 849 |
return pp2; |
| 850 |
} |
| 851 |
return Flowable.never(); |
| 852 |
} |
| 853 |
}) |
| 854 |
.test(); |
| 855 |
|
| 856 |
final TestException ex1 = new TestException(); |
| 857 |
|
| 858 |
Runnable r1 = new Runnable() { |
| 859 |
@Override |
| 860 |
public void run() { |
| 861 |
pp1.onError(ex1); |
| 862 |
} |
| 863 |
}; |
| 864 |
|
| 865 |
final TestException ex2 = new TestException(); |
| 866 |
|
| 867 |
Runnable r2 = new Runnable() { |
| 868 |
@Override |
| 869 |
public void run() { |
| 870 |
pp2.onError(ex2); |
| 871 |
} |
| 872 |
}; |
| 873 |
|
| 874 |
TestHelper.race(r1, r2); |
| 875 |
|
| 876 |
for (Throwable e : errors) { |
| 877 |
assertTrue(e.toString(), e instanceof TestException); |
| 878 |
} |
| 879 |
} finally { |
| 880 |
RxJavaPlugins.reset(); |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
@Test |
| 886 |
public void nextCancelRace() { |
| 887 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 888 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 889 |
|
| 890 |
final TestSubscriber<Integer> ts = pp1.switchMap(new Function<Integer, Flowable<Integer>>() { |
| 891 |
@Override |
| 892 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 893 |
return Flowable.never(); |
| 894 |
} |
| 895 |
}) |
| 896 |
.test(); |
| 897 |
|
| 898 |
Runnable r1 = new Runnable() { |
| 899 |
@Override |
| 900 |
public void run() { |
| 901 |
pp1.onNext(2); |
| 902 |
} |
| 903 |
}; |
| 904 |
|
| 905 |
Runnable r2 = new Runnable() { |
| 906 |
@Override |
| 907 |
public void run() { |
| 908 |
ts.cancel(); |
| 909 |
} |
| 910 |
}; |
| 911 |
|
| 912 |
TestHelper.race(r1, r2); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
@Test |
| 917 |
public void mapperThrows() { |
| 918 |
Flowable.just(1).hide() |
| 919 |
.switchMap(new Function<Integer, Flowable<Object>>() { |
| 920 |
@Override |
| 921 |
public Flowable<Object> apply(Integer v) throws Exception { |
| 922 |
throw new TestException(); |
| 923 |
} |
| 924 |
}) |
| 925 |
.test() |
| 926 |
.assertFailure(TestException.class); |
| 927 |
} |
| 928 |
|
| 929 |
@Test |
| 930 |
public void badMainSource() { |
| 931 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 932 |
try { |
| 933 |
new Flowable<Integer>() { |
| 934 |
@Override |
| 935 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 936 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 937 |
subscriber.onComplete(); |
| 938 |
subscriber.onError(new TestException()); |
| 939 |
subscriber.onComplete(); |
| 940 |
} |
| 941 |
} |
| 942 |
.switchMap(Functions.justFunction(Flowable.never())) |
| 943 |
.test() |
| 944 |
.assertResult(); |
| 945 |
|
| 946 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 947 |
} finally { |
| 948 |
RxJavaPlugins.reset(); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
@Test |
| 953 |
public void emptyInner() { |
| 954 |
Flowable.range(1, 5) |
| 955 |
.switchMap(Functions.justFunction(Flowable.empty())) |
| 956 |
.test() |
| 957 |
.assertResult(); |
| 958 |
} |
| 959 |
|
| 960 |
@Test |
| 961 |
public void justInner() { |
| 962 |
Flowable.range(1, 5) |
| 963 |
.switchMap(Functions.justFunction(Flowable.just(1))) |
| 964 |
.test() |
| 965 |
.assertResult(1, 1, 1, 1, 1); |
| 966 |
} |
| 967 |
|
| 968 |
@Test |
| 969 |
public void badInnerSource() { |
| 970 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 971 |
try { |
| 972 |
Flowable.just(1).hide() |
| 973 |
.switchMap(Functions.justFunction(new Flowable<Integer>() { |
| 974 |
@Override |
| 975 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 976 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 977 |
subscriber.onError(new TestException()); |
| 978 |
subscriber.onComplete(); |
| 979 |
subscriber.onError(new TestException()); |
| 980 |
subscriber.onComplete(); |
| 981 |
} |
| 982 |
})) |
| 983 |
.test() |
| 984 |
.assertFailure(TestException.class); |
| 985 |
|
| 986 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 987 |
} finally { |
| 988 |
RxJavaPlugins.reset(); |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
@Test |
| 993 |
public void innerCompletesReentrant() { |
| 994 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 995 |
|
| 996 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 997 |
@Override |
| 998 |
public void onNext(Integer t) { |
| 999 |
super.onNext(t); |
| 1000 |
pp.onComplete(); |
| 1001 |
} |
| 1002 |
}; |
| 1003 |
|
| 1004 |
Flowable.just(1).hide() |
| 1005 |
.switchMap(Functions.justFunction(pp)) |
| 1006 |
.subscribe(ts); |
| 1007 |
|
| 1008 |
pp.onNext(1); |
| 1009 |
|
| 1010 |
ts.assertResult(1); |
| 1011 |
} |
| 1012 |
|
| 1013 |
@Test |
| 1014 |
public void innerErrorsReentrant() { |
| 1015 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1016 |
|
| 1017 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1018 |
@Override |
| 1019 |
public void onNext(Integer t) { |
| 1020 |
super.onNext(t); |
| 1021 |
pp.onError(new TestException()); |
| 1022 |
} |
| 1023 |
}; |
| 1024 |
|
| 1025 |
Flowable.just(1).hide() |
| 1026 |
.switchMap(Functions.justFunction(pp)) |
| 1027 |
.subscribe(ts); |
| 1028 |
|
| 1029 |
pp.onNext(1); |
| 1030 |
|
| 1031 |
ts.assertFailure(TestException.class, 1); |
| 1032 |
} |
| 1033 |
|
| 1034 |
@Test |
| 1035 |
public void scalarMap() { |
| 1036 |
Flowable.switchOnNext(Flowable.just(Flowable.just(1))) |
| 1037 |
.test() |
| 1038 |
.assertResult(1); |
| 1039 |
} |
| 1040 |
|
| 1041 |
@Test |
| 1042 |
public void scalarMapDelayError() { |
| 1043 |
Flowable.switchOnNextDelayError(Flowable.just(Flowable.just(1))) |
| 1044 |
.test() |
| 1045 |
.assertResult(1); |
| 1046 |
} |
| 1047 |
|
| 1048 |
@Test |
| 1049 |
public void scalarXMap() { |
| 1050 |
Flowable.fromCallable(Functions.justCallable(1)) |
| 1051 |
.switchMap(Functions.justFunction(Flowable.just(1))) |
| 1052 |
.test() |
| 1053 |
.assertResult(1); |
| 1054 |
} |
| 1055 |
|
| 1056 |
@Test |
| 1057 |
public void badSource() { |
| 1058 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 1059 |
@Override |
| 1060 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 1061 |
return f.switchMap(Functions.justFunction(Flowable.just(1))); |
| 1062 |
} |
| 1063 |
}, false, 1, 1, 1); |
| 1064 |
} |
| 1065 |
|
| 1066 |
@Test |
| 1067 |
public void innerOverflow() { |
| 1068 |
Flowable.just(1).hide() |
| 1069 |
.switchMap(Functions.justFunction(new Flowable<Integer>() { |
| 1070 |
@Override |
| 1071 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 1072 |
s.onSubscribe(new BooleanSubscription()); |
| 1073 |
for (int i = 0; i < 10; i++) { |
| 1074 |
s.onNext(i); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
}), 8) |
| 1078 |
.test(1L) |
| 1079 |
.assertFailure(MissingBackpressureException.class, 0); |
| 1080 |
} |
| 1081 |
|
| 1082 |
@Test |
| 1083 |
public void drainCancelRace() { |
| 1084 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1085 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1086 |
|
| 1087 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1088 |
|
| 1089 |
Flowable.just(1).hide() |
| 1090 |
.switchMap(Functions.justFunction(pp)) |
| 1091 |
.subscribe(ts); |
| 1092 |
|
| 1093 |
Runnable r1 = new Runnable() { |
| 1094 |
@Override |
| 1095 |
public void run() { |
| 1096 |
ts.cancel(); |
| 1097 |
} |
| 1098 |
}; |
| 1099 |
|
| 1100 |
Runnable r2 = new Runnable() { |
| 1101 |
@Override |
| 1102 |
public void run() { |
| 1103 |
pp.onNext(1); |
| 1104 |
} |
| 1105 |
}; |
| 1106 |
|
| 1107 |
TestHelper.race(r1, r2); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
@Test |
| 1112 |
public void fusedInnerCrash() { |
| 1113 |
Flowable.just(1).hide() |
| 1114 |
.switchMap(Functions.justFunction(Flowable.just(1) |
| 1115 |
.map(new Function<Integer, Object>() { |
| 1116 |
@Override |
| 1117 |
public Object apply(Integer v) throws Exception { |
| 1118 |
throw new TestException(); |
| 1119 |
} |
| 1120 |
}) |
| 1121 |
.compose(TestHelper.<Object>flowableStripBoundary()) |
| 1122 |
) |
| 1123 |
) |
| 1124 |
.test() |
| 1125 |
.assertFailure(TestException.class); |
| 1126 |
} |
| 1127 |
|
| 1128 |
@Test |
| 1129 |
public void innerCancelledOnMainError() { |
| 1130 |
final PublishProcessor<Integer> main = PublishProcessor.create(); |
| 1131 |
final PublishProcessor<Integer> inner = PublishProcessor.create(); |
| 1132 |
|
| 1133 |
TestSubscriber<Integer> ts = main.switchMap(Functions.justFunction(inner)) |
| 1134 |
.test(); |
| 1135 |
|
| 1136 |
assertTrue(main.hasSubscribers()); |
| 1137 |
|
| 1138 |
main.onNext(1); |
| 1139 |
|
| 1140 |
assertTrue(inner.hasSubscribers()); |
| 1141 |
|
| 1142 |
main.onError(new TestException()); |
| 1143 |
|
| 1144 |
assertFalse(inner.hasSubscribers()); |
| 1145 |
|
| 1146 |
ts.assertFailure(TestException.class); |
| 1147 |
} |
| 1148 |
|
| 1149 |
@Test |
| 1150 |
public void fusedBoundary() { |
| 1151 |
String thread = Thread.currentThread().getName(); |
| 1152 |
|
| 1153 |
Flowable.range(1, 10000) |
| 1154 |
.switchMap(new Function<Integer, Flowable<Object>>() { |
| 1155 |
@Override |
| 1156 |
public Flowable<Object> apply(Integer v) |
| 1157 |
throws Exception { |
| 1158 |
return Flowable.just(2).hide() |
| 1159 |
.observeOn(Schedulers.single()) |
| 1160 |
.map(new Function<Integer, Object>() { |
| 1161 |
@Override |
| 1162 |
public Object apply(Integer w) throws Exception { |
| 1163 |
return Thread.currentThread().getName(); |
| 1164 |
} |
| 1165 |
}); |
| 1166 |
} |
| 1167 |
}) |
| 1168 |
.to(TestHelper.<Object>testConsumer()) |
| 1169 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1170 |
.assertNever(thread) |
| 1171 |
.assertNoErrors() |
| 1172 |
.assertComplete(); |
| 1173 |
} |
| 1174 |
|
| 1175 |
@Test |
| 1176 |
public void undeliverableUponCancel() { |
| 1177 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1178 |
try { |
| 1179 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1180 |
|
| 1181 |
Flowable.just(1) |
| 1182 |
.map(new Function<Integer, Integer>() { |
| 1183 |
@Override |
| 1184 |
public Integer apply(Integer v) throws Throwable { |
| 1185 |
ts.cancel(); |
| 1186 |
throw new TestException(); |
| 1187 |
} |
| 1188 |
}) |
| 1189 |
.switchMap(new Function<Integer, Publisher<Integer>>() { |
| 1190 |
@Override |
| 1191 |
public Publisher<Integer> apply(Integer v) throws Throwable { |
| 1192 |
return Flowable.just(v).hide(); |
| 1193 |
} |
| 1194 |
}) |
| 1195 |
.subscribe(ts); |
| 1196 |
|
| 1197 |
ts.assertEmpty(); |
| 1198 |
|
| 1199 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1200 |
} finally { |
| 1201 |
RxJavaPlugins.reset(); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
@Test |
| 1206 |
public void switchMapFusedIterable() { |
| 1207 |
Flowable.range(1, 2) |
| 1208 |
.switchMap(new Function<Integer, Publisher<Integer>>() { |
| 1209 |
@Override |
| 1210 |
public Publisher<Integer> apply(Integer v) |
| 1211 |
throws Throwable { |
| 1212 |
return Flowable.fromIterable(Arrays.asList(v * 10)); |
| 1213 |
} |
| 1214 |
}) |
| 1215 |
.test() |
| 1216 |
.assertResult(10, 20); |
| 1217 |
} |
| 1218 |
|
| 1219 |
@Test |
| 1220 |
public void switchMapHiddenIterable() { |
| 1221 |
Flowable.range(1, 2) |
| 1222 |
.switchMap(new Function<Integer, Publisher<Integer>>() { |
| 1223 |
@Override |
| 1224 |
public Publisher<Integer> apply(Integer v) |
| 1225 |
throws Throwable { |
| 1226 |
return Flowable.fromIterable(Arrays.asList(v * 10)).hide(); |
| 1227 |
} |
| 1228 |
}) |
| 1229 |
.test() |
| 1230 |
.assertResult(10, 20); |
| 1231 |
} |
| 1232 |
|
| 1233 |
@Test |
| 1234 |
public void asyncFusedInner() { |
| 1235 |
Flowable.just(1) |
| 1236 |
.hide() |
| 1237 |
.switchMap(v -> Flowable.fromCallable(() -> 1)) |
| 1238 |
.test() |
| 1239 |
.assertResult(1); |
| 1240 |
} |
| 1241 |
|
| 1242 |
@Test |
| 1243 |
public void innerIgnoresCancelAndErrors() throws Throwable { |
| 1244 |
TestHelper.withErrorTracking(errors -> { |
| 1245 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1246 |
|
| 1247 |
TestSubscriber<Object> ts = pp |
| 1248 |
.switchMap(v -> { |
| 1249 |
if (v == 1) { |
| 1250 |
return Flowable.unsafeCreate(s -> { |
| 1251 |
s.onSubscribe(new BooleanSubscription()); |
| 1252 |
pp.onNext(2); |
| 1253 |
s.onError(new TestException()); |
| 1254 |
}); |
| 1255 |
} |
| 1256 |
return Flowable.never(); |
| 1257 |
}) |
| 1258 |
.test(); |
| 1259 |
|
| 1260 |
pp.onNext(1); |
| 1261 |
|
| 1262 |
ts.assertEmpty(); |
| 1263 |
|
| 1264 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1265 |
}); |
| 1266 |
} |
| 1267 |
|
| 1268 |
@Test |
| 1269 |
public void doubleOnSubscribe() { |
| 1270 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> f.switchMap(v -> Flowable.never())); |
| 1271 |
} |
| 1272 |
|
| 1273 |
@Test |
| 1274 |
public void badRequest() { |
| 1275 |
TestHelper.assertBadRequestReported(Flowable.never().switchMap(v -> Flowable.never())); |
| 1276 |
} |
| 1277 |
|
| 1278 |
@Test |
| 1279 |
public void innerFailed() { |
| 1280 |
BehaviorProcessor.createDefault(Flowable.error(new TestException())) |
| 1281 |
.switchMap(v -> v) |
| 1282 |
.test() |
| 1283 |
.assertFailure(TestException.class) |
| 1284 |
; |
| 1285 |
} |
| 1286 |
|
| 1287 |
@Test |
| 1288 |
public void innerCompleted() { |
| 1289 |
BehaviorProcessor.createDefault(Flowable.empty().hide()) |
| 1290 |
.switchMap(v -> v) |
| 1291 |
.test() |
| 1292 |
.assertEmpty() |
| 1293 |
; |
| 1294 |
} |
| 1295 |
|
| 1296 |
@Test |
| 1297 |
public void innerCompletedBackpressureBoundary() { |
| 1298 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1299 |
|
| 1300 |
TestSubscriber<Integer> ts = BehaviorProcessor.createDefault(pp) |
| 1301 |
.onBackpressureBuffer() |
| 1302 |
.switchMap(v -> v) |
| 1303 |
.test(1L) |
| 1304 |
; |
| 1305 |
|
| 1306 |
ts.assertEmpty(); |
| 1307 |
|
| 1308 |
pp.onNext(1); |
| 1309 |
pp.onComplete(); |
| 1310 |
|
| 1311 |
ts.assertValuesOnly(1); |
| 1312 |
} |
| 1313 |
|
| 1314 |
@Test |
| 1315 |
public void innerCompletedDelayError() { |
| 1316 |
BehaviorProcessor.createDefault(Flowable.empty().hide()) |
| 1317 |
.switchMapDelayError(v -> v) |
| 1318 |
.test() |
| 1319 |
.assertEmpty() |
| 1320 |
; |
| 1321 |
} |
| 1322 |
|
| 1323 |
@Test |
| 1324 |
public void innerCompletedBackpressureBoundaryDelayError() { |
| 1325 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1326 |
|
| 1327 |
TestSubscriber<Integer> ts = BehaviorProcessor.createDefault(pp) |
| 1328 |
.onBackpressureBuffer() |
| 1329 |
.switchMapDelayError(v -> v) |
| 1330 |
.test(1L) |
| 1331 |
; |
| 1332 |
|
| 1333 |
ts.assertEmpty(); |
| 1334 |
|
| 1335 |
pp.onNext(1); |
| 1336 |
pp.onComplete(); |
| 1337 |
|
| 1338 |
ts.assertValuesOnly(1); |
| 1339 |
} |
| 1340 |
|
| 1341 |
@Test |
| 1342 |
public void cancellationShouldTriggerInnerCancellationRace() throws Throwable { |
| 1343 |
AtomicInteger outer = new AtomicInteger(); |
| 1344 |
AtomicInteger inner = new AtomicInteger(); |
| 1345 |
|
| 1346 |
int n = 10_000; |
| 1347 |
for (int i = 0; i < n; i++) { |
| 1348 |
Flowable.<Integer>create(it -> { |
| 1349 |
it.onNext(0); |
| 1350 |
}, BackpressureStrategy.MISSING) |
| 1351 |
.switchMap(v -> createFlowable(inner)) |
| 1352 |
.observeOn(Schedulers.computation()) |
| 1353 |
.doFinally(() -> { |
| 1354 |
outer.incrementAndGet(); |
| 1355 |
}) |
| 1356 |
.take(1) |
| 1357 |
.blockingSubscribe(v -> { }, Throwable::printStackTrace); |
| 1358 |
} |
| 1359 |
|
| 1360 |
Thread.sleep(100); |
| 1361 |
assertEquals(inner.get(), outer.get()); |
| 1362 |
assertEquals(n, inner.get()); |
| 1363 |
} |
| 1364 |
|
| 1365 |
Flowable<Integer> createFlowable(AtomicInteger inner) { |
| 1366 |
return Flowable.<Integer>unsafeCreate(s -> { |
| 1367 |
SerializedSubscriber<Integer> it = new SerializedSubscriber<>(s); |
| 1368 |
it.onSubscribe(new BooleanSubscription()); |
| 1369 |
Schedulers.io().scheduleDirect(() -> { |
| 1370 |
it.onNext(1); |
| 1371 |
}, 0, TimeUnit.MILLISECONDS); |
| 1372 |
Schedulers.io().scheduleDirect(() -> { |
| 1373 |
it.onNext(2); |
| 1374 |
}, 0, TimeUnit.MILLISECONDS); |
| 1375 |
}) |
| 1376 |
.doFinally(() -> { |
| 1377 |
inner.incrementAndGet(); |
| 1378 |
}); |
| 1379 |
} |
| 1380 |
} |