| 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.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
import org.reactivestreams.*; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.Function; |
| 25 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 26 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 27 |
import io.reactivex.rxjava3.testsupport.*; |
| 28 |
|
| 29 |
public class FlowableTakeUntilTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void takeUntil() { |
| 33 |
Subscription sSource = mock(Subscription.class); |
| 34 |
Subscription sOther = mock(Subscription.class); |
| 35 |
TestObservable source = new TestObservable(sSource); |
| 36 |
TestObservable other = new TestObservable(sOther); |
| 37 |
|
| 38 |
Subscriber<String> result = TestHelper.mockSubscriber(); |
| 39 |
Flowable<String> stringObservable = Flowable.unsafeCreate(source) |
| 40 |
.takeUntil(Flowable.unsafeCreate(other)); |
| 41 |
stringObservable.subscribe(result); |
| 42 |
source.sendOnNext("one"); |
| 43 |
source.sendOnNext("two"); |
| 44 |
other.sendOnNext("three"); |
| 45 |
source.sendOnNext("four"); |
| 46 |
source.sendOnCompleted(); |
| 47 |
other.sendOnCompleted(); |
| 48 |
|
| 49 |
verify(result, times(1)).onNext("one"); |
| 50 |
verify(result, times(1)).onNext("two"); |
| 51 |
verify(result, times(0)).onNext("three"); |
| 52 |
verify(result, times(0)).onNext("four"); |
| 53 |
verify(sSource, times(1)).cancel(); |
| 54 |
verify(sOther, times(1)).cancel(); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void takeUntilSourceCompleted() { |
| 60 |
Subscription sSource = mock(Subscription.class); |
| 61 |
Subscription sOther = mock(Subscription.class); |
| 62 |
TestObservable source = new TestObservable(sSource); |
| 63 |
TestObservable other = new TestObservable(sOther); |
| 64 |
|
| 65 |
Subscriber<String> result = TestHelper.mockSubscriber(); |
| 66 |
Flowable<String> stringObservable = Flowable.unsafeCreate(source).takeUntil(Flowable.unsafeCreate(other)); |
| 67 |
stringObservable.subscribe(result); |
| 68 |
source.sendOnNext("one"); |
| 69 |
source.sendOnNext("two"); |
| 70 |
source.sendOnCompleted(); |
| 71 |
|
| 72 |
verify(result, times(1)).onNext("one"); |
| 73 |
verify(result, times(1)).onNext("two"); |
| 74 |
verify(sSource, never()).cancel(); |
| 75 |
verify(sOther, times(1)).cancel(); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void takeUntilSourceError() { |
| 81 |
Subscription sSource = mock(Subscription.class); |
| 82 |
Subscription sOther = mock(Subscription.class); |
| 83 |
TestObservable source = new TestObservable(sSource); |
| 84 |
TestObservable other = new TestObservable(sOther); |
| 85 |
Throwable error = new Throwable(); |
| 86 |
|
| 87 |
Subscriber<String> result = TestHelper.mockSubscriber(); |
| 88 |
Flowable<String> stringObservable = Flowable.unsafeCreate(source).takeUntil(Flowable.unsafeCreate(other)); |
| 89 |
stringObservable.subscribe(result); |
| 90 |
source.sendOnNext("one"); |
| 91 |
source.sendOnNext("two"); |
| 92 |
source.sendOnError(error); |
| 93 |
source.sendOnNext("three"); |
| 94 |
|
| 95 |
verify(result, times(1)).onNext("one"); |
| 96 |
verify(result, times(1)).onNext("two"); |
| 97 |
verify(result, times(0)).onNext("three"); |
| 98 |
verify(result, times(1)).onError(error); |
| 99 |
verify(sSource, never()).cancel(); |
| 100 |
verify(sOther, times(1)).cancel(); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void takeUntilOtherError() { |
| 106 |
Subscription sSource = mock(Subscription.class); |
| 107 |
Subscription sOther = mock(Subscription.class); |
| 108 |
TestObservable source = new TestObservable(sSource); |
| 109 |
TestObservable other = new TestObservable(sOther); |
| 110 |
Throwable error = new Throwable(); |
| 111 |
|
| 112 |
Subscriber<String> result = TestHelper.mockSubscriber(); |
| 113 |
Flowable<String> stringObservable = Flowable.unsafeCreate(source).takeUntil(Flowable.unsafeCreate(other)); |
| 114 |
stringObservable.subscribe(result); |
| 115 |
source.sendOnNext("one"); |
| 116 |
source.sendOnNext("two"); |
| 117 |
other.sendOnError(error); |
| 118 |
source.sendOnNext("three"); |
| 119 |
|
| 120 |
verify(result, times(1)).onNext("one"); |
| 121 |
verify(result, times(1)).onNext("two"); |
| 122 |
verify(result, times(0)).onNext("three"); |
| 123 |
verify(result, times(1)).onError(error); |
| 124 |
verify(result, times(0)).onComplete(); |
| 125 |
verify(sSource, times(1)).cancel(); |
| 126 |
verify(sOther, never()).cancel(); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* If the 'other' onCompletes then we unsubscribe from the source and onComplete. |
| 132 |
*/ |
| 133 |
@Test |
| 134 |
public void takeUntilOtherCompleted() { |
| 135 |
Subscription sSource = mock(Subscription.class); |
| 136 |
Subscription sOther = mock(Subscription.class); |
| 137 |
TestObservable source = new TestObservable(sSource); |
| 138 |
TestObservable other = new TestObservable(sOther); |
| 139 |
|
| 140 |
Subscriber<String> result = TestHelper.mockSubscriber(); |
| 141 |
Flowable<String> stringObservable = Flowable.unsafeCreate(source).takeUntil(Flowable.unsafeCreate(other)); |
| 142 |
stringObservable.subscribe(result); |
| 143 |
source.sendOnNext("one"); |
| 144 |
source.sendOnNext("two"); |
| 145 |
other.sendOnCompleted(); |
| 146 |
source.sendOnNext("three"); |
| 147 |
|
| 148 |
verify(result, times(1)).onNext("one"); |
| 149 |
verify(result, times(1)).onNext("two"); |
| 150 |
verify(result, times(0)).onNext("three"); |
| 151 |
verify(result, times(1)).onComplete(); |
| 152 |
verify(sSource, times(1)).cancel(); |
| 153 |
verify(sOther, never()).cancel(); // unsubscribed since SafeSubscriber unsubscribes after onComplete |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
private static class TestObservable implements Publisher<String> { |
| 158 |
|
| 159 |
Subscriber<? super String> subscriber; |
| 160 |
Subscription upstream; |
| 161 |
|
| 162 |
TestObservable(Subscription s) { |
| 163 |
this.upstream = s; |
| 164 |
} |
| 165 |
|
| 166 |
/* used to simulate subscription */ |
| 167 |
public void sendOnCompleted() { |
| 168 |
subscriber.onComplete(); |
| 169 |
} |
| 170 |
|
| 171 |
/* used to simulate subscription */ |
| 172 |
public void sendOnNext(String value) { |
| 173 |
subscriber.onNext(value); |
| 174 |
} |
| 175 |
|
| 176 |
/* used to simulate subscription */ |
| 177 |
public void sendOnError(Throwable e) { |
| 178 |
subscriber.onError(e); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 183 |
this.subscriber = subscriber; |
| 184 |
subscriber.onSubscribe(upstream); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void untilFires() { |
| 190 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 191 |
PublishProcessor<Integer> until = PublishProcessor.create(); |
| 192 |
|
| 193 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 194 |
|
| 195 |
source.takeUntil(until).subscribe(ts); |
| 196 |
|
| 197 |
assertTrue(source.hasSubscribers()); |
| 198 |
assertTrue(until.hasSubscribers()); |
| 199 |
|
| 200 |
source.onNext(1); |
| 201 |
|
| 202 |
ts.assertValue(1); |
| 203 |
until.onNext(1); |
| 204 |
|
| 205 |
ts.assertValue(1); |
| 206 |
ts.assertNoErrors(); |
| 207 |
ts.assertTerminated(); |
| 208 |
|
| 209 |
assertFalse("Source still has observers", source.hasSubscribers()); |
| 210 |
assertFalse("Until still has observers", until.hasSubscribers()); |
| 211 |
assertFalse("TestSubscriber is unsubscribed", ts.isCancelled()); |
| 212 |
} |
| 213 |
|
| 214 |
@Test |
| 215 |
public void mainCompletes() { |
| 216 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 217 |
PublishProcessor<Integer> until = PublishProcessor.create(); |
| 218 |
|
| 219 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 220 |
|
| 221 |
source.takeUntil(until).subscribe(ts); |
| 222 |
|
| 223 |
assertTrue(source.hasSubscribers()); |
| 224 |
assertTrue(until.hasSubscribers()); |
| 225 |
|
| 226 |
source.onNext(1); |
| 227 |
source.onComplete(); |
| 228 |
|
| 229 |
ts.assertValue(1); |
| 230 |
ts.assertNoErrors(); |
| 231 |
ts.assertTerminated(); |
| 232 |
|
| 233 |
assertFalse("Source still has observers", source.hasSubscribers()); |
| 234 |
assertFalse("Until still has observers", until.hasSubscribers()); |
| 235 |
assertFalse("TestSubscriber is unsubscribed", ts.isCancelled()); |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void downstreamUnsubscribes() { |
| 240 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 241 |
PublishProcessor<Integer> until = PublishProcessor.create(); |
| 242 |
|
| 243 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 244 |
|
| 245 |
source.takeUntil(until).take(1).subscribe(ts); |
| 246 |
|
| 247 |
assertTrue(source.hasSubscribers()); |
| 248 |
assertTrue(until.hasSubscribers()); |
| 249 |
|
| 250 |
source.onNext(1); |
| 251 |
|
| 252 |
ts.assertValue(1); |
| 253 |
ts.assertNoErrors(); |
| 254 |
ts.assertTerminated(); |
| 255 |
|
| 256 |
assertFalse("Source still has observers", source.hasSubscribers()); |
| 257 |
assertFalse("Until still has observers", until.hasSubscribers()); |
| 258 |
assertFalse("TestSubscriber is unsubscribed", ts.isCancelled()); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void backpressure() { |
| 263 |
PublishProcessor<Integer> until = PublishProcessor.create(); |
| 264 |
|
| 265 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 266 |
|
| 267 |
Flowable.range(1, 10).takeUntil(until).subscribe(ts); |
| 268 |
|
| 269 |
assertTrue(until.hasSubscribers()); |
| 270 |
|
| 271 |
ts.request(1); |
| 272 |
|
| 273 |
ts.assertValue(1); |
| 274 |
ts.assertNoErrors(); |
| 275 |
ts.assertNotComplete(); |
| 276 |
|
| 277 |
until.onNext(5); |
| 278 |
|
| 279 |
ts.assertComplete(); |
| 280 |
ts.assertNoErrors(); |
| 281 |
|
| 282 |
assertFalse("Until still has observers", until.hasSubscribers()); |
| 283 |
assertFalse("TestSubscriber is unsubscribed", ts.isCancelled()); |
| 284 |
} |
| 285 |
|
| 286 |
@Test |
| 287 |
public void dispose() { |
| 288 |
TestHelper.checkDisposed(PublishProcessor.create().takeUntil(Flowable.never())); |
| 289 |
} |
| 290 |
|
| 291 |
@Test |
| 292 |
public void doubleOnSubscribe() { |
| 293 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Integer>, Flowable<Integer>>() { |
| 294 |
@Override |
| 295 |
public Flowable<Integer> apply(Flowable<Integer> c) throws Exception { |
| 296 |
return c.takeUntil(Flowable.never()); |
| 297 |
} |
| 298 |
}); |
| 299 |
} |
| 300 |
|
| 301 |
@Test |
| 302 |
public void untilPublisherMainSuccess() { |
| 303 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 304 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 305 |
|
| 306 |
TestSubscriber<Integer> ts = main.takeUntil(other).test(); |
| 307 |
|
| 308 |
assertTrue("Main no subscribers?", main.hasSubscribers()); |
| 309 |
assertTrue("Other no subscribers?", other.hasSubscribers()); |
| 310 |
|
| 311 |
main.onNext(1); |
| 312 |
main.onNext(2); |
| 313 |
main.onComplete(); |
| 314 |
|
| 315 |
assertFalse("Main has subscribers?", main.hasSubscribers()); |
| 316 |
assertFalse("Other has subscribers?", other.hasSubscribers()); |
| 317 |
|
| 318 |
ts.assertResult(1, 2); |
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void untilPublisherMainComplete() { |
| 323 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 324 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 325 |
|
| 326 |
TestSubscriber<Integer> ts = main.takeUntil(other).test(); |
| 327 |
|
| 328 |
assertTrue("Main no subscribers?", main.hasSubscribers()); |
| 329 |
assertTrue("Other no subscribers?", other.hasSubscribers()); |
| 330 |
|
| 331 |
main.onComplete(); |
| 332 |
|
| 333 |
assertFalse("Main has subscribers?", main.hasSubscribers()); |
| 334 |
assertFalse("Other has subscribers?", other.hasSubscribers()); |
| 335 |
|
| 336 |
ts.assertResult(); |
| 337 |
} |
| 338 |
|
| 339 |
@Test |
| 340 |
public void untilPublisherMainError() { |
| 341 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 342 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 343 |
|
| 344 |
TestSubscriber<Integer> ts = main.takeUntil(other).test(); |
| 345 |
|
| 346 |
assertTrue("Main no subscribers?", main.hasSubscribers()); |
| 347 |
assertTrue("Other no subscribers?", other.hasSubscribers()); |
| 348 |
|
| 349 |
main.onError(new TestException()); |
| 350 |
|
| 351 |
assertFalse("Main has subscribers?", main.hasSubscribers()); |
| 352 |
assertFalse("Other has subscribers?", other.hasSubscribers()); |
| 353 |
|
| 354 |
ts.assertFailure(TestException.class); |
| 355 |
} |
| 356 |
|
| 357 |
@Test |
| 358 |
public void untilPublisherOtherOnNext() { |
| 359 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 360 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 361 |
|
| 362 |
TestSubscriber<Integer> ts = main.takeUntil(other).test(); |
| 363 |
|
| 364 |
assertTrue("Main no subscribers?", main.hasSubscribers()); |
| 365 |
assertTrue("Other no subscribers?", other.hasSubscribers()); |
| 366 |
|
| 367 |
other.onNext(1); |
| 368 |
|
| 369 |
assertFalse("Main has subscribers?", main.hasSubscribers()); |
| 370 |
assertFalse("Other has subscribers?", other.hasSubscribers()); |
| 371 |
|
| 372 |
ts.assertResult(); |
| 373 |
} |
| 374 |
|
| 375 |
@Test |
| 376 |
public void untilPublisherOtherOnComplete() { |
| 377 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 378 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 379 |
|
| 380 |
TestSubscriber<Integer> ts = main.takeUntil(other).test(); |
| 381 |
|
| 382 |
assertTrue("Main no subscribers?", main.hasSubscribers()); |
| 383 |
assertTrue("Other no subscribers?", other.hasSubscribers()); |
| 384 |
|
| 385 |
other.onComplete(); |
| 386 |
|
| 387 |
assertFalse("Main has subscribers?", main.hasSubscribers()); |
| 388 |
assertFalse("Other has subscribers?", other.hasSubscribers()); |
| 389 |
|
| 390 |
ts.assertResult(); |
| 391 |
} |
| 392 |
|
| 393 |
@Test |
| 394 |
public void untilPublisherOtherError() { |
| 395 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 396 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 397 |
|
| 398 |
TestSubscriber<Integer> ts = main.takeUntil(other).test(); |
| 399 |
|
| 400 |
assertTrue("Main no subscribers?", main.hasSubscribers()); |
| 401 |
assertTrue("Other no subscribers?", other.hasSubscribers()); |
| 402 |
|
| 403 |
other.onError(new TestException()); |
| 404 |
|
| 405 |
assertFalse("Main has subscribers?", main.hasSubscribers()); |
| 406 |
assertFalse("Other has subscribers?", other.hasSubscribers()); |
| 407 |
|
| 408 |
ts.assertFailure(TestException.class); |
| 409 |
} |
| 410 |
|
| 411 |
@Test |
| 412 |
public void untilPublisherDispose() { |
| 413 |
PublishProcessor<Integer> main = PublishProcessor.create(); |
| 414 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 415 |
|
| 416 |
TestSubscriber<Integer> ts = main.takeUntil(other).test(); |
| 417 |
|
| 418 |
assertTrue("Main no subscribers?", main.hasSubscribers()); |
| 419 |
assertTrue("Other no subscribers?", other.hasSubscribers()); |
| 420 |
|
| 421 |
ts.cancel(); |
| 422 |
|
| 423 |
assertFalse("Main has subscribers?", main.hasSubscribers()); |
| 424 |
assertFalse("Other has subscribers?", other.hasSubscribers()); |
| 425 |
|
| 426 |
ts.assertEmpty(); |
| 427 |
} |
| 428 |
|
| 429 |
} |