Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleSafeSubscribeTest.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.assertTrue;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.io.IOException;
21
22 import org.junit.Test;
23 import org.mockito.InOrder;
24
25 import io.reactivex.rxjava3.annotations.NonNull;
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.disposables.Disposable;
28 import io.reactivex.rxjava3.exceptions.*;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class SingleSafeSubscribeTest {
32
33 @Test
34 public void normalSuccess() throws Throwable {
35 TestHelper.withErrorTracking(errors -> {
36 @SuppressWarnings("unchecked")
37 SingleObserver<Integer> consumer = mock(SingleObserver.class);
38
39 Single.just(1)
40 .safeSubscribe(consumer);
41
42 InOrder order = inOrder(consumer);
43 order.verify(consumer).onSubscribe(any(Disposable.class));
44 order.verify(consumer).onSuccess(1);
45 order.verifyNoMoreInteractions();
46
47 assertTrue("" + errors, errors.isEmpty());
48 });
49 }
50
51 @Test
52 public void normalError() throws Throwable {
53 TestHelper.withErrorTracking(errors -> {
54 @SuppressWarnings("unchecked")
55 SingleObserver<Integer> consumer = mock(SingleObserver.class);
56
57 Single.<Integer>error(new TestException())
58 .safeSubscribe(consumer);
59
60 InOrder order = inOrder(consumer);
61 order.verify(consumer).onSubscribe(any(Disposable.class));
62 order.verify(consumer).onError(any(TestException.class));
63 order.verifyNoMoreInteractions();
64
65 assertTrue("" + errors, errors.isEmpty());
66 });
67 }
68
69 @Test
70 public void onSubscribeCrash() throws Throwable {
71 TestHelper.withErrorTracking(errors -> {
72 @SuppressWarnings("unchecked")
73 SingleObserver<Integer> consumer = mock(SingleObserver.class);
74 doThrow(new TestException()).when(consumer).onSubscribe(any());
75
76 Disposable d = Disposable.empty();
77
78 new Single<Integer>() {
79 @Override
80 protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
81 observer.onSubscribe(d);
82 // none of the following should arrive at the consumer
83 observer.onSuccess(1);
84 observer.onError(new IOException());
85 }
86 }
87 .safeSubscribe(consumer);
88
89 InOrder order = inOrder(consumer);
90 order.verify(consumer).onSubscribe(any(Disposable.class));
91 order.verifyNoMoreInteractions();
92
93 assertTrue(d.isDisposed());
94
95 TestHelper.assertUndeliverable(errors, 0, TestException.class);
96 TestHelper.assertUndeliverable(errors, 1, IOException.class);
97 });
98 }
99
100 @Test
101 public void onSuccessCrash() throws Throwable {
102 TestHelper.withErrorTracking(errors -> {
103 @SuppressWarnings("unchecked")
104 SingleObserver<Integer> consumer = mock(SingleObserver.class);
105 doThrow(new TestException()).when(consumer).onSuccess(any());
106
107 new Single<Integer>() {
108 @Override
109 protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
110 observer.onSubscribe(Disposable.empty());
111 observer.onSuccess(1);
112 }
113 }
114 .safeSubscribe(consumer);
115
116 InOrder order = inOrder(consumer);
117 order.verify(consumer).onSubscribe(any(Disposable.class));
118 order.verify(consumer).onSuccess(1);
119 order.verifyNoMoreInteractions();
120
121 TestHelper.assertUndeliverable(errors, 0, TestException.class);
122 });
123 }
124
125 @Test
126 public void onErrorCrash() throws Throwable {
127 TestHelper.withErrorTracking(errors -> {
128 @SuppressWarnings("unchecked")
129 SingleObserver<Integer> consumer = mock(SingleObserver.class);
130 doThrow(new TestException()).when(consumer).onError(any());
131
132 new Single<Integer>() {
133 @Override
134 protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
135 observer.onSubscribe(Disposable.empty());
136 // none of the following should arrive at the consumer
137 observer.onError(new IOException());
138 }
139 }
140 .safeSubscribe(consumer);
141
142 InOrder order = inOrder(consumer);
143 order.verify(consumer).onSubscribe(any(Disposable.class));
144 order.verify(consumer).onError(any(IOException.class));
145 order.verifyNoMoreInteractions();
146
147 TestHelper.assertError(errors, 0, CompositeException.class);
148
149 CompositeException compositeException = (CompositeException)errors.get(0);
150 TestHelper.assertError(compositeException.getExceptions(), 0, IOException.class);
151 TestHelper.assertError(compositeException.getExceptions(), 1, TestException.class);
152 });
153 }
154 }