| 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.junit.Assert.assertEquals; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.Arrays; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.mockito.InOrder; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.Function; |
| 29 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 32 |
|
| 33 |
public class ObservableSkipLastTest extends RxJavaTest { |
| 34 |
|
| 35 |
@Test |
| 36 |
public void skipLastEmpty() { |
| 37 |
Observable<String> o = Observable.<String> empty().skipLast(2); |
| 38 |
|
| 39 |
Observer<String> observer = TestHelper.mockObserver(); |
| 40 |
o.subscribe(observer); |
| 41 |
verify(observer, never()).onNext(any(String.class)); |
| 42 |
verify(observer, never()).onError(any(Throwable.class)); |
| 43 |
verify(observer, times(1)).onComplete(); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void skipLast1() { |
| 48 |
Observable<String> o = Observable.fromIterable(Arrays.asList("one", "two", "three")).skipLast(2); |
| 49 |
|
| 50 |
Observer<String> observer = TestHelper.mockObserver(); |
| 51 |
InOrder inOrder = inOrder(observer); |
| 52 |
o.subscribe(observer); |
| 53 |
inOrder.verify(observer, never()).onNext("two"); |
| 54 |
inOrder.verify(observer, never()).onNext("three"); |
| 55 |
verify(observer, times(1)).onNext("one"); |
| 56 |
verify(observer, never()).onError(any(Throwable.class)); |
| 57 |
verify(observer, times(1)).onComplete(); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void skipLast2() { |
| 62 |
Observable<String> o = Observable.fromIterable(Arrays.asList("one", "two")).skipLast(2); |
| 63 |
|
| 64 |
Observer<String> observer = TestHelper.mockObserver(); |
| 65 |
o.subscribe(observer); |
| 66 |
verify(observer, never()).onNext(any(String.class)); |
| 67 |
verify(observer, never()).onError(any(Throwable.class)); |
| 68 |
verify(observer, times(1)).onComplete(); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void skipLastWithZeroCount() { |
| 73 |
Observable<String> w = Observable.just("one", "two"); |
| 74 |
Observable<String> observable = w.skipLast(0); |
| 75 |
|
| 76 |
Observer<String> observer = TestHelper.mockObserver(); |
| 77 |
observable.subscribe(observer); |
| 78 |
verify(observer, times(1)).onNext("one"); |
| 79 |
verify(observer, times(1)).onNext("two"); |
| 80 |
verify(observer, never()).onError(any(Throwable.class)); |
| 81 |
verify(observer, times(1)).onComplete(); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void skipLastWithBackpressure() { |
| 86 |
Observable<Integer> o = Observable.range(0, Flowable.bufferSize() * 2).skipLast(Flowable.bufferSize() + 10); |
| 87 |
TestObserver<Integer> to = new TestObserver<>(); |
| 88 |
o.observeOn(Schedulers.computation()).subscribe(to); |
| 89 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 90 |
to.assertNoErrors(); |
| 91 |
assertEquals((Flowable.bufferSize()) - 10, to.values().size()); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
@Test(expected = IllegalArgumentException.class) |
| 96 |
public void skipLastWithNegativeCount() { |
| 97 |
Observable.just("one").skipLast(-1); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void dispose() { |
| 102 |
TestHelper.checkDisposed(Observable.just(1).skipLast(1)); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void error() { |
| 107 |
Observable.error(new TestException()) |
| 108 |
.skipLast(1) |
| 109 |
.test() |
| 110 |
.assertFailure(TestException.class); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void doubleOnSubscribe() { |
| 115 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, Observable<Object>>() { |
| 116 |
@Override |
| 117 |
public Observable<Object> apply(Observable<Object> o) |
| 118 |
throws Exception { |
| 119 |
return o.skipLast(1); |
| 120 |
} |
| 121 |
}); |
| 122 |
} |
| 123 |
} |