| 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.RxJavaTest; |
| 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 FlowableThrottleLastTests 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.throttleLast(500, TimeUnit.MILLISECONDS, s).subscribe(subscriber); |
| 38 |
|
| 39 |
// send events with simulated time increments |
| 40 |
s.advanceTimeTo(0, TimeUnit.MILLISECONDS); |
| 41 |
o.onNext(1); // skip |
| 42 |
o.onNext(2); // deliver |
| 43 |
s.advanceTimeTo(501, TimeUnit.MILLISECONDS); |
| 44 |
o.onNext(3); // skip |
| 45 |
s.advanceTimeTo(600, TimeUnit.MILLISECONDS); |
| 46 |
o.onNext(4); // skip |
| 47 |
s.advanceTimeTo(700, TimeUnit.MILLISECONDS); |
| 48 |
o.onNext(5); // skip |
| 49 |
o.onNext(6); // deliver |
| 50 |
s.advanceTimeTo(1001, TimeUnit.MILLISECONDS); |
| 51 |
o.onNext(7); // deliver |
| 52 |
s.advanceTimeTo(1501, TimeUnit.MILLISECONDS); |
| 53 |
o.onComplete(); |
| 54 |
|
| 55 |
InOrder inOrder = inOrder(subscriber); |
| 56 |
inOrder.verify(subscriber).onNext(2); |
| 57 |
inOrder.verify(subscriber).onNext(6); |
| 58 |
inOrder.verify(subscriber).onNext(7); |
| 59 |
inOrder.verify(subscriber).onComplete(); |
| 60 |
inOrder.verifyNoMoreInteractions(); |
| 61 |
} |
| 62 |
} |