| 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.fail; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.*; |
| 32 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 33 |
import io.reactivex.rxjava3.testsupport.*; |
| 34 |
|
| 35 |
public class FlowableTakeWhileTest extends RxJavaTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void takeWhile1() { |
| 39 |
Flowable<Integer> w = Flowable.just(1, 2, 3); |
| 40 |
Flowable<Integer> take = w.takeWhile(new Predicate<Integer>() { |
| 41 |
@Override |
| 42 |
public boolean test(Integer input) { |
| 43 |
return input < 3; |
| 44 |
} |
| 45 |
}); |
| 46 |
|
| 47 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 48 |
take.subscribe(subscriber); |
| 49 |
|
| 50 |
verify(subscriber, times(1)).onNext(1); |
| 51 |
verify(subscriber, times(1)).onNext(2); |
| 52 |
verify(subscriber, never()).onNext(3); |
| 53 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 54 |
verify(subscriber, times(1)).onComplete(); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void takeWhileOnSubject1() { |
| 59 |
FlowableProcessor<Integer> s = PublishProcessor.create(); |
| 60 |
Flowable<Integer> take = s.takeWhile(new Predicate<Integer>() { |
| 61 |
@Override |
| 62 |
public boolean test(Integer input) { |
| 63 |
return input < 3; |
| 64 |
} |
| 65 |
}); |
| 66 |
|
| 67 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 68 |
take.subscribe(subscriber); |
| 69 |
|
| 70 |
s.onNext(1); |
| 71 |
s.onNext(2); |
| 72 |
s.onNext(3); |
| 73 |
s.onNext(4); |
| 74 |
s.onNext(5); |
| 75 |
s.onComplete(); |
| 76 |
|
| 77 |
verify(subscriber, times(1)).onNext(1); |
| 78 |
verify(subscriber, times(1)).onNext(2); |
| 79 |
verify(subscriber, never()).onNext(3); |
| 80 |
verify(subscriber, never()).onNext(4); |
| 81 |
verify(subscriber, never()).onNext(5); |
| 82 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 83 |
verify(subscriber, times(1)).onComplete(); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void takeWhile2() { |
| 88 |
Flowable<String> w = Flowable.just("one", "two", "three"); |
| 89 |
Flowable<String> take = w.takeWhile(new Predicate<String>() { |
| 90 |
int index; |
| 91 |
|
| 92 |
@Override |
| 93 |
public boolean test(String input) { |
| 94 |
return index++ < 2; |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 99 |
take.subscribe(subscriber); |
| 100 |
|
| 101 |
verify(subscriber, times(1)).onNext("one"); |
| 102 |
verify(subscriber, times(1)).onNext("two"); |
| 103 |
verify(subscriber, never()).onNext("three"); |
| 104 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 105 |
verify(subscriber, times(1)).onComplete(); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void takeWhileDoesntLeakErrors() { |
| 110 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 111 |
try { |
| 112 |
Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() { |
| 113 |
@Override |
| 114 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 115 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 116 |
subscriber.onNext("one"); |
| 117 |
subscriber.onError(new TestException("test failed")); |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
source.takeWhile(new Predicate<String>() { |
| 122 |
@Override |
| 123 |
public boolean test(String s) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
}).blockingLast(""); |
| 127 |
|
| 128 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "test failed"); |
| 129 |
} finally { |
| 130 |
RxJavaPlugins.reset(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void takeWhileProtectsPredicateCall() { |
| 136 |
TestFlowable source = new TestFlowable(mock(Subscription.class), "one"); |
| 137 |
final RuntimeException testException = new RuntimeException("test exception"); |
| 138 |
|
| 139 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 140 |
Flowable<String> take = Flowable.unsafeCreate(source) |
| 141 |
.takeWhile(new Predicate<String>() { |
| 142 |
@Override |
| 143 |
public boolean test(String s) { |
| 144 |
throw testException; |
| 145 |
} |
| 146 |
}); |
| 147 |
take.subscribe(subscriber); |
| 148 |
|
| 149 |
// wait for the Flowable to complete |
| 150 |
try { |
| 151 |
source.t.join(); |
| 152 |
} catch (Throwable e) { |
| 153 |
e.printStackTrace(); |
| 154 |
fail(e.getMessage()); |
| 155 |
} |
| 156 |
|
| 157 |
verify(subscriber, never()).onNext(any(String.class)); |
| 158 |
verify(subscriber, times(1)).onError(testException); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void unsubscribeAfterTake() { |
| 163 |
Subscription s = mock(Subscription.class); |
| 164 |
TestFlowable w = new TestFlowable(s, "one", "two", "three"); |
| 165 |
|
| 166 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 167 |
Flowable<String> take = Flowable.unsafeCreate(w) |
| 168 |
.takeWhile(new Predicate<String>() { |
| 169 |
int index; |
| 170 |
|
| 171 |
@Override |
| 172 |
public boolean test(String s) { |
| 173 |
return index++ < 1; |
| 174 |
} |
| 175 |
}); |
| 176 |
take.subscribe(subscriber); |
| 177 |
|
| 178 |
// wait for the Flowable to complete |
| 179 |
try { |
| 180 |
w.t.join(); |
| 181 |
} catch (Throwable e) { |
| 182 |
e.printStackTrace(); |
| 183 |
fail(e.getMessage()); |
| 184 |
} |
| 185 |
|
| 186 |
System.out.println("TestFlowable thread finished"); |
| 187 |
verify(subscriber, times(1)).onNext("one"); |
| 188 |
verify(subscriber, never()).onNext("two"); |
| 189 |
verify(subscriber, never()).onNext("three"); |
| 190 |
verify(s, times(1)).cancel(); |
| 191 |
} |
| 192 |
|
| 193 |
private static class TestFlowable implements Publisher<String> { |
| 194 |
|
| 195 |
final Subscription upstream; |
| 196 |
final String[] values; |
| 197 |
Thread t; |
| 198 |
|
| 199 |
TestFlowable(Subscription s, String... values) { |
| 200 |
this.upstream = s; |
| 201 |
this.values = values; |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 206 |
System.out.println("TestFlowable subscribed to ..."); |
| 207 |
subscriber.onSubscribe(upstream); |
| 208 |
t = new Thread(new Runnable() { |
| 209 |
|
| 210 |
@Override |
| 211 |
public void run() { |
| 212 |
try { |
| 213 |
System.out.println("running TestFlowable thread"); |
| 214 |
for (String s : values) { |
| 215 |
System.out.println("TestFlowable onNext: " + s); |
| 216 |
subscriber.onNext(s); |
| 217 |
} |
| 218 |
subscriber.onComplete(); |
| 219 |
} catch (Throwable e) { |
| 220 |
throw new RuntimeException(e); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
}); |
| 225 |
System.out.println("starting TestFlowable thread"); |
| 226 |
t.start(); |
| 227 |
System.out.println("done starting TestFlowable thread"); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void backpressure() { |
| 233 |
Flowable<Integer> source = Flowable.range(1, 1000).takeWhile(new Predicate<Integer>() { |
| 234 |
@Override |
| 235 |
public boolean test(Integer t1) { |
| 236 |
return t1 < 100; |
| 237 |
} |
| 238 |
}); |
| 239 |
TestSubscriber<Integer> ts = new TestSubscriber<>(5L); |
| 240 |
|
| 241 |
source.subscribe(ts); |
| 242 |
|
| 243 |
ts.assertNoErrors(); |
| 244 |
ts.assertValues(1, 2, 3, 4, 5); |
| 245 |
|
| 246 |
ts.request(5); |
| 247 |
|
| 248 |
ts.assertNoErrors(); |
| 249 |
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 250 |
} |
| 251 |
|
| 252 |
@Test |
| 253 |
public void noUnsubscribeDownstream() { |
| 254 |
Flowable<Integer> source = Flowable.range(1, 1000).takeWhile(new Predicate<Integer>() { |
| 255 |
@Override |
| 256 |
public boolean test(Integer t1) { |
| 257 |
return t1 < 2; |
| 258 |
} |
| 259 |
}); |
| 260 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 261 |
|
| 262 |
source.subscribe(ts); |
| 263 |
|
| 264 |
ts.assertNoErrors(); |
| 265 |
ts.assertValue(1); |
| 266 |
|
| 267 |
Assert.assertFalse("Unsubscribed!", ts.isCancelled()); |
| 268 |
} |
| 269 |
|
| 270 |
@Test |
| 271 |
public void errorCauseIncludesLastValue() { |
| 272 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(); |
| 273 |
Flowable.just("abc").takeWhile(new Predicate<String>() { |
| 274 |
@Override |
| 275 |
public boolean test(String t1) { |
| 276 |
throw new TestException(); |
| 277 |
} |
| 278 |
}).subscribe(ts); |
| 279 |
|
| 280 |
ts.assertTerminated(); |
| 281 |
ts.assertNoValues(); |
| 282 |
ts.assertError(TestException.class); |
| 283 |
// FIXME last cause value not recorded |
| 284 |
// assertTrue(ts.getOnErrorEvents().get(0).getCause().getMessage().contains("abc")); |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void dispose() { |
| 289 |
TestHelper.checkDisposed(PublishProcessor.create().takeWhile(Functions.alwaysTrue())); |
| 290 |
} |
| 291 |
|
| 292 |
@Test |
| 293 |
public void doubleOnSubscribe() { |
| 294 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 295 |
@Override |
| 296 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 297 |
return f.takeWhile(Functions.alwaysTrue()); |
| 298 |
} |
| 299 |
}); |
| 300 |
} |
| 301 |
|
| 302 |
@Test |
| 303 |
public void badSource() { |
| 304 |
new Flowable<Integer>() { |
| 305 |
@Override |
| 306 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 307 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 308 |
subscriber.onComplete(); |
| 309 |
subscriber.onComplete(); |
| 310 |
} |
| 311 |
} |
| 312 |
.takeWhile(Functions.alwaysTrue()) |
| 313 |
.test() |
| 314 |
.assertResult(); |
| 315 |
} |
| 316 |
|
| 317 |
} |