Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDoOnLifecycleTest.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.flowable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21 import org.reactivestreams.*;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class FlowableDoOnLifecycleTest extends RxJavaTest {
32
33 @Test
34 public void onSubscribeCrashed() {
35 Flowable.just(1)
36 .doOnLifecycle(new Consumer<Subscription>() {
37 @Override
38 public void accept(Subscription s) throws Exception {
39 throw new TestException();
40 }
41 }, Functions.EMPTY_LONG_CONSUMER, Functions.EMPTY_ACTION)
42 .test()
43 .assertFailure(TestException.class);
44 }
45
46 @Test
47 public void doubleOnSubscribe() {
48 final int[] calls = { 0, 0 };
49
50 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() {
51 @Override
52 public Publisher<Object> apply(Flowable<Object> f) throws Exception {
53 return f
54 .doOnLifecycle(new Consumer<Subscription>() {
55 @Override
56 public void accept(Subscription s) throws Exception {
57 calls[0]++;
58 }
59 }, Functions.EMPTY_LONG_CONSUMER, new Action() {
60 @Override
61 public void run() throws Exception {
62 calls[1]++;
63 }
64 });
65 }
66 });
67
68 assertEquals(2, calls[0]);
69 assertEquals(0, calls[1]);
70 }
71
72 @Test
73 public void dispose() {
74 final int[] calls = { 0, 0 };
75
76 TestHelper.checkDisposed(Flowable.just(1)
77 .doOnLifecycle(new Consumer<Subscription>() {
78 @Override
79 public void accept(Subscription s) throws Exception {
80 calls[0]++;
81 }
82 }, Functions.EMPTY_LONG_CONSUMER, new Action() {
83 @Override
84 public void run() throws Exception {
85 calls[1]++;
86 }
87 })
88 );
89
90 assertEquals(1, calls[0]);
91 assertEquals(1, calls[1]);
92 }
93
94 @Test
95 public void requestCrashed() {
96 List<Throwable> errors = TestHelper.trackPluginErrors();
97 try {
98 Flowable.just(1)
99 .doOnLifecycle(Functions.emptyConsumer(),
100 new LongConsumer() {
101 @Override
102 public void accept(long v) throws Exception {
103 throw new TestException();
104 }
105 },
106 Functions.EMPTY_ACTION)
107 .test()
108 .assertResult(1);
109
110 TestHelper.assertUndeliverable(errors, 0, TestException.class);
111 } finally {
112 RxJavaPlugins.reset();
113 }
114 }
115
116 @Test
117 public void cancelCrashed() {
118 List<Throwable> errors = TestHelper.trackPluginErrors();
119 try {
120 Flowable.just(1)
121 .doOnLifecycle(Functions.emptyConsumer(),
122 Functions.EMPTY_LONG_CONSUMER,
123 new Action() {
124 @Override
125 public void run() throws Exception {
126 throw new TestException();
127 }
128 })
129 .take(1)
130 .test()
131 .assertResult(1);
132
133 TestHelper.assertUndeliverable(errors, 0, TestException.class);
134 } finally {
135 RxJavaPlugins.reset();
136 }
137 }
138
139 @Test
140 public void onSubscribeCrash() {
141 List<Throwable> errors = TestHelper.trackPluginErrors();
142 try {
143 final BooleanSubscription bs = new BooleanSubscription();
144
145 new Flowable<Integer>() {
146 @Override
147 protected void subscribeActual(Subscriber<? super Integer> s) {
148 s.onSubscribe(bs);
149 s.onError(new TestException("Second"));
150 s.onComplete();
151 }
152 }
153 .doOnSubscribe(new Consumer<Subscription>() {
154 @Override
155 public void accept(Subscription s) throws Exception {
156 throw new TestException("First");
157 }
158 })
159 .to(TestHelper.<Integer>testConsumer())
160 .assertFailureAndMessage(TestException.class, "First");
161
162 assertTrue(bs.isCancelled());
163
164 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
165 } finally {
166 RxJavaPlugins.reset();
167 }
168 }
169 }