| 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.*; |
| 19 |
import java.util.concurrent.CountDownLatch; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import org.junit.Test; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.*; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 29 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.*; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 34 |
|
| 35 |
public class CompletableConcatTest extends RxJavaTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void overflowReported() { |
| 39 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 40 |
try { |
| 41 |
Completable.concat( |
| 42 |
Flowable.fromPublisher(new Publisher<Completable>() { |
| 43 |
@Override |
| 44 |
public void subscribe(Subscriber<? super Completable> s) { |
| 45 |
s.onSubscribe(new BooleanSubscription()); |
| 46 |
s.onNext(Completable.never()); |
| 47 |
s.onNext(Completable.never()); |
| 48 |
s.onNext(Completable.never()); |
| 49 |
s.onNext(Completable.never()); |
| 50 |
s.onComplete(); |
| 51 |
} |
| 52 |
}), 1 |
| 53 |
) |
| 54 |
.test() |
| 55 |
.assertFailure(MissingBackpressureException.class); |
| 56 |
|
| 57 |
TestHelper.assertError(errors, 0, MissingBackpressureException.class); |
| 58 |
} finally { |
| 59 |
RxJavaPlugins.reset(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void invalidPrefetch() { |
| 65 |
try { |
| 66 |
Completable.concat(Flowable.just(Completable.complete()), -99); |
| 67 |
fail("Should have thrown IllegalArgumentExceptio"); |
| 68 |
} catch (IllegalArgumentException ex) { |
| 69 |
assertEquals("prefetch > 0 required but it was -99", ex.getMessage()); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void dispose() { |
| 75 |
TestHelper.checkDisposed(Completable.concat(Flowable.just(Completable.complete()))); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void errorRace() { |
| 80 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 81 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 82 |
|
| 83 |
try { |
| 84 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 85 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 86 |
|
| 87 |
TestObserver<Void> to = Completable.concat(pp1.map(new Function<Integer, Completable>() { |
| 88 |
@Override |
| 89 |
public Completable apply(Integer v) throws Exception { |
| 90 |
return pp2.ignoreElements(); |
| 91 |
} |
| 92 |
})).test(); |
| 93 |
|
| 94 |
pp1.onNext(1); |
| 95 |
|
| 96 |
final TestException ex = new TestException(); |
| 97 |
|
| 98 |
Runnable r1 = new Runnable() { |
| 99 |
@Override |
| 100 |
public void run() { |
| 101 |
pp1.onError(ex); |
| 102 |
} |
| 103 |
}; |
| 104 |
Runnable r2 = new Runnable() { |
| 105 |
@Override |
| 106 |
public void run() { |
| 107 |
pp2.onError(ex); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
TestHelper.race(r1, r2); |
| 112 |
|
| 113 |
to.assertFailure(TestException.class); |
| 114 |
|
| 115 |
if (!errors.isEmpty()) { |
| 116 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 117 |
} |
| 118 |
} finally { |
| 119 |
RxJavaPlugins.reset(); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void synchronousFusedCrash() { |
| 126 |
Completable.concat(Flowable.range(1, 2).map(new Function<Integer, Completable>() { |
| 127 |
@Override |
| 128 |
public Completable apply(Integer v) throws Exception { |
| 129 |
throw new TestException(); |
| 130 |
} |
| 131 |
})) |
| 132 |
.test() |
| 133 |
.assertFailure(TestException.class); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void unboundedIn() { |
| 138 |
Completable.concat(Flowable.just(Completable.complete()).hide(), Integer.MAX_VALUE) |
| 139 |
.test() |
| 140 |
.assertResult(); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void syncFusedUnboundedIn() { |
| 145 |
Completable.concat(Flowable.just(Completable.complete()), Integer.MAX_VALUE) |
| 146 |
.test() |
| 147 |
.assertResult(); |
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void asyncFusedUnboundedIn() { |
| 152 |
UnicastProcessor<Completable> up = UnicastProcessor.create(); |
| 153 |
up.onNext(Completable.complete()); |
| 154 |
up.onComplete(); |
| 155 |
|
| 156 |
Completable.concat(up, Integer.MAX_VALUE) |
| 157 |
.test() |
| 158 |
.assertResult(); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void arrayCancelled() { |
| 163 |
Completable.concatArray(Completable.complete(), Completable.complete()) |
| 164 |
.test(true) |
| 165 |
.assertEmpty(); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void arrayFirstCancels() { |
| 170 |
final TestObserver<Void> to = new TestObserver<>(); |
| 171 |
|
| 172 |
Completable.concatArray(new Completable() { |
| 173 |
@Override |
| 174 |
protected void subscribeActual(CompletableObserver observer) { |
| 175 |
observer.onSubscribe(Disposable.empty()); |
| 176 |
to.dispose(); |
| 177 |
observer.onComplete(); |
| 178 |
} |
| 179 |
}, Completable.complete()) |
| 180 |
.subscribe(to); |
| 181 |
|
| 182 |
to.assertEmpty(); |
| 183 |
} |
| 184 |
|
| 185 |
@Test |
| 186 |
public void iterableCancelled() { |
| 187 |
Completable.concat(Arrays.asList(Completable.complete(), Completable.complete())) |
| 188 |
.test(true) |
| 189 |
.assertEmpty(); |
| 190 |
} |
| 191 |
|
| 192 |
@Test |
| 193 |
public void iterableFirstCancels() { |
| 194 |
final TestObserver<Void> to = new TestObserver<>(); |
| 195 |
|
| 196 |
Completable.concat(Arrays.asList(new Completable() { |
| 197 |
@Override |
| 198 |
protected void subscribeActual(CompletableObserver observer) { |
| 199 |
observer.onSubscribe(Disposable.empty()); |
| 200 |
to.dispose(); |
| 201 |
observer.onComplete(); |
| 202 |
} |
| 203 |
}, Completable.complete())) |
| 204 |
.subscribe(to); |
| 205 |
|
| 206 |
to.assertEmpty(); |
| 207 |
} |
| 208 |
|
| 209 |
@Test |
| 210 |
public void arrayCancelRace() { |
| 211 |
Completable[] a = new Completable[1024]; |
| 212 |
Arrays.fill(a, Completable.complete()); |
| 213 |
|
| 214 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 215 |
|
| 216 |
final Completable c = Completable.concatArray(a); |
| 217 |
|
| 218 |
final TestObserver<Void> to = new TestObserver<>(); |
| 219 |
|
| 220 |
Runnable r1 = new Runnable() { |
| 221 |
@Override |
| 222 |
public void run() { |
| 223 |
c.subscribe(to); |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
Runnable r2 = new Runnable() { |
| 228 |
@Override |
| 229 |
public void run() { |
| 230 |
to.dispose(); |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
TestHelper.race(r1, r2); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void iterableCancelRace() { |
| 240 |
Completable[] a = new Completable[1024]; |
| 241 |
Arrays.fill(a, Completable.complete()); |
| 242 |
|
| 243 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 244 |
|
| 245 |
final Completable c = Completable.concat(Arrays.asList(a)); |
| 246 |
|
| 247 |
final TestObserver<Void> to = new TestObserver<>(); |
| 248 |
|
| 249 |
Runnable r1 = new Runnable() { |
| 250 |
@Override |
| 251 |
public void run() { |
| 252 |
c.subscribe(to); |
| 253 |
} |
| 254 |
}; |
| 255 |
|
| 256 |
Runnable r2 = new Runnable() { |
| 257 |
@Override |
| 258 |
public void run() { |
| 259 |
to.dispose(); |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
TestHelper.race(r1, r2); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
@Test |
| 268 |
public void noInterrupt() throws InterruptedException { |
| 269 |
for (int k = 0; k < 100; k++) { |
| 270 |
final int count = 10; |
| 271 |
final CountDownLatch latch = new CountDownLatch(count); |
| 272 |
final boolean[] interrupted = { false }; |
| 273 |
|
| 274 |
for (int i = 0; i < count; i++) { |
| 275 |
Completable c0 = Completable.fromAction(new Action() { |
| 276 |
@Override |
| 277 |
public void run() throws Exception { |
| 278 |
try { |
| 279 |
Thread.sleep(30); |
| 280 |
} catch (InterruptedException e) { |
| 281 |
System.out.println("Interrupted! " + Thread.currentThread()); |
| 282 |
interrupted[0] = true; |
| 283 |
} |
| 284 |
} |
| 285 |
}); |
| 286 |
Completable.concat(Arrays.asList(Completable.complete() |
| 287 |
.subscribeOn(Schedulers.io()) |
| 288 |
.observeOn(Schedulers.io()), |
| 289 |
c0) |
| 290 |
) |
| 291 |
.subscribe(new Action() { |
| 292 |
@Override |
| 293 |
public void run() throws Exception { |
| 294 |
latch.countDown(); |
| 295 |
} |
| 296 |
}); |
| 297 |
} |
| 298 |
|
| 299 |
latch.await(); |
| 300 |
assertFalse("The second Completable was interrupted!", interrupted[0]); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
@Test |
| 305 |
public void doubleOnSubscribe() { |
| 306 |
TestHelper.<Completable>checkDoubleOnSubscribeFlowableToCompletable(f -> Completable.concat(f)); |
| 307 |
} |
| 308 |
} |