Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableSafeSubscribeTest.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.completable;
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 CompletableSafeSubscribeTest {
32
33 @Test
34 public void normalError() throws Throwable {
35 TestHelper.withErrorTracking(errors -> {
36 CompletableObserver consumer = mock(CompletableObserver.class);
37
38 Completable.error(new TestException())
39 .safeSubscribe(consumer);
40
41 InOrder order = inOrder(consumer);
42 order.verify(consumer).onSubscribe(any(Disposable.class));
43 order.verify(consumer).onError(any(TestException.class));
44 order.verifyNoMoreInteractions();
45
46 assertTrue("" + errors, errors.isEmpty());
47 });
48 }
49
50 @Test
51 public void normalEmpty() throws Throwable {
52 TestHelper.withErrorTracking(errors -> {
53 CompletableObserver consumer = mock(CompletableObserver.class);
54
55 Completable.complete()
56 .safeSubscribe(consumer);
57
58 InOrder order = inOrder(consumer);
59 order.verify(consumer).onSubscribe(any(Disposable.class));
60 order.verify(consumer).onComplete();
61 order.verifyNoMoreInteractions();
62 });
63 }
64
65 @Test
66 public void onSubscribeCrash() throws Throwable {
67 TestHelper.withErrorTracking(errors -> {
68 CompletableObserver consumer = mock(CompletableObserver.class);
69 doThrow(new TestException()).when(consumer).onSubscribe(any());
70
71 Disposable d = Disposable.empty();
72
73 new Completable() {
74 @Override
75 protected void subscribeActual(@NonNull CompletableObserver observer) {
76 observer.onSubscribe(d);
77 // none of the following should arrive at the consumer
78 observer.onError(new IOException());
79 observer.onComplete();
80 }
81 }
82 .safeSubscribe(consumer);
83
84 InOrder order = inOrder(consumer);
85 order.verify(consumer).onSubscribe(any(Disposable.class));
86 order.verifyNoMoreInteractions();
87
88 assertTrue(d.isDisposed());
89
90 TestHelper.assertUndeliverable(errors, 0, TestException.class);
91 TestHelper.assertUndeliverable(errors, 1, IOException.class);
92 });
93 }
94
95 @Test
96 public void onErrorCrash() throws Throwable {
97 TestHelper.withErrorTracking(errors -> {
98 CompletableObserver consumer = mock(CompletableObserver.class);
99 doThrow(new TestException()).when(consumer).onError(any());
100
101 new Completable() {
102 @Override
103 protected void subscribeActual(@NonNull CompletableObserver observer) {
104 observer.onSubscribe(Disposable.empty());
105 // none of the following should arrive at the consumer
106 observer.onError(new IOException());
107 }
108 }
109 .safeSubscribe(consumer);
110
111 InOrder order = inOrder(consumer);
112 order.verify(consumer).onSubscribe(any(Disposable.class));
113 order.verify(consumer).onError(any(IOException.class));
114 order.verifyNoMoreInteractions();
115
116 TestHelper.assertError(errors, 0, CompositeException.class);
117
118 CompositeException compositeException = (CompositeException)errors.get(0);
119 TestHelper.assertError(compositeException.getExceptions(), 0, IOException.class);
120 TestHelper.assertError(compositeException.getExceptions(), 1, TestException.class);
121 });
122 }
123
124 @Test
125 public void onCompleteCrash() throws Throwable {
126 TestHelper.withErrorTracking(errors -> {
127 CompletableObserver consumer = mock(CompletableObserver.class);
128 doThrow(new TestException()).when(consumer).onComplete();
129
130 new Completable() {
131 @Override
132 protected void subscribeActual(@NonNull CompletableObserver observer) {
133 observer.onSubscribe(Disposable.empty());
134 // none of the following should arrive at the consumer
135 observer.onComplete();
136 }
137 }
138 .safeSubscribe(consumer);
139
140 InOrder order = inOrder(consumer);
141 order.verify(consumer).onSubscribe(any(Disposable.class));
142 order.verify(consumer).onComplete();
143 order.verifyNoMoreInteractions();
144
145 TestHelper.assertUndeliverable(errors, 0, TestException.class);
146 });
147 }
148 }