Debian Codegraph @

src/test/java/io/reactivex/rxjava3/flowable/FlowableThrottleWithTimeoutTests.java

raw
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.flowable;
15
16 import static org.mockito.Mockito.inOrder;
17
18 import java.util.concurrent.TimeUnit;
19
20 import org.junit.Test;
21 import org.mockito.InOrder;
22 import org.reactivestreams.Subscriber;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.processors.PublishProcessor;
26 import io.reactivex.rxjava3.schedulers.TestScheduler;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class FlowableThrottleWithTimeoutTests extends RxJavaTest {
30
31 @Test
32 public void throttle() {
33 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
34
35 TestScheduler s = new TestScheduler();
36 PublishProcessor<Integer> o = PublishProcessor.create();
37 o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s)
38 .subscribe(subscriber);
39
40 // send events with simulated time increments
41 s.advanceTimeTo(0, TimeUnit.MILLISECONDS);
42 o.onNext(1); // skip
43 o.onNext(2); // deliver
44 s.advanceTimeTo(501, TimeUnit.MILLISECONDS);
45 o.onNext(3); // skip
46 s.advanceTimeTo(600, TimeUnit.MILLISECONDS);
47 o.onNext(4); // skip
48 s.advanceTimeTo(700, TimeUnit.MILLISECONDS);
49 o.onNext(5); // skip
50 o.onNext(6); // deliver at 1300 after 500ms has passed since onNext(5)
51 s.advanceTimeTo(1300, TimeUnit.MILLISECONDS);
52 o.onNext(7); // deliver
53 s.advanceTimeTo(1800, TimeUnit.MILLISECONDS);
54 o.onComplete();
55
56 InOrder inOrder = inOrder(subscriber);
57 inOrder.verify(subscriber).onNext(2);
58 inOrder.verify(subscriber).onNext(6);
59 inOrder.verify(subscriber).onNext(7);
60 inOrder.verify(subscriber).onComplete();
61 inOrder.verifyNoMoreInteractions();
62 }
63
64 @Test
65 public void throttleFirstDefaultScheduler() {
66 Flowable.just(1).throttleWithTimeout(100, TimeUnit.MILLISECONDS)
67 .test()
68 .awaitDone(5, TimeUnit.SECONDS)
69 .assertResult(1);
70 }
71 }