| 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.List; |
| 19 |
import java.util.concurrent.atomic.AtomicReference; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 27 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 28 |
import io.reactivex.rxjava3.subjects.CompletableSubject; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class CompletableTakeUntilTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void consumerDisposes() { |
| 35 |
CompletableSubject cs1 = CompletableSubject.create(); |
| 36 |
CompletableSubject cs2 = CompletableSubject.create(); |
| 37 |
|
| 38 |
TestObserver<Void> to = cs1.takeUntil(cs2).test(); |
| 39 |
|
| 40 |
to.assertEmpty(); |
| 41 |
|
| 42 |
assertTrue(cs1.hasObservers()); |
| 43 |
assertTrue(cs2.hasObservers()); |
| 44 |
|
| 45 |
to.dispose(); |
| 46 |
|
| 47 |
assertFalse(cs1.hasObservers()); |
| 48 |
assertFalse(cs2.hasObservers()); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void mainCompletes() { |
| 53 |
CompletableSubject cs1 = CompletableSubject.create(); |
| 54 |
CompletableSubject cs2 = CompletableSubject.create(); |
| 55 |
|
| 56 |
TestObserver<Void> to = cs1.takeUntil(cs2).test(); |
| 57 |
|
| 58 |
to.assertEmpty(); |
| 59 |
|
| 60 |
assertTrue(cs1.hasObservers()); |
| 61 |
assertTrue(cs2.hasObservers()); |
| 62 |
|
| 63 |
cs1.onComplete(); |
| 64 |
|
| 65 |
assertFalse(cs1.hasObservers()); |
| 66 |
assertFalse(cs2.hasObservers()); |
| 67 |
|
| 68 |
to.assertResult(); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void otherCompletes() { |
| 73 |
CompletableSubject cs1 = CompletableSubject.create(); |
| 74 |
CompletableSubject cs2 = CompletableSubject.create(); |
| 75 |
|
| 76 |
TestObserver<Void> to = cs1.takeUntil(cs2).test(); |
| 77 |
|
| 78 |
to.assertEmpty(); |
| 79 |
|
| 80 |
assertTrue(cs1.hasObservers()); |
| 81 |
assertTrue(cs2.hasObservers()); |
| 82 |
|
| 83 |
cs2.onComplete(); |
| 84 |
|
| 85 |
assertFalse(cs1.hasObservers()); |
| 86 |
assertFalse(cs2.hasObservers()); |
| 87 |
|
| 88 |
to.assertResult(); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void mainErrors() { |
| 93 |
CompletableSubject cs1 = CompletableSubject.create(); |
| 94 |
CompletableSubject cs2 = CompletableSubject.create(); |
| 95 |
|
| 96 |
TestObserver<Void> to = cs1.takeUntil(cs2).test(); |
| 97 |
|
| 98 |
to.assertEmpty(); |
| 99 |
|
| 100 |
assertTrue(cs1.hasObservers()); |
| 101 |
assertTrue(cs2.hasObservers()); |
| 102 |
|
| 103 |
cs1.onError(new TestException()); |
| 104 |
|
| 105 |
assertFalse(cs1.hasObservers()); |
| 106 |
assertFalse(cs2.hasObservers()); |
| 107 |
|
| 108 |
to.assertFailure(TestException.class); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void otherErrors() { |
| 113 |
CompletableSubject cs1 = CompletableSubject.create(); |
| 114 |
CompletableSubject cs2 = CompletableSubject.create(); |
| 115 |
|
| 116 |
TestObserver<Void> to = cs1.takeUntil(cs2).test(); |
| 117 |
|
| 118 |
to.assertEmpty(); |
| 119 |
|
| 120 |
assertTrue(cs1.hasObservers()); |
| 121 |
assertTrue(cs2.hasObservers()); |
| 122 |
|
| 123 |
cs2.onError(new TestException()); |
| 124 |
|
| 125 |
assertFalse(cs1.hasObservers()); |
| 126 |
assertFalse(cs2.hasObservers()); |
| 127 |
|
| 128 |
to.assertFailure(TestException.class); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void isDisposed() { |
| 133 |
CompletableSubject cs1 = CompletableSubject.create(); |
| 134 |
CompletableSubject cs2 = CompletableSubject.create(); |
| 135 |
|
| 136 |
TestHelper.checkDisposed(cs1.takeUntil(cs2)); |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void mainErrorLate() { |
| 141 |
|
| 142 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 143 |
try { |
| 144 |
|
| 145 |
new Completable() { |
| 146 |
@Override |
| 147 |
protected void subscribeActual(CompletableObserver observer) { |
| 148 |
observer.onSubscribe(Disposable.empty()); |
| 149 |
observer.onError(new TestException()); |
| 150 |
} |
| 151 |
}.takeUntil(Completable.complete()) |
| 152 |
.test() |
| 153 |
.assertResult(); |
| 154 |
|
| 155 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 156 |
} finally { |
| 157 |
RxJavaPlugins.reset(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void mainCompleteLate() { |
| 163 |
|
| 164 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 165 |
try { |
| 166 |
|
| 167 |
new Completable() { |
| 168 |
@Override |
| 169 |
protected void subscribeActual(CompletableObserver observer) { |
| 170 |
observer.onSubscribe(Disposable.empty()); |
| 171 |
observer.onComplete(); |
| 172 |
} |
| 173 |
}.takeUntil(Completable.complete()) |
| 174 |
.test() |
| 175 |
.assertResult(); |
| 176 |
|
| 177 |
assertTrue(errors.isEmpty()); |
| 178 |
} finally { |
| 179 |
RxJavaPlugins.reset(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void otherErrorLate() { |
| 185 |
|
| 186 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 187 |
try { |
| 188 |
|
| 189 |
final AtomicReference<CompletableObserver> ref = new AtomicReference<>(); |
| 190 |
|
| 191 |
Completable.complete() |
| 192 |
.takeUntil(new Completable() { |
| 193 |
@Override |
| 194 |
protected void subscribeActual(CompletableObserver observer) { |
| 195 |
observer.onSubscribe(Disposable.empty()); |
| 196 |
ref.set(observer); |
| 197 |
} |
| 198 |
}) |
| 199 |
.test() |
| 200 |
.assertResult(); |
| 201 |
|
| 202 |
ref.get().onError(new TestException()); |
| 203 |
|
| 204 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 205 |
} finally { |
| 206 |
RxJavaPlugins.reset(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void otherCompleteLate() { |
| 212 |
|
| 213 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 214 |
try { |
| 215 |
|
| 216 |
final AtomicReference<CompletableObserver> ref = new AtomicReference<>(); |
| 217 |
|
| 218 |
Completable.complete() |
| 219 |
.takeUntil(new Completable() { |
| 220 |
@Override |
| 221 |
protected void subscribeActual(CompletableObserver observer) { |
| 222 |
observer.onSubscribe(Disposable.empty()); |
| 223 |
ref.set(observer); |
| 224 |
} |
| 225 |
}) |
| 226 |
.test() |
| 227 |
.assertResult(); |
| 228 |
|
| 229 |
ref.get().onComplete(); |
| 230 |
|
| 231 |
assertTrue(errors.isEmpty()); |
| 232 |
} finally { |
| 233 |
RxJavaPlugins.reset(); |
| 234 |
} |
| 235 |
} |
| 236 |
} |