| 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.junit.Assert.assertEquals; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
import java.util.concurrent.atomic.AtomicInteger; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.mockito.InOrder; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 29 |
import io.reactivex.rxjava3.core.*; |
| 30 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 31 |
import io.reactivex.rxjava3.functions.*; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.subscribers.*; |
| 34 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 35 |
|
| 36 |
public class FlowableTakeLastTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void takeLastEmpty() { |
| 40 |
Flowable<String> w = Flowable.empty(); |
| 41 |
Flowable<String> take = w.takeLast(2); |
| 42 |
|
| 43 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 44 |
take.subscribe(subscriber); |
| 45 |
|
| 46 |
verify(subscriber, never()).onNext(any(String.class)); |
| 47 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 48 |
verify(subscriber, times(1)).onComplete(); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void takeLast1() { |
| 53 |
Flowable<String> w = Flowable.just("one", "two", "three"); |
| 54 |
Flowable<String> take = w.takeLast(2); |
| 55 |
|
| 56 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 57 |
InOrder inOrder = inOrder(subscriber); |
| 58 |
take.subscribe(subscriber); |
| 59 |
|
| 60 |
inOrder.verify(subscriber, times(1)).onNext("two"); |
| 61 |
inOrder.verify(subscriber, times(1)).onNext("three"); |
| 62 |
verify(subscriber, never()).onNext("one"); |
| 63 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 64 |
verify(subscriber, times(1)).onComplete(); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void takeLast2() { |
| 69 |
Flowable<String> w = Flowable.just("one"); |
| 70 |
Flowable<String> take = w.takeLast(10); |
| 71 |
|
| 72 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 73 |
take.subscribe(subscriber); |
| 74 |
|
| 75 |
verify(subscriber, times(1)).onNext("one"); |
| 76 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 77 |
verify(subscriber, times(1)).onComplete(); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void takeLastWithZeroCount() { |
| 82 |
Flowable<String> w = Flowable.just("one"); |
| 83 |
Flowable<String> take = w.takeLast(0); |
| 84 |
|
| 85 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 86 |
take.subscribe(subscriber); |
| 87 |
|
| 88 |
verify(subscriber, never()).onNext("one"); |
| 89 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 90 |
verify(subscriber, times(1)).onComplete(); |
| 91 |
} |
| 92 |
|
| 93 |
@Test(expected = IllegalArgumentException.class) |
| 94 |
public void takeLastWithNegativeCount() { |
| 95 |
Flowable.just("one").takeLast(-1); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void backpressure1() { |
| 100 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 101 |
Flowable.range(1, 100000).takeLast(1) |
| 102 |
.observeOn(Schedulers.newThread()) |
| 103 |
.map(newSlowProcessor()).subscribe(ts); |
| 104 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 105 |
ts.assertNoErrors(); |
| 106 |
ts.assertValue(100000); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void backpressure2() { |
| 111 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 112 |
Flowable.range(1, 100000).takeLast(Flowable.bufferSize() * 4) |
| 113 |
.observeOn(Schedulers.newThread()).map(newSlowProcessor()).subscribe(ts); |
| 114 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 115 |
ts.assertNoErrors(); |
| 116 |
assertEquals(Flowable.bufferSize() * 4, ts.values().size()); |
| 117 |
} |
| 118 |
|
| 119 |
private Function<Integer, Integer> newSlowProcessor() { |
| 120 |
return new Function<Integer, Integer>() { |
| 121 |
int c; |
| 122 |
|
| 123 |
@Override |
| 124 |
public Integer apply(Integer i) { |
| 125 |
if (c++ < 100) { |
| 126 |
try { |
| 127 |
Thread.sleep(1); |
| 128 |
} catch (InterruptedException e) { |
| 129 |
} |
| 130 |
} |
| 131 |
return i; |
| 132 |
} |
| 133 |
|
| 134 |
}; |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void issue1522() { |
| 139 |
// https://github.com/ReactiveX/RxJava/issues/1522 |
| 140 |
assertEquals(0, Flowable |
| 141 |
.empty() |
| 142 |
.count() |
| 143 |
.toFlowable() |
| 144 |
.filter(new Predicate<Long>() { |
| 145 |
@Override |
| 146 |
public boolean test(Long v) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
}) |
| 150 |
.toList() |
| 151 |
.blockingGet().size()); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void ignoreRequest1() { |
| 156 |
// If `takeLast` does not ignore `request` properly, StackOverflowError will be thrown. |
| 157 |
Flowable.range(0, 100000).takeLast(100000).subscribe(new DefaultSubscriber<Integer>() { |
| 158 |
|
| 159 |
@Override |
| 160 |
public void onStart() { |
| 161 |
request(Long.MAX_VALUE); |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public void onComplete() { |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public void onError(Throwable e) { |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void onNext(Integer integer) { |
| 175 |
request(Long.MAX_VALUE); |
| 176 |
} |
| 177 |
}); |
| 178 |
} |
| 179 |
|
| 180 |
@Test |
| 181 |
public void ignoreRequest2() { |
| 182 |
// If `takeLast` does not ignore `request` properly, StackOverflowError will be thrown. |
| 183 |
Flowable.range(0, 100000).takeLast(100000).subscribe(new DefaultSubscriber<Integer>() { |
| 184 |
|
| 185 |
@Override |
| 186 |
public void onStart() { |
| 187 |
request(1); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void onComplete() { |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void onError(Throwable e) { |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public void onNext(Integer integer) { |
| 200 |
request(1); |
| 201 |
} |
| 202 |
}); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void ignoreRequest3() { |
| 207 |
// If `takeLast` does not ignore `request` properly, it will enter an infinite loop. |
| 208 |
Flowable.range(0, 100000).takeLast(100000).subscribe(new DefaultSubscriber<Integer>() { |
| 209 |
|
| 210 |
@Override |
| 211 |
public void onStart() { |
| 212 |
request(1); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void onComplete() { |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public void onError(Throwable e) { |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void onNext(Integer integer) { |
| 226 |
request(Long.MAX_VALUE); |
| 227 |
} |
| 228 |
}); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void ignoreRequest4() { |
| 233 |
// If `takeLast` does not ignore `request` properly, StackOverflowError will be thrown. |
| 234 |
Flowable.range(0, 100000).takeLast(100000).subscribe(new DefaultSubscriber<Integer>() { |
| 235 |
|
| 236 |
@Override |
| 237 |
public void onStart() { |
| 238 |
request(Long.MAX_VALUE); |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public void onComplete() { |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public void onError(Throwable e) { |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public void onNext(Integer integer) { |
| 252 |
request(1); |
| 253 |
} |
| 254 |
}); |
| 255 |
} |
| 256 |
|
| 257 |
@Test |
| 258 |
public void unsubscribeTakesEffectEarlyOnFastPath() { |
| 259 |
final AtomicInteger count = new AtomicInteger(); |
| 260 |
Flowable.range(0, 100000).takeLast(100000).subscribe(new DefaultSubscriber<Integer>() { |
| 261 |
|
| 262 |
@Override |
| 263 |
public void onStart() { |
| 264 |
request(Long.MAX_VALUE); |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public void onComplete() { |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public void onError(Throwable e) { |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public void onNext(Integer integer) { |
| 278 |
count.incrementAndGet(); |
| 279 |
cancel(); |
| 280 |
} |
| 281 |
}); |
| 282 |
assertEquals(1, count.get()); |
| 283 |
} |
| 284 |
|
| 285 |
@Test |
| 286 |
public void requestOverflow() { |
| 287 |
final List<Integer> list = new ArrayList<>(); |
| 288 |
Flowable.range(1, 100).takeLast(50).subscribe(new DefaultSubscriber<Integer>() { |
| 289 |
|
| 290 |
@Override |
| 291 |
public void onStart() { |
| 292 |
request(2); |
| 293 |
} |
| 294 |
|
| 295 |
@Override |
| 296 |
public void onComplete() { |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public void onError(Throwable e) { |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
public void onNext(Integer t) { |
| 307 |
list.add(t); |
| 308 |
request(Long.MAX_VALUE - 1); |
| 309 |
}}); |
| 310 |
assertEquals(50, list.size()); |
| 311 |
} |
| 312 |
|
| 313 |
@Test |
| 314 |
public void dispose() { |
| 315 |
TestHelper.checkDisposed(Flowable.range(1, 10).takeLast(5)); |
| 316 |
} |
| 317 |
|
| 318 |
@Test |
| 319 |
public void doubleOnSubscribe() { |
| 320 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 321 |
@Override |
| 322 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 323 |
return f.takeLast(5); |
| 324 |
} |
| 325 |
}); |
| 326 |
} |
| 327 |
|
| 328 |
@Test |
| 329 |
public void error() { |
| 330 |
Flowable.error(new TestException()) |
| 331 |
.takeLast(5) |
| 332 |
.test() |
| 333 |
.assertFailure(TestException.class); |
| 334 |
} |
| 335 |
|
| 336 |
@Test |
| 337 |
public void takeLastTake() { |
| 338 |
Flowable.range(1, 10) |
| 339 |
.takeLast(5) |
| 340 |
.take(2) |
| 341 |
.test() |
| 342 |
.assertResult(6, 7); |
| 343 |
} |
| 344 |
|
| 345 |
@Test |
| 346 |
public void badRequest() { |
| 347 |
TestHelper.assertBadRequestReported(Flowable.never().takeLast(2)); |
| 348 |
} |
| 349 |
|
| 350 |
@Test |
| 351 |
public void cancelThenRequest() { |
| 352 |
Flowable.never().takeLast(2) |
| 353 |
.subscribe(new FlowableSubscriber<Object>() { |
| 354 |
|
| 355 |
@Override |
| 356 |
public void onNext(@NonNull Object t) { |
| 357 |
} |
| 358 |
|
| 359 |
@Override |
| 360 |
public void onError(Throwable t) { |
| 361 |
} |
| 362 |
|
| 363 |
@Override |
| 364 |
public void onComplete() { |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public void onSubscribe(@NonNull Subscription s) { |
| 369 |
s.cancel(); |
| 370 |
s.request(1); |
| 371 |
} |
| 372 |
}); |
| 373 |
} |
| 374 |
|
| 375 |
@Test |
| 376 |
public void noRequestEmpty() { |
| 377 |
Flowable.empty() |
| 378 |
.takeLast(2) |
| 379 |
.test(0L) |
| 380 |
.assertResult(); |
| 381 |
} |
| 382 |
|
| 383 |
@Test |
| 384 |
public void moreValuesRemainingThanRequested() { |
| 385 |
Flowable.range(1, 4) |
| 386 |
.takeLast(3) |
| 387 |
.test(0L) |
| 388 |
.assertEmpty() |
| 389 |
.requestMore(2) |
| 390 |
.assertValuesOnly(2, 3) |
| 391 |
.requestMore(2) |
| 392 |
.assertResult(2, 3, 4); |
| 393 |
} |
| 394 |
} |