| 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.assertTrue; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.concurrent.TimeUnit; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.Maybe; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 28 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class MaybeBlockingSubscribeTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void noArgSuccess() { |
| 35 |
Maybe.just(1) |
| 36 |
.blockingSubscribe(); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void noArgSuccessAsync() { |
| 41 |
Maybe.just(1) |
| 42 |
.delay(100, TimeUnit.MILLISECONDS) |
| 43 |
.blockingSubscribe(); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void noArgEmpty() { |
| 48 |
Maybe.empty() |
| 49 |
.blockingSubscribe(); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void noArgEmptyAsync() { |
| 54 |
Maybe.empty() |
| 55 |
.delay(100, TimeUnit.MILLISECONDS) |
| 56 |
.blockingSubscribe(); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void noArgError() throws Throwable { |
| 61 |
TestHelper.withErrorTracking(errors -> { |
| 62 |
Maybe.error(new TestException()) |
| 63 |
.blockingSubscribe(); |
| 64 |
|
| 65 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void noArgErrorAsync() throws Throwable { |
| 71 |
TestHelper.withErrorTracking(errors -> { |
| 72 |
Maybe.error(new TestException()) |
| 73 |
.delay(100, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 74 |
.blockingSubscribe(); |
| 75 |
|
| 76 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void oneArgSuccess() throws Throwable { |
| 82 |
@SuppressWarnings("unchecked") |
| 83 |
Consumer<Integer> success = mock(Consumer.class); |
| 84 |
|
| 85 |
Maybe.just(1) |
| 86 |
.blockingSubscribe(success); |
| 87 |
|
| 88 |
verify(success).accept(1); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void oneArgSuccessAsync() throws Throwable { |
| 93 |
@SuppressWarnings("unchecked") |
| 94 |
Consumer<Integer> success = mock(Consumer.class); |
| 95 |
|
| 96 |
Maybe.just(1) |
| 97 |
.delay(50, TimeUnit.MILLISECONDS) |
| 98 |
.blockingSubscribe(success); |
| 99 |
|
| 100 |
verify(success).accept(1); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void oneArgEmpty() throws Throwable { |
| 105 |
@SuppressWarnings("unchecked") |
| 106 |
Consumer<Integer> success = mock(Consumer.class); |
| 107 |
|
| 108 |
Maybe.<Integer>empty() |
| 109 |
.blockingSubscribe(success); |
| 110 |
|
| 111 |
verify(success, never()).accept(any()); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void oneArgEmptyAsync() throws Throwable { |
| 116 |
@SuppressWarnings("unchecked") |
| 117 |
Consumer<Integer> success = mock(Consumer.class); |
| 118 |
|
| 119 |
Maybe.<Integer>empty() |
| 120 |
.delay(50, TimeUnit.MILLISECONDS) |
| 121 |
.blockingSubscribe(success); |
| 122 |
|
| 123 |
verify(success, never()).accept(any()); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void oneArgSuccessFails() throws Throwable { |
| 128 |
TestHelper.withErrorTracking(errors -> { |
| 129 |
@SuppressWarnings("unchecked") |
| 130 |
Consumer<Integer> success = mock(Consumer.class); |
| 131 |
doThrow(new TestException()).when(success).accept(any()); |
| 132 |
|
| 133 |
Maybe.just(1) |
| 134 |
.blockingSubscribe(success); |
| 135 |
|
| 136 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 137 |
|
| 138 |
verify(success).accept(1); |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void oneArgError() throws Throwable { |
| 144 |
TestHelper.withErrorTracking(errors -> { |
| 145 |
@SuppressWarnings("unchecked") |
| 146 |
Consumer<Integer> success = mock(Consumer.class); |
| 147 |
|
| 148 |
Maybe.<Integer>error(new TestException()) |
| 149 |
.blockingSubscribe(success); |
| 150 |
|
| 151 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 152 |
|
| 153 |
verify(success, never()).accept(any()); |
| 154 |
}); |
| 155 |
} |
| 156 |
|
| 157 |
@Test |
| 158 |
public void oneArgErrorAsync() throws Throwable { |
| 159 |
TestHelper.withErrorTracking(errors -> { |
| 160 |
@SuppressWarnings("unchecked") |
| 161 |
Consumer<Integer> success = mock(Consumer.class); |
| 162 |
|
| 163 |
Maybe.<Integer>error(new TestException()) |
| 164 |
.delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 165 |
.blockingSubscribe(success); |
| 166 |
|
| 167 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 168 |
|
| 169 |
verify(success, never()).accept(any()); |
| 170 |
}); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void twoArgSuccess() throws Throwable { |
| 175 |
@SuppressWarnings("unchecked") |
| 176 |
Consumer<Integer> success = mock(Consumer.class); |
| 177 |
@SuppressWarnings("unchecked") |
| 178 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 179 |
|
| 180 |
Maybe.just(1) |
| 181 |
.blockingSubscribe(success, consumer); |
| 182 |
|
| 183 |
verify(success).accept(1); |
| 184 |
verify(consumer, never()).accept(any()); |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void twoArgSuccessAsync() throws Throwable { |
| 189 |
@SuppressWarnings("unchecked") |
| 190 |
Consumer<Integer> success = mock(Consumer.class); |
| 191 |
@SuppressWarnings("unchecked") |
| 192 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 193 |
|
| 194 |
Maybe.just(1) |
| 195 |
.delay(50, TimeUnit.MILLISECONDS) |
| 196 |
.blockingSubscribe(success, consumer); |
| 197 |
|
| 198 |
verify(success).accept(any()); |
| 199 |
verify(consumer, never()).accept(any()); |
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
public void twoArgEmpty() throws Throwable { |
| 204 |
@SuppressWarnings("unchecked") |
| 205 |
Consumer<Integer> success = mock(Consumer.class); |
| 206 |
@SuppressWarnings("unchecked") |
| 207 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 208 |
|
| 209 |
Maybe.<Integer>empty() |
| 210 |
.blockingSubscribe(success, consumer); |
| 211 |
|
| 212 |
verify(success, never()).accept(any()); |
| 213 |
verify(consumer, never()).accept(any()); |
| 214 |
} |
| 215 |
|
| 216 |
@Test |
| 217 |
public void twoArgEmptyAsync() throws Throwable { |
| 218 |
@SuppressWarnings("unchecked") |
| 219 |
Consumer<Integer> success = mock(Consumer.class); |
| 220 |
@SuppressWarnings("unchecked") |
| 221 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 222 |
|
| 223 |
Maybe.<Integer>empty() |
| 224 |
.delay(50, TimeUnit.MILLISECONDS) |
| 225 |
.blockingSubscribe(success, consumer); |
| 226 |
|
| 227 |
verify(success, never()).accept(any()); |
| 228 |
verify(consumer, never()).accept(any()); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void twoArgSuccessFails() throws Throwable { |
| 233 |
TestHelper.withErrorTracking(errors -> { |
| 234 |
@SuppressWarnings("unchecked") |
| 235 |
Consumer<Integer> success = mock(Consumer.class); |
| 236 |
doThrow(new TestException()).when(success).accept(any()); |
| 237 |
@SuppressWarnings("unchecked") |
| 238 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 239 |
|
| 240 |
Maybe.just(1) |
| 241 |
.blockingSubscribe(success, consumer); |
| 242 |
|
| 243 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 244 |
|
| 245 |
verify(success).accept(any()); |
| 246 |
verify(consumer, never()).accept(any()); |
| 247 |
}); |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void twoArgError() throws Throwable { |
| 252 |
TestHelper.withErrorTracking(errors -> { |
| 253 |
@SuppressWarnings("unchecked") |
| 254 |
Consumer<Integer> success = mock(Consumer.class); |
| 255 |
@SuppressWarnings("unchecked") |
| 256 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 257 |
|
| 258 |
Maybe.<Integer>error(new TestException()) |
| 259 |
.blockingSubscribe(success, consumer); |
| 260 |
|
| 261 |
assertTrue("" + errors, errors.isEmpty()); |
| 262 |
|
| 263 |
verify(success, never()).accept(any()); |
| 264 |
verify(consumer).accept(any(TestException.class)); |
| 265 |
}); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void twoArgErrorAsync() throws Throwable { |
| 270 |
TestHelper.withErrorTracking(errors -> { |
| 271 |
@SuppressWarnings("unchecked") |
| 272 |
Consumer<Integer> success = mock(Consumer.class); |
| 273 |
@SuppressWarnings("unchecked") |
| 274 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 275 |
|
| 276 |
Maybe.<Integer>error(new TestException()) |
| 277 |
.delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 278 |
.blockingSubscribe(success, consumer); |
| 279 |
|
| 280 |
assertTrue("" + errors, errors.isEmpty()); |
| 281 |
|
| 282 |
verify(success, never()).accept(any()); |
| 283 |
verify(consumer).accept(any(TestException.class)); |
| 284 |
}); |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void twoArgErrorFails() throws Throwable { |
| 289 |
TestHelper.withErrorTracking(errors -> { |
| 290 |
@SuppressWarnings("unchecked") |
| 291 |
Consumer<Integer> success = mock(Consumer.class); |
| 292 |
@SuppressWarnings("unchecked") |
| 293 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 294 |
doThrow(new TestException()).when(consumer).accept(any()); |
| 295 |
|
| 296 |
Maybe.<Integer>error(new TestException()) |
| 297 |
.delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 298 |
.blockingSubscribe(success, consumer); |
| 299 |
|
| 300 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 301 |
|
| 302 |
verify(success, never()).accept(any()); |
| 303 |
verify(consumer).accept(any(TestException.class)); |
| 304 |
}); |
| 305 |
} |
| 306 |
|
| 307 |
@Test |
| 308 |
public void threeArgSuccess() throws Throwable { |
| 309 |
@SuppressWarnings("unchecked") |
| 310 |
Consumer<Integer> success = mock(Consumer.class); |
| 311 |
@SuppressWarnings("unchecked") |
| 312 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 313 |
Action action = mock(Action.class); |
| 314 |
|
| 315 |
Maybe.just(1) |
| 316 |
.blockingSubscribe(success, consumer, action); |
| 317 |
|
| 318 |
verify(success).accept(any()); |
| 319 |
verify(consumer, never()).accept(any(Throwable.class)); |
| 320 |
verify(action, never()).run(); |
| 321 |
} |
| 322 |
|
| 323 |
@Test |
| 324 |
public void threeArgEmpty() throws Throwable { |
| 325 |
@SuppressWarnings("unchecked") |
| 326 |
Consumer<Integer> success = mock(Consumer.class); |
| 327 |
@SuppressWarnings("unchecked") |
| 328 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 329 |
Action action = mock(Action.class); |
| 330 |
|
| 331 |
Maybe.<Integer>empty() |
| 332 |
.blockingSubscribe(success, consumer, action); |
| 333 |
|
| 334 |
verify(success, never()).accept(any()); |
| 335 |
verify(consumer, never()).accept(any(Throwable.class)); |
| 336 |
verify(action).run(); |
| 337 |
} |
| 338 |
|
| 339 |
@Test |
| 340 |
public void threeArgError() throws Throwable { |
| 341 |
@SuppressWarnings("unchecked") |
| 342 |
Consumer<Integer> success = mock(Consumer.class); |
| 343 |
@SuppressWarnings("unchecked") |
| 344 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 345 |
Action action = mock(Action.class); |
| 346 |
|
| 347 |
Maybe.<Integer>error(new TestException()) |
| 348 |
.blockingSubscribe(success, consumer, action); |
| 349 |
|
| 350 |
verify(success, never()).accept(any()); |
| 351 |
verify(consumer).accept(any(TestException.class)); |
| 352 |
verify(action, never()).run(); |
| 353 |
} |
| 354 |
|
| 355 |
@Test |
| 356 |
public void threeArgEmptyFails() throws Throwable { |
| 357 |
TestHelper.withErrorTracking(errors -> { |
| 358 |
@SuppressWarnings("unchecked") |
| 359 |
Consumer<Integer> success = mock(Consumer.class); |
| 360 |
@SuppressWarnings("unchecked") |
| 361 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 362 |
|
| 363 |
Action action = mock(Action.class); |
| 364 |
doThrow(new TestException()).when(action).run(); |
| 365 |
|
| 366 |
Maybe.<Integer>empty() |
| 367 |
.delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 368 |
.blockingSubscribe(success, consumer, action); |
| 369 |
|
| 370 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 371 |
|
| 372 |
verify(success, never()).accept(any()); |
| 373 |
verify(consumer, never()).accept(any()); |
| 374 |
verify(action).run(); |
| 375 |
}); |
| 376 |
} |
| 377 |
|
| 378 |
@Test |
| 379 |
public void threeArgInterrupted() throws Throwable { |
| 380 |
TestHelper.withErrorTracking(errors -> { |
| 381 |
Action onDispose = mock(Action.class); |
| 382 |
|
| 383 |
@SuppressWarnings("unchecked") |
| 384 |
Consumer<Integer> success = mock(Consumer.class); |
| 385 |
@SuppressWarnings("unchecked") |
| 386 |
Consumer<? super Throwable> consumer = mock(Consumer.class); |
| 387 |
Action action = mock(Action.class); |
| 388 |
|
| 389 |
Thread.currentThread().interrupt(); |
| 390 |
|
| 391 |
Maybe.<Integer>never() |
| 392 |
.doOnDispose(onDispose) |
| 393 |
.blockingSubscribe(success, consumer, action); |
| 394 |
|
| 395 |
assertTrue("" + errors, errors.isEmpty()); |
| 396 |
|
| 397 |
verify(onDispose).run(); |
| 398 |
verify(success, never()).accept(any()); |
| 399 |
verify(action, never()).run(); |
| 400 |
verify(consumer).accept(any(InterruptedException.class)); |
| 401 |
}); |
| 402 |
} |
| 403 |
|
| 404 |
@Test |
| 405 |
public void observerSuccess() { |
| 406 |
TestObserver<Integer> to = new TestObserver<>(); |
| 407 |
|
| 408 |
Maybe.just(1) |
| 409 |
.blockingSubscribe(to); |
| 410 |
|
| 411 |
to.assertResult(1); |
| 412 |
} |
| 413 |
|
| 414 |
@Test |
| 415 |
public void observerSuccessAsync() { |
| 416 |
TestObserver<Integer> to = new TestObserver<>(); |
| 417 |
|
| 418 |
Maybe.just(1) |
| 419 |
.delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 420 |
.blockingSubscribe(to); |
| 421 |
|
| 422 |
to.assertResult(1); |
| 423 |
} |
| 424 |
|
| 425 |
@Test |
| 426 |
public void observerEmpty() { |
| 427 |
TestObserver<Integer> to = new TestObserver<>(); |
| 428 |
|
| 429 |
Maybe.<Integer>empty() |
| 430 |
.blockingSubscribe(to); |
| 431 |
|
| 432 |
to.assertResult(); |
| 433 |
} |
| 434 |
|
| 435 |
@Test |
| 436 |
public void observerEmptyAsync() { |
| 437 |
TestObserver<Integer> to = new TestObserver<>(); |
| 438 |
|
| 439 |
Maybe.<Integer>empty() |
| 440 |
.delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 441 |
.blockingSubscribe(to); |
| 442 |
|
| 443 |
to.assertResult(); |
| 444 |
} |
| 445 |
|
| 446 |
@Test |
| 447 |
public void observerError() { |
| 448 |
TestObserver<Object> to = new TestObserver<>(); |
| 449 |
|
| 450 |
Maybe.error(new TestException()) |
| 451 |
.blockingSubscribe(to); |
| 452 |
|
| 453 |
to.assertFailure(TestException.class); |
| 454 |
} |
| 455 |
|
| 456 |
@Test |
| 457 |
public void observerErrorAsync() { |
| 458 |
TestObserver<Object> to = new TestObserver<>(); |
| 459 |
|
| 460 |
Maybe.error(new TestException()) |
| 461 |
.delay(50, TimeUnit.MILLISECONDS, Schedulers.computation()) |
| 462 |
.blockingSubscribe(to); |
| 463 |
|
| 464 |
to.assertFailure(TestException.class); |
| 465 |
} |
| 466 |
|
| 467 |
@Test |
| 468 |
public void observerDispose() throws Throwable { |
| 469 |
Action onDispose = mock(Action.class); |
| 470 |
|
| 471 |
TestObserver<Object> to = new TestObserver<>(); |
| 472 |
to.dispose(); |
| 473 |
|
| 474 |
Maybe.never() |
| 475 |
.doOnDispose(onDispose) |
| 476 |
.blockingSubscribe(to); |
| 477 |
|
| 478 |
to.assertEmpty(); |
| 479 |
|
| 480 |
verify(onDispose).run(); |
| 481 |
} |
| 482 |
|
| 483 |
@Test |
| 484 |
public void ovserverInterrupted() throws Throwable { |
| 485 |
TestHelper.withErrorTracking(errors -> { |
| 486 |
Action onDispose = mock(Action.class); |
| 487 |
|
| 488 |
TestObserver<Object> to = new TestObserver<>(); |
| 489 |
|
| 490 |
Thread.currentThread().interrupt(); |
| 491 |
|
| 492 |
Maybe.never() |
| 493 |
.doOnDispose(onDispose) |
| 494 |
.blockingSubscribe(to); |
| 495 |
|
| 496 |
assertTrue("" + errors, errors.isEmpty()); |
| 497 |
|
| 498 |
verify(onDispose).run(); |
| 499 |
to.assertFailure(InterruptedException.class); |
| 500 |
}); |
| 501 |
} |
| 502 |
} |