| 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.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 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.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 28 |
import io.reactivex.rxjava3.subjects.MaybeSubject; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class MaybeTakeUntilTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void normalPublisher() { |
| 35 |
Maybe.just(1).takeUntil(Flowable.never()) |
| 36 |
.test() |
| 37 |
.assertResult(1); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void normalMaybe() { |
| 42 |
Maybe.just(1).takeUntil(Maybe.never()) |
| 43 |
.test() |
| 44 |
.assertResult(1); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void untilFirstPublisher() { |
| 49 |
Maybe.just(1).takeUntil(Flowable.just("one")) |
| 50 |
.test() |
| 51 |
.assertResult(); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void untilFirstMaybe() { |
| 56 |
Maybe.just(1).takeUntil(Maybe.just("one")) |
| 57 |
.test() |
| 58 |
.assertResult(); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void disposed() { |
| 63 |
TestHelper.checkDisposed(PublishProcessor.create().singleElement().takeUntil(Maybe.never())); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void doubleOnSubscribe() { |
| 68 |
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() { |
| 69 |
@Override |
| 70 |
public MaybeSource<Object> apply(Maybe<Object> m) throws Exception { |
| 71 |
return m.takeUntil(Maybe.never()); |
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void mainErrors() { |
| 78 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 79 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 80 |
|
| 81 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2.singleElement()).test(); |
| 82 |
|
| 83 |
assertTrue(pp1.hasSubscribers()); |
| 84 |
assertTrue(pp2.hasSubscribers()); |
| 85 |
|
| 86 |
pp1.onError(new TestException()); |
| 87 |
|
| 88 |
assertFalse(pp1.hasSubscribers()); |
| 89 |
assertFalse(pp2.hasSubscribers()); |
| 90 |
|
| 91 |
to.assertFailure(TestException.class); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void otherErrors() { |
| 96 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 97 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 98 |
|
| 99 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2.singleElement()).test(); |
| 100 |
|
| 101 |
assertTrue(pp1.hasSubscribers()); |
| 102 |
assertTrue(pp2.hasSubscribers()); |
| 103 |
|
| 104 |
pp2.onError(new TestException()); |
| 105 |
|
| 106 |
assertFalse(pp1.hasSubscribers()); |
| 107 |
assertFalse(pp2.hasSubscribers()); |
| 108 |
|
| 109 |
to.assertFailure(TestException.class); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void mainCompletes() { |
| 114 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 115 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 116 |
|
| 117 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2.singleElement()).test(); |
| 118 |
|
| 119 |
assertTrue(pp1.hasSubscribers()); |
| 120 |
assertTrue(pp2.hasSubscribers()); |
| 121 |
|
| 122 |
pp1.onComplete(); |
| 123 |
|
| 124 |
assertFalse(pp1.hasSubscribers()); |
| 125 |
assertFalse(pp2.hasSubscribers()); |
| 126 |
|
| 127 |
to.assertResult(); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void otherCompletes() { |
| 132 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 133 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 134 |
|
| 135 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2.singleElement()).test(); |
| 136 |
|
| 137 |
assertTrue(pp1.hasSubscribers()); |
| 138 |
assertTrue(pp2.hasSubscribers()); |
| 139 |
|
| 140 |
pp2.onComplete(); |
| 141 |
|
| 142 |
assertFalse(pp1.hasSubscribers()); |
| 143 |
assertFalse(pp2.hasSubscribers()); |
| 144 |
|
| 145 |
to.assertResult(); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void onErrorRace() { |
| 150 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 151 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 152 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 153 |
|
| 154 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2.singleElement()).test(); |
| 155 |
|
| 156 |
final TestException ex1 = new TestException(); |
| 157 |
final TestException ex2 = new TestException(); |
| 158 |
|
| 159 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 160 |
try { |
| 161 |
|
| 162 |
Runnable r1 = new Runnable() { |
| 163 |
@Override |
| 164 |
public void run() { |
| 165 |
pp1.onError(ex1); |
| 166 |
} |
| 167 |
}; |
| 168 |
Runnable r2 = new Runnable() { |
| 169 |
@Override |
| 170 |
public void run() { |
| 171 |
pp2.onError(ex2); |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
TestHelper.race(r1, r2); |
| 176 |
|
| 177 |
to.assertFailure(TestException.class); |
| 178 |
|
| 179 |
if (!errors.isEmpty()) { |
| 180 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 181 |
} |
| 182 |
|
| 183 |
} finally { |
| 184 |
RxJavaPlugins.reset(); |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void onCompleteRace() { |
| 192 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 193 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 194 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 195 |
|
| 196 |
TestObserver<Integer> to = pp1.singleElement().takeUntil(pp2.singleElement()).test(); |
| 197 |
|
| 198 |
Runnable r1 = new Runnable() { |
| 199 |
@Override |
| 200 |
public void run() { |
| 201 |
pp1.onComplete(); |
| 202 |
} |
| 203 |
}; |
| 204 |
Runnable r2 = new Runnable() { |
| 205 |
@Override |
| 206 |
public void run() { |
| 207 |
pp2.onComplete(); |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
TestHelper.race(r1, r2); |
| 212 |
|
| 213 |
to.assertResult(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
@Test |
| 218 |
public void untilMaybeMainSuccess() { |
| 219 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 220 |
MaybeSubject<Integer> other = MaybeSubject.create(); |
| 221 |
|
| 222 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 223 |
|
| 224 |
assertTrue("Main no observers?", main.hasObservers()); |
| 225 |
assertTrue("Other no observers?", other.hasObservers()); |
| 226 |
|
| 227 |
main.onSuccess(1); |
| 228 |
|
| 229 |
assertFalse("Main has observers?", main.hasObservers()); |
| 230 |
assertFalse("Other has observers?", other.hasObservers()); |
| 231 |
|
| 232 |
to.assertResult(1); |
| 233 |
} |
| 234 |
|
| 235 |
@Test |
| 236 |
public void untilMaybeMainComplete() { |
| 237 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 238 |
MaybeSubject<Integer> other = MaybeSubject.create(); |
| 239 |
|
| 240 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 241 |
|
| 242 |
assertTrue("Main no observers?", main.hasObservers()); |
| 243 |
assertTrue("Other no observers?", other.hasObservers()); |
| 244 |
|
| 245 |
main.onComplete(); |
| 246 |
|
| 247 |
assertFalse("Main has observers?", main.hasObservers()); |
| 248 |
assertFalse("Other has observers?", other.hasObservers()); |
| 249 |
|
| 250 |
to.assertResult(); |
| 251 |
} |
| 252 |
|
| 253 |
@Test |
| 254 |
public void untilMaybeMainError() { |
| 255 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 256 |
MaybeSubject<Integer> other = MaybeSubject.create(); |
| 257 |
|
| 258 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 259 |
|
| 260 |
assertTrue("Main no observers?", main.hasObservers()); |
| 261 |
assertTrue("Other no observers?", other.hasObservers()); |
| 262 |
|
| 263 |
main.onError(new TestException()); |
| 264 |
|
| 265 |
assertFalse("Main has observers?", main.hasObservers()); |
| 266 |
assertFalse("Other has observers?", other.hasObservers()); |
| 267 |
|
| 268 |
to.assertFailure(TestException.class); |
| 269 |
} |
| 270 |
|
| 271 |
@Test |
| 272 |
public void untilMaybeOtherSuccess() { |
| 273 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 274 |
MaybeSubject<Integer> other = MaybeSubject.create(); |
| 275 |
|
| 276 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 277 |
|
| 278 |
assertTrue("Main no observers?", main.hasObservers()); |
| 279 |
assertTrue("Other no observers?", other.hasObservers()); |
| 280 |
|
| 281 |
other.onSuccess(1); |
| 282 |
|
| 283 |
assertFalse("Main has observers?", main.hasObservers()); |
| 284 |
assertFalse("Other has observers?", other.hasObservers()); |
| 285 |
|
| 286 |
to.assertResult(); |
| 287 |
} |
| 288 |
|
| 289 |
@Test |
| 290 |
public void untilMaybeOtherComplete() { |
| 291 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 292 |
MaybeSubject<Integer> other = MaybeSubject.create(); |
| 293 |
|
| 294 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 295 |
|
| 296 |
assertTrue("Main no observers?", main.hasObservers()); |
| 297 |
assertTrue("Other no observers?", other.hasObservers()); |
| 298 |
|
| 299 |
other.onComplete(); |
| 300 |
|
| 301 |
assertFalse("Main has observers?", main.hasObservers()); |
| 302 |
assertFalse("Other has observers?", other.hasObservers()); |
| 303 |
|
| 304 |
to.assertResult(); |
| 305 |
} |
| 306 |
|
| 307 |
@Test |
| 308 |
public void untilMaybeOtherError() { |
| 309 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 310 |
MaybeSubject<Integer> other = MaybeSubject.create(); |
| 311 |
|
| 312 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 313 |
|
| 314 |
assertTrue("Main no observers?", main.hasObservers()); |
| 315 |
assertTrue("Other no observers?", other.hasObservers()); |
| 316 |
|
| 317 |
other.onError(new TestException()); |
| 318 |
|
| 319 |
assertFalse("Main has observers?", main.hasObservers()); |
| 320 |
assertFalse("Other has observers?", other.hasObservers()); |
| 321 |
|
| 322 |
to.assertFailure(TestException.class); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void untilMaybeDispose() { |
| 327 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 328 |
MaybeSubject<Integer> other = MaybeSubject.create(); |
| 329 |
|
| 330 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 331 |
|
| 332 |
assertTrue("Main no observers?", main.hasObservers()); |
| 333 |
assertTrue("Other no observers?", other.hasObservers()); |
| 334 |
|
| 335 |
to.dispose(); |
| 336 |
|
| 337 |
assertFalse("Main has observers?", main.hasObservers()); |
| 338 |
assertFalse("Other has observers?", other.hasObservers()); |
| 339 |
|
| 340 |
to.assertEmpty(); |
| 341 |
} |
| 342 |
|
| 343 |
@Test |
| 344 |
public void untilPublisherMainSuccess() { |
| 345 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 346 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 347 |
|
| 348 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 349 |
|
| 350 |
assertTrue("Main no observers?", main.hasObservers()); |
| 351 |
assertTrue("Other no observers?", other.hasSubscribers()); |
| 352 |
|
| 353 |
main.onSuccess(1); |
| 354 |
|
| 355 |
assertFalse("Main has observers?", main.hasObservers()); |
| 356 |
assertFalse("Other has observers?", other.hasSubscribers()); |
| 357 |
|
| 358 |
to.assertResult(1); |
| 359 |
} |
| 360 |
|
| 361 |
@Test |
| 362 |
public void untilPublisherMainComplete() { |
| 363 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 364 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 365 |
|
| 366 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 367 |
|
| 368 |
assertTrue("Main no observers?", main.hasObservers()); |
| 369 |
assertTrue("Other no observers?", other.hasSubscribers()); |
| 370 |
|
| 371 |
main.onComplete(); |
| 372 |
|
| 373 |
assertFalse("Main has observers?", main.hasObservers()); |
| 374 |
assertFalse("Other has observers?", other.hasSubscribers()); |
| 375 |
|
| 376 |
to.assertResult(); |
| 377 |
} |
| 378 |
|
| 379 |
@Test |
| 380 |
public void untilPublisherMainError() { |
| 381 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 382 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 383 |
|
| 384 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 385 |
|
| 386 |
assertTrue("Main no observers?", main.hasObservers()); |
| 387 |
assertTrue("Other no observers?", other.hasSubscribers()); |
| 388 |
|
| 389 |
main.onError(new TestException()); |
| 390 |
|
| 391 |
assertFalse("Main has observers?", main.hasObservers()); |
| 392 |
assertFalse("Other has observers?", other.hasSubscribers()); |
| 393 |
|
| 394 |
to.assertFailure(TestException.class); |
| 395 |
} |
| 396 |
|
| 397 |
@Test |
| 398 |
public void untilPublisherOtherOnNext() { |
| 399 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 400 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 401 |
|
| 402 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 403 |
|
| 404 |
assertTrue("Main no observers?", main.hasObservers()); |
| 405 |
assertTrue("Other no observers?", other.hasSubscribers()); |
| 406 |
|
| 407 |
other.onNext(1); |
| 408 |
|
| 409 |
assertFalse("Main has observers?", main.hasObservers()); |
| 410 |
assertFalse("Other has observers?", other.hasSubscribers()); |
| 411 |
|
| 412 |
to.assertResult(); |
| 413 |
} |
| 414 |
|
| 415 |
@Test |
| 416 |
public void untilPublisherOtherOnComplete() { |
| 417 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 418 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 419 |
|
| 420 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 421 |
|
| 422 |
assertTrue("Main no observers?", main.hasObservers()); |
| 423 |
assertTrue("Other no observers?", other.hasSubscribers()); |
| 424 |
|
| 425 |
other.onComplete(); |
| 426 |
|
| 427 |
assertFalse("Main has observers?", main.hasObservers()); |
| 428 |
assertFalse("Other has observers?", other.hasSubscribers()); |
| 429 |
|
| 430 |
to.assertResult(); |
| 431 |
} |
| 432 |
|
| 433 |
@Test |
| 434 |
public void untilPublisherOtherError() { |
| 435 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 436 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 437 |
|
| 438 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 439 |
|
| 440 |
assertTrue("Main no observers?", main.hasObservers()); |
| 441 |
assertTrue("Other no observers?", other.hasSubscribers()); |
| 442 |
|
| 443 |
other.onError(new TestException()); |
| 444 |
|
| 445 |
assertFalse("Main has observers?", main.hasObservers()); |
| 446 |
assertFalse("Other has observers?", other.hasSubscribers()); |
| 447 |
|
| 448 |
to.assertFailure(TestException.class); |
| 449 |
} |
| 450 |
|
| 451 |
@Test |
| 452 |
public void untilPublisherDispose() { |
| 453 |
MaybeSubject<Integer> main = MaybeSubject.create(); |
| 454 |
PublishProcessor<Integer> other = PublishProcessor.create(); |
| 455 |
|
| 456 |
TestObserver<Integer> to = main.takeUntil(other).test(); |
| 457 |
|
| 458 |
assertTrue("Main no observers?", main.hasObservers()); |
| 459 |
assertTrue("Other no observers?", other.hasSubscribers()); |
| 460 |
|
| 461 |
to.dispose(); |
| 462 |
|
| 463 |
assertFalse("Main has observers?", main.hasObservers()); |
| 464 |
assertFalse("Other has observers?", other.hasSubscribers()); |
| 465 |
|
| 466 |
to.assertEmpty(); |
| 467 |
} |
| 468 |
} |