| 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.assertFalse; |
| 17 |
|
| 18 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.functions.Action; |
| 24 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 25 |
import io.reactivex.rxjava3.testsupport.TestSubscriberEx; |
| 26 |
|
| 27 |
public class FlowableToCompletableTest extends RxJavaTest { |
| 28 |
|
| 29 |
@Test |
| 30 |
public void justSingleItemObservable() { |
| 31 |
TestSubscriber<String> subscriber = TestSubscriber.create(); |
| 32 |
Completable cmp = Flowable.just("Hello World!").ignoreElements(); |
| 33 |
cmp.<String>toFlowable().subscribe(subscriber); |
| 34 |
|
| 35 |
subscriber.assertNoValues(); |
| 36 |
subscriber.assertComplete(); |
| 37 |
subscriber.assertNoErrors(); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void errorObservable() { |
| 42 |
TestSubscriber<String> subscriber = TestSubscriber.create(); |
| 43 |
IllegalArgumentException error = new IllegalArgumentException("Error"); |
| 44 |
Completable cmp = Flowable.<String>error(error).ignoreElements(); |
| 45 |
cmp.<String>toFlowable().subscribe(subscriber); |
| 46 |
|
| 47 |
subscriber.assertError(error); |
| 48 |
subscriber.assertNoValues(); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void justTwoEmissionsObservableThrowsError() { |
| 53 |
TestSubscriber<String> subscriber = TestSubscriber.create(); |
| 54 |
Completable cmp = Flowable.just("First", "Second").ignoreElements(); |
| 55 |
cmp.<String>toFlowable().subscribe(subscriber); |
| 56 |
|
| 57 |
subscriber.assertNoErrors(); |
| 58 |
subscriber.assertNoValues(); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void emptyObservable() { |
| 63 |
TestSubscriber<String> subscriber = TestSubscriber.create(); |
| 64 |
Completable cmp = Flowable.<String>empty().ignoreElements(); |
| 65 |
cmp.<String>toFlowable().subscribe(subscriber); |
| 66 |
|
| 67 |
subscriber.assertNoErrors(); |
| 68 |
subscriber.assertNoValues(); |
| 69 |
subscriber.assertComplete(); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void neverObservable() { |
| 74 |
TestSubscriberEx<String> subscriber = new TestSubscriberEx<>(); |
| 75 |
Completable cmp = Flowable.<String>never().ignoreElements(); |
| 76 |
cmp.<String>toFlowable().subscribe(subscriber); |
| 77 |
|
| 78 |
subscriber.assertNotTerminated(); |
| 79 |
subscriber.assertNoValues(); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void shouldUseUnsafeSubscribeInternallyNotSubscribe() { |
| 84 |
TestSubscriber<String> subscriber = TestSubscriber.create(); |
| 85 |
final AtomicBoolean unsubscribed = new AtomicBoolean(false); |
| 86 |
Completable cmp = Flowable.just("Hello World!").doOnCancel(new Action() { |
| 87 |
|
| 88 |
@Override |
| 89 |
public void run() { |
| 90 |
unsubscribed.set(true); |
| 91 |
}}).ignoreElements(); |
| 92 |
|
| 93 |
cmp.<String>toFlowable().subscribe(subscriber); |
| 94 |
|
| 95 |
subscriber.assertComplete(); |
| 96 |
|
| 97 |
assertFalse(unsubscribed.get()); |
| 98 |
} |
| 99 |
} |