| 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.Mockito.*; |
| 17 |
|
| 18 |
import java.util.concurrent.TimeUnit; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
import org.reactivestreams.Publisher; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 27 |
import io.reactivex.rxjava3.schedulers.TestScheduler; |
| 28 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class FlowableThrottleLatestTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void just() { |
| 35 |
Flowable.just(1) |
| 36 |
.throttleLatest(1, TimeUnit.MINUTES) |
| 37 |
.test() |
| 38 |
.assertResult(1); |
| 39 |
} |
| 40 |
|
| 41 |
@Test |
| 42 |
public void range() { |
| 43 |
Flowable.range(1, 5) |
| 44 |
.throttleLatest(1, TimeUnit.MINUTES) |
| 45 |
.test() |
| 46 |
.assertResult(1); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void rangeEmitLatest() { |
| 51 |
Flowable.range(1, 5) |
| 52 |
.throttleLatest(1, TimeUnit.MINUTES, true) |
| 53 |
.test() |
| 54 |
.assertResult(1, 5); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void error() { |
| 59 |
Flowable.error(new TestException()) |
| 60 |
.throttleLatest(1, TimeUnit.MINUTES) |
| 61 |
.test() |
| 62 |
.assertFailure(TestException.class); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void doubleOnSubscribe() { |
| 67 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() { |
| 68 |
@Override |
| 69 |
public Publisher<Object> apply(Flowable<Object> f) throws Exception { |
| 70 |
return f.throttleLatest(1, TimeUnit.MINUTES); |
| 71 |
} |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void badRequest() { |
| 77 |
TestHelper.assertBadRequestReported( |
| 78 |
Flowable.never() |
| 79 |
.throttleLatest(1, TimeUnit.MINUTES) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void normal() { |
| 85 |
TestScheduler sch = new TestScheduler(); |
| 86 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 87 |
|
| 88 |
TestSubscriber<Integer> ts = pp.throttleLatest(1, TimeUnit.SECONDS, sch).test(); |
| 89 |
|
| 90 |
pp.onNext(1); |
| 91 |
|
| 92 |
ts.assertValuesOnly(1); |
| 93 |
|
| 94 |
pp.onNext(2); |
| 95 |
|
| 96 |
ts.assertValuesOnly(1); |
| 97 |
|
| 98 |
pp.onNext(3); |
| 99 |
|
| 100 |
ts.assertValuesOnly(1); |
| 101 |
|
| 102 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 103 |
|
| 104 |
ts.assertValuesOnly(1, 3); |
| 105 |
|
| 106 |
pp.onNext(4); |
| 107 |
|
| 108 |
ts.assertValuesOnly(1, 3); |
| 109 |
|
| 110 |
pp.onNext(5); |
| 111 |
|
| 112 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 113 |
|
| 114 |
ts.assertValuesOnly(1, 3, 5); |
| 115 |
|
| 116 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 117 |
|
| 118 |
ts.assertValuesOnly(1, 3, 5); |
| 119 |
|
| 120 |
pp.onNext(6); |
| 121 |
|
| 122 |
ts.assertValuesOnly(1, 3, 5, 6); |
| 123 |
|
| 124 |
pp.onNext(7); |
| 125 |
pp.onComplete(); |
| 126 |
|
| 127 |
ts.assertResult(1, 3, 5, 6); |
| 128 |
|
| 129 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 130 |
|
| 131 |
ts.assertResult(1, 3, 5, 6); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void normalEmitLast() { |
| 136 |
TestScheduler sch = new TestScheduler(); |
| 137 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 138 |
|
| 139 |
TestSubscriber<Integer> ts = pp.throttleLatest(1, TimeUnit.SECONDS, sch, true).test(); |
| 140 |
|
| 141 |
pp.onNext(1); |
| 142 |
|
| 143 |
ts.assertValuesOnly(1); |
| 144 |
|
| 145 |
pp.onNext(2); |
| 146 |
|
| 147 |
ts.assertValuesOnly(1); |
| 148 |
|
| 149 |
pp.onNext(3); |
| 150 |
|
| 151 |
ts.assertValuesOnly(1); |
| 152 |
|
| 153 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 154 |
|
| 155 |
ts.assertValuesOnly(1, 3); |
| 156 |
|
| 157 |
pp.onNext(4); |
| 158 |
|
| 159 |
ts.assertValuesOnly(1, 3); |
| 160 |
|
| 161 |
pp.onNext(5); |
| 162 |
|
| 163 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 164 |
|
| 165 |
ts.assertValuesOnly(1, 3, 5); |
| 166 |
|
| 167 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 168 |
|
| 169 |
ts.assertValuesOnly(1, 3, 5); |
| 170 |
|
| 171 |
pp.onNext(6); |
| 172 |
|
| 173 |
ts.assertValuesOnly(1, 3, 5, 6); |
| 174 |
|
| 175 |
pp.onNext(7); |
| 176 |
pp.onComplete(); |
| 177 |
|
| 178 |
ts.assertResult(1, 3, 5, 6, 7); |
| 179 |
|
| 180 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 181 |
|
| 182 |
ts.assertResult(1, 3, 5, 6, 7); |
| 183 |
} |
| 184 |
|
| 185 |
@Test |
| 186 |
public void missingBackpressureExceptionFirst() throws Throwable { |
| 187 |
TestScheduler sch = new TestScheduler(); |
| 188 |
Action onCancel = mock(Action.class); |
| 189 |
|
| 190 |
Flowable.just(1, 2) |
| 191 |
.doOnCancel(onCancel) |
| 192 |
.throttleLatest(1, TimeUnit.MINUTES, sch) |
| 193 |
.test(0) |
| 194 |
.assertFailure(MissingBackpressureException.class); |
| 195 |
|
| 196 |
verify(onCancel).run(); |
| 197 |
} |
| 198 |
|
| 199 |
@Test |
| 200 |
public void missingBackpressureExceptionLatest() throws Throwable { |
| 201 |
TestScheduler sch = new TestScheduler(); |
| 202 |
Action onCancel = mock(Action.class); |
| 203 |
|
| 204 |
TestSubscriber<Integer> ts = Flowable.just(1, 2) |
| 205 |
.concatWith(Flowable.<Integer>never()) |
| 206 |
.doOnCancel(onCancel) |
| 207 |
.throttleLatest(1, TimeUnit.SECONDS, sch, true) |
| 208 |
.test(1); |
| 209 |
|
| 210 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 211 |
|
| 212 |
ts.assertFailure(MissingBackpressureException.class, 1); |
| 213 |
|
| 214 |
verify(onCancel).run(); |
| 215 |
} |
| 216 |
|
| 217 |
@Test |
| 218 |
public void missingBackpressureExceptionLatestComplete() throws Throwable { |
| 219 |
TestScheduler sch = new TestScheduler(); |
| 220 |
Action onCancel = mock(Action.class); |
| 221 |
|
| 222 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 223 |
|
| 224 |
TestSubscriber<Integer> ts = pp |
| 225 |
.doOnCancel(onCancel) |
| 226 |
.throttleLatest(1, TimeUnit.SECONDS, sch, true) |
| 227 |
.test(1); |
| 228 |
|
| 229 |
pp.onNext(1); |
| 230 |
pp.onNext(2); |
| 231 |
|
| 232 |
ts.assertValuesOnly(1); |
| 233 |
|
| 234 |
pp.onComplete(); |
| 235 |
|
| 236 |
ts.assertFailure(MissingBackpressureException.class, 1); |
| 237 |
|
| 238 |
verify(onCancel, never()).run(); |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
public void take() throws Throwable { |
| 243 |
Action onCancel = mock(Action.class); |
| 244 |
|
| 245 |
Flowable.range(1, 5) |
| 246 |
.doOnCancel(onCancel) |
| 247 |
.throttleLatest(1, TimeUnit.MINUTES) |
| 248 |
.take(1) |
| 249 |
.test() |
| 250 |
.assertResult(1); |
| 251 |
|
| 252 |
verify(onCancel).run(); |
| 253 |
} |
| 254 |
|
| 255 |
@Test |
| 256 |
public void reentrantComplete() { |
| 257 |
TestScheduler sch = new TestScheduler(); |
| 258 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 259 |
|
| 260 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 261 |
@Override |
| 262 |
public void onNext(Integer t) { |
| 263 |
super.onNext(t); |
| 264 |
if (t == 1) { |
| 265 |
pp.onNext(2); |
| 266 |
} |
| 267 |
if (t == 2) { |
| 268 |
pp.onComplete(); |
| 269 |
} |
| 270 |
} |
| 271 |
}; |
| 272 |
|
| 273 |
pp.throttleLatest(1, TimeUnit.SECONDS, sch).subscribe(ts); |
| 274 |
|
| 275 |
pp.onNext(1); |
| 276 |
|
| 277 |
sch.advanceTimeBy(1, TimeUnit.SECONDS); |
| 278 |
|
| 279 |
ts.assertResult(1, 2); |
| 280 |
} |
| 281 |
} |