| 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.mockito.ArgumentMatchers.any; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
import org.reactivestreams.Subscriber; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.Supplier; |
| 25 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 26 |
|
| 27 |
@SuppressWarnings("unchecked") |
| 28 |
public class FlowableDeferTest extends RxJavaTest { |
| 29 |
|
| 30 |
@Test |
| 31 |
public void defer() throws Throwable { |
| 32 |
|
| 33 |
Supplier<Flowable<String>> factory = mock(Supplier.class); |
| 34 |
|
| 35 |
Flowable<String> firstObservable = Flowable.just("one", "two"); |
| 36 |
Flowable<String> secondObservable = Flowable.just("three", "four"); |
| 37 |
when(factory.get()).thenReturn(firstObservable, secondObservable); |
| 38 |
|
| 39 |
Flowable<String> deferred = Flowable.defer(factory); |
| 40 |
|
| 41 |
verifyNoInteractions(factory); |
| 42 |
|
| 43 |
Subscriber<String> firstSubscriber = TestHelper.mockSubscriber(); |
| 44 |
deferred.subscribe(firstSubscriber); |
| 45 |
|
| 46 |
verify(factory, times(1)).get(); |
| 47 |
verify(firstSubscriber, times(1)).onNext("one"); |
| 48 |
verify(firstSubscriber, times(1)).onNext("two"); |
| 49 |
verify(firstSubscriber, times(0)).onNext("three"); |
| 50 |
verify(firstSubscriber, times(0)).onNext("four"); |
| 51 |
verify(firstSubscriber, times(1)).onComplete(); |
| 52 |
|
| 53 |
Subscriber<String> secondSubscriber = TestHelper.mockSubscriber(); |
| 54 |
deferred.subscribe(secondSubscriber); |
| 55 |
|
| 56 |
verify(factory, times(2)).get(); |
| 57 |
verify(secondSubscriber, times(0)).onNext("one"); |
| 58 |
verify(secondSubscriber, times(0)).onNext("two"); |
| 59 |
verify(secondSubscriber, times(1)).onNext("three"); |
| 60 |
verify(secondSubscriber, times(1)).onNext("four"); |
| 61 |
verify(secondSubscriber, times(1)).onComplete(); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void deferFunctionThrows() throws Throwable { |
| 67 |
Supplier<Flowable<String>> factory = mock(Supplier.class); |
| 68 |
|
| 69 |
when(factory.get()).thenThrow(new TestException()); |
| 70 |
|
| 71 |
Flowable<String> result = Flowable.defer(factory); |
| 72 |
|
| 73 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 74 |
|
| 75 |
result.subscribe(subscriber); |
| 76 |
|
| 77 |
verify(subscriber).onError(any(TestException.class)); |
| 78 |
verify(subscriber, never()).onNext(any(String.class)); |
| 79 |
verify(subscriber, never()).onComplete(); |
| 80 |
} |
| 81 |
} |