Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleFromPublisherTest.java

raw
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.single;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21 import org.reactivestreams.Subscriber;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
26 import io.reactivex.rxjava3.observers.TestObserver;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.processors.PublishProcessor;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class SingleFromPublisherTest extends RxJavaTest {
32
33 @Test
34 public void just() {
35 Single.fromPublisher(Flowable.just(1))
36 .test()
37 .assertResult(1);
38 }
39
40 @Test
41 public void range() {
42 Single.fromPublisher(Flowable.range(1, 3))
43 .test()
44 .assertFailure(IndexOutOfBoundsException.class);
45 }
46
47 @Test
48 public void empty() {
49 Single.fromPublisher(Flowable.empty())
50 .test()
51 .assertFailure(NoSuchElementException.class);
52 }
53
54 @Test
55 public void error() {
56 Single.fromPublisher(Flowable.error(new TestException()))
57 .test()
58 .assertFailure(TestException.class);
59 }
60
61 @Test
62 public void dispose() {
63 PublishProcessor<Integer> pp = PublishProcessor.create();
64
65 TestObserver<Integer> to = Single.fromPublisher(pp).test();
66
67 assertTrue(pp.hasSubscribers());
68
69 pp.onNext(1);
70
71 to.dispose();
72
73 assertFalse(pp.hasSubscribers());
74 }
75
76 @Test
77 public void isDisposed() {
78 TestHelper.checkDisposed(Single.fromPublisher(Flowable.never()));
79 }
80
81 @Test
82 public void badSource() {
83 List<Throwable> errors = TestHelper.trackPluginErrors();
84
85 try {
86 Single.fromPublisher(new Flowable<Integer>() {
87 @Override
88 protected void subscribeActual(Subscriber<? super Integer> s) {
89 s.onSubscribe(new BooleanSubscription());
90 BooleanSubscription s2 = new BooleanSubscription();
91 s.onSubscribe(s2);
92 assertTrue(s2.isCancelled());
93
94 s.onNext(1);
95 s.onComplete();
96 s.onNext(2);
97 s.onError(new TestException());
98 s.onComplete();
99 }
100 })
101 .test()
102 .assertResult(1);
103
104 TestHelper.assertError(errors, 0, IllegalStateException.class, "Subscription already set!");
105 TestHelper.assertUndeliverable(errors, 1, TestException.class);
106 } finally {
107 RxJavaPlugins.reset();
108 }
109 }
110 }