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