Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleDoAfterTerminateTest.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.assertEquals;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.internal.functions.Functions;
26 import io.reactivex.rxjava3.observers.TestObserver;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.subjects.PublishSubject;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class SingleDoAfterTerminateTest extends RxJavaTest {
32
33 private final int[] call = { 0 };
34
35 private final Action afterTerminate = new Action() {
36 @Override
37 public void run() throws Exception {
38 call[0]++;
39 }
40 };
41
42 private final TestObserver<Integer> to = new TestObserver<>();
43
44 @Test
45 public void just() {
46 Single.just(1)
47 .doAfterTerminate(afterTerminate)
48 .subscribeWith(to)
49 .assertResult(1);
50
51 assertAfterTerminateCalledOnce();
52 }
53
54 @Test
55 public void error() {
56 Single.<Integer>error(new TestException())
57 .doAfterTerminate(afterTerminate)
58 .subscribeWith(to)
59 .assertFailure(TestException.class);
60
61 assertAfterTerminateCalledOnce();
62 }
63
64 @Test
65 public void justConditional() {
66 Single.just(1)
67 .doAfterTerminate(afterTerminate)
68 .filter(Functions.alwaysTrue())
69 .subscribeWith(to)
70 .assertResult(1);
71
72 assertAfterTerminateCalledOnce();
73 }
74
75 @Test
76 public void errorConditional() {
77 Single.<Integer>error(new TestException())
78 .doAfterTerminate(afterTerminate)
79 .filter(Functions.alwaysTrue())
80 .subscribeWith(to)
81 .assertFailure(TestException.class);
82
83 assertAfterTerminateCalledOnce();
84 }
85
86 @Test
87 public void actionThrows() {
88 List<Throwable> errors = TestHelper.trackPluginErrors();
89 try {
90 Single.just(1)
91 .doAfterTerminate(new Action() {
92 @Override
93 public void run() throws Exception {
94 throw new TestException();
95 }
96 })
97 .test()
98 .assertResult(1);
99
100 TestHelper.assertUndeliverable(errors, 0, TestException.class);
101 } finally {
102 RxJavaPlugins.reset();
103 }
104 }
105
106 @Test
107 public void dispose() {
108 TestHelper.checkDisposed(PublishSubject.<Integer>create().singleOrError().doAfterTerminate(afterTerminate));
109 }
110
111 @Test
112 public void doubleOnSubscribe() {
113 TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Integer>, SingleSource<Integer>>() {
114 @Override
115 public SingleSource<Integer> apply(Single<Integer> m) throws Exception {
116 return m.doAfterTerminate(afterTerminate);
117 }
118 });
119 }
120
121 private void assertAfterTerminateCalledOnce() {
122 assertEquals(1, call[0]);
123 }
124 }