| 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.any; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.Subscriber; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 28 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 31 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class FlowableTakeUntilPredicateTest extends RxJavaTest { |
| 35 |
@Test |
| 36 |
public void takeEmpty() { |
| 37 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 38 |
|
| 39 |
Flowable.empty().takeUntil(new Predicate<Object>() { |
| 40 |
@Override |
| 41 |
public boolean test(Object v) { |
| 42 |
return true; |
| 43 |
} |
| 44 |
}).subscribe(subscriber); |
| 45 |
|
| 46 |
verify(subscriber, never()).onNext(any()); |
| 47 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 48 |
verify(subscriber).onComplete(); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void takeAll() { |
| 53 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 54 |
|
| 55 |
Flowable.just(1, 2).takeUntil(new Predicate<Integer>() { |
| 56 |
@Override |
| 57 |
public boolean test(Integer v) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
}).subscribe(subscriber); |
| 61 |
|
| 62 |
verify(subscriber).onNext(1); |
| 63 |
verify(subscriber).onNext(2); |
| 64 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 65 |
verify(subscriber).onComplete(); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void takeFirst() { |
| 70 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 71 |
|
| 72 |
Flowable.just(1, 2).takeUntil(new Predicate<Integer>() { |
| 73 |
@Override |
| 74 |
public boolean test(Integer v) { |
| 75 |
return true; |
| 76 |
} |
| 77 |
}).subscribe(subscriber); |
| 78 |
|
| 79 |
verify(subscriber).onNext(1); |
| 80 |
verify(subscriber, never()).onNext(2); |
| 81 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 82 |
verify(subscriber).onComplete(); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void takeSome() { |
| 87 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 88 |
|
| 89 |
Flowable.just(1, 2, 3).takeUntil(new Predicate<Integer>() { |
| 90 |
@Override |
| 91 |
public boolean test(Integer t1) { |
| 92 |
return t1 == 2; |
| 93 |
} |
| 94 |
}) |
| 95 |
.subscribe(subscriber); |
| 96 |
|
| 97 |
verify(subscriber).onNext(1); |
| 98 |
verify(subscriber).onNext(2); |
| 99 |
verify(subscriber, never()).onNext(3); |
| 100 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 101 |
verify(subscriber).onComplete(); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void functionThrows() { |
| 106 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 107 |
|
| 108 |
Predicate<Integer> predicate = new Predicate<Integer>() { |
| 109 |
@Override |
| 110 |
public boolean test(Integer t1) { |
| 111 |
throw new TestException("Forced failure"); |
| 112 |
} |
| 113 |
}; |
| 114 |
Flowable.just(1, 2, 3).takeUntil(predicate).subscribe(subscriber); |
| 115 |
|
| 116 |
verify(subscriber).onNext(1); |
| 117 |
verify(subscriber, never()).onNext(2); |
| 118 |
verify(subscriber, never()).onNext(3); |
| 119 |
verify(subscriber).onError(any(TestException.class)); |
| 120 |
verify(subscriber, never()).onComplete(); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void sourceThrows() { |
| 125 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 126 |
|
| 127 |
Flowable.just(1) |
| 128 |
.concatWith(Flowable.<Integer>error(new TestException())) |
| 129 |
.concatWith(Flowable.just(2)) |
| 130 |
.takeUntil(new Predicate<Integer>() { |
| 131 |
@Override |
| 132 |
public boolean test(Integer v) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
}).subscribe(subscriber); |
| 136 |
|
| 137 |
verify(subscriber).onNext(1); |
| 138 |
verify(subscriber, never()).onNext(2); |
| 139 |
verify(subscriber).onError(any(TestException.class)); |
| 140 |
verify(subscriber, never()).onComplete(); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void backpressure() { |
| 145 |
TestSubscriber<Integer> ts = new TestSubscriber<>(5L); |
| 146 |
|
| 147 |
Flowable.range(1, 1000).takeUntil(new Predicate<Integer>() { |
| 148 |
@Override |
| 149 |
public boolean test(Integer v) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
}).subscribe(ts); |
| 153 |
|
| 154 |
ts.assertNoErrors(); |
| 155 |
ts.assertValues(1, 2, 3, 4, 5); |
| 156 |
ts.assertNotComplete(); |
| 157 |
} |
| 158 |
|
| 159 |
@Test |
| 160 |
public void errorIncludesLastValueAsCause() { |
| 161 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(); |
| 162 |
final TestException e = new TestException("Forced failure"); |
| 163 |
Predicate<String> predicate = new Predicate<String>() { |
| 164 |
@Override |
| 165 |
public boolean test(String t) { |
| 166 |
throw e; |
| 167 |
} |
| 168 |
}; |
| 169 |
Flowable.just("abc").takeUntil(predicate).subscribe(ts); |
| 170 |
|
| 171 |
ts.assertTerminated(); |
| 172 |
ts.assertNotComplete(); |
| 173 |
ts.assertError(TestException.class); |
| 174 |
// FIXME last cause value is not saved |
| 175 |
// assertTrue(ts.errors().get(0).getCause().getMessage().contains("abc")); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void dispose() { |
| 180 |
TestHelper.checkDisposed(PublishProcessor.create().takeUntil(Functions.alwaysFalse())); |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void doubleOnSubscribe() { |
| 185 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 186 |
@Override |
| 187 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 188 |
return f.takeUntil(Functions.alwaysFalse()); |
| 189 |
} |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
@Test |
| 194 |
public void badSource() { |
| 195 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 196 |
try { |
| 197 |
new Flowable<Integer>() { |
| 198 |
@Override |
| 199 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 200 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 201 |
subscriber.onComplete(); |
| 202 |
subscriber.onNext(1); |
| 203 |
subscriber.onError(new TestException()); |
| 204 |
subscriber.onComplete(); |
| 205 |
} |
| 206 |
} |
| 207 |
.takeUntil(Functions.alwaysFalse()) |
| 208 |
.test() |
| 209 |
.assertResult(); |
| 210 |
|
| 211 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 212 |
} finally { |
| 213 |
RxJavaPlugins.reset(); |
| 214 |
} |
| 215 |
} |
| 216 |
} |