| 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.List; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
import java.util.concurrent.atomic.AtomicReference; |
| 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.disposables.Disposable; |
| 30 |
import io.reactivex.rxjava3.exceptions.*; |
| 31 |
import io.reactivex.rxjava3.functions.Function; |
| 32 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 33 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableDebounceTimed.*; |
| 34 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 35 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 36 |
import io.reactivex.rxjava3.processors.*; |
| 37 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 38 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 39 |
import io.reactivex.rxjava3.testsupport.*; |
| 40 |
|
| 41 |
public class FlowableDebounceTest extends RxJavaTest { |
| 42 |
|
| 43 |
private TestScheduler scheduler; |
| 44 |
private Subscriber<String> Subscriber; |
| 45 |
private Scheduler.Worker innerScheduler; |
| 46 |
|
| 47 |
@Before |
| 48 |
public void before() { |
| 49 |
scheduler = new TestScheduler(); |
| 50 |
Subscriber = TestHelper.mockSubscriber(); |
| 51 |
innerScheduler = scheduler.createWorker(); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void debounceWithCompleted() { |
| 56 |
Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() { |
| 57 |
@Override |
| 58 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 59 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 60 |
publishNext(subscriber, 100, "one"); // Should be skipped since "two" will arrive before the timeout expires. |
| 61 |
publishNext(subscriber, 400, "two"); // Should be published since "three" will arrive after the timeout expires. |
| 62 |
publishNext(subscriber, 900, "three"); // Should be skipped since onComplete will arrive before the timeout expires. |
| 63 |
publishCompleted(subscriber, 1000); // Should be published as soon as the timeout expires. |
| 64 |
} |
| 65 |
}); |
| 66 |
|
| 67 |
Flowable<String> sampled = source.debounce(400, TimeUnit.MILLISECONDS, scheduler); |
| 68 |
sampled.subscribe(Subscriber); |
| 69 |
|
| 70 |
scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS); |
| 71 |
InOrder inOrder = inOrder(Subscriber); |
| 72 |
// must go to 800 since it must be 400 after when two is sent, which is at 400 |
| 73 |
scheduler.advanceTimeTo(800, TimeUnit.MILLISECONDS); |
| 74 |
inOrder.verify(Subscriber, times(1)).onNext("two"); |
| 75 |
scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS); |
| 76 |
inOrder.verify(Subscriber, times(1)).onComplete(); |
| 77 |
inOrder.verifyNoMoreInteractions(); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void debounceNeverEmits() { |
| 82 |
Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() { |
| 83 |
@Override |
| 84 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 85 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 86 |
// all should be skipped since they are happening faster than the 200ms timeout |
| 87 |
publishNext(subscriber, 100, "a"); // Should be skipped |
| 88 |
publishNext(subscriber, 200, "b"); // Should be skipped |
| 89 |
publishNext(subscriber, 300, "c"); // Should be skipped |
| 90 |
publishNext(subscriber, 400, "d"); // Should be skipped |
| 91 |
publishNext(subscriber, 500, "e"); // Should be skipped |
| 92 |
publishNext(subscriber, 600, "f"); // Should be skipped |
| 93 |
publishNext(subscriber, 700, "g"); // Should be skipped |
| 94 |
publishNext(subscriber, 800, "h"); // Should be skipped |
| 95 |
publishCompleted(subscriber, 900); // Should be published as soon as the timeout expires. |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
Flowable<String> sampled = source.debounce(200, TimeUnit.MILLISECONDS, scheduler); |
| 100 |
sampled.subscribe(Subscriber); |
| 101 |
|
| 102 |
scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS); |
| 103 |
InOrder inOrder = inOrder(Subscriber); |
| 104 |
inOrder.verify(Subscriber, times(0)).onNext(anyString()); |
| 105 |
scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS); |
| 106 |
inOrder.verify(Subscriber, times(1)).onComplete(); |
| 107 |
inOrder.verifyNoMoreInteractions(); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void debounceWithError() { |
| 112 |
Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() { |
| 113 |
@Override |
| 114 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 115 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 116 |
Exception error = new TestException(); |
| 117 |
publishNext(subscriber, 100, "one"); // Should be published since "two" will arrive after the timeout expires. |
| 118 |
publishNext(subscriber, 600, "two"); // Should be skipped since onError will arrive before the timeout expires. |
| 119 |
publishError(subscriber, 700, error); // Should be published as soon as the timeout expires. |
| 120 |
} |
| 121 |
}); |
| 122 |
|
| 123 |
Flowable<String> sampled = source.debounce(400, TimeUnit.MILLISECONDS, scheduler); |
| 124 |
sampled.subscribe(Subscriber); |
| 125 |
|
| 126 |
scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS); |
| 127 |
InOrder inOrder = inOrder(Subscriber); |
| 128 |
// 100 + 400 means it triggers at 500 |
| 129 |
scheduler.advanceTimeTo(500, TimeUnit.MILLISECONDS); |
| 130 |
inOrder.verify(Subscriber).onNext("one"); |
| 131 |
scheduler.advanceTimeTo(701, TimeUnit.MILLISECONDS); |
| 132 |
inOrder.verify(Subscriber).onError(any(TestException.class)); |
| 133 |
inOrder.verifyNoMoreInteractions(); |
| 134 |
} |
| 135 |
|
| 136 |
private <T> void publishCompleted(final Subscriber<T> subscriber, long delay) { |
| 137 |
innerScheduler.schedule(new Runnable() { |
| 138 |
@Override |
| 139 |
public void run() { |
| 140 |
subscriber.onComplete(); |
| 141 |
} |
| 142 |
}, delay, TimeUnit.MILLISECONDS); |
| 143 |
} |
| 144 |
|
| 145 |
private <T> void publishError(final Subscriber<T> subscriber, long delay, final Exception error) { |
| 146 |
innerScheduler.schedule(new Runnable() { |
| 147 |
@Override |
| 148 |
public void run() { |
| 149 |
subscriber.onError(error); |
| 150 |
} |
| 151 |
}, delay, TimeUnit.MILLISECONDS); |
| 152 |
} |
| 153 |
|
| 154 |
private <T> void publishNext(final Subscriber<T> subscriber, final long delay, final T value) { |
| 155 |
innerScheduler.schedule(new Runnable() { |
| 156 |
@Override |
| 157 |
public void run() { |
| 158 |
subscriber.onNext(value); |
| 159 |
} |
| 160 |
}, delay, TimeUnit.MILLISECONDS); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void debounceSelectorNormal1() { |
| 165 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 166 |
final PublishProcessor<Integer> debouncer = PublishProcessor.create(); |
| 167 |
Function<Integer, Flowable<Integer>> debounceSel = new Function<Integer, Flowable<Integer>>() { |
| 168 |
|
| 169 |
@Override |
| 170 |
public Flowable<Integer> apply(Integer t1) { |
| 171 |
return debouncer; |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 176 |
InOrder inOrder = inOrder(subscriber); |
| 177 |
|
| 178 |
source.debounce(debounceSel).subscribe(subscriber); |
| 179 |
|
| 180 |
source.onNext(1); |
| 181 |
debouncer.onNext(1); |
| 182 |
|
| 183 |
source.onNext(2); |
| 184 |
source.onNext(3); |
| 185 |
source.onNext(4); |
| 186 |
|
| 187 |
debouncer.onNext(2); |
| 188 |
|
| 189 |
source.onNext(5); |
| 190 |
source.onComplete(); |
| 191 |
|
| 192 |
inOrder.verify(subscriber).onNext(1); |
| 193 |
inOrder.verify(subscriber).onNext(4); |
| 194 |
inOrder.verify(subscriber).onNext(5); |
| 195 |
inOrder.verify(subscriber).onComplete(); |
| 196 |
|
| 197 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void debounceSelectorFuncThrows() { |
| 202 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 203 |
Function<Integer, Flowable<Integer>> debounceSel = new Function<Integer, Flowable<Integer>>() { |
| 204 |
|
| 205 |
@Override |
| 206 |
public Flowable<Integer> apply(Integer t1) { |
| 207 |
throw new TestException(); |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 212 |
|
| 213 |
source.debounce(debounceSel).subscribe(subscriber); |
| 214 |
|
| 215 |
source.onNext(1); |
| 216 |
|
| 217 |
verify(subscriber, never()).onNext(any()); |
| 218 |
verify(subscriber, never()).onComplete(); |
| 219 |
verify(subscriber).onError(any(TestException.class)); |
| 220 |
} |
| 221 |
|
| 222 |
@Test |
| 223 |
public void debounceSelectorFlowableThrows() { |
| 224 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 225 |
Function<Integer, Flowable<Integer>> debounceSel = new Function<Integer, Flowable<Integer>>() { |
| 226 |
|
| 227 |
@Override |
| 228 |
public Flowable<Integer> apply(Integer t1) { |
| 229 |
return Flowable.error(new TestException()); |
| 230 |
} |
| 231 |
}; |
| 232 |
|
| 233 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 234 |
|
| 235 |
source.debounce(debounceSel).subscribe(subscriber); |
| 236 |
|
| 237 |
source.onNext(1); |
| 238 |
|
| 239 |
verify(subscriber, never()).onNext(any()); |
| 240 |
verify(subscriber, never()).onComplete(); |
| 241 |
verify(subscriber).onError(any(TestException.class)); |
| 242 |
} |
| 243 |
|
| 244 |
@Test |
| 245 |
public void debounceTimedLastIsNotLost() { |
| 246 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 247 |
|
| 248 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 249 |
|
| 250 |
source.debounce(100, TimeUnit.MILLISECONDS, scheduler).subscribe(subscriber); |
| 251 |
|
| 252 |
source.onNext(1); |
| 253 |
source.onComplete(); |
| 254 |
|
| 255 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 256 |
|
| 257 |
verify(subscriber).onNext(1); |
| 258 |
verify(subscriber).onComplete(); |
| 259 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 260 |
} |
| 261 |
|
| 262 |
@Test |
| 263 |
public void debounceSelectorLastIsNotLost() { |
| 264 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 265 |
final PublishProcessor<Integer> debouncer = PublishProcessor.create(); |
| 266 |
|
| 267 |
Function<Integer, Flowable<Integer>> debounceSel = new Function<Integer, Flowable<Integer>>() { |
| 268 |
|
| 269 |
@Override |
| 270 |
public Flowable<Integer> apply(Integer t1) { |
| 271 |
return debouncer; |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 276 |
|
| 277 |
source.debounce(debounceSel).subscribe(subscriber); |
| 278 |
|
| 279 |
source.onNext(1); |
| 280 |
source.onComplete(); |
| 281 |
|
| 282 |
debouncer.onComplete(); |
| 283 |
|
| 284 |
verify(subscriber).onNext(1); |
| 285 |
verify(subscriber).onComplete(); |
| 286 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 287 |
} |
| 288 |
|
| 289 |
@Test |
| 290 |
public void debounceWithTimeBackpressure() throws InterruptedException { |
| 291 |
TestScheduler scheduler = new TestScheduler(); |
| 292 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(); |
| 293 |
Flowable.merge( |
| 294 |
Flowable.just(1), |
| 295 |
Flowable.just(2).delay(10, TimeUnit.MILLISECONDS, scheduler) |
| 296 |
).debounce(20, TimeUnit.MILLISECONDS, scheduler).take(1).subscribe(subscriber); |
| 297 |
|
| 298 |
scheduler.advanceTimeBy(30, TimeUnit.MILLISECONDS); |
| 299 |
|
| 300 |
subscriber.assertValue(2); |
| 301 |
subscriber.assertTerminated(); |
| 302 |
subscriber.assertNoErrors(); |
| 303 |
} |
| 304 |
|
| 305 |
@Test |
| 306 |
public void debounceDefaultScheduler() throws Exception { |
| 307 |
|
| 308 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 309 |
|
| 310 |
Flowable.range(1, 1000).debounce(1, TimeUnit.SECONDS).subscribe(ts); |
| 311 |
|
| 312 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 313 |
ts.assertValue(1000); |
| 314 |
ts.assertNoErrors(); |
| 315 |
ts.assertComplete(); |
| 316 |
} |
| 317 |
|
| 318 |
@Test |
| 319 |
public void debounceDefault() throws Exception { |
| 320 |
|
| 321 |
Flowable.just(1).debounce(1, TimeUnit.SECONDS) |
| 322 |
.test() |
| 323 |
.awaitDone(5, TimeUnit.SECONDS) |
| 324 |
.assertResult(1); |
| 325 |
} |
| 326 |
|
| 327 |
@Test |
| 328 |
public void dispose() { |
| 329 |
TestHelper.checkDisposed(PublishProcessor.create().debounce(1, TimeUnit.SECONDS, new TestScheduler())); |
| 330 |
|
| 331 |
TestHelper.checkDisposed(PublishProcessor.create().debounce(Functions.justFunction(Flowable.never()))); |
| 332 |
|
| 333 |
Disposable d = new FlowableDebounceTimed.DebounceEmitter<>(1, 1, null); |
| 334 |
assertFalse(d.isDisposed()); |
| 335 |
|
| 336 |
d.dispose(); |
| 337 |
|
| 338 |
assertTrue(d.isDisposed()); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void badSource() { |
| 343 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 344 |
try { |
| 345 |
new Flowable<Integer>() { |
| 346 |
@Override |
| 347 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 348 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 349 |
subscriber.onComplete(); |
| 350 |
subscriber.onNext(1); |
| 351 |
subscriber.onError(new TestException()); |
| 352 |
subscriber.onComplete(); |
| 353 |
} |
| 354 |
} |
| 355 |
.debounce(1, TimeUnit.SECONDS, new TestScheduler()) |
| 356 |
.test() |
| 357 |
.assertResult(); |
| 358 |
|
| 359 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 360 |
} finally { |
| 361 |
RxJavaPlugins.reset(); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
@Test |
| 366 |
public void badSourceSelector() { |
| 367 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 368 |
@Override |
| 369 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 370 |
return f.debounce(new Function<Integer, Flowable<Long>>() { |
| 371 |
@Override |
| 372 |
public Flowable<Long> apply(Integer v) throws Exception { |
| 373 |
return Flowable.timer(1, TimeUnit.SECONDS); |
| 374 |
} |
| 375 |
}); |
| 376 |
} |
| 377 |
}, false, 1, 1, 1); |
| 378 |
|
| 379 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 380 |
@Override |
| 381 |
public Object apply(final Flowable<Integer> f) throws Exception { |
| 382 |
return Flowable.just(1).debounce(new Function<Integer, Flowable<Integer>>() { |
| 383 |
@Override |
| 384 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 385 |
return f; |
| 386 |
} |
| 387 |
}); |
| 388 |
} |
| 389 |
}, false, 1, 1, 1); |
| 390 |
} |
| 391 |
|
| 392 |
@Test |
| 393 |
public void debounceWithEmpty() { |
| 394 |
Flowable.just(1).debounce(Functions.justFunction(Flowable.empty())) |
| 395 |
.test() |
| 396 |
.assertResult(1); |
| 397 |
} |
| 398 |
|
| 399 |
@Test |
| 400 |
public void backpressureNoRequest() { |
| 401 |
Flowable.just(1) |
| 402 |
.debounce(Functions.justFunction(Flowable.timer(1, TimeUnit.MILLISECONDS))) |
| 403 |
.test(0L) |
| 404 |
.awaitDone(5, TimeUnit.SECONDS) |
| 405 |
.assertFailure(MissingBackpressureException.class); |
| 406 |
} |
| 407 |
|
| 408 |
@Test |
| 409 |
public void backpressureNoRequestTimed() { |
| 410 |
Flowable.just(1) |
| 411 |
.debounce(1, TimeUnit.MILLISECONDS) |
| 412 |
.test(0L) |
| 413 |
.awaitDone(5, TimeUnit.SECONDS) |
| 414 |
.assertFailure(MissingBackpressureException.class); |
| 415 |
} |
| 416 |
|
| 417 |
@Test |
| 418 |
public void doubleOnSubscribe() { |
| 419 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 420 |
@Override |
| 421 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 422 |
return f.debounce(Functions.justFunction(Flowable.never())); |
| 423 |
} |
| 424 |
}); |
| 425 |
} |
| 426 |
|
| 427 |
@Test |
| 428 |
public void disposeInOnNext() { |
| 429 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 430 |
|
| 431 |
BehaviorProcessor.createDefault(1) |
| 432 |
.debounce(new Function<Integer, Flowable<Object>>() { |
| 433 |
@Override |
| 434 |
public Flowable<Object> apply(Integer o) throws Exception { |
| 435 |
ts.cancel(); |
| 436 |
return Flowable.never(); |
| 437 |
} |
| 438 |
}) |
| 439 |
.subscribeWith(ts) |
| 440 |
.assertEmpty(); |
| 441 |
|
| 442 |
assertTrue(ts.isCancelled()); |
| 443 |
} |
| 444 |
|
| 445 |
@Test |
| 446 |
public void disposedInOnComplete() { |
| 447 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 448 |
|
| 449 |
new Flowable<Integer>() { |
| 450 |
@Override |
| 451 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 452 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 453 |
ts.cancel(); |
| 454 |
subscriber.onComplete(); |
| 455 |
} |
| 456 |
} |
| 457 |
.debounce(Functions.justFunction(Flowable.never())) |
| 458 |
.subscribeWith(ts) |
| 459 |
.assertEmpty(); |
| 460 |
} |
| 461 |
|
| 462 |
@Test |
| 463 |
public void emitLate() { |
| 464 |
final AtomicReference<Subscriber<? super Integer>> ref = new AtomicReference<>(); |
| 465 |
|
| 466 |
TestSubscriber<Integer> ts = Flowable.range(1, 2) |
| 467 |
.debounce(new Function<Integer, Flowable<Integer>>() { |
| 468 |
@Override |
| 469 |
public Flowable<Integer> apply(Integer o) throws Exception { |
| 470 |
if (o != 1) { |
| 471 |
return Flowable.never(); |
| 472 |
} |
| 473 |
return new Flowable<Integer>() { |
| 474 |
@Override |
| 475 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 476 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 477 |
ref.set(subscriber); |
| 478 |
} |
| 479 |
}; |
| 480 |
} |
| 481 |
}) |
| 482 |
.test(); |
| 483 |
|
| 484 |
ref.get().onNext(1); |
| 485 |
|
| 486 |
ts |
| 487 |
.assertResult(2); |
| 488 |
} |
| 489 |
|
| 490 |
@Test |
| 491 |
public void badRequestReported() { |
| 492 |
TestHelper.assertBadRequestReported(Flowable.never().debounce(Functions.justFunction(Flowable.never()))); |
| 493 |
} |
| 494 |
|
| 495 |
@Test |
| 496 |
public void timedDoubleOnSubscribe() { |
| 497 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() { |
| 498 |
@Override |
| 499 |
public Publisher<Object> apply(Flowable<Object> f) |
| 500 |
throws Exception { |
| 501 |
return f.debounce(1, TimeUnit.SECONDS); |
| 502 |
} |
| 503 |
}); |
| 504 |
} |
| 505 |
|
| 506 |
@Test |
| 507 |
public void timedDisposedIgnoredBySource() { |
| 508 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 509 |
|
| 510 |
new Flowable<Integer>() { |
| 511 |
@Override |
| 512 |
protected void subscribeActual( |
| 513 |
org.reactivestreams.Subscriber<? super Integer> s) { |
| 514 |
s.onSubscribe(new BooleanSubscription()); |
| 515 |
ts.cancel(); |
| 516 |
s.onNext(1); |
| 517 |
s.onComplete(); |
| 518 |
} |
| 519 |
} |
| 520 |
.debounce(1, TimeUnit.SECONDS) |
| 521 |
.subscribe(ts); |
| 522 |
} |
| 523 |
|
| 524 |
@Test |
| 525 |
public void timedBadRequest() { |
| 526 |
TestHelper.assertBadRequestReported(Flowable.never().debounce(1, TimeUnit.SECONDS)); |
| 527 |
} |
| 528 |
|
| 529 |
@Test |
| 530 |
public void timedLateEmit() { |
| 531 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 532 |
DebounceTimedSubscriber<Integer> sub = new DebounceTimedSubscriber<>( |
| 533 |
ts, 1, TimeUnit.SECONDS, new TestScheduler().createWorker()); |
| 534 |
|
| 535 |
sub.onSubscribe(new BooleanSubscription()); |
| 536 |
|
| 537 |
DebounceEmitter<Integer> de = new DebounceEmitter<>(1, 50, sub); |
| 538 |
de.emit(); |
| 539 |
de.emit(); |
| 540 |
|
| 541 |
ts.assertEmpty(); |
| 542 |
} |
| 543 |
|
| 544 |
@Test |
| 545 |
public void timedError() { |
| 546 |
Flowable.error(new TestException()) |
| 547 |
.debounce(1, TimeUnit.SECONDS) |
| 548 |
.test() |
| 549 |
.assertFailure(TestException.class); |
| 550 |
} |
| 551 |
|
| 552 |
@Test |
| 553 |
public void debounceOnEmpty() { |
| 554 |
Flowable.empty().debounce(new Function<Object, Publisher<Object>>() { |
| 555 |
@Override |
| 556 |
public Publisher<Object> apply(Object o) { |
| 557 |
return Flowable.just(new Object()); |
| 558 |
} |
| 559 |
}).subscribe(); |
| 560 |
} |
| 561 |
} |