Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableAndThenCompletableTest.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.*;
17
18 import java.util.concurrent.CountDownLatch;
19 import java.util.concurrent.atomic.AtomicInteger;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.Action;
26 import io.reactivex.rxjava3.observers.TestObserver;
27 import io.reactivex.rxjava3.schedulers.Schedulers;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class CompletableAndThenCompletableTest extends RxJavaTest {
31 @Test
32 public void andThenCompletableCompleteComplete() {
33 Completable.complete()
34 .andThen(Completable.complete())
35 .test()
36 .assertComplete();
37 }
38
39 @Test
40 public void andThenCompletableCompleteError() {
41 Completable.complete()
42 .andThen(Completable.error(new TestException("test")))
43 .to(TestHelper.testConsumer())
44 .assertNotComplete()
45 .assertNoValues()
46 .assertError(TestException.class)
47 .assertErrorMessage("test");
48 }
49
50 @Test
51 public void andThenCompletableCompleteNever() {
52 Completable.complete()
53 .andThen(Completable.never())
54 .test()
55 .assertNoValues()
56 .assertNoErrors()
57 .assertNotComplete();
58 }
59
60 @Test
61 public void andThenCompletableErrorComplete() {
62 Completable.error(new TestException("bla"))
63 .andThen(Completable.complete())
64 .to(TestHelper.testConsumer())
65 .assertNotComplete()
66 .assertNoValues()
67 .assertError(TestException.class)
68 .assertErrorMessage("bla");
69 }
70
71 @Test
72 public void andThenCompletableErrorNever() {
73 Completable.error(new TestException("bla"))
74 .andThen(Completable.never())
75 .to(TestHelper.testConsumer())
76 .assertNotComplete()
77 .assertNoValues()
78 .assertError(TestException.class)
79 .assertErrorMessage("bla");
80 }
81
82 @Test
83 public void andThenCompletableErrorError() {
84 Completable.error(new TestException("error1"))
85 .andThen(Completable.error(new TestException("error2")))
86 .to(TestHelper.testConsumer())
87 .assertNotComplete()
88 .assertNoValues()
89 .assertError(TestException.class)
90 .assertErrorMessage("error1");
91 }
92
93 @Test
94 public void andThenCanceled() {
95 final AtomicInteger completableRunCount = new AtomicInteger();
96 Completable.fromRunnable(new Runnable() {
97 @Override
98 public void run() {
99 completableRunCount.incrementAndGet();
100 }
101 })
102 .andThen(Completable.complete())
103 .test(true)
104 .assertEmpty();
105 assertEquals(0, completableRunCount.get());
106 }
107
108 @Test
109 public void andThenFirstCancels() {
110 final TestObserver<Void> to = new TestObserver<>();
111 Completable.fromRunnable(new Runnable() {
112 @Override
113 public void run() {
114 to.dispose();
115 }
116 })
117 .andThen(Completable.complete())
118 .subscribe(to);
119 to
120 .assertNotComplete()
121 .assertNoErrors();
122 }
123
124 @Test
125 public void andThenSecondCancels() {
126 final TestObserver<Void> to = new TestObserver<>();
127 Completable.complete()
128 .andThen(Completable.fromRunnable(new Runnable() {
129 @Override
130 public void run() {
131 to.dispose();
132 }
133 }))
134 .subscribe(to);
135 to
136 .assertNotComplete()
137 .assertNoErrors();
138 }
139
140 @Test
141 public void andThenDisposed() {
142 TestHelper.checkDisposed(Completable.complete()
143 .andThen(Completable.complete()));
144 }
145
146 @Test
147 public void andThenNoInterrupt() throws InterruptedException {
148 for (int k = 0; k < 100; k++) {
149 final int count = 10;
150 final CountDownLatch latch = new CountDownLatch(count);
151 final boolean[] interrupted = {false};
152
153 for (int i = 0; i < count; i++) {
154 Completable.complete()
155 .subscribeOn(Schedulers.io())
156 .observeOn(Schedulers.io())
157 .andThen(Completable.fromAction(new Action() {
158 @Override
159 public void run() throws Exception {
160 try {
161 Thread.sleep(30);
162 } catch (InterruptedException e) {
163 System.out.println("Interrupted! " + Thread.currentThread());
164 interrupted[0] = true;
165 }
166 }
167 }))
168 .subscribe(new Action() {
169 @Override
170 public void run() throws Exception {
171 latch.countDown();
172 }
173 });
174 }
175
176 latch.await();
177 assertFalse("The second Completable was interrupted!", interrupted[0]);
178 }
179 }
180
181 @Test
182 public void doubleOnSubscribe() {
183 TestHelper.checkDoubleOnSubscribeCompletable(c -> c.andThen(c));
184 }
185 }