| 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.Arrays; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
import java.util.concurrent.atomic.AtomicLong; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.reactivestreams.Subscriber; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 30 |
import io.reactivex.rxjava3.testsupport.*; |
| 31 |
|
| 32 |
public class FlowableSkipTest extends RxJavaTest { |
| 33 |
|
| 34 |
@Test(expected = IllegalArgumentException.class) |
| 35 |
public void skipNegativeElements() { |
| 36 |
|
| 37 |
Flowable<String> skip = Flowable.just("one", "two", "three").skip(-99); |
| 38 |
|
| 39 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 40 |
skip.subscribe(subscriber); |
| 41 |
verify(subscriber, times(1)).onNext("one"); |
| 42 |
verify(subscriber, times(1)).onNext("two"); |
| 43 |
verify(subscriber, times(1)).onNext("three"); |
| 44 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 45 |
verify(subscriber, times(1)).onComplete(); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void skipZeroElements() { |
| 50 |
|
| 51 |
Flowable<String> skip = Flowable.just("one", "two", "three").skip(0); |
| 52 |
|
| 53 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 54 |
skip.subscribe(subscriber); |
| 55 |
verify(subscriber, times(1)).onNext("one"); |
| 56 |
verify(subscriber, times(1)).onNext("two"); |
| 57 |
verify(subscriber, times(1)).onNext("three"); |
| 58 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 59 |
verify(subscriber, times(1)).onComplete(); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void skipOneElement() { |
| 64 |
|
| 65 |
Flowable<String> skip = Flowable.just("one", "two", "three").skip(1); |
| 66 |
|
| 67 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 68 |
skip.subscribe(subscriber); |
| 69 |
verify(subscriber, never()).onNext("one"); |
| 70 |
verify(subscriber, times(1)).onNext("two"); |
| 71 |
verify(subscriber, times(1)).onNext("three"); |
| 72 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 73 |
verify(subscriber, times(1)).onComplete(); |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void skipTwoElements() { |
| 78 |
|
| 79 |
Flowable<String> skip = Flowable.just("one", "two", "three").skip(2); |
| 80 |
|
| 81 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 82 |
skip.subscribe(subscriber); |
| 83 |
verify(subscriber, never()).onNext("one"); |
| 84 |
verify(subscriber, never()).onNext("two"); |
| 85 |
verify(subscriber, times(1)).onNext("three"); |
| 86 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 87 |
verify(subscriber, times(1)).onComplete(); |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void skipEmptyStream() { |
| 92 |
|
| 93 |
Flowable<String> w = Flowable.empty(); |
| 94 |
Flowable<String> skip = w.skip(1); |
| 95 |
|
| 96 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 97 |
skip.subscribe(subscriber); |
| 98 |
verify(subscriber, never()).onNext(any(String.class)); |
| 99 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 100 |
verify(subscriber, times(1)).onComplete(); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void skipMultipleObservers() { |
| 105 |
|
| 106 |
Flowable<String> skip = Flowable.just("one", "two", "three") |
| 107 |
.skip(2); |
| 108 |
|
| 109 |
Subscriber<String> subscriber1 = TestHelper.mockSubscriber(); |
| 110 |
skip.subscribe(subscriber1); |
| 111 |
|
| 112 |
Subscriber<String> subscriber2 = TestHelper.mockSubscriber(); |
| 113 |
skip.subscribe(subscriber2); |
| 114 |
|
| 115 |
verify(subscriber1, times(1)).onNext(any(String.class)); |
| 116 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 117 |
verify(subscriber1, times(1)).onComplete(); |
| 118 |
|
| 119 |
verify(subscriber2, times(1)).onNext(any(String.class)); |
| 120 |
verify(subscriber2, never()).onError(any(Throwable.class)); |
| 121 |
verify(subscriber2, times(1)).onComplete(); |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void skipError() { |
| 126 |
|
| 127 |
Exception e = new Exception(); |
| 128 |
|
| 129 |
Flowable<String> ok = Flowable.just("one"); |
| 130 |
Flowable<String> error = Flowable.error(e); |
| 131 |
|
| 132 |
Flowable<String> skip = Flowable.concat(ok, error).skip(100); |
| 133 |
|
| 134 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 135 |
skip.subscribe(subscriber); |
| 136 |
|
| 137 |
verify(subscriber, never()).onNext(any(String.class)); |
| 138 |
verify(subscriber, times(1)).onError(e); |
| 139 |
verify(subscriber, never()).onComplete(); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void backpressureMultipleSmallAsyncRequests() throws InterruptedException { |
| 145 |
final AtomicLong requests = new AtomicLong(0); |
| 146 |
TestSubscriber<Long> ts = new TestSubscriber<>(0L); |
| 147 |
Flowable.interval(100, TimeUnit.MILLISECONDS) |
| 148 |
.doOnRequest(new LongConsumer() { |
| 149 |
@Override |
| 150 |
public void accept(long n) { |
| 151 |
requests.addAndGet(n); |
| 152 |
} |
| 153 |
}).skip(4).subscribe(ts); |
| 154 |
Thread.sleep(100); |
| 155 |
ts.request(1); |
| 156 |
ts.request(1); |
| 157 |
Thread.sleep(100); |
| 158 |
ts.cancel(); |
| 159 |
ts.assertNoErrors(); |
| 160 |
assertEquals(6, requests.get()); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void requestOverflowDoesNotOccur() { |
| 165 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(Long.MAX_VALUE - 1); |
| 166 |
Flowable.range(1, 10).skip(5).subscribe(ts); |
| 167 |
ts.assertTerminated(); |
| 168 |
ts.assertComplete(); |
| 169 |
ts.assertNoErrors(); |
| 170 |
assertEquals(Arrays.asList(6, 7, 8, 9, 10), ts.values()); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void dispose() { |
| 175 |
TestHelper.checkDisposed(Flowable.just(1).skip(2)); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void doubleOnSubscribe() { |
| 180 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 181 |
@Override |
| 182 |
public Flowable<Object> apply(Flowable<Object> f) |
| 183 |
throws Exception { |
| 184 |
return f.skip(1); |
| 185 |
} |
| 186 |
}); |
| 187 |
} |
| 188 |
|
| 189 |
} |