| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.*; |
| 20 |
import java.util.concurrent.atomic.*; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.core.Observable; |
| 26 |
import io.reactivex.rxjava3.core.Observer; |
| 27 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 28 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 29 |
import io.reactivex.rxjava3.functions.*; |
| 30 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 31 |
import io.reactivex.rxjava3.observers.*; |
| 32 |
import io.reactivex.rxjava3.schedulers.*; |
| 33 |
import io.reactivex.rxjava3.subjects.*; |
| 34 |
import io.reactivex.rxjava3.testsupport.*; |
| 35 |
|
| 36 |
public class ObservableWindowWithTimeTest extends RxJavaTest { |
| 37 |
|
| 38 |
private TestScheduler scheduler; |
| 39 |
private Scheduler.Worker innerScheduler; |
| 40 |
|
| 41 |
@Before |
| 42 |
public void before() { |
| 43 |
scheduler = new TestScheduler(); |
| 44 |
innerScheduler = scheduler.createWorker(); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void timedAndCount() { |
| 49 |
final List<String> list = new ArrayList<>(); |
| 50 |
final List<List<String>> lists = new ArrayList<>(); |
| 51 |
|
| 52 |
Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() { |
| 53 |
@Override |
| 54 |
public void subscribe(Observer<? super String> observer) { |
| 55 |
observer.onSubscribe(Disposable.empty()); |
| 56 |
push(observer, "one", 10); |
| 57 |
push(observer, "two", 90); |
| 58 |
push(observer, "three", 110); |
| 59 |
push(observer, "four", 190); |
| 60 |
push(observer, "five", 210); |
| 61 |
complete(observer, 250); |
| 62 |
} |
| 63 |
}); |
| 64 |
|
| 65 |
Observable<Observable<String>> windowed = source.window(100, TimeUnit.MILLISECONDS, scheduler, 2); |
| 66 |
windowed.subscribe(observeWindow(list, lists)); |
| 67 |
|
| 68 |
scheduler.advanceTimeTo(95, TimeUnit.MILLISECONDS); |
| 69 |
assertEquals(1, lists.size()); |
| 70 |
assertEquals(lists.get(0), list("one", "two")); |
| 71 |
|
| 72 |
scheduler.advanceTimeTo(195, TimeUnit.MILLISECONDS); |
| 73 |
assertEquals(3, lists.size()); |
| 74 |
assertTrue(lists.get(1).isEmpty()); |
| 75 |
assertEquals(lists.get(2), list("three", "four")); |
| 76 |
|
| 77 |
scheduler.advanceTimeTo(300, TimeUnit.MILLISECONDS); |
| 78 |
assertEquals(5, lists.size()); |
| 79 |
assertTrue(lists.get(3).isEmpty()); |
| 80 |
assertEquals(lists.get(4), list("five")); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void timed() { |
| 85 |
final List<String> list = new ArrayList<>(); |
| 86 |
final List<List<String>> lists = new ArrayList<>(); |
| 87 |
|
| 88 |
Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() { |
| 89 |
@Override |
| 90 |
public void subscribe(Observer<? super String> observer) { |
| 91 |
observer.onSubscribe(Disposable.empty()); |
| 92 |
push(observer, "one", 98); |
| 93 |
push(observer, "two", 99); |
| 94 |
push(observer, "three", 99); // FIXME happens after the window is open |
| 95 |
push(observer, "four", 101); |
| 96 |
push(observer, "five", 102); |
| 97 |
complete(observer, 150); |
| 98 |
} |
| 99 |
}); |
| 100 |
|
| 101 |
Observable<Observable<String>> windowed = source.window(100, TimeUnit.MILLISECONDS, scheduler); |
| 102 |
windowed.subscribe(observeWindow(list, lists)); |
| 103 |
|
| 104 |
scheduler.advanceTimeTo(101, TimeUnit.MILLISECONDS); |
| 105 |
assertEquals(1, lists.size()); |
| 106 |
assertEquals(lists.get(0), list("one", "two", "three")); |
| 107 |
|
| 108 |
scheduler.advanceTimeTo(201, TimeUnit.MILLISECONDS); |
| 109 |
assertEquals(2, lists.size()); |
| 110 |
assertEquals(lists.get(1), list("four", "five")); |
| 111 |
} |
| 112 |
|
| 113 |
private List<String> list(String... args) { |
| 114 |
List<String> list = new ArrayList<>(); |
| 115 |
for (String arg : args) { |
| 116 |
list.add(arg); |
| 117 |
} |
| 118 |
return list; |
| 119 |
} |
| 120 |
|
| 121 |
private <T> void push(final Observer<T> observer, final T value, int delay) { |
| 122 |
innerScheduler.schedule(new Runnable() { |
| 123 |
@Override |
| 124 |
public void run() { |
| 125 |
observer.onNext(value); |
| 126 |
} |
| 127 |
}, delay, TimeUnit.MILLISECONDS); |
| 128 |
} |
| 129 |
|
| 130 |
private void complete(final Observer<?> observer, int delay) { |
| 131 |
innerScheduler.schedule(new Runnable() { |
| 132 |
@Override |
| 133 |
public void run() { |
| 134 |
observer.onComplete(); |
| 135 |
} |
| 136 |
}, delay, TimeUnit.MILLISECONDS); |
| 137 |
} |
| 138 |
|
| 139 |
private <T> Consumer<Observable<T>> observeWindow(final List<T> list, final List<List<T>> lists) { |
| 140 |
return new Consumer<Observable<T>>() { |
| 141 |
@Override |
| 142 |
public void accept(Observable<T> stringObservable) { |
| 143 |
stringObservable.subscribe(new DefaultObserver<T>() { |
| 144 |
@Override |
| 145 |
public void onComplete() { |
| 146 |
lists.add(new ArrayList<>(list)); |
| 147 |
list.clear(); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void onError(Throwable e) { |
| 152 |
Assert.fail(e.getMessage()); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void onNext(T args) { |
| 157 |
list.add(args); |
| 158 |
} |
| 159 |
}); |
| 160 |
} |
| 161 |
}; |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void exactWindowSize() { |
| 166 |
Observable<Observable<Integer>> source = Observable.range(1, 10) |
| 167 |
.window(1, TimeUnit.MINUTES, scheduler, 3); |
| 168 |
|
| 169 |
final List<Integer> list = new ArrayList<>(); |
| 170 |
final List<List<Integer>> lists = new ArrayList<>(); |
| 171 |
|
| 172 |
source.subscribe(observeWindow(list, lists)); |
| 173 |
|
| 174 |
assertEquals(4, lists.size()); |
| 175 |
assertEquals(3, lists.get(0).size()); |
| 176 |
assertEquals(Arrays.asList(1, 2, 3), lists.get(0)); |
| 177 |
assertEquals(3, lists.get(1).size()); |
| 178 |
assertEquals(Arrays.asList(4, 5, 6), lists.get(1)); |
| 179 |
assertEquals(3, lists.get(2).size()); |
| 180 |
assertEquals(Arrays.asList(7, 8, 9), lists.get(2)); |
| 181 |
assertEquals(1, lists.get(3).size()); |
| 182 |
assertEquals(Arrays.asList(10), lists.get(3)); |
| 183 |
} |
| 184 |
|
| 185 |
@Test |
| 186 |
public void takeFlatMapCompletes() { |
| 187 |
TestObserver<Integer> to = new TestObserver<>(); |
| 188 |
|
| 189 |
final AtomicInteger wip = new AtomicInteger(); |
| 190 |
|
| 191 |
final int indicator = 999999999; |
| 192 |
|
| 193 |
ObservableWindowWithSizeTest.hotStream() |
| 194 |
.window(300, TimeUnit.MILLISECONDS) |
| 195 |
.take(10) |
| 196 |
.doOnComplete(new Action() { |
| 197 |
@Override |
| 198 |
public void run() { |
| 199 |
System.out.println("Main done!"); |
| 200 |
} |
| 201 |
}) |
| 202 |
.flatMap(new Function<Observable<Integer>, Observable<Integer>>() { |
| 203 |
@Override |
| 204 |
public Observable<Integer> apply(Observable<Integer> w) { |
| 205 |
return w.startWithItem(indicator) |
| 206 |
.doOnComplete(new Action() { |
| 207 |
@Override |
| 208 |
public void run() { |
| 209 |
System.out.println("inner done: " + wip.incrementAndGet()); |
| 210 |
} |
| 211 |
}) |
| 212 |
; |
| 213 |
} |
| 214 |
}) |
| 215 |
.doOnNext(new Consumer<Integer>() { |
| 216 |
@Override |
| 217 |
public void accept(Integer pv) { |
| 218 |
System.out.println(pv); |
| 219 |
} |
| 220 |
}) |
| 221 |
.subscribe(to); |
| 222 |
|
| 223 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 224 |
to.assertComplete(); |
| 225 |
Assert.assertTrue(to.values().size() != 0); |
| 226 |
} |
| 227 |
|
| 228 |
@Test |
| 229 |
public void timespanTimeskipDefaultScheduler() { |
| 230 |
Observable.just(1) |
| 231 |
.window(1, 1, TimeUnit.MINUTES) |
| 232 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 233 |
.test() |
| 234 |
.awaitDone(5, TimeUnit.SECONDS) |
| 235 |
.assertResult(1); |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void timespanTimeskipCustomScheduler() { |
| 240 |
Observable.just(1) |
| 241 |
.window(1, 1, TimeUnit.MINUTES, Schedulers.io()) |
| 242 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 243 |
.test() |
| 244 |
.awaitDone(5, TimeUnit.SECONDS) |
| 245 |
.assertResult(1); |
| 246 |
} |
| 247 |
|
| 248 |
@Test |
| 249 |
public void timespanTimeskipCustomSchedulerBufferSize() { |
| 250 |
Observable.range(1, 10) |
| 251 |
.window(1, 1, TimeUnit.MINUTES, Schedulers.io(), 2) |
| 252 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 253 |
.test() |
| 254 |
.awaitDone(5, TimeUnit.SECONDS) |
| 255 |
.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 256 |
} |
| 257 |
|
| 258 |
@Test |
| 259 |
public void timespanDefaultSchedulerSize() { |
| 260 |
Observable.range(1, 10) |
| 261 |
.window(1, TimeUnit.MINUTES, 20) |
| 262 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 263 |
.test() |
| 264 |
.awaitDone(5, TimeUnit.SECONDS) |
| 265 |
.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void timespanDefaultSchedulerSizeRestart() { |
| 270 |
Observable.range(1, 10) |
| 271 |
.window(1, TimeUnit.MINUTES, 20, true) |
| 272 |
.flatMap(Functions.<Observable<Integer>>identity(), true) |
| 273 |
.test() |
| 274 |
.awaitDone(5, TimeUnit.SECONDS) |
| 275 |
.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 276 |
} |
| 277 |
|
| 278 |
@Test |
| 279 |
public void invalidSpan() { |
| 280 |
try { |
| 281 |
Observable.just(1).window(-99, 1, TimeUnit.SECONDS); |
| 282 |
fail("Should have thrown!"); |
| 283 |
} catch (IllegalArgumentException ex) { |
| 284 |
assertEquals("timespan > 0 required but it was -99", ex.getMessage()); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
@Test |
| 289 |
public void timeskipJustOverlap() { |
| 290 |
Observable.just(1) |
| 291 |
.window(2, 1, TimeUnit.MINUTES, Schedulers.single()) |
| 292 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 293 |
.test() |
| 294 |
.awaitDone(5, TimeUnit.SECONDS) |
| 295 |
.assertResult(1); |
| 296 |
} |
| 297 |
|
| 298 |
@Test |
| 299 |
public void timeskipJustSkip() { |
| 300 |
Observable.just(1) |
| 301 |
.window(1, 2, TimeUnit.MINUTES, Schedulers.single()) |
| 302 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 303 |
.test() |
| 304 |
.awaitDone(5, TimeUnit.SECONDS) |
| 305 |
.assertResult(1); |
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void timeskipSkipping() { |
| 310 |
TestScheduler scheduler = new TestScheduler(); |
| 311 |
|
| 312 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 313 |
|
| 314 |
TestObserver<Integer> to = ps.window(1, 2, TimeUnit.SECONDS, scheduler) |
| 315 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 316 |
.test(); |
| 317 |
|
| 318 |
ps.onNext(1); |
| 319 |
ps.onNext(2); |
| 320 |
|
| 321 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 322 |
|
| 323 |
ps.onNext(3); |
| 324 |
ps.onNext(4); |
| 325 |
|
| 326 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 327 |
|
| 328 |
ps.onNext(5); |
| 329 |
ps.onNext(6); |
| 330 |
|
| 331 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 332 |
|
| 333 |
ps.onNext(7); |
| 334 |
ps.onComplete(); |
| 335 |
|
| 336 |
to.assertResult(1, 2, 5, 6); |
| 337 |
} |
| 338 |
|
| 339 |
@Test |
| 340 |
public void timeskipOverlapping() { |
| 341 |
TestScheduler scheduler = new TestScheduler(); |
| 342 |
|
| 343 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 344 |
|
| 345 |
TestObserver<Integer> to = ps.window(2, 1, TimeUnit.SECONDS, scheduler) |
| 346 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 347 |
.test(); |
| 348 |
|
| 349 |
ps.onNext(1); |
| 350 |
ps.onNext(2); |
| 351 |
|
| 352 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 353 |
|
| 354 |
ps.onNext(3); |
| 355 |
ps.onNext(4); |
| 356 |
|
| 357 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 358 |
|
| 359 |
ps.onNext(5); |
| 360 |
ps.onNext(6); |
| 361 |
|
| 362 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 363 |
|
| 364 |
ps.onNext(7); |
| 365 |
ps.onComplete(); |
| 366 |
|
| 367 |
to.assertResult(1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7); |
| 368 |
} |
| 369 |
|
| 370 |
@Test |
| 371 |
@SuppressUndeliverable |
| 372 |
public void exactOnError() { |
| 373 |
TestScheduler scheduler = new TestScheduler(); |
| 374 |
|
| 375 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 376 |
|
| 377 |
TestObserver<Integer> to = ps.window(1, 1, TimeUnit.SECONDS, scheduler) |
| 378 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 379 |
.test(); |
| 380 |
|
| 381 |
ps.onError(new TestException()); |
| 382 |
|
| 383 |
to.assertFailure(TestException.class); |
| 384 |
} |
| 385 |
|
| 386 |
@Test |
| 387 |
@SuppressUndeliverable |
| 388 |
public void overlappingOnError() { |
| 389 |
TestScheduler scheduler = new TestScheduler(); |
| 390 |
|
| 391 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 392 |
|
| 393 |
TestObserver<Integer> to = ps.window(2, 1, TimeUnit.SECONDS, scheduler) |
| 394 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 395 |
.test(); |
| 396 |
|
| 397 |
ps.onError(new TestException()); |
| 398 |
|
| 399 |
to.assertFailure(TestException.class); |
| 400 |
} |
| 401 |
|
| 402 |
@Test |
| 403 |
@SuppressUndeliverable |
| 404 |
public void skipOnError() { |
| 405 |
TestScheduler scheduler = new TestScheduler(); |
| 406 |
|
| 407 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 408 |
|
| 409 |
TestObserver<Integer> to = ps.window(1, 2, TimeUnit.SECONDS, scheduler) |
| 410 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 411 |
.test(); |
| 412 |
|
| 413 |
ps.onError(new TestException()); |
| 414 |
|
| 415 |
to.assertFailure(TestException.class); |
| 416 |
} |
| 417 |
|
| 418 |
@Test |
| 419 |
public void dispose() { |
| 420 |
TestHelper.checkDisposed(Observable.range(1, 5).window(1, TimeUnit.DAYS, Schedulers.single())); |
| 421 |
|
| 422 |
TestHelper.checkDisposed(Observable.range(1, 5).window(2, 1, TimeUnit.DAYS, Schedulers.single())); |
| 423 |
|
| 424 |
TestHelper.checkDisposed(Observable.range(1, 5).window(1, 2, TimeUnit.DAYS, Schedulers.single())); |
| 425 |
|
| 426 |
TestHelper.checkDisposed(Observable.never() |
| 427 |
.window(1, TimeUnit.DAYS, Schedulers.single(), 2, true)); |
| 428 |
} |
| 429 |
|
| 430 |
@Test |
| 431 |
public void restartTimer() { |
| 432 |
Observable.range(1, 5) |
| 433 |
.window(1, TimeUnit.DAYS, Schedulers.single(), 2, true) |
| 434 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 435 |
.test() |
| 436 |
.assertResult(1, 2, 3, 4, 5); |
| 437 |
} |
| 438 |
|
| 439 |
@Test |
| 440 |
@SuppressUndeliverable |
| 441 |
public void exactBoundaryError() { |
| 442 |
Observable.error(new TestException()) |
| 443 |
.window(1, TimeUnit.DAYS, Schedulers.single(), 2, true) |
| 444 |
.to(TestHelper.<Observable<Object>>testConsumer()) |
| 445 |
.assertSubscribed() |
| 446 |
.assertError(TestException.class) |
| 447 |
.assertNotComplete(); |
| 448 |
} |
| 449 |
|
| 450 |
@Test |
| 451 |
public void restartTimerMany() { |
| 452 |
Observable.intervalRange(1, 1000, 1, 1, TimeUnit.MILLISECONDS) |
| 453 |
.window(1, TimeUnit.MILLISECONDS, Schedulers.single(), 2, true) |
| 454 |
.flatMap(Functions.<Observable<Long>>identity()) |
| 455 |
.take(500) |
| 456 |
.to(TestHelper.<Long>testConsumer()) |
| 457 |
.awaitDone(5, TimeUnit.SECONDS) |
| 458 |
.assertSubscribed() |
| 459 |
.assertValueCount(500) |
| 460 |
.assertNoErrors() |
| 461 |
.assertComplete(); |
| 462 |
} |
| 463 |
|
| 464 |
@Test |
| 465 |
public void exactUnboundedReentrant() { |
| 466 |
TestScheduler scheduler = new TestScheduler(); |
| 467 |
|
| 468 |
final Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 469 |
|
| 470 |
TestObserver<Integer> to = new TestObserver<Integer>() { |
| 471 |
@Override |
| 472 |
public void onNext(Integer t) { |
| 473 |
super.onNext(t); |
| 474 |
if (t == 1) { |
| 475 |
ps.onNext(2); |
| 476 |
ps.onComplete(); |
| 477 |
} |
| 478 |
} |
| 479 |
}; |
| 480 |
|
| 481 |
ps.window(1, TimeUnit.MILLISECONDS, scheduler) |
| 482 |
.flatMap(new Function<Observable<Integer>, ObservableSource<Integer>>() { |
| 483 |
@Override |
| 484 |
public ObservableSource<Integer> apply(Observable<Integer> v) throws Exception { |
| 485 |
return v; |
| 486 |
} |
| 487 |
}) |
| 488 |
.subscribe(to); |
| 489 |
|
| 490 |
ps.onNext(1); |
| 491 |
|
| 492 |
to |
| 493 |
.awaitDone(1, TimeUnit.SECONDS) |
| 494 |
.assertResult(1, 2); |
| 495 |
} |
| 496 |
|
| 497 |
@Test |
| 498 |
public void exactBoundedReentrant() { |
| 499 |
TestScheduler scheduler = new TestScheduler(); |
| 500 |
|
| 501 |
final Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 502 |
|
| 503 |
TestObserver<Integer> to = new TestObserver<Integer>() { |
| 504 |
@Override |
| 505 |
public void onNext(Integer t) { |
| 506 |
super.onNext(t); |
| 507 |
if (t == 1) { |
| 508 |
ps.onNext(2); |
| 509 |
ps.onComplete(); |
| 510 |
} |
| 511 |
} |
| 512 |
}; |
| 513 |
|
| 514 |
ps.window(1, TimeUnit.MILLISECONDS, scheduler, 10, true) |
| 515 |
.flatMap(new Function<Observable<Integer>, ObservableSource<Integer>>() { |
| 516 |
@Override |
| 517 |
public ObservableSource<Integer> apply(Observable<Integer> v) throws Exception { |
| 518 |
return v; |
| 519 |
} |
| 520 |
}) |
| 521 |
.subscribe(to); |
| 522 |
|
| 523 |
ps.onNext(1); |
| 524 |
|
| 525 |
to |
| 526 |
.awaitDone(1, TimeUnit.SECONDS) |
| 527 |
.assertResult(1, 2); |
| 528 |
} |
| 529 |
|
| 530 |
@Test |
| 531 |
public void exactBoundedReentrant2() { |
| 532 |
TestScheduler scheduler = new TestScheduler(); |
| 533 |
|
| 534 |
final Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 535 |
|
| 536 |
TestObserver<Integer> to = new TestObserver<Integer>() { |
| 537 |
@Override |
| 538 |
public void onNext(Integer t) { |
| 539 |
super.onNext(t); |
| 540 |
if (t == 1) { |
| 541 |
ps.onNext(2); |
| 542 |
ps.onComplete(); |
| 543 |
} |
| 544 |
} |
| 545 |
}; |
| 546 |
|
| 547 |
ps.window(1, TimeUnit.MILLISECONDS, scheduler, 2, true) |
| 548 |
.flatMap(new Function<Observable<Integer>, ObservableSource<Integer>>() { |
| 549 |
@Override |
| 550 |
public ObservableSource<Integer> apply(Observable<Integer> v) throws Exception { |
| 551 |
return v; |
| 552 |
} |
| 553 |
}) |
| 554 |
.subscribe(to); |
| 555 |
|
| 556 |
ps.onNext(1); |
| 557 |
|
| 558 |
to |
| 559 |
.awaitDone(1, TimeUnit.SECONDS) |
| 560 |
.assertResult(1, 2); |
| 561 |
} |
| 562 |
|
| 563 |
@Test |
| 564 |
public void skipReentrant() { |
| 565 |
TestScheduler scheduler = new TestScheduler(); |
| 566 |
|
| 567 |
final Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 568 |
|
| 569 |
TestObserver<Integer> to = new TestObserver<Integer>() { |
| 570 |
@Override |
| 571 |
public void onNext(Integer t) { |
| 572 |
super.onNext(t); |
| 573 |
if (t == 1) { |
| 574 |
ps.onNext(2); |
| 575 |
ps.onComplete(); |
| 576 |
} |
| 577 |
} |
| 578 |
}; |
| 579 |
|
| 580 |
ps.window(1, 2, TimeUnit.MILLISECONDS, scheduler) |
| 581 |
.flatMap(new Function<Observable<Integer>, ObservableSource<Integer>>() { |
| 582 |
@Override |
| 583 |
public ObservableSource<Integer> apply(Observable<Integer> v) throws Exception { |
| 584 |
return v; |
| 585 |
} |
| 586 |
}) |
| 587 |
.subscribe(to); |
| 588 |
|
| 589 |
ps.onNext(1); |
| 590 |
|
| 591 |
to |
| 592 |
.awaitDone(1, TimeUnit.SECONDS) |
| 593 |
.assertResult(1, 2); |
| 594 |
} |
| 595 |
|
| 596 |
@Test |
| 597 |
public void sizeTimeTimeout() { |
| 598 |
TestScheduler scheduler = new TestScheduler(); |
| 599 |
Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 600 |
|
| 601 |
TestObserver<Observable<Integer>> to = ps.window(5, TimeUnit.MILLISECONDS, scheduler, 100) |
| 602 |
.test() |
| 603 |
.assertValueCount(1); |
| 604 |
|
| 605 |
scheduler.advanceTimeBy(5, TimeUnit.MILLISECONDS); |
| 606 |
|
| 607 |
to.assertValueCount(2) |
| 608 |
.assertNoErrors() |
| 609 |
.assertNotComplete(); |
| 610 |
|
| 611 |
to.values().get(0).test().assertResult(); |
| 612 |
} |
| 613 |
|
| 614 |
@Test |
| 615 |
public void periodicWindowCompletion() { |
| 616 |
TestScheduler scheduler = new TestScheduler(); |
| 617 |
Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 618 |
|
| 619 |
TestObserver<Observable<Integer>> to = ps.window(5, TimeUnit.MILLISECONDS, scheduler, Long.MAX_VALUE, false) |
| 620 |
.test(); |
| 621 |
|
| 622 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 623 |
|
| 624 |
to.assertValueCount(21) |
| 625 |
.assertNoErrors() |
| 626 |
.assertNotComplete(); |
| 627 |
} |
| 628 |
|
| 629 |
@Test |
| 630 |
public void periodicWindowCompletionRestartTimer() { |
| 631 |
TestScheduler scheduler = new TestScheduler(); |
| 632 |
Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 633 |
|
| 634 |
TestObserver<Observable<Integer>> to = ps.window(5, TimeUnit.MILLISECONDS, scheduler, Long.MAX_VALUE, true) |
| 635 |
.test(); |
| 636 |
|
| 637 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 638 |
|
| 639 |
to.assertValueCount(21) |
| 640 |
.assertNoErrors() |
| 641 |
.assertNotComplete(); |
| 642 |
} |
| 643 |
|
| 644 |
@Test |
| 645 |
public void periodicWindowCompletionBounded() { |
| 646 |
TestScheduler scheduler = new TestScheduler(); |
| 647 |
Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 648 |
|
| 649 |
TestObserver<Observable<Integer>> to = ps.window(5, TimeUnit.MILLISECONDS, scheduler, 5, false) |
| 650 |
.test(); |
| 651 |
|
| 652 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 653 |
|
| 654 |
to.assertValueCount(21) |
| 655 |
.assertNoErrors() |
| 656 |
.assertNotComplete(); |
| 657 |
} |
| 658 |
|
| 659 |
@Test |
| 660 |
public void periodicWindowCompletionRestartTimerBounded() { |
| 661 |
TestScheduler scheduler = new TestScheduler(); |
| 662 |
Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 663 |
|
| 664 |
TestObserver<Observable<Integer>> to = ps.window(5, TimeUnit.MILLISECONDS, scheduler, 5, true) |
| 665 |
.test(); |
| 666 |
|
| 667 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 668 |
|
| 669 |
to.assertValueCount(21) |
| 670 |
.assertNoErrors() |
| 671 |
.assertNotComplete(); |
| 672 |
} |
| 673 |
|
| 674 |
@Test |
| 675 |
public void periodicWindowCompletionRestartTimerBoundedSomeData() { |
| 676 |
TestScheduler scheduler = new TestScheduler(); |
| 677 |
Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 678 |
|
| 679 |
TestObserver<Observable<Integer>> to = ps.window(5, TimeUnit.MILLISECONDS, scheduler, 2, true) |
| 680 |
.test(); |
| 681 |
|
| 682 |
ps.onNext(1); |
| 683 |
ps.onNext(2); |
| 684 |
|
| 685 |
scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); |
| 686 |
|
| 687 |
to.assertValueCount(22) |
| 688 |
.assertNoErrors() |
| 689 |
.assertNotComplete(); |
| 690 |
} |
| 691 |
|
| 692 |
@Test |
| 693 |
public void countRestartsOnTimeTick() { |
| 694 |
TestScheduler scheduler = new TestScheduler(); |
| 695 |
Subject<Integer> ps = PublishSubject.<Integer>create(); |
| 696 |
|
| 697 |
TestObserver<Observable<Integer>> to = ps.window(5, TimeUnit.MILLISECONDS, scheduler, 5, true) |
| 698 |
.test(); |
| 699 |
|
| 700 |
// window #1 |
| 701 |
ps.onNext(1); |
| 702 |
ps.onNext(2); |
| 703 |
|
| 704 |
scheduler.advanceTimeBy(5, TimeUnit.MILLISECONDS); |
| 705 |
|
| 706 |
// window #2 |
| 707 |
ps.onNext(3); |
| 708 |
ps.onNext(4); |
| 709 |
ps.onNext(5); |
| 710 |
ps.onNext(6); |
| 711 |
|
| 712 |
to.assertValueCount(2) |
| 713 |
.assertNoErrors() |
| 714 |
.assertNotComplete(); |
| 715 |
} |
| 716 |
|
| 717 |
@Test |
| 718 |
public void exactTimeBoundNoInterruptWindowOutputOnComplete() throws Exception { |
| 719 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 720 |
|
| 721 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 722 |
|
| 723 |
final CountDownLatch doOnNextDone = new CountDownLatch(1); |
| 724 |
final CountDownLatch secondWindowProcessing = new CountDownLatch(1); |
| 725 |
|
| 726 |
ps.window(100, TimeUnit.MILLISECONDS) |
| 727 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 728 |
int count; |
| 729 |
@Override |
| 730 |
public void accept(Observable<Integer> v) throws Exception { |
| 731 |
System.out.println(Thread.currentThread()); |
| 732 |
if (count++ == 1) { |
| 733 |
secondWindowProcessing.countDown(); |
| 734 |
try { |
| 735 |
Thread.sleep(200); |
| 736 |
isInterrupted.set(Thread.interrupted()); |
| 737 |
} catch (InterruptedException ex) { |
| 738 |
isInterrupted.set(true); |
| 739 |
} |
| 740 |
doOnNextDone.countDown(); |
| 741 |
} |
| 742 |
} |
| 743 |
}) |
| 744 |
.test(); |
| 745 |
|
| 746 |
ps.onNext(1); |
| 747 |
|
| 748 |
assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS)); |
| 749 |
|
| 750 |
ps.onComplete(); |
| 751 |
|
| 752 |
assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS)); |
| 753 |
|
| 754 |
assertFalse("The doOnNext got interrupted!", isInterrupted.get()); |
| 755 |
} |
| 756 |
|
| 757 |
@Test |
| 758 |
@SuppressUndeliverable |
| 759 |
public void exactTimeBoundNoInterruptWindowOutputOnError() throws Exception { |
| 760 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 761 |
|
| 762 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 763 |
|
| 764 |
final CountDownLatch doOnNextDone = new CountDownLatch(1); |
| 765 |
final CountDownLatch secondWindowProcessing = new CountDownLatch(1); |
| 766 |
|
| 767 |
ps.window(100, TimeUnit.MILLISECONDS) |
| 768 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 769 |
int count; |
| 770 |
@Override |
| 771 |
public void accept(Observable<Integer> v) throws Exception { |
| 772 |
System.out.println(Thread.currentThread()); |
| 773 |
if (count++ == 1) { |
| 774 |
secondWindowProcessing.countDown(); |
| 775 |
try { |
| 776 |
Thread.sleep(200); |
| 777 |
isInterrupted.set(Thread.interrupted()); |
| 778 |
} catch (InterruptedException ex) { |
| 779 |
isInterrupted.set(true); |
| 780 |
} |
| 781 |
doOnNextDone.countDown(); |
| 782 |
} |
| 783 |
} |
| 784 |
}) |
| 785 |
.test(); |
| 786 |
|
| 787 |
ps.onNext(1); |
| 788 |
|
| 789 |
assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS)); |
| 790 |
|
| 791 |
ps.onError(new TestException()); |
| 792 |
|
| 793 |
assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS)); |
| 794 |
|
| 795 |
assertFalse("The doOnNext got interrupted!", isInterrupted.get()); |
| 796 |
} |
| 797 |
|
| 798 |
@Test |
| 799 |
public void exactTimeAndSizeBoundNoInterruptWindowOutputOnComplete() throws Exception { |
| 800 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 801 |
|
| 802 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 803 |
|
| 804 |
final CountDownLatch doOnNextDone = new CountDownLatch(1); |
| 805 |
final CountDownLatch secondWindowProcessing = new CountDownLatch(1); |
| 806 |
|
| 807 |
ps.window(100, TimeUnit.MILLISECONDS, 10) |
| 808 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 809 |
int count; |
| 810 |
@Override |
| 811 |
public void accept(Observable<Integer> v) throws Exception { |
| 812 |
System.out.println(Thread.currentThread()); |
| 813 |
if (count++ == 1) { |
| 814 |
secondWindowProcessing.countDown(); |
| 815 |
try { |
| 816 |
Thread.sleep(200); |
| 817 |
isInterrupted.set(Thread.interrupted()); |
| 818 |
} catch (InterruptedException ex) { |
| 819 |
isInterrupted.set(true); |
| 820 |
} |
| 821 |
doOnNextDone.countDown(); |
| 822 |
} |
| 823 |
} |
| 824 |
}) |
| 825 |
.test(); |
| 826 |
|
| 827 |
ps.onNext(1); |
| 828 |
|
| 829 |
assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS)); |
| 830 |
|
| 831 |
ps.onComplete(); |
| 832 |
|
| 833 |
assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS)); |
| 834 |
|
| 835 |
assertFalse("The doOnNext got interrupted!", isInterrupted.get()); |
| 836 |
} |
| 837 |
|
| 838 |
@Test |
| 839 |
@SuppressUndeliverable |
| 840 |
public void exactTimeAndSizeBoundNoInterruptWindowOutputOnError() throws Exception { |
| 841 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 842 |
|
| 843 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 844 |
|
| 845 |
final CountDownLatch doOnNextDone = new CountDownLatch(1); |
| 846 |
final CountDownLatch secondWindowProcessing = new CountDownLatch(1); |
| 847 |
|
| 848 |
ps.window(100, TimeUnit.MILLISECONDS, 10) |
| 849 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 850 |
int count; |
| 851 |
@Override |
| 852 |
public void accept(Observable<Integer> v) throws Exception { |
| 853 |
System.out.println(Thread.currentThread()); |
| 854 |
if (count++ == 1) { |
| 855 |
secondWindowProcessing.countDown(); |
| 856 |
try { |
| 857 |
Thread.sleep(200); |
| 858 |
isInterrupted.set(Thread.interrupted()); |
| 859 |
} catch (InterruptedException ex) { |
| 860 |
isInterrupted.set(true); |
| 861 |
} |
| 862 |
doOnNextDone.countDown(); |
| 863 |
} |
| 864 |
} |
| 865 |
}) |
| 866 |
.test(); |
| 867 |
|
| 868 |
ps.onNext(1); |
| 869 |
|
| 870 |
assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS)); |
| 871 |
|
| 872 |
ps.onError(new TestException()); |
| 873 |
|
| 874 |
assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS)); |
| 875 |
|
| 876 |
assertFalse("The doOnNext got interrupted!", isInterrupted.get()); |
| 877 |
} |
| 878 |
|
| 879 |
@Test |
| 880 |
public void skipTimeAndSizeBoundNoInterruptWindowOutputOnComplete() throws Exception { |
| 881 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 882 |
|
| 883 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 884 |
|
| 885 |
final CountDownLatch doOnNextDone = new CountDownLatch(1); |
| 886 |
final CountDownLatch secondWindowProcessing = new CountDownLatch(1); |
| 887 |
|
| 888 |
ps.window(90, 100, TimeUnit.MILLISECONDS) |
| 889 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 890 |
int count; |
| 891 |
@Override |
| 892 |
public void accept(Observable<Integer> v) throws Exception { |
| 893 |
System.out.println(Thread.currentThread()); |
| 894 |
if (count++ == 1) { |
| 895 |
secondWindowProcessing.countDown(); |
| 896 |
try { |
| 897 |
Thread.sleep(200); |
| 898 |
isInterrupted.set(Thread.interrupted()); |
| 899 |
} catch (InterruptedException ex) { |
| 900 |
isInterrupted.set(true); |
| 901 |
} |
| 902 |
doOnNextDone.countDown(); |
| 903 |
} |
| 904 |
} |
| 905 |
}) |
| 906 |
.test(); |
| 907 |
|
| 908 |
ps.onNext(1); |
| 909 |
|
| 910 |
assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS)); |
| 911 |
|
| 912 |
ps.onComplete(); |
| 913 |
|
| 914 |
assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS)); |
| 915 |
|
| 916 |
assertFalse("The doOnNext got interrupted!", isInterrupted.get()); |
| 917 |
} |
| 918 |
|
| 919 |
@Test |
| 920 |
@SuppressUndeliverable |
| 921 |
public void skipTimeAndSizeBoundNoInterruptWindowOutputOnError() throws Exception { |
| 922 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 923 |
|
| 924 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 925 |
|
| 926 |
final CountDownLatch doOnNextDone = new CountDownLatch(1); |
| 927 |
final CountDownLatch secondWindowProcessing = new CountDownLatch(1); |
| 928 |
|
| 929 |
ps.window(90, 100, TimeUnit.MILLISECONDS) |
| 930 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 931 |
int count; |
| 932 |
@Override |
| 933 |
public void accept(Observable<Integer> v) throws Exception { |
| 934 |
System.out.println(Thread.currentThread()); |
| 935 |
if (count++ == 1) { |
| 936 |
secondWindowProcessing.countDown(); |
| 937 |
try { |
| 938 |
Thread.sleep(200); |
| 939 |
isInterrupted.set(Thread.interrupted()); |
| 940 |
} catch (InterruptedException ex) { |
| 941 |
isInterrupted.set(true); |
| 942 |
} |
| 943 |
doOnNextDone.countDown(); |
| 944 |
} |
| 945 |
} |
| 946 |
}) |
| 947 |
.test(); |
| 948 |
|
| 949 |
ps.onNext(1); |
| 950 |
|
| 951 |
assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS)); |
| 952 |
|
| 953 |
ps.onError(new TestException()); |
| 954 |
|
| 955 |
assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS)); |
| 956 |
|
| 957 |
assertFalse("The doOnNext got interrupted!", isInterrupted.get()); |
| 958 |
} |
| 959 |
|
| 960 |
@Test |
| 961 |
public void cancellingWindowCancelsUpstreamExactTime() { |
| 962 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 963 |
|
| 964 |
TestObserver<Integer> to = ps.window(10, TimeUnit.MINUTES) |
| 965 |
.take(1) |
| 966 |
.flatMap(new Function<Observable<Integer>, Observable<Integer>>() { |
| 967 |
@Override |
| 968 |
public Observable<Integer> apply(Observable<Integer> w) throws Throwable { |
| 969 |
return w.take(1); |
| 970 |
} |
| 971 |
}) |
| 972 |
.test(); |
| 973 |
|
| 974 |
assertTrue(ps.hasObservers()); |
| 975 |
|
| 976 |
ps.onNext(1); |
| 977 |
|
| 978 |
to |
| 979 |
.assertResult(1); |
| 980 |
|
| 981 |
assertFalse("Subject still has observers!", ps.hasObservers()); |
| 982 |
} |
| 983 |
|
| 984 |
@Test |
| 985 |
public void windowAbandonmentCancelsUpstreamExactTime() { |
| 986 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 987 |
|
| 988 |
final AtomicReference<Observable<Integer>> inner = new AtomicReference<>(); |
| 989 |
|
| 990 |
TestObserver<Observable<Integer>> to = ps.window(10, TimeUnit.MINUTES) |
| 991 |
.take(1) |
| 992 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 993 |
@Override |
| 994 |
public void accept(Observable<Integer> v) throws Throwable { |
| 995 |
inner.set(v); |
| 996 |
} |
| 997 |
}) |
| 998 |
.test(); |
| 999 |
|
| 1000 |
assertFalse("Subject still has observers!", ps.hasObservers()); |
| 1001 |
|
| 1002 |
to |
| 1003 |
.assertValueCount(1) |
| 1004 |
.assertNoErrors() |
| 1005 |
.assertComplete(); |
| 1006 |
|
| 1007 |
inner.get().test().assertResult(); |
| 1008 |
} |
| 1009 |
|
| 1010 |
@Test |
| 1011 |
public void cancellingWindowCancelsUpstreamExactTimeAndSize() { |
| 1012 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 1013 |
|
| 1014 |
TestObserver<Integer> to = ps.window(10, TimeUnit.MINUTES, 100) |
| 1015 |
.take(1) |
| 1016 |
.flatMap(new Function<Observable<Integer>, Observable<Integer>>() { |
| 1017 |
@Override |
| 1018 |
public Observable<Integer> apply(Observable<Integer> w) throws Throwable { |
| 1019 |
return w.take(1); |
| 1020 |
} |
| 1021 |
}) |
| 1022 |
.test(); |
| 1023 |
|
| 1024 |
assertTrue(ps.hasObservers()); |
| 1025 |
|
| 1026 |
ps.onNext(1); |
| 1027 |
|
| 1028 |
to |
| 1029 |
.assertResult(1); |
| 1030 |
|
| 1031 |
assertFalse("Subject still has observers!", ps.hasObservers()); |
| 1032 |
} |
| 1033 |
|
| 1034 |
@Test |
| 1035 |
public void windowAbandonmentCancelsUpstreamExactTimeAndSize() { |
| 1036 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 1037 |
|
| 1038 |
final AtomicReference<Observable<Integer>> inner = new AtomicReference<>(); |
| 1039 |
|
| 1040 |
TestObserver<Observable<Integer>> to = ps.window(10, TimeUnit.MINUTES, 100) |
| 1041 |
.take(1) |
| 1042 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 1043 |
@Override |
| 1044 |
public void accept(Observable<Integer> v) throws Throwable { |
| 1045 |
inner.set(v); |
| 1046 |
} |
| 1047 |
}) |
| 1048 |
.test(); |
| 1049 |
|
| 1050 |
assertFalse("Subject still has observers!", ps.hasObservers()); |
| 1051 |
|
| 1052 |
to |
| 1053 |
.assertValueCount(1) |
| 1054 |
.assertNoErrors() |
| 1055 |
.assertComplete(); |
| 1056 |
|
| 1057 |
inner.get().test().assertResult(); |
| 1058 |
} |
| 1059 |
|
| 1060 |
@Test |
| 1061 |
public void cancellingWindowCancelsUpstreamExactTimeSkip() { |
| 1062 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 1063 |
|
| 1064 |
TestObserver<Integer> to = ps.window(10, 15, TimeUnit.MINUTES) |
| 1065 |
.take(1) |
| 1066 |
.flatMap(new Function<Observable<Integer>, Observable<Integer>>() { |
| 1067 |
@Override |
| 1068 |
public Observable<Integer> apply(Observable<Integer> w) throws Throwable { |
| 1069 |
return w.take(1); |
| 1070 |
} |
| 1071 |
}) |
| 1072 |
.test(); |
| 1073 |
|
| 1074 |
assertTrue(ps.hasObservers()); |
| 1075 |
|
| 1076 |
ps.onNext(1); |
| 1077 |
|
| 1078 |
to |
| 1079 |
.assertResult(1); |
| 1080 |
|
| 1081 |
assertFalse("Subject still has observers!", ps.hasObservers()); |
| 1082 |
} |
| 1083 |
|
| 1084 |
@Test |
| 1085 |
public void windowAbandonmentCancelsUpstreamExactTimeSkip() { |
| 1086 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 1087 |
|
| 1088 |
final AtomicReference<Observable<Integer>> inner = new AtomicReference<>(); |
| 1089 |
|
| 1090 |
TestObserver<Observable<Integer>> to = ps.window(10, 15, TimeUnit.MINUTES) |
| 1091 |
.take(1) |
| 1092 |
.doOnNext(new Consumer<Observable<Integer>>() { |
| 1093 |
@Override |
| 1094 |
public void accept(Observable<Integer> v) throws Throwable { |
| 1095 |
inner.set(v); |
| 1096 |
} |
| 1097 |
}) |
| 1098 |
.test(); |
| 1099 |
|
| 1100 |
assertFalse("Subject still has observers!", ps.hasObservers()); |
| 1101 |
|
| 1102 |
to |
| 1103 |
.assertValueCount(1) |
| 1104 |
.assertNoErrors() |
| 1105 |
.assertComplete(); |
| 1106 |
|
| 1107 |
inner.get().test().assertResult(); |
| 1108 |
} |
| 1109 |
|
| 1110 |
@Test |
| 1111 |
public void doubleOnSubscribe() { |
| 1112 |
TestHelper.checkDoubleOnSubscribeObservable(o -> o.window(1, TimeUnit.SECONDS)); |
| 1113 |
} |
| 1114 |
|
| 1115 |
@Test |
| 1116 |
public void timedBoundarySignalAndDisposeRace() { |
| 1117 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1118 |
TestScheduler scheduler = new TestScheduler(); |
| 1119 |
|
| 1120 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 1121 |
|
| 1122 |
TestObserver<Observable<Integer>> to = ps.window(1, TimeUnit.MINUTES, scheduler, 1) |
| 1123 |
.test(); |
| 1124 |
|
| 1125 |
TestHelper.race( |
| 1126 |
() -> ps.onNext(1), |
| 1127 |
() -> to.dispose() |
| 1128 |
); |
| 1129 |
} |
| 1130 |
} |
| 1131 |
} |