| 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.io.IOException; |
| 19 |
import java.util.*; |
| 20 |
import java.util.concurrent.TimeUnit; |
| 21 |
import java.util.concurrent.atomic.*; |
| 22 |
|
| 23 |
import org.junit.*; |
| 24 |
import org.reactivestreams.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.*; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 30 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.processors.*; |
| 33 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 34 |
import io.reactivex.rxjava3.subscribers.*; |
| 35 |
import io.reactivex.rxjava3.testsupport.*; |
| 36 |
|
| 37 |
public class FlowableWindowWithStartEndFlowableTest extends RxJavaTest { |
| 38 |
|
| 39 |
private TestScheduler scheduler; |
| 40 |
private Scheduler.Worker innerScheduler; |
| 41 |
|
| 42 |
@Before |
| 43 |
public void before() { |
| 44 |
scheduler = new TestScheduler(); |
| 45 |
innerScheduler = scheduler.createWorker(); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void flowableBasedOpenerAndCloser() { |
| 50 |
final List<String> list = new ArrayList<>(); |
| 51 |
final List<List<String>> lists = new ArrayList<>(); |
| 52 |
|
| 53 |
Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() { |
| 54 |
@Override |
| 55 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 56 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 57 |
push(subscriber, "one", 10); |
| 58 |
push(subscriber, "two", 60); |
| 59 |
push(subscriber, "three", 110); |
| 60 |
push(subscriber, "four", 160); |
| 61 |
push(subscriber, "five", 210); |
| 62 |
complete(subscriber, 500); |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
Flowable<Object> openings = Flowable.unsafeCreate(new Publisher<Object>() { |
| 67 |
@Override |
| 68 |
public void subscribe(Subscriber<? super Object> subscriber) { |
| 69 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 70 |
push(subscriber, new Object(), 50); |
| 71 |
push(subscriber, new Object(), 200); |
| 72 |
complete(subscriber, 250); |
| 73 |
} |
| 74 |
}); |
| 75 |
|
| 76 |
Function<Object, Flowable<Object>> closer = new Function<Object, Flowable<Object>>() { |
| 77 |
@Override |
| 78 |
public Flowable<Object> apply(Object opening) { |
| 79 |
return Flowable.unsafeCreate(new Publisher<Object>() { |
| 80 |
@Override |
| 81 |
public void subscribe(Subscriber<? super Object> subscriber) { |
| 82 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 83 |
push(subscriber, new Object(), 100); |
| 84 |
complete(subscriber, 101); |
| 85 |
} |
| 86 |
}); |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
Flowable<Flowable<String>> windowed = source.window(openings, closer); |
| 91 |
windowed.subscribe(observeWindow(list, lists)); |
| 92 |
|
| 93 |
scheduler.advanceTimeTo(500, TimeUnit.MILLISECONDS); |
| 94 |
assertEquals(2, lists.size()); |
| 95 |
assertEquals(lists.get(0), list("two", "three")); |
| 96 |
assertEquals(lists.get(1), list("five")); |
| 97 |
} |
| 98 |
|
| 99 |
private List<String> list(String... args) { |
| 100 |
List<String> list = new ArrayList<>(); |
| 101 |
for (String arg : args) { |
| 102 |
list.add(arg); |
| 103 |
} |
| 104 |
return list; |
| 105 |
} |
| 106 |
|
| 107 |
private <T> void push(final Subscriber<T> subscriber, final T value, int delay) { |
| 108 |
innerScheduler.schedule(new Runnable() { |
| 109 |
@Override |
| 110 |
public void run() { |
| 111 |
subscriber.onNext(value); |
| 112 |
} |
| 113 |
}, delay, TimeUnit.MILLISECONDS); |
| 114 |
} |
| 115 |
|
| 116 |
private void complete(final Subscriber<?> subscriber, int delay) { |
| 117 |
innerScheduler.schedule(new Runnable() { |
| 118 |
@Override |
| 119 |
public void run() { |
| 120 |
subscriber.onComplete(); |
| 121 |
} |
| 122 |
}, delay, TimeUnit.MILLISECONDS); |
| 123 |
} |
| 124 |
|
| 125 |
private Consumer<Flowable<String>> observeWindow(final List<String> list, final List<List<String>> lists) { |
| 126 |
return new Consumer<Flowable<String>>() { |
| 127 |
@Override |
| 128 |
public void accept(Flowable<String> stringFlowable) { |
| 129 |
stringFlowable.subscribe(new DefaultSubscriber<String>() { |
| 130 |
@Override |
| 131 |
public void onComplete() { |
| 132 |
lists.add(new ArrayList<>(list)); |
| 133 |
list.clear(); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void onError(Throwable e) { |
| 138 |
fail(e.getMessage()); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void onNext(String args) { |
| 143 |
list.add(args); |
| 144 |
} |
| 145 |
}); |
| 146 |
} |
| 147 |
}; |
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void noUnsubscribeAndNoLeak() { |
| 152 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 153 |
|
| 154 |
PublishProcessor<Integer> open = PublishProcessor.create(); |
| 155 |
final PublishProcessor<Integer> close = PublishProcessor.create(); |
| 156 |
|
| 157 |
TestSubscriber<Flowable<Integer>> ts = new TestSubscriber<>(); |
| 158 |
|
| 159 |
source.window(open, new Function<Integer, Flowable<Integer>>() { |
| 160 |
@Override |
| 161 |
public Flowable<Integer> apply(Integer t) { |
| 162 |
return close; |
| 163 |
} |
| 164 |
}) |
| 165 |
.doOnNext(new Consumer<Flowable<Integer>>() { |
| 166 |
@Override |
| 167 |
public void accept(Flowable<Integer> w) throws Throwable { |
| 168 |
w.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()); // avoid abandonment |
| 169 |
} |
| 170 |
}) |
| 171 |
.subscribe(ts); |
| 172 |
|
| 173 |
open.onNext(1); |
| 174 |
source.onNext(1); |
| 175 |
|
| 176 |
assertTrue(open.hasSubscribers()); |
| 177 |
assertTrue(close.hasSubscribers()); |
| 178 |
|
| 179 |
close.onNext(1); |
| 180 |
|
| 181 |
assertFalse(close.hasSubscribers()); |
| 182 |
|
| 183 |
source.onComplete(); |
| 184 |
|
| 185 |
ts.assertComplete(); |
| 186 |
ts.assertNoErrors(); |
| 187 |
ts.assertValueCount(1); |
| 188 |
|
| 189 |
assertFalse(ts.isCancelled()); |
| 190 |
assertFalse(open.hasSubscribers()); |
| 191 |
assertFalse(close.hasSubscribers()); |
| 192 |
} |
| 193 |
|
| 194 |
@Test |
| 195 |
public void unsubscribeAll() { |
| 196 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 197 |
|
| 198 |
PublishProcessor<Integer> open = PublishProcessor.create(); |
| 199 |
final PublishProcessor<Integer> close = PublishProcessor.create(); |
| 200 |
|
| 201 |
TestSubscriber<Flowable<Integer>> ts = new TestSubscriber<>(); |
| 202 |
|
| 203 |
source.window(open, new Function<Integer, Flowable<Integer>>() { |
| 204 |
@Override |
| 205 |
public Flowable<Integer> apply(Integer t) { |
| 206 |
return close; |
| 207 |
} |
| 208 |
}) |
| 209 |
.doOnNext(new Consumer<Flowable<Integer>>() { |
| 210 |
@Override |
| 211 |
public void accept(Flowable<Integer> w) throws Throwable { |
| 212 |
w.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()); // avoid abandonment |
| 213 |
} |
| 214 |
}) |
| 215 |
.subscribe(ts); |
| 216 |
|
| 217 |
open.onNext(1); |
| 218 |
|
| 219 |
assertTrue(open.hasSubscribers()); |
| 220 |
assertTrue(close.hasSubscribers()); |
| 221 |
|
| 222 |
ts.cancel(); |
| 223 |
|
| 224 |
// Disposing the outer sequence stops the opening of new windows |
| 225 |
assertFalse(open.hasSubscribers()); |
| 226 |
// FIXME subject has subscribers because of the open window |
| 227 |
assertTrue(close.hasSubscribers()); |
| 228 |
} |
| 229 |
|
| 230 |
@Test |
| 231 |
public void dispose() { |
| 232 |
TestHelper.checkDisposed(Flowable.just(1).window(Flowable.just(2), Functions.justFunction(Flowable.never()))); |
| 233 |
} |
| 234 |
|
| 235 |
@Test |
| 236 |
public void reentrant() { |
| 237 |
final FlowableProcessor<Integer> pp = PublishProcessor.<Integer>create(); |
| 238 |
|
| 239 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 240 |
@Override |
| 241 |
public void onNext(Integer t) { |
| 242 |
super.onNext(t); |
| 243 |
if (t == 1) { |
| 244 |
pp.onNext(2); |
| 245 |
pp.onComplete(); |
| 246 |
} |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
pp.window(BehaviorProcessor.createDefault(1), Functions.justFunction(Flowable.never())) |
| 251 |
.flatMap(new Function<Flowable<Integer>, Flowable<Integer>>() { |
| 252 |
@Override |
| 253 |
public Flowable<Integer> apply(Flowable<Integer> v) throws Exception { |
| 254 |
return v; |
| 255 |
} |
| 256 |
}) |
| 257 |
.subscribe(ts); |
| 258 |
|
| 259 |
pp.onNext(1); |
| 260 |
|
| 261 |
ts |
| 262 |
.awaitDone(1, TimeUnit.SECONDS) |
| 263 |
.assertResult(1, 2); |
| 264 |
} |
| 265 |
|
| 266 |
@Test |
| 267 |
public void boundarySelectorNormal() { |
| 268 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 269 |
PublishProcessor<Integer> start = PublishProcessor.create(); |
| 270 |
final PublishProcessor<Integer> end = PublishProcessor.create(); |
| 271 |
|
| 272 |
TestSubscriber<Integer> ts = source.window(start, new Function<Integer, Flowable<Integer>>() { |
| 273 |
@Override |
| 274 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 275 |
return end; |
| 276 |
} |
| 277 |
}) |
| 278 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 279 |
.test(); |
| 280 |
|
| 281 |
start.onNext(0); |
| 282 |
|
| 283 |
source.onNext(1); |
| 284 |
source.onNext(2); |
| 285 |
source.onNext(3); |
| 286 |
source.onNext(4); |
| 287 |
|
| 288 |
start.onNext(1); |
| 289 |
|
| 290 |
source.onNext(5); |
| 291 |
source.onNext(6); |
| 292 |
|
| 293 |
end.onNext(1); |
| 294 |
|
| 295 |
start.onNext(2); |
| 296 |
|
| 297 |
TestHelper.emit(source, 7, 8); |
| 298 |
|
| 299 |
ts.assertResult(1, 2, 3, 4, 5, 5, 6, 6, 7, 8); |
| 300 |
} |
| 301 |
|
| 302 |
@Test |
| 303 |
public void startError() { |
| 304 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 305 |
PublishProcessor<Integer> start = PublishProcessor.create(); |
| 306 |
final PublishProcessor<Integer> end = PublishProcessor.create(); |
| 307 |
|
| 308 |
TestSubscriber<Integer> ts = source.window(start, new Function<Integer, Flowable<Integer>>() { |
| 309 |
@Override |
| 310 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 311 |
return end; |
| 312 |
} |
| 313 |
}) |
| 314 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 315 |
.test(); |
| 316 |
|
| 317 |
start.onError(new TestException()); |
| 318 |
|
| 319 |
ts.assertFailure(TestException.class); |
| 320 |
|
| 321 |
assertFalse("Source has observers!", source.hasSubscribers()); |
| 322 |
assertFalse("Start has observers!", start.hasSubscribers()); |
| 323 |
assertFalse("End has observers!", end.hasSubscribers()); |
| 324 |
} |
| 325 |
|
| 326 |
@Test |
| 327 |
@SuppressUndeliverable |
| 328 |
public void endError() { |
| 329 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 330 |
PublishProcessor<Integer> start = PublishProcessor.create(); |
| 331 |
final PublishProcessor<Integer> end = PublishProcessor.create(); |
| 332 |
|
| 333 |
TestSubscriber<Integer> ts = source.window(start, new Function<Integer, Flowable<Integer>>() { |
| 334 |
@Override |
| 335 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 336 |
return end; |
| 337 |
} |
| 338 |
}) |
| 339 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 340 |
.test(); |
| 341 |
|
| 342 |
start.onNext(1); |
| 343 |
end.onError(new TestException()); |
| 344 |
|
| 345 |
ts.assertFailure(TestException.class); |
| 346 |
|
| 347 |
assertFalse("Source has observers!", source.hasSubscribers()); |
| 348 |
assertFalse("Start has observers!", start.hasSubscribers()); |
| 349 |
assertFalse("End has observers!", end.hasSubscribers()); |
| 350 |
} |
| 351 |
|
| 352 |
@Test |
| 353 |
public void mainError() { |
| 354 |
Flowable.<Integer>error(new TestException()) |
| 355 |
.window(Flowable.never(), Functions.justFunction(Flowable.just(1))) |
| 356 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 357 |
.test() |
| 358 |
.assertFailure(TestException.class); |
| 359 |
} |
| 360 |
|
| 361 |
@Test |
| 362 |
public void windowCloseIngoresCancel() { |
| 363 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 364 |
try { |
| 365 |
BehaviorProcessor.createDefault(1) |
| 366 |
.window(BehaviorProcessor.createDefault(1), new Function<Integer, Publisher<Integer>>() { |
| 367 |
@Override |
| 368 |
public Publisher<Integer> apply(Integer f) throws Exception { |
| 369 |
return new Flowable<Integer>() { |
| 370 |
@Override |
| 371 |
protected void subscribeActual( |
| 372 |
Subscriber<? super Integer> s) { |
| 373 |
s.onSubscribe(new BooleanSubscription()); |
| 374 |
s.onNext(1); |
| 375 |
s.onNext(2); |
| 376 |
s.onError(new TestException()); |
| 377 |
} |
| 378 |
}; |
| 379 |
} |
| 380 |
}) |
| 381 |
.doOnNext(new Consumer<Flowable<Integer>>() { |
| 382 |
@Override |
| 383 |
public void accept(Flowable<Integer> w) throws Throwable { |
| 384 |
w.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()); // avoid abandonment |
| 385 |
} |
| 386 |
}) |
| 387 |
.test() |
| 388 |
.assertValueCount(1) |
| 389 |
.assertNoErrors() |
| 390 |
.assertNotComplete(); |
| 391 |
|
| 392 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 393 |
} finally { |
| 394 |
RxJavaPlugins.reset(); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
static Flowable<Integer> flowableDisposed(final AtomicBoolean ref) { |
| 399 |
return Flowable.just(1).concatWith(Flowable.<Integer>never()) |
| 400 |
.doOnCancel(new Action() { |
| 401 |
@Override |
| 402 |
public void run() throws Exception { |
| 403 |
ref.set(true); |
| 404 |
} |
| 405 |
}); |
| 406 |
} |
| 407 |
|
| 408 |
@Test |
| 409 |
public void mainAndBoundaryDisposeOnNoWindows() { |
| 410 |
AtomicBoolean mainDisposed = new AtomicBoolean(); |
| 411 |
AtomicBoolean openDisposed = new AtomicBoolean(); |
| 412 |
final AtomicBoolean closeDisposed = new AtomicBoolean(); |
| 413 |
|
| 414 |
flowableDisposed(mainDisposed) |
| 415 |
.window(flowableDisposed(openDisposed), new Function<Integer, Flowable<Integer>>() { |
| 416 |
@Override |
| 417 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 418 |
return flowableDisposed(closeDisposed); |
| 419 |
} |
| 420 |
}) |
| 421 |
.doOnNext(new Consumer<Flowable<Integer>>() { |
| 422 |
@Override |
| 423 |
public void accept(Flowable<Integer> w) throws Throwable { |
| 424 |
w.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()); // avoid abandonment |
| 425 |
} |
| 426 |
}) |
| 427 |
.to(TestHelper.<Flowable<Integer>>testConsumer()) |
| 428 |
.assertSubscribed() |
| 429 |
.assertNoErrors() |
| 430 |
.assertNotComplete() |
| 431 |
.cancel(); |
| 432 |
|
| 433 |
assertTrue(mainDisposed.get()); |
| 434 |
assertTrue(openDisposed.get()); |
| 435 |
assertTrue(closeDisposed.get()); |
| 436 |
} |
| 437 |
|
| 438 |
@Test |
| 439 |
public void mainWindowMissingBackpressure() { |
| 440 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 441 |
PublishProcessor<Integer> boundary = PublishProcessor.create(); |
| 442 |
|
| 443 |
TestSubscriber<Flowable<Integer>> ts = source.window(boundary, Functions.justFunction(Flowable.never())) |
| 444 |
.test(0L) |
| 445 |
; |
| 446 |
|
| 447 |
ts.assertEmpty(); |
| 448 |
|
| 449 |
boundary.onNext(1); |
| 450 |
|
| 451 |
ts.assertFailure(MissingBackpressureException.class); |
| 452 |
|
| 453 |
assertFalse(source.hasSubscribers()); |
| 454 |
assertFalse(boundary.hasSubscribers()); |
| 455 |
} |
| 456 |
|
| 457 |
@Test |
| 458 |
public void cancellingWindowCancelsUpstream() { |
| 459 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 460 |
|
| 461 |
TestSubscriber<Integer> ts = pp.window(Flowable.just(1).concatWith(Flowable.<Integer>never()), Functions.justFunction(Flowable.never())) |
| 462 |
.take(1) |
| 463 |
.flatMap(new Function<Flowable<Integer>, Publisher<Integer>>() { |
| 464 |
@Override |
| 465 |
public Publisher<Integer> apply(Flowable<Integer> w) throws Throwable { |
| 466 |
return w.take(1); |
| 467 |
} |
| 468 |
}) |
| 469 |
.test(); |
| 470 |
|
| 471 |
assertTrue(pp.hasSubscribers()); |
| 472 |
|
| 473 |
pp.onNext(1); |
| 474 |
|
| 475 |
ts |
| 476 |
.assertResult(1); |
| 477 |
|
| 478 |
assertFalse("Processor still has subscribers!", pp.hasSubscribers()); |
| 479 |
} |
| 480 |
|
| 481 |
@Test |
| 482 |
public void windowAbandonmentCancelsUpstream() { |
| 483 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 484 |
|
| 485 |
final AtomicReference<Flowable<Integer>> inner = new AtomicReference<>(); |
| 486 |
|
| 487 |
TestSubscriber<Flowable<Integer>> ts = pp.window(Flowable.<Integer>just(1).concatWith(Flowable.<Integer>never()), |
| 488 |
Functions.justFunction(Flowable.never())) |
| 489 |
.doOnNext(new Consumer<Flowable<Integer>>() { |
| 490 |
@Override |
| 491 |
public void accept(Flowable<Integer> v) throws Throwable { |
| 492 |
inner.set(v); |
| 493 |
} |
| 494 |
}) |
| 495 |
.test(); |
| 496 |
|
| 497 |
assertTrue(pp.hasSubscribers()); |
| 498 |
|
| 499 |
ts |
| 500 |
.assertValueCount(1) |
| 501 |
; |
| 502 |
|
| 503 |
pp.onNext(1); |
| 504 |
|
| 505 |
assertTrue(pp.hasSubscribers()); |
| 506 |
|
| 507 |
ts.cancel(); |
| 508 |
|
| 509 |
ts |
| 510 |
.assertValueCount(1) |
| 511 |
.assertNoErrors() |
| 512 |
.assertNotComplete(); |
| 513 |
|
| 514 |
assertFalse("Processor still has subscribers!", pp.hasSubscribers()); |
| 515 |
|
| 516 |
inner.get().test().assertResult(); |
| 517 |
} |
| 518 |
|
| 519 |
@Test |
| 520 |
public void closingIndicatorFunctionCrash() { |
| 521 |
|
| 522 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 523 |
PublishProcessor<Integer> boundary = PublishProcessor.create(); |
| 524 |
|
| 525 |
TestSubscriber<Flowable<Integer>> ts = source.window(boundary, new Function<Integer, Publisher<Object>>() { |
| 526 |
@Override |
| 527 |
public Publisher<Object> apply(Integer end) throws Throwable { |
| 528 |
throw new TestException(); |
| 529 |
} |
| 530 |
}) |
| 531 |
.test() |
| 532 |
; |
| 533 |
|
| 534 |
ts.assertEmpty(); |
| 535 |
|
| 536 |
boundary.onNext(1); |
| 537 |
|
| 538 |
ts.assertFailure(TestException.class); |
| 539 |
|
| 540 |
assertFalse(source.hasSubscribers()); |
| 541 |
assertFalse(boundary.hasSubscribers()); |
| 542 |
|
| 543 |
} |
| 544 |
|
| 545 |
@Test |
| 546 |
public void doubleOnSubscribe() { |
| 547 |
TestHelper.checkDoubleOnSubscribeFlowable(o -> o.window(Flowable.never(), v -> Flowable.never())); |
| 548 |
} |
| 549 |
|
| 550 |
@Test |
| 551 |
public void openError() throws Throwable { |
| 552 |
TestHelper.withErrorTracking(errors -> { |
| 553 |
TestException ex1 = new TestException(); |
| 554 |
TestException ex2 = new TestException(); |
| 555 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 556 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 557 |
AtomicReference<Subscriber<? super Integer>> ref2 = new AtomicReference<>(); |
| 558 |
|
| 559 |
Flowable<Integer> f1 = Flowable.<Integer>fromPublisher(ref1::set); |
| 560 |
Flowable<Integer> f2 = Flowable.<Integer>fromPublisher(ref2::set); |
| 561 |
|
| 562 |
TestSubscriber<Flowable<Integer>> ts = BehaviorProcessor.createDefault(1) |
| 563 |
.window(f1, v -> f2) |
| 564 |
.doOnNext(w -> w.test()) |
| 565 |
.test(); |
| 566 |
|
| 567 |
ref1.get().onSubscribe(new BooleanSubscription()); |
| 568 |
ref1.get().onNext(1); |
| 569 |
ref2.get().onSubscribe(new BooleanSubscription()); |
| 570 |
|
| 571 |
TestHelper.race( |
| 572 |
() -> ref1.get().onError(ex1), |
| 573 |
() -> ref2.get().onError(ex2) |
| 574 |
); |
| 575 |
|
| 576 |
ts.assertError(RuntimeException.class); |
| 577 |
|
| 578 |
if (!errors.isEmpty()) { |
| 579 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 580 |
} |
| 581 |
|
| 582 |
errors.clear(); |
| 583 |
} |
| 584 |
}); |
| 585 |
} |
| 586 |
|
| 587 |
@Test |
| 588 |
public void closeError() throws Throwable { |
| 589 |
TestHelper.withErrorTracking(errors -> { |
| 590 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 591 |
AtomicReference<Subscriber<? super Integer>> ref2 = new AtomicReference<>(); |
| 592 |
|
| 593 |
Flowable<Integer> f1 = Flowable.<Integer>unsafeCreate(ref1::set); |
| 594 |
Flowable<Integer> f2 = Flowable.<Integer>unsafeCreate(ref2::set); |
| 595 |
|
| 596 |
TestSubscriber<Integer> ts = BehaviorProcessor.createDefault(1) |
| 597 |
.window(f1, v -> f2) |
| 598 |
.flatMap(v -> v) |
| 599 |
.test(); |
| 600 |
|
| 601 |
ref1.get().onSubscribe(new BooleanSubscription()); |
| 602 |
ref1.get().onNext(1); |
| 603 |
ref2.get().onSubscribe(new BooleanSubscription()); |
| 604 |
|
| 605 |
ref2.get().onError(new TestException()); |
| 606 |
ref2.get().onError(new TestException()); |
| 607 |
|
| 608 |
ts.assertFailure(TestException.class); |
| 609 |
|
| 610 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 611 |
}); |
| 612 |
} |
| 613 |
|
| 614 |
@Test |
| 615 |
public void upstreamFailsBeforeFirstWindow() { |
| 616 |
Flowable.error(new TestException()) |
| 617 |
.window(Flowable.never(), v -> Flowable.never()) |
| 618 |
.test() |
| 619 |
.assertFailure(TestException.class); |
| 620 |
} |
| 621 |
|
| 622 |
@Test |
| 623 |
public void windowOpenMainCompletes() { |
| 624 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 625 |
|
| 626 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 627 |
Flowable<Integer> f1 = Flowable.<Integer>unsafeCreate(ref1::set); |
| 628 |
|
| 629 |
AtomicInteger counter = new AtomicInteger(); |
| 630 |
|
| 631 |
TestSubscriber<Flowable<Object>> ts = pp |
| 632 |
.window(f1, v -> Flowable.never()) |
| 633 |
.doOnNext(w -> { |
| 634 |
if (counter.getAndIncrement() == 0) { |
| 635 |
ref1.get().onNext(2); |
| 636 |
pp.onNext(1); |
| 637 |
pp.onComplete(); |
| 638 |
} |
| 639 |
w.test(); |
| 640 |
}) |
| 641 |
.test(); |
| 642 |
|
| 643 |
ref1.get().onSubscribe(new BooleanSubscription()); |
| 644 |
ref1.get().onNext(1); |
| 645 |
|
| 646 |
ts.assertComplete(); |
| 647 |
} |
| 648 |
|
| 649 |
@Test |
| 650 |
public void windowOpenMainError() { |
| 651 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 652 |
|
| 653 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 654 |
Flowable<Integer> f1 = Flowable.<Integer>unsafeCreate(ref1::set); |
| 655 |
|
| 656 |
AtomicInteger counter = new AtomicInteger(); |
| 657 |
|
| 658 |
TestSubscriber<Flowable<Object>> ts = pp |
| 659 |
.window(f1, v -> Flowable.never()) |
| 660 |
.doOnNext(w -> { |
| 661 |
if (counter.getAndIncrement() == 0) { |
| 662 |
ref1.get().onNext(2); |
| 663 |
pp.onNext(1); |
| 664 |
pp.onError(new TestException()); |
| 665 |
} |
| 666 |
w.test(); |
| 667 |
}) |
| 668 |
.test(); |
| 669 |
|
| 670 |
ref1.get().onSubscribe(new BooleanSubscription()); |
| 671 |
ref1.get().onNext(1); |
| 672 |
|
| 673 |
ts.assertError(TestException.class); |
| 674 |
} |
| 675 |
|
| 676 |
@Test |
| 677 |
public void windowOpenIgnoresDispose() { |
| 678 |
AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>(); |
| 679 |
|
| 680 |
PublishProcessor<Object> pp = PublishProcessor.create(); |
| 681 |
Flowable<Integer> f1 = Flowable.<Integer>unsafeCreate(ref1::set); |
| 682 |
|
| 683 |
TestSubscriber<Flowable<Object>> ts = pp |
| 684 |
.window(f1, v -> Flowable.never()) |
| 685 |
.take(1) |
| 686 |
.doOnNext(w -> { |
| 687 |
w.test(); |
| 688 |
}) |
| 689 |
.test(); |
| 690 |
|
| 691 |
ref1.get().onSubscribe(new BooleanSubscription()); |
| 692 |
ref1.get().onNext(1); |
| 693 |
ref1.get().onNext(2); |
| 694 |
|
| 695 |
ts.assertValueCount(1); |
| 696 |
} |
| 697 |
|
| 698 |
@Test |
| 699 |
public void badRequest() { |
| 700 |
TestHelper.assertBadRequestReported(Flowable.never().window(Flowable.never(), v -> Flowable.never())); |
| 701 |
} |
| 702 |
|
| 703 |
@Test |
| 704 |
public void mainIgnoresCancelBeforeOnError() throws Throwable { |
| 705 |
TestHelper.withErrorTracking(errors -> { |
| 706 |
Flowable.fromPublisher(s -> { |
| 707 |
s.onSubscribe(new BooleanSubscription()); |
| 708 |
s.onNext(1); |
| 709 |
s.onError(new IOException()); |
| 710 |
}) |
| 711 |
.window(BehaviorProcessor.createDefault(1), v -> Flowable.error(new TestException())) |
| 712 |
.doOnNext(w -> w.test()) |
| 713 |
.test() |
| 714 |
.assertError(TestException.class); |
| 715 |
|
| 716 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 717 |
}); |
| 718 |
} |
| 719 |
} |