| 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.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.atomic.*; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.mockito.InOrder; |
| 25 |
import org.reactivestreams.*; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 31 |
import io.reactivex.rxjava3.subscribers.*; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
public class FlowableSingleTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void singleFlowable() { |
| 38 |
Flowable<Integer> flowable = Flowable.just(1).singleElement().toFlowable(); |
| 39 |
|
| 40 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 41 |
flowable.subscribe(subscriber); |
| 42 |
|
| 43 |
InOrder inOrder = inOrder(subscriber); |
| 44 |
inOrder.verify(subscriber, times(1)).onNext(1); |
| 45 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 46 |
inOrder.verifyNoMoreInteractions(); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void singleWithTooManyElementsFlowable() { |
| 51 |
Flowable<Integer> flowable = Flowable.just(1, 2).singleElement().toFlowable(); |
| 52 |
|
| 53 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 54 |
flowable.subscribe(subscriber); |
| 55 |
|
| 56 |
InOrder inOrder = inOrder(subscriber); |
| 57 |
inOrder.verify(subscriber, times(1)).onError( |
| 58 |
isA(IllegalArgumentException.class)); |
| 59 |
inOrder.verifyNoMoreInteractions(); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void singleWithEmptyFlowable() { |
| 64 |
Flowable<Integer> flowable = Flowable.<Integer> empty().singleElement().toFlowable(); |
| 65 |
|
| 66 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 67 |
flowable.subscribe(subscriber); |
| 68 |
|
| 69 |
InOrder inOrder = inOrder(subscriber); |
| 70 |
inOrder.verify(subscriber).onComplete(); |
| 71 |
inOrder.verify(subscriber, never()).onError(any(Throwable.class)); |
| 72 |
inOrder.verifyNoMoreInteractions(); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void singleDoesNotRequestMoreThanItNeedsIf1Then2RequestedFlowable() { |
| 77 |
final List<Long> requests = new ArrayList<>(); |
| 78 |
Flowable.just(1) |
| 79 |
// |
| 80 |
.doOnRequest(new LongConsumer() { |
| 81 |
@Override |
| 82 |
public void accept(long n) { |
| 83 |
requests.add(n); |
| 84 |
} |
| 85 |
}) |
| 86 |
// |
| 87 |
.singleElement() |
| 88 |
// |
| 89 |
.toFlowable() |
| 90 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onStart() { |
| 94 |
request(1); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onComplete() { |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onError(Throwable e) { |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onNext(Integer t) { |
| 109 |
request(2); |
| 110 |
} |
| 111 |
}); |
| 112 |
// FIXME single now triggers fast-path |
| 113 |
assertEquals(Arrays.asList(Long.MAX_VALUE), requests); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void singleDoesNotRequestMoreThanItNeedsIf3RequestedFlowable() { |
| 118 |
final List<Long> requests = new ArrayList<>(); |
| 119 |
Flowable.just(1) |
| 120 |
// |
| 121 |
.doOnRequest(new LongConsumer() { |
| 122 |
@Override |
| 123 |
public void accept(long n) { |
| 124 |
requests.add(n); |
| 125 |
} |
| 126 |
}) |
| 127 |
// |
| 128 |
.singleElement() |
| 129 |
// |
| 130 |
.toFlowable() |
| 131 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 132 |
|
| 133 |
@Override |
| 134 |
public void onStart() { |
| 135 |
request(3); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void onComplete() { |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public void onError(Throwable e) { |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public void onNext(Integer t) { |
| 150 |
} |
| 151 |
}); |
| 152 |
// FIXME single now triggers fast-path |
| 153 |
assertEquals(Arrays.asList(Long.MAX_VALUE), requests); |
| 154 |
} |
| 155 |
|
| 156 |
@Test |
| 157 |
public void singleRequestsExactlyWhatItNeedsIf1RequestedFlowable() { |
| 158 |
final List<Long> requests = new ArrayList<>(); |
| 159 |
Flowable.just(1) |
| 160 |
// |
| 161 |
.doOnRequest(new LongConsumer() { |
| 162 |
@Override |
| 163 |
public void accept(long n) { |
| 164 |
requests.add(n); |
| 165 |
} |
| 166 |
}) |
| 167 |
// |
| 168 |
.singleElement() |
| 169 |
// |
| 170 |
.toFlowable() |
| 171 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 172 |
|
| 173 |
@Override |
| 174 |
public void onStart() { |
| 175 |
request(1); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void onComplete() { |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void onError(Throwable e) { |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void onNext(Integer t) { |
| 190 |
} |
| 191 |
}); |
| 192 |
// FIXME single now triggers fast-path |
| 193 |
assertEquals(Arrays.asList(Long.MAX_VALUE), requests); |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void singleWithPredicateFlowable() { |
| 198 |
Flowable<Integer> flowable = Flowable.just(1, 2) |
| 199 |
.filter( |
| 200 |
new Predicate<Integer>() { |
| 201 |
|
| 202 |
@Override |
| 203 |
public boolean test(Integer t1) { |
| 204 |
return t1 % 2 == 0; |
| 205 |
} |
| 206 |
}) |
| 207 |
.singleElement().toFlowable(); |
| 208 |
|
| 209 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 210 |
flowable.subscribe(subscriber); |
| 211 |
|
| 212 |
InOrder inOrder = inOrder(subscriber); |
| 213 |
inOrder.verify(subscriber, times(1)).onNext(2); |
| 214 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 215 |
inOrder.verifyNoMoreInteractions(); |
| 216 |
} |
| 217 |
|
| 218 |
@Test |
| 219 |
public void singleWithPredicateAndTooManyElementsFlowable() { |
| 220 |
Flowable<Integer> flowable = Flowable.just(1, 2, 3, 4) |
| 221 |
.filter( |
| 222 |
new Predicate<Integer>() { |
| 223 |
|
| 224 |
@Override |
| 225 |
public boolean test(Integer t1) { |
| 226 |
return t1 % 2 == 0; |
| 227 |
} |
| 228 |
}) |
| 229 |
.singleElement().toFlowable(); |
| 230 |
|
| 231 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 232 |
flowable.subscribe(subscriber); |
| 233 |
|
| 234 |
InOrder inOrder = inOrder(subscriber); |
| 235 |
inOrder.verify(subscriber, times(1)).onError( |
| 236 |
isA(IllegalArgumentException.class)); |
| 237 |
inOrder.verifyNoMoreInteractions(); |
| 238 |
} |
| 239 |
|
| 240 |
@Test |
| 241 |
public void singleWithPredicateAndEmptyFlowable() { |
| 242 |
Flowable<Integer> flowable = Flowable.just(1) |
| 243 |
.filter( |
| 244 |
new Predicate<Integer>() { |
| 245 |
|
| 246 |
@Override |
| 247 |
public boolean test(Integer t1) { |
| 248 |
return t1 % 2 == 0; |
| 249 |
} |
| 250 |
}) |
| 251 |
.singleElement().toFlowable(); |
| 252 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 253 |
flowable.subscribe(subscriber); |
| 254 |
|
| 255 |
InOrder inOrder = inOrder(subscriber); |
| 256 |
inOrder.verify(subscriber).onComplete(); |
| 257 |
inOrder.verify(subscriber, never()).onError(any(Throwable.class)); |
| 258 |
inOrder.verifyNoMoreInteractions(); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void singleOrDefaultFlowable() { |
| 263 |
Flowable<Integer> flowable = Flowable.just(1).single(2).toFlowable(); |
| 264 |
|
| 265 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 266 |
flowable.subscribe(subscriber); |
| 267 |
|
| 268 |
InOrder inOrder = inOrder(subscriber); |
| 269 |
inOrder.verify(subscriber, times(1)).onNext(1); |
| 270 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 271 |
inOrder.verifyNoMoreInteractions(); |
| 272 |
} |
| 273 |
|
| 274 |
@Test |
| 275 |
public void singleOrDefaultWithTooManyElementsFlowable() { |
| 276 |
Flowable<Integer> flowable = Flowable.just(1, 2).single(3).toFlowable(); |
| 277 |
|
| 278 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 279 |
flowable.subscribe(subscriber); |
| 280 |
|
| 281 |
InOrder inOrder = inOrder(subscriber); |
| 282 |
inOrder.verify(subscriber, times(1)).onError( |
| 283 |
isA(IllegalArgumentException.class)); |
| 284 |
inOrder.verifyNoMoreInteractions(); |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void singleOrDefaultWithEmptyFlowable() { |
| 289 |
Flowable<Integer> flowable = Flowable.<Integer> empty() |
| 290 |
.single(1).toFlowable(); |
| 291 |
|
| 292 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 293 |
flowable.subscribe(subscriber); |
| 294 |
|
| 295 |
InOrder inOrder = inOrder(subscriber); |
| 296 |
inOrder.verify(subscriber, times(1)).onNext(1); |
| 297 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 298 |
inOrder.verifyNoMoreInteractions(); |
| 299 |
} |
| 300 |
|
| 301 |
@Test |
| 302 |
public void singleOrDefaultWithPredicateFlowable() { |
| 303 |
Flowable<Integer> flowable = Flowable.just(1, 2) |
| 304 |
.filter(new Predicate<Integer>() { |
| 305 |
@Override |
| 306 |
public boolean test(Integer t1) { |
| 307 |
return t1 % 2 == 0; |
| 308 |
} |
| 309 |
}) |
| 310 |
.single(4).toFlowable(); |
| 311 |
|
| 312 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 313 |
flowable.subscribe(subscriber); |
| 314 |
|
| 315 |
InOrder inOrder = inOrder(subscriber); |
| 316 |
inOrder.verify(subscriber, times(1)).onNext(2); |
| 317 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 318 |
inOrder.verifyNoMoreInteractions(); |
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void singleOrDefaultWithPredicateAndTooManyElementsFlowable() { |
| 323 |
Flowable<Integer> flowable = Flowable.just(1, 2, 3, 4) |
| 324 |
.filter(new Predicate<Integer>() { |
| 325 |
@Override |
| 326 |
public boolean test(Integer t1) { |
| 327 |
return t1 % 2 == 0; |
| 328 |
} |
| 329 |
}) |
| 330 |
.single(6).toFlowable(); |
| 331 |
|
| 332 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 333 |
flowable.subscribe(subscriber); |
| 334 |
|
| 335 |
InOrder inOrder = inOrder(subscriber); |
| 336 |
inOrder.verify(subscriber, times(1)).onError( |
| 337 |
isA(IllegalArgumentException.class)); |
| 338 |
inOrder.verifyNoMoreInteractions(); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void singleOrDefaultWithPredicateAndEmptyFlowable() { |
| 343 |
Flowable<Integer> flowable = Flowable.just(1) |
| 344 |
.filter(new Predicate<Integer>() { |
| 345 |
@Override |
| 346 |
public boolean test(Integer t1) { |
| 347 |
return t1 % 2 == 0; |
| 348 |
} |
| 349 |
}) |
| 350 |
.single(2).toFlowable(); |
| 351 |
|
| 352 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 353 |
flowable.subscribe(subscriber); |
| 354 |
|
| 355 |
InOrder inOrder = inOrder(subscriber); |
| 356 |
inOrder.verify(subscriber, times(1)).onNext(2); |
| 357 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 358 |
inOrder.verifyNoMoreInteractions(); |
| 359 |
} |
| 360 |
|
| 361 |
@Test |
| 362 |
public void singleWithBackpressureFlowable() { |
| 363 |
Flowable<Integer> flowable = Flowable.just(1, 2).singleElement().toFlowable(); |
| 364 |
|
| 365 |
Subscriber<Integer> subscriber = spy(new DefaultSubscriber<Integer>() { |
| 366 |
|
| 367 |
@Override |
| 368 |
public void onStart() { |
| 369 |
request(1); |
| 370 |
} |
| 371 |
|
| 372 |
@Override |
| 373 |
public void onComplete() { |
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
@Override |
| 378 |
public void onError(Throwable e) { |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
public void onNext(Integer integer) { |
| 384 |
request(1); |
| 385 |
} |
| 386 |
}); |
| 387 |
flowable.subscribe(subscriber); |
| 388 |
|
| 389 |
InOrder inOrder = inOrder(subscriber); |
| 390 |
inOrder.verify(subscriber, times(1)).onError(isA(IllegalArgumentException.class)); |
| 391 |
inOrder.verifyNoMoreInteractions(); |
| 392 |
} |
| 393 |
|
| 394 |
@Test |
| 395 |
public void single() { |
| 396 |
Maybe<Integer> maybe = Flowable.just(1).singleElement(); |
| 397 |
|
| 398 |
MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver(); |
| 399 |
maybe.subscribe(observer); |
| 400 |
|
| 401 |
InOrder inOrder = inOrder(observer); |
| 402 |
inOrder.verify(observer, times(1)).onSuccess(1); |
| 403 |
inOrder.verifyNoMoreInteractions(); |
| 404 |
} |
| 405 |
|
| 406 |
@Test |
| 407 |
public void singleWithTooManyElements() { |
| 408 |
Maybe<Integer> maybe = Flowable.just(1, 2).singleElement(); |
| 409 |
|
| 410 |
MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver(); |
| 411 |
maybe.subscribe(observer); |
| 412 |
|
| 413 |
InOrder inOrder = inOrder(observer); |
| 414 |
inOrder.verify(observer, times(1)).onError( |
| 415 |
isA(IllegalArgumentException.class)); |
| 416 |
inOrder.verifyNoMoreInteractions(); |
| 417 |
} |
| 418 |
|
| 419 |
@Test |
| 420 |
public void singleWithEmpty() { |
| 421 |
Maybe<Integer> maybe = Flowable.<Integer> empty().singleElement(); |
| 422 |
|
| 423 |
MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver(); |
| 424 |
maybe.subscribe(observer); |
| 425 |
|
| 426 |
InOrder inOrder = inOrder(observer); |
| 427 |
inOrder.verify(observer).onComplete(); |
| 428 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 429 |
inOrder.verifyNoMoreInteractions(); |
| 430 |
} |
| 431 |
|
| 432 |
@Test |
| 433 |
public void singleDoesNotRequestMoreThanItNeedsToEmitItem() { |
| 434 |
final AtomicLong request = new AtomicLong(); |
| 435 |
Flowable.just(1).doOnRequest(new LongConsumer() { |
| 436 |
@Override |
| 437 |
public void accept(long n) { |
| 438 |
request.addAndGet(n); |
| 439 |
} |
| 440 |
}).blockingSingle(); |
| 441 |
// FIXME single now triggers fast-path |
| 442 |
assertEquals(Long.MAX_VALUE, request.get()); |
| 443 |
} |
| 444 |
|
| 445 |
@Test |
| 446 |
public void singleDoesNotRequestMoreThanItNeedsToEmitErrorFromEmpty() { |
| 447 |
final AtomicLong request = new AtomicLong(); |
| 448 |
try { |
| 449 |
Flowable.empty().doOnRequest(new LongConsumer() { |
| 450 |
@Override |
| 451 |
public void accept(long n) { |
| 452 |
request.addAndGet(n); |
| 453 |
} |
| 454 |
}).blockingSingle(); |
| 455 |
} catch (NoSuchElementException e) { |
| 456 |
// FIXME single now triggers fast-path |
| 457 |
assertEquals(Long.MAX_VALUE, request.get()); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
@Test |
| 462 |
public void singleDoesNotRequestMoreThanItNeedsToEmitErrorFromMoreThanOne() { |
| 463 |
final AtomicLong request = new AtomicLong(); |
| 464 |
try { |
| 465 |
Flowable.just(1, 2).doOnRequest(new LongConsumer() { |
| 466 |
@Override |
| 467 |
public void accept(long n) { |
| 468 |
request.addAndGet(n); |
| 469 |
} |
| 470 |
}).blockingSingle(); |
| 471 |
} catch (IllegalArgumentException e) { |
| 472 |
// FIXME single now triggers fast-path |
| 473 |
assertEquals(Long.MAX_VALUE, request.get()); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
@Test |
| 478 |
public void singleWithPredicate() { |
| 479 |
Maybe<Integer> maybe = Flowable.just(1, 2) |
| 480 |
.filter( |
| 481 |
new Predicate<Integer>() { |
| 482 |
|
| 483 |
@Override |
| 484 |
public boolean test(Integer t1) { |
| 485 |
return t1 % 2 == 0; |
| 486 |
} |
| 487 |
}) |
| 488 |
.singleElement(); |
| 489 |
|
| 490 |
MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver(); |
| 491 |
maybe.subscribe(observer); |
| 492 |
|
| 493 |
InOrder inOrder = inOrder(observer); |
| 494 |
inOrder.verify(observer, times(1)).onSuccess(2); |
| 495 |
inOrder.verifyNoMoreInteractions(); |
| 496 |
} |
| 497 |
|
| 498 |
@Test |
| 499 |
public void singleWithPredicateAndTooManyElements() { |
| 500 |
Maybe<Integer> maybe = Flowable.just(1, 2, 3, 4) |
| 501 |
.filter( |
| 502 |
new Predicate<Integer>() { |
| 503 |
|
| 504 |
@Override |
| 505 |
public boolean test(Integer t1) { |
| 506 |
return t1 % 2 == 0; |
| 507 |
} |
| 508 |
}) |
| 509 |
.singleElement(); |
| 510 |
|
| 511 |
MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver(); |
| 512 |
maybe.subscribe(observer); |
| 513 |
|
| 514 |
InOrder inOrder = inOrder(observer); |
| 515 |
inOrder.verify(observer, times(1)).onError( |
| 516 |
isA(IllegalArgumentException.class)); |
| 517 |
inOrder.verifyNoMoreInteractions(); |
| 518 |
} |
| 519 |
|
| 520 |
@Test |
| 521 |
public void singleWithPredicateAndEmpty() { |
| 522 |
Maybe<Integer> maybe = Flowable.just(1) |
| 523 |
.filter( |
| 524 |
new Predicate<Integer>() { |
| 525 |
|
| 526 |
@Override |
| 527 |
public boolean test(Integer t1) { |
| 528 |
return t1 % 2 == 0; |
| 529 |
} |
| 530 |
}) |
| 531 |
.singleElement(); |
| 532 |
|
| 533 |
MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver(); |
| 534 |
maybe.subscribe(observer); |
| 535 |
|
| 536 |
InOrder inOrder = inOrder(observer); |
| 537 |
inOrder.verify(observer).onComplete(); |
| 538 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 539 |
inOrder.verifyNoMoreInteractions(); |
| 540 |
} |
| 541 |
|
| 542 |
@Test |
| 543 |
public void singleOrDefault() { |
| 544 |
Single<Integer> single = Flowable.just(1).single(2); |
| 545 |
|
| 546 |
SingleObserver<Integer> observer = TestHelper.mockSingleObserver(); |
| 547 |
single.subscribe(observer); |
| 548 |
|
| 549 |
InOrder inOrder = inOrder(observer); |
| 550 |
inOrder.verify(observer, times(1)).onSuccess(1); |
| 551 |
inOrder.verifyNoMoreInteractions(); |
| 552 |
} |
| 553 |
|
| 554 |
@Test |
| 555 |
public void singleOrDefaultWithTooManyElements() { |
| 556 |
Single<Integer> single = Flowable.just(1, 2).single(3); |
| 557 |
|
| 558 |
SingleObserver<Integer> observer = TestHelper.mockSingleObserver(); |
| 559 |
single.subscribe(observer); |
| 560 |
|
| 561 |
InOrder inOrder = inOrder(observer); |
| 562 |
inOrder.verify(observer, times(1)).onError( |
| 563 |
isA(IllegalArgumentException.class)); |
| 564 |
inOrder.verifyNoMoreInteractions(); |
| 565 |
} |
| 566 |
|
| 567 |
@Test |
| 568 |
public void singleOrDefaultWithEmpty() { |
| 569 |
Single<Integer> single = Flowable.<Integer> empty() |
| 570 |
.single(1); |
| 571 |
|
| 572 |
SingleObserver<Integer> observer = TestHelper.mockSingleObserver(); |
| 573 |
single.subscribe(observer); |
| 574 |
|
| 575 |
InOrder inOrder = inOrder(observer); |
| 576 |
inOrder.verify(observer, times(1)).onSuccess(1); |
| 577 |
inOrder.verifyNoMoreInteractions(); |
| 578 |
} |
| 579 |
|
| 580 |
@Test |
| 581 |
public void singleOrDefaultWithPredicate() { |
| 582 |
Single<Integer> single = Flowable.just(1, 2) |
| 583 |
.filter(new Predicate<Integer>() { |
| 584 |
@Override |
| 585 |
public boolean test(Integer t1) { |
| 586 |
return t1 % 2 == 0; |
| 587 |
} |
| 588 |
}) |
| 589 |
.single(4); |
| 590 |
|
| 591 |
SingleObserver<Integer> observer = TestHelper.mockSingleObserver(); |
| 592 |
single.subscribe(observer); |
| 593 |
|
| 594 |
InOrder inOrder = inOrder(observer); |
| 595 |
inOrder.verify(observer, times(1)).onSuccess(2); |
| 596 |
inOrder.verifyNoMoreInteractions(); |
| 597 |
} |
| 598 |
|
| 599 |
@Test |
| 600 |
public void singleOrDefaultWithPredicateAndTooManyElements() { |
| 601 |
Single<Integer> single = Flowable.just(1, 2, 3, 4) |
| 602 |
.filter(new Predicate<Integer>() { |
| 603 |
@Override |
| 604 |
public boolean test(Integer t1) { |
| 605 |
return t1 % 2 == 0; |
| 606 |
} |
| 607 |
}) |
| 608 |
.single(6); |
| 609 |
|
| 610 |
SingleObserver<Integer> observer = TestHelper.mockSingleObserver(); |
| 611 |
single.subscribe(observer); |
| 612 |
|
| 613 |
InOrder inOrder = inOrder(observer); |
| 614 |
inOrder.verify(observer, times(1)).onError( |
| 615 |
isA(IllegalArgumentException.class)); |
| 616 |
inOrder.verifyNoMoreInteractions(); |
| 617 |
} |
| 618 |
|
| 619 |
@Test |
| 620 |
public void singleOrDefaultWithPredicateAndEmpty() { |
| 621 |
Single<Integer> single = Flowable.just(1) |
| 622 |
.filter(new Predicate<Integer>() { |
| 623 |
@Override |
| 624 |
public boolean test(Integer t1) { |
| 625 |
return t1 % 2 == 0; |
| 626 |
} |
| 627 |
}) |
| 628 |
.single(2); |
| 629 |
|
| 630 |
SingleObserver<Integer> observer = TestHelper.mockSingleObserver(); |
| 631 |
single.subscribe(observer); |
| 632 |
|
| 633 |
InOrder inOrder = inOrder(observer); |
| 634 |
inOrder.verify(observer, times(1)).onSuccess(2); |
| 635 |
inOrder.verifyNoMoreInteractions(); |
| 636 |
} |
| 637 |
|
| 638 |
@Test |
| 639 |
public void issue1527() throws InterruptedException { |
| 640 |
//https://github.com/ReactiveX/RxJava/pull/1527 |
| 641 |
Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6); |
| 642 |
Maybe<Integer> reduced = source.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 643 |
@Override |
| 644 |
public Integer apply(Integer i1, Integer i2) { |
| 645 |
return i1 + i2; |
| 646 |
} |
| 647 |
}); |
| 648 |
|
| 649 |
Integer r = reduced.blockingGet(); |
| 650 |
assertEquals(21, r.intValue()); |
| 651 |
} |
| 652 |
|
| 653 |
@Test |
| 654 |
public void singleOrErrorNoElement() { |
| 655 |
Flowable.empty() |
| 656 |
.singleOrError() |
| 657 |
.test() |
| 658 |
.assertNoValues() |
| 659 |
.assertError(NoSuchElementException.class); |
| 660 |
} |
| 661 |
|
| 662 |
@Test |
| 663 |
public void singleOrErrorOneElement() { |
| 664 |
Flowable.just(1) |
| 665 |
.singleOrError() |
| 666 |
.test() |
| 667 |
.assertNoErrors() |
| 668 |
.assertValue(1); |
| 669 |
} |
| 670 |
|
| 671 |
@Test |
| 672 |
public void singleOrErrorMultipleElements() { |
| 673 |
Flowable.just(1, 2, 3) |
| 674 |
.singleOrError() |
| 675 |
.test() |
| 676 |
.assertNoValues() |
| 677 |
.assertError(IllegalArgumentException.class); |
| 678 |
} |
| 679 |
|
| 680 |
@Test |
| 681 |
public void singleOrErrorError() { |
| 682 |
Flowable.error(new RuntimeException("error")) |
| 683 |
.singleOrError() |
| 684 |
.to(TestHelper.testConsumer()) |
| 685 |
.assertNoValues() |
| 686 |
.assertErrorMessage("error") |
| 687 |
.assertError(RuntimeException.class); |
| 688 |
} |
| 689 |
|
| 690 |
@Test |
| 691 |
public void issue1527Flowable() throws InterruptedException { |
| 692 |
//https://github.com/ReactiveX/RxJava/pull/1527 |
| 693 |
Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6); |
| 694 |
Flowable<Integer> reduced = source.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 695 |
@Override |
| 696 |
public Integer apply(Integer i1, Integer i2) { |
| 697 |
return i1 + i2; |
| 698 |
} |
| 699 |
}).toFlowable(); |
| 700 |
|
| 701 |
Integer r = reduced.blockingFirst(); |
| 702 |
assertEquals(21, r.intValue()); |
| 703 |
} |
| 704 |
|
| 705 |
@Test |
| 706 |
public void singleElementOperatorDoNotSwallowExceptionWhenDone() { |
| 707 |
final Throwable exception = new RuntimeException("some error"); |
| 708 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 709 |
|
| 710 |
try { |
| 711 |
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { |
| 712 |
@Override public void accept(final Throwable throwable) throws Exception { |
| 713 |
error.set(throwable); |
| 714 |
} |
| 715 |
}); |
| 716 |
|
| 717 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 718 |
@Override public void subscribe(final Subscriber<? super Integer> subscriber) { |
| 719 |
subscriber.onComplete(); |
| 720 |
subscriber.onError(exception); |
| 721 |
} |
| 722 |
}).singleElement().test().assertComplete(); |
| 723 |
|
| 724 |
assertSame(exception, error.get().getCause()); |
| 725 |
} finally { |
| 726 |
RxJavaPlugins.reset(); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
@Test |
| 731 |
public void badSource() { |
| 732 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() { |
| 733 |
@Override |
| 734 |
public Object apply(Flowable<Object> f) throws Exception { |
| 735 |
return f.singleOrError(); |
| 736 |
} |
| 737 |
}, false, 1, 1, 1); |
| 738 |
|
| 739 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() { |
| 740 |
@Override |
| 741 |
public Object apply(Flowable<Object> f) throws Exception { |
| 742 |
return f.singleElement(); |
| 743 |
} |
| 744 |
}, false, 1, 1, 1); |
| 745 |
|
| 746 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() { |
| 747 |
@Override |
| 748 |
public Object apply(Flowable<Object> f) throws Exception { |
| 749 |
return f.singleOrError().toFlowable(); |
| 750 |
} |
| 751 |
}, false, 1, 1, 1); |
| 752 |
} |
| 753 |
|
| 754 |
@Test |
| 755 |
public void doubleOnSubscribe() { |
| 756 |
TestHelper.checkDoubleOnSubscribeFlowableToSingle(new Function<Flowable<Object>, SingleSource<Object>>() { |
| 757 |
@Override |
| 758 |
public SingleSource<Object> apply(Flowable<Object> f) throws Exception { |
| 759 |
return f.singleOrError(); |
| 760 |
} |
| 761 |
}); |
| 762 |
|
| 763 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 764 |
@Override |
| 765 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 766 |
return f.singleOrError().toFlowable(); |
| 767 |
} |
| 768 |
}); |
| 769 |
|
| 770 |
TestHelper.checkDoubleOnSubscribeFlowableToMaybe(new Function<Flowable<Object>, MaybeSource<Object>>() { |
| 771 |
@Override |
| 772 |
public MaybeSource<Object> apply(Flowable<Object> f) throws Exception { |
| 773 |
return f.singleElement(); |
| 774 |
} |
| 775 |
}); |
| 776 |
|
| 777 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 778 |
@Override |
| 779 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 780 |
return f.singleElement().toFlowable(); |
| 781 |
} |
| 782 |
}); |
| 783 |
} |
| 784 |
|
| 785 |
@Test |
| 786 |
public void cancelAsFlowable() { |
| 787 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 788 |
|
| 789 |
TestSubscriber<Integer> ts = pp.singleOrError().toFlowable().test(); |
| 790 |
|
| 791 |
assertTrue(pp.hasSubscribers()); |
| 792 |
|
| 793 |
ts.assertEmpty(); |
| 794 |
|
| 795 |
ts.cancel(); |
| 796 |
|
| 797 |
assertFalse(pp.hasSubscribers()); |
| 798 |
} |
| 799 |
|
| 800 |
@Test |
| 801 |
public void singleOrError() { |
| 802 |
Flowable.empty() |
| 803 |
.singleOrError() |
| 804 |
.toFlowable() |
| 805 |
.test() |
| 806 |
.assertFailure(NoSuchElementException.class); |
| 807 |
} |
| 808 |
|
| 809 |
@Test |
| 810 |
public void dispose() { |
| 811 |
TestHelper.checkDisposed(PublishProcessor.create().single(1)); |
| 812 |
} |
| 813 |
} |