Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableThrottleLatestTest.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.observable;
15
16 import static org.mockito.Mockito.*;
17
18 import java.util.concurrent.TimeUnit;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.observers.TestObserver;
26 import io.reactivex.rxjava3.schedulers.TestScheduler;
27 import io.reactivex.rxjava3.subjects.PublishSubject;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class ObservableThrottleLatestTest extends RxJavaTest {
31
32 @Test
33 public void just() {
34 Observable.just(1)
35 .throttleLatest(1, TimeUnit.MINUTES)
36 .test()
37 .assertResult(1);
38 }
39
40 @Test
41 public void range() {
42 Observable.range(1, 5)
43 .throttleLatest(1, TimeUnit.MINUTES)
44 .test()
45 .assertResult(1);
46 }
47
48 @Test
49 public void rangeEmitLatest() {
50 Observable.range(1, 5)
51 .throttleLatest(1, TimeUnit.MINUTES, true)
52 .test()
53 .assertResult(1, 5);
54 }
55
56 @Test
57 public void error() {
58 Observable.error(new TestException())
59 .throttleLatest(1, TimeUnit.MINUTES)
60 .test()
61 .assertFailure(TestException.class);
62 }
63
64 @Test
65 public void doubleOnSubscribe() {
66 TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, Observable<Object>>() {
67 @Override
68 public Observable<Object> apply(Observable<Object> f) throws Exception {
69 return f.throttleLatest(1, TimeUnit.MINUTES);
70 }
71 });
72 }
73
74 @Test
75 public void disposed() {
76 TestHelper.checkDisposed(
77 Observable.never()
78 .throttleLatest(1, TimeUnit.MINUTES)
79 );
80 }
81
82 @Test
83 public void normal() {
84 TestScheduler sch = new TestScheduler();
85 PublishSubject<Integer> ps = PublishSubject.create();
86
87 TestObserver<Integer> to = ps.throttleLatest(1, TimeUnit.SECONDS, sch).test();
88
89 ps.onNext(1);
90
91 to.assertValuesOnly(1);
92
93 ps.onNext(2);
94
95 to.assertValuesOnly(1);
96
97 ps.onNext(3);
98
99 to.assertValuesOnly(1);
100
101 sch.advanceTimeBy(1, TimeUnit.SECONDS);
102
103 to.assertValuesOnly(1, 3);
104
105 ps.onNext(4);
106
107 to.assertValuesOnly(1, 3);
108
109 ps.onNext(5);
110
111 sch.advanceTimeBy(1, TimeUnit.SECONDS);
112
113 to.assertValuesOnly(1, 3, 5);
114
115 sch.advanceTimeBy(1, TimeUnit.SECONDS);
116
117 to.assertValuesOnly(1, 3, 5);
118
119 ps.onNext(6);
120
121 to.assertValuesOnly(1, 3, 5, 6);
122
123 ps.onNext(7);
124 ps.onComplete();
125
126 to.assertResult(1, 3, 5, 6);
127
128 sch.advanceTimeBy(1, TimeUnit.SECONDS);
129
130 to.assertResult(1, 3, 5, 6);
131 }
132
133 @Test
134 public void normalEmitLast() {
135 TestScheduler sch = new TestScheduler();
136 PublishSubject<Integer> ps = PublishSubject.create();
137
138 TestObserver<Integer> to = ps.throttleLatest(1, TimeUnit.SECONDS, sch, true).test();
139
140 ps.onNext(1);
141
142 to.assertValuesOnly(1);
143
144 ps.onNext(2);
145
146 to.assertValuesOnly(1);
147
148 ps.onNext(3);
149
150 to.assertValuesOnly(1);
151
152 sch.advanceTimeBy(1, TimeUnit.SECONDS);
153
154 to.assertValuesOnly(1, 3);
155
156 ps.onNext(4);
157
158 to.assertValuesOnly(1, 3);
159
160 ps.onNext(5);
161
162 sch.advanceTimeBy(1, TimeUnit.SECONDS);
163
164 to.assertValuesOnly(1, 3, 5);
165
166 sch.advanceTimeBy(1, TimeUnit.SECONDS);
167
168 to.assertValuesOnly(1, 3, 5);
169
170 ps.onNext(6);
171
172 to.assertValuesOnly(1, 3, 5, 6);
173
174 ps.onNext(7);
175 ps.onComplete();
176
177 to.assertResult(1, 3, 5, 6, 7);
178
179 sch.advanceTimeBy(1, TimeUnit.SECONDS);
180
181 to.assertResult(1, 3, 5, 6, 7);
182 }
183
184 @Test
185 public void take() throws Throwable {
186 Action onCancel = mock(Action.class);
187
188 Observable.range(1, 5)
189 .doOnDispose(onCancel)
190 .throttleLatest(1, TimeUnit.MINUTES)
191 .take(1)
192 .test()
193 .assertResult(1);
194
195 verify(onCancel).run();
196 }
197
198 @Test
199 public void reentrantComplete() {
200 TestScheduler sch = new TestScheduler();
201 final PublishSubject<Integer> ps = PublishSubject.create();
202
203 TestObserver<Integer> to = new TestObserver<Integer>() {
204 @Override
205 public void onNext(Integer t) {
206 super.onNext(t);
207 if (t == 1) {
208 ps.onNext(2);
209 }
210 if (t == 2) {
211 ps.onComplete();
212 }
213 }
214 };
215
216 ps.throttleLatest(1, TimeUnit.SECONDS, sch).subscribe(to);
217
218 ps.onNext(1);
219
220 sch.advanceTimeBy(1, TimeUnit.SECONDS);
221
222 to.assertResult(1, 2);
223 }
224 }