| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.disposables.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.Function; |
| 26 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 27 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 28 |
import io.reactivex.rxjava3.subjects.*; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class ObservableConcatMapCompletableTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void asyncFused() throws Exception { |
| 35 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 36 |
|
| 37 |
TestObserver<Void> to = us.concatMapCompletable(completableComplete(), 2).test(); |
| 38 |
|
| 39 |
us.onNext(1); |
| 40 |
us.onComplete(); |
| 41 |
|
| 42 |
to.assertComplete(); |
| 43 |
to.assertValueCount(0); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void notFused() throws Exception { |
| 48 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 49 |
TestObserver<Void> to = us.hide().concatMapCompletable(completableComplete(), 2).test(); |
| 50 |
|
| 51 |
us.onNext(1); |
| 52 |
us.onNext(2); |
| 53 |
us.onComplete(); |
| 54 |
|
| 55 |
to.assertComplete(); |
| 56 |
to.assertValueCount(0); |
| 57 |
to.assertNoErrors(); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void dispose() { |
| 62 |
TestHelper.checkDisposed(Observable.<Integer>just(1).hide() |
| 63 |
.concatMapCompletable(completableError())); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void mainError() { |
| 68 |
Observable.<Integer>error(new TestException()) |
| 69 |
.concatMapCompletable(completableComplete()) |
| 70 |
.test() |
| 71 |
.assertFailure(TestException.class); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void innerError() { |
| 76 |
Observable.<Integer>just(1).hide() |
| 77 |
.concatMapCompletable(completableError()) |
| 78 |
.test() |
| 79 |
.assertFailure(TestException.class); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void badSource() { |
| 84 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 85 |
try { |
| 86 |
new Observable<Integer>() { |
| 87 |
@Override |
| 88 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 89 |
observer.onSubscribe(Disposable.empty()); |
| 90 |
|
| 91 |
observer.onNext(1); |
| 92 |
observer.onComplete(); |
| 93 |
observer.onNext(2); |
| 94 |
observer.onError(new TestException()); |
| 95 |
observer.onComplete(); |
| 96 |
} |
| 97 |
} |
| 98 |
.concatMapCompletable(completableComplete()) |
| 99 |
.test() |
| 100 |
.assertComplete(); |
| 101 |
|
| 102 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 103 |
} finally { |
| 104 |
RxJavaPlugins.reset(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void onErrorRace() { |
| 110 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 111 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 112 |
try { |
| 113 |
final PublishSubject<Integer> ps1 = PublishSubject.create(); |
| 114 |
final PublishSubject<Integer> ps2 = PublishSubject.create(); |
| 115 |
|
| 116 |
TestObserver<Void> to = ps1.concatMapCompletable(new Function<Integer, CompletableSource>() { |
| 117 |
@Override |
| 118 |
public CompletableSource apply(Integer v) throws Exception { |
| 119 |
return Completable.fromObservable(ps2); |
| 120 |
} |
| 121 |
}).test(); |
| 122 |
|
| 123 |
final TestException ex1 = new TestException(); |
| 124 |
final TestException ex2 = new TestException(); |
| 125 |
|
| 126 |
Runnable r1 = new Runnable() { |
| 127 |
@Override |
| 128 |
public void run() { |
| 129 |
ps1.onError(ex1); |
| 130 |
} |
| 131 |
}; |
| 132 |
Runnable r2 = new Runnable() { |
| 133 |
@Override |
| 134 |
public void run() { |
| 135 |
ps2.onError(ex2); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
TestHelper.race(r1, r2); |
| 140 |
|
| 141 |
to.assertFailure(TestException.class); |
| 142 |
|
| 143 |
if (!errors.isEmpty()) { |
| 144 |
TestHelper.assertError(errors, 0, TestException.class); |
| 145 |
} |
| 146 |
} finally { |
| 147 |
RxJavaPlugins.reset(); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void mapperThrows() { |
| 154 |
Observable.just(1).hide() |
| 155 |
.concatMapCompletable(completableThrows()) |
| 156 |
.test() |
| 157 |
.assertFailure(TestException.class); |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void fusedPollThrows() { |
| 162 |
Observable.just(1) |
| 163 |
.map(new Function<Integer, Integer>() { |
| 164 |
@Override |
| 165 |
public Integer apply(Integer v) throws Exception { |
| 166 |
throw new TestException(); |
| 167 |
} |
| 168 |
}) |
| 169 |
.concatMapCompletable(completableComplete()) |
| 170 |
.test() |
| 171 |
.assertFailure(TestException.class); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void concatReportsDisposedOnComplete() { |
| 176 |
final Disposable[] disposable = { null }; |
| 177 |
|
| 178 |
Observable.just(1) |
| 179 |
.hide() |
| 180 |
.concatMapCompletable(completableComplete()) |
| 181 |
.subscribe(new CompletableObserver() { |
| 182 |
|
| 183 |
@Override |
| 184 |
public void onSubscribe(Disposable d) { |
| 185 |
disposable[0] = d; |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void onError(Throwable e) { |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void onComplete() { |
| 194 |
} |
| 195 |
}); |
| 196 |
|
| 197 |
assertTrue(disposable[0].isDisposed()); |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void concatReportsDisposedOnError() { |
| 202 |
final Disposable[] disposable = { null }; |
| 203 |
|
| 204 |
Observable.just(1) |
| 205 |
.hide() |
| 206 |
.concatMapCompletable(completableError()) |
| 207 |
.subscribe(new CompletableObserver() { |
| 208 |
|
| 209 |
@Override |
| 210 |
public void onSubscribe(Disposable d) { |
| 211 |
disposable[0] = d; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public void onError(Throwable e) { |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public void onComplete() { |
| 220 |
} |
| 221 |
}); |
| 222 |
|
| 223 |
assertTrue(disposable[0].isDisposed()); |
| 224 |
} |
| 225 |
|
| 226 |
private Function<Integer, CompletableSource> completableComplete() { |
| 227 |
return new Function<Integer, CompletableSource>() { |
| 228 |
@Override |
| 229 |
public CompletableSource apply(Integer v) throws Exception { |
| 230 |
return Completable.complete(); |
| 231 |
} |
| 232 |
}; |
| 233 |
} |
| 234 |
|
| 235 |
private Function<Integer, CompletableSource> completableError() { |
| 236 |
return new Function<Integer, CompletableSource>() { |
| 237 |
@Override |
| 238 |
public CompletableSource apply(Integer v) throws Exception { |
| 239 |
return Completable.error(new TestException()); |
| 240 |
} |
| 241 |
}; |
| 242 |
} |
| 243 |
|
| 244 |
private Function<Integer, CompletableSource> completableThrows() { |
| 245 |
return new Function<Integer, CompletableSource>() { |
| 246 |
@Override |
| 247 |
public CompletableSource apply(Integer v) throws Exception { |
| 248 |
throw new TestException(); |
| 249 |
} |
| 250 |
}; |
| 251 |
} |
| 252 |
} |