Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDoAfterTerminateTest.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 import static org.mockito.Mockito.*;
18
19 import java.util.List;
20
21 import org.junit.*;
22 import org.mockito.Mockito;
23 import org.reactivestreams.Subscriber;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.functions.Action;
27 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.subscribers.TestSubscriber;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class FlowableDoAfterTerminateTest extends RxJavaTest {
33
34 private Action aAction0;
35 private Subscriber<String> subscriber;
36
37 @Before
38 public void before() {
39 aAction0 = Mockito.mock(Action.class);
40 subscriber = TestHelper.mockSubscriber();
41 }
42
43 private void checkActionCalled(Flowable<String> input) {
44 input.doAfterTerminate(aAction0).subscribe(subscriber);
45 try {
46 verify(aAction0, times(1)).run();
47 } catch (Throwable ex) {
48 throw ExceptionHelper.wrapOrThrow(ex);
49 }
50 }
51
52 @Test
53 public void doAfterTerminateCalledOnComplete() {
54 checkActionCalled(Flowable.fromArray("1", "2", "3"));
55 }
56
57 @Test
58 public void doAfterTerminateCalledOnError() {
59 checkActionCalled(Flowable.<String> error(new RuntimeException("expected")));
60 }
61
62 @Test
63 public void nullActionShouldBeCheckedInConstructor() {
64 try {
65 Flowable.empty().doAfterTerminate(null);
66 fail("Should have thrown NullPointerException");
67 } catch (NullPointerException expected) {
68 assertEquals("onAfterTerminate is null", expected.getMessage());
69 }
70 }
71
72 @Test
73 public void nullFinallyActionShouldBeCheckedASAP() {
74 try {
75 Flowable
76 .just("value")
77 .doAfterTerminate(null);
78
79 fail();
80 } catch (NullPointerException expected) {
81
82 }
83 }
84
85 @Test
86 public void ifFinallyActionThrowsExceptionShouldNotBeSwallowedAndActionShouldBeCalledOnce() throws Throwable {
87 List<Throwable> errors = TestHelper.trackPluginErrors();
88 try {
89 Action finallyAction = Mockito.mock(Action.class);
90 doThrow(new IllegalStateException()).when(finallyAction).run();
91
92 TestSubscriber<String> testSubscriber = new TestSubscriber<>();
93
94 Flowable
95 .just("value")
96 .doAfterTerminate(finallyAction)
97 .subscribe(testSubscriber);
98
99 testSubscriber.assertValue("value");
100
101 verify(finallyAction).run();
102
103 TestHelper.assertError(errors, 0, IllegalStateException.class);
104 // Actual result:
105 // Not only IllegalStateException was swallowed
106 // But finallyAction was called twice!
107 } finally {
108 RxJavaPlugins.reset();
109 }
110 }
111 }