| 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.ArgumentMatchers.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
import org.mockito.InOrder; |
| 21 |
import org.reactivestreams.Subscriber; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 27 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class FlowableSkipWhileTest extends RxJavaTest { |
| 31 |
|
| 32 |
Subscriber<Integer> w = TestHelper.mockSubscriber(); |
| 33 |
|
| 34 |
private static final Predicate<Integer> LESS_THAN_FIVE = new Predicate<Integer>() { |
| 35 |
@Override |
| 36 |
public boolean test(Integer v) { |
| 37 |
if (v == 42) { |
| 38 |
throw new RuntimeException("that's not the answer to everything!"); |
| 39 |
} |
| 40 |
return v < 5; |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
private static final Predicate<Integer> INDEX_LESS_THAN_THREE = new Predicate<Integer>() { |
| 45 |
int index; |
| 46 |
@Override |
| 47 |
public boolean test(Integer value) { |
| 48 |
return index++ < 3; |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
@Test |
| 53 |
public void skipWithIndex() { |
| 54 |
Flowable<Integer> src = Flowable.just(1, 2, 3, 4, 5); |
| 55 |
src.skipWhile(INDEX_LESS_THAN_THREE).subscribe(w); |
| 56 |
|
| 57 |
InOrder inOrder = inOrder(w); |
| 58 |
inOrder.verify(w, times(1)).onNext(4); |
| 59 |
inOrder.verify(w, times(1)).onNext(5); |
| 60 |
inOrder.verify(w, times(1)).onComplete(); |
| 61 |
inOrder.verify(w, never()).onError(any(Throwable.class)); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void skipEmpty() { |
| 66 |
Flowable<Integer> src = Flowable.empty(); |
| 67 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 68 |
verify(w, never()).onNext(anyInt()); |
| 69 |
verify(w, never()).onError(any(Throwable.class)); |
| 70 |
verify(w, times(1)).onComplete(); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void skipEverything() { |
| 75 |
Flowable<Integer> src = Flowable.just(1, 2, 3, 4, 3, 2, 1); |
| 76 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 77 |
verify(w, never()).onNext(anyInt()); |
| 78 |
verify(w, never()).onError(any(Throwable.class)); |
| 79 |
verify(w, times(1)).onComplete(); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void skipNothing() { |
| 84 |
Flowable<Integer> src = Flowable.just(5, 3, 1); |
| 85 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 86 |
|
| 87 |
InOrder inOrder = inOrder(w); |
| 88 |
inOrder.verify(w, times(1)).onNext(5); |
| 89 |
inOrder.verify(w, times(1)).onNext(3); |
| 90 |
inOrder.verify(w, times(1)).onNext(1); |
| 91 |
inOrder.verify(w, times(1)).onComplete(); |
| 92 |
inOrder.verify(w, never()).onError(any(Throwable.class)); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void skipSome() { |
| 97 |
Flowable<Integer> src = Flowable.just(1, 2, 3, 4, 5, 3, 1, 5); |
| 98 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 99 |
|
| 100 |
InOrder inOrder = inOrder(w); |
| 101 |
inOrder.verify(w, times(1)).onNext(5); |
| 102 |
inOrder.verify(w, times(1)).onNext(3); |
| 103 |
inOrder.verify(w, times(1)).onNext(1); |
| 104 |
inOrder.verify(w, times(1)).onNext(5); |
| 105 |
inOrder.verify(w, times(1)).onComplete(); |
| 106 |
inOrder.verify(w, never()).onError(any(Throwable.class)); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void skipError() { |
| 111 |
Flowable<Integer> src = Flowable.just(1, 2, 42, 5, 3, 1); |
| 112 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 113 |
|
| 114 |
InOrder inOrder = inOrder(w); |
| 115 |
inOrder.verify(w, never()).onNext(anyInt()); |
| 116 |
inOrder.verify(w, never()).onComplete(); |
| 117 |
inOrder.verify(w, times(1)).onError(any(RuntimeException.class)); |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void skipManySubscribers() { |
| 122 |
Flowable<Integer> src = Flowable.range(1, 10).skipWhile(LESS_THAN_FIVE); |
| 123 |
int n = 5; |
| 124 |
for (int i = 0; i < n; i++) { |
| 125 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 126 |
InOrder inOrder = inOrder(subscriber); |
| 127 |
|
| 128 |
src.subscribe(subscriber); |
| 129 |
|
| 130 |
for (int j = 5; j < 10; j++) { |
| 131 |
inOrder.verify(subscriber).onNext(j); |
| 132 |
} |
| 133 |
inOrder.verify(subscriber).onComplete(); |
| 134 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void dispose() { |
| 140 |
TestHelper.checkDisposed(PublishProcessor.create().skipWhile(Functions.alwaysFalse())); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void doubleOnSubscribe() { |
| 145 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 146 |
@Override |
| 147 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 148 |
return f.skipWhile(Functions.alwaysFalse()); |
| 149 |
} |
| 150 |
}); |
| 151 |
} |
| 152 |
|
| 153 |
@Test |
| 154 |
public void error() { |
| 155 |
Flowable.error(new TestException()) |
| 156 |
.skipWhile(Functions.alwaysFalse()) |
| 157 |
.test() |
| 158 |
.assertFailure(TestException.class); |
| 159 |
} |
| 160 |
} |