Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableThrottleFirstTest.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.internal.operators.flowable;
15
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 import org.junit.*;
23 import org.mockito.InOrder;
24 import org.reactivestreams.*;
25
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.exceptions.*;
28 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.PublishProcessor;
31 import io.reactivex.rxjava3.schedulers.TestScheduler;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class FlowableThrottleFirstTest extends RxJavaTest {
35
36 private TestScheduler scheduler;
37 private Scheduler.Worker innerScheduler;
38 private Subscriber<String> subscriber;
39
40 @Before
41 public void before() {
42 scheduler = new TestScheduler();
43 innerScheduler = scheduler.createWorker();
44 subscriber = TestHelper.mockSubscriber();
45 }
46
47 @Test
48 public void throttlingWithCompleted() {
49 Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() {
50 @Override
51 public void subscribe(Subscriber<? super String> subscriber) {
52 subscriber.onSubscribe(new BooleanSubscription());
53 publishNext(subscriber, 100, "one"); // publish as it's first
54 publishNext(subscriber, 300, "two"); // skip as it's last within the first 400
55 publishNext(subscriber, 900, "three"); // publish
56 publishNext(subscriber, 905, "four"); // skip
57 publishCompleted(subscriber, 1000); // Should be published as soon as the timeout expires.
58 }
59 });
60
61 Flowable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler);
62 sampled.subscribe(subscriber);
63
64 InOrder inOrder = inOrder(subscriber);
65
66 scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
67 inOrder.verify(subscriber, times(1)).onNext("one");
68 inOrder.verify(subscriber, times(0)).onNext("two");
69 inOrder.verify(subscriber, times(1)).onNext("three");
70 inOrder.verify(subscriber, times(0)).onNext("four");
71 inOrder.verify(subscriber, times(1)).onComplete();
72 inOrder.verifyNoMoreInteractions();
73 }
74
75 @Test
76 public void throttlingWithError() {
77 Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() {
78 @Override
79 public void subscribe(Subscriber<? super String> subscriber) {
80 subscriber.onSubscribe(new BooleanSubscription());
81 Exception error = new TestException();
82 publishNext(subscriber, 100, "one"); // Should be published since it is first
83 publishNext(subscriber, 200, "two"); // Should be skipped since onError will arrive before the timeout expires
84 publishError(subscriber, 300, error); // Should be published as soon as the timeout expires.
85 }
86 });
87
88 Flowable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler);
89 sampled.subscribe(subscriber);
90
91 InOrder inOrder = inOrder(subscriber);
92
93 scheduler.advanceTimeTo(400, TimeUnit.MILLISECONDS);
94 inOrder.verify(subscriber).onNext("one");
95 inOrder.verify(subscriber).onError(any(TestException.class));
96 inOrder.verifyNoMoreInteractions();
97 }
98
99 private <T> void publishCompleted(final Subscriber<T> subscriber, long delay) {
100 innerScheduler.schedule(new Runnable() {
101 @Override
102 public void run() {
103 subscriber.onComplete();
104 }
105 }, delay, TimeUnit.MILLISECONDS);
106 }
107
108 private <T> void publishError(final Subscriber<T> subscriber, long delay, final Exception error) {
109 innerScheduler.schedule(new Runnable() {
110 @Override
111 public void run() {
112 subscriber.onError(error);
113 }
114 }, delay, TimeUnit.MILLISECONDS);
115 }
116
117 private <T> void publishNext(final Subscriber<T> subscriber, long delay, final T value) {
118 innerScheduler.schedule(new Runnable() {
119 @Override
120 public void run() {
121 subscriber.onNext(value);
122 }
123 }, delay, TimeUnit.MILLISECONDS);
124 }
125
126 @Test
127 public void throttle() {
128 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
129 TestScheduler s = new TestScheduler();
130 PublishProcessor<Integer> o = PublishProcessor.create();
131 o.throttleFirst(500, TimeUnit.MILLISECONDS, s).subscribe(subscriber);
132
133 // send events with simulated time increments
134 s.advanceTimeTo(0, TimeUnit.MILLISECONDS);
135 o.onNext(1); // deliver
136 o.onNext(2); // skip
137 s.advanceTimeTo(501, TimeUnit.MILLISECONDS);
138 o.onNext(3); // deliver
139 s.advanceTimeTo(600, TimeUnit.MILLISECONDS);
140 o.onNext(4); // skip
141 s.advanceTimeTo(700, TimeUnit.MILLISECONDS);
142 o.onNext(5); // skip
143 o.onNext(6); // skip
144 s.advanceTimeTo(1001, TimeUnit.MILLISECONDS);
145 o.onNext(7); // deliver
146 s.advanceTimeTo(1501, TimeUnit.MILLISECONDS);
147 o.onComplete();
148
149 InOrder inOrder = inOrder(subscriber);
150 inOrder.verify(subscriber).onNext(1);
151 inOrder.verify(subscriber).onNext(3);
152 inOrder.verify(subscriber).onNext(7);
153 inOrder.verify(subscriber).onComplete();
154 inOrder.verifyNoMoreInteractions();
155 }
156
157 @Test
158 public void throttleFirstDefaultScheduler() {
159 Flowable.just(1).throttleFirst(100, TimeUnit.MILLISECONDS)
160 .test()
161 .awaitDone(5, TimeUnit.SECONDS)
162 .assertResult(1);
163 }
164
165 @Test
166 public void dispose() {
167 TestHelper.checkDisposed(Flowable.just(1).throttleFirst(1, TimeUnit.DAYS));
168 }
169
170 @Test
171 public void badSource() {
172 List<Throwable> errors = TestHelper.trackPluginErrors();
173 try {
174 new Flowable<Integer>() {
175 @Override
176 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
177 subscriber.onSubscribe(new BooleanSubscription());
178 subscriber.onNext(1);
179 subscriber.onNext(2);
180 subscriber.onComplete();
181 subscriber.onNext(3);
182 subscriber.onError(new TestException());
183 subscriber.onComplete();
184 }
185 }
186 .throttleFirst(1, TimeUnit.DAYS)
187 .test()
188 .assertResult(1);
189
190 TestHelper.assertUndeliverable(errors, 0, TestException.class);
191 } finally {
192 RxJavaPlugins.reset();
193 }
194 }
195
196 @Test
197 public void backpressureNoRequest() {
198 Flowable.range(1, 3)
199 .throttleFirst(1, TimeUnit.MINUTES)
200 .test(0L)
201 .assertFailure(MissingBackpressureException.class);
202 }
203
204 @Test
205 public void badRequest() {
206 TestHelper.assertBadRequestReported(Flowable.never().throttleFirst(1, TimeUnit.MINUTES));
207 }
208
209 @Test
210 public void doubleOnSubscribe() {
211 TestHelper.checkDoubleOnSubscribeFlowable(f -> f.throttleFirst(1, TimeUnit.MINUTES));
212 }
213 }