| 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.*; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.List; |
| 20 |
import java.util.concurrent.atomic.AtomicReference; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.disposables.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.Function; |
| 28 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.subjects.*; |
| 31 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 32 |
|
| 33 |
public class ObservableMergeWithSingleTest extends RxJavaTest { |
| 34 |
|
| 35 |
@Test |
| 36 |
public void normal() { |
| 37 |
Observable.range(1, 5) |
| 38 |
.mergeWith(Single.just(100)) |
| 39 |
.test() |
| 40 |
.assertResult(1, 2, 3, 4, 5, 100); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void normalLong() { |
| 45 |
Observable.range(1, 512) |
| 46 |
.mergeWith(Single.just(100)) |
| 47 |
.test() |
| 48 |
.assertValueCount(513) |
| 49 |
.assertComplete(); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void take() { |
| 54 |
Observable.range(1, 5) |
| 55 |
.mergeWith(Single.just(100)) |
| 56 |
.take(3) |
| 57 |
.test() |
| 58 |
.assertResult(1, 2, 3); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void cancel() { |
| 63 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 64 |
final SingleSubject<Integer> cs = SingleSubject.create(); |
| 65 |
|
| 66 |
TestObserver<Integer> to = ps.mergeWith(cs).test(); |
| 67 |
|
| 68 |
assertTrue(ps.hasObservers()); |
| 69 |
assertTrue(cs.hasObservers()); |
| 70 |
|
| 71 |
to.dispose(); |
| 72 |
|
| 73 |
assertFalse(ps.hasObservers()); |
| 74 |
assertFalse(cs.hasObservers()); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void mainError() { |
| 79 |
Observable.error(new TestException()) |
| 80 |
.mergeWith(Single.just(100)) |
| 81 |
.test() |
| 82 |
.assertFailure(TestException.class); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void otherError() { |
| 87 |
Observable.never() |
| 88 |
.mergeWith(Single.error(new TestException())) |
| 89 |
.test() |
| 90 |
.assertFailure(TestException.class); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void completeRace() { |
| 95 |
for (int i = 0; i < 10000; i++) { |
| 96 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 97 |
final SingleSubject<Integer> cs = SingleSubject.create(); |
| 98 |
|
| 99 |
TestObserver<Integer> to = ps.mergeWith(cs).test(); |
| 100 |
|
| 101 |
Runnable r1 = new Runnable() { |
| 102 |
@Override |
| 103 |
public void run() { |
| 104 |
ps.onNext(1); |
| 105 |
ps.onComplete(); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
Runnable r2 = new Runnable() { |
| 110 |
@Override |
| 111 |
public void run() { |
| 112 |
cs.onSuccess(1); |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
TestHelper.race(r1, r2); |
| 117 |
|
| 118 |
to.assertResult(1, 1); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void onNextSlowPath() { |
| 124 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 125 |
final SingleSubject<Integer> cs = SingleSubject.create(); |
| 126 |
|
| 127 |
TestObserver<Integer> to = ps.mergeWith(cs).subscribeWith(new TestObserver<Integer>() { |
| 128 |
@Override |
| 129 |
public void onNext(Integer t) { |
| 130 |
super.onNext(t); |
| 131 |
if (t == 1) { |
| 132 |
ps.onNext(2); |
| 133 |
} |
| 134 |
} |
| 135 |
}); |
| 136 |
|
| 137 |
ps.onNext(1); |
| 138 |
cs.onSuccess(3); |
| 139 |
|
| 140 |
ps.onNext(4); |
| 141 |
ps.onComplete(); |
| 142 |
|
| 143 |
to.assertResult(1, 2, 3, 4); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void onSuccessSlowPath() { |
| 148 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 149 |
final SingleSubject<Integer> cs = SingleSubject.create(); |
| 150 |
|
| 151 |
TestObserver<Integer> to = ps.mergeWith(cs).subscribeWith(new TestObserver<Integer>() { |
| 152 |
@Override |
| 153 |
public void onNext(Integer t) { |
| 154 |
super.onNext(t); |
| 155 |
if (t == 1) { |
| 156 |
cs.onSuccess(2); |
| 157 |
} |
| 158 |
} |
| 159 |
}); |
| 160 |
|
| 161 |
ps.onNext(1); |
| 162 |
|
| 163 |
ps.onNext(3); |
| 164 |
ps.onComplete(); |
| 165 |
|
| 166 |
to.assertResult(1, 2, 3); |
| 167 |
} |
| 168 |
|
| 169 |
@Test |
| 170 |
public void onErrorMainOverflow() { |
| 171 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 172 |
try { |
| 173 |
final AtomicReference<Observer<?>> observerRef = new AtomicReference<>(); |
| 174 |
TestObserver<Integer> to = new Observable<Integer>() { |
| 175 |
@Override |
| 176 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 177 |
observer.onSubscribe(Disposable.empty()); |
| 178 |
observerRef.set(observer); |
| 179 |
} |
| 180 |
} |
| 181 |
.mergeWith(Single.<Integer>error(new IOException())) |
| 182 |
.test(); |
| 183 |
|
| 184 |
observerRef.get().onError(new TestException()); |
| 185 |
|
| 186 |
to.assertFailure(IOException.class) |
| 187 |
; |
| 188 |
|
| 189 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 190 |
} finally { |
| 191 |
RxJavaPlugins.reset(); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
@Test |
| 196 |
public void onErrorOtherOverflow() { |
| 197 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 198 |
try { |
| 199 |
Observable.error(new IOException()) |
| 200 |
.mergeWith(Single.error(new TestException())) |
| 201 |
.test() |
| 202 |
.assertFailure(IOException.class) |
| 203 |
; |
| 204 |
|
| 205 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 206 |
} finally { |
| 207 |
RxJavaPlugins.reset(); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
@Test |
| 212 |
public void doubleOnSubscribeMain() { |
| 213 |
TestHelper.checkDoubleOnSubscribeObservable( |
| 214 |
new Function<Observable<Object>, Observable<Object>>() { |
| 215 |
@Override |
| 216 |
public Observable<Object> apply(Observable<Object> f) |
| 217 |
throws Exception { |
| 218 |
return f.mergeWith(Single.just(1)); |
| 219 |
} |
| 220 |
} |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void isDisposed() { |
| 226 |
new Observable<Integer>() { |
| 227 |
@Override |
| 228 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 229 |
observer.onSubscribe(Disposable.empty()); |
| 230 |
|
| 231 |
assertFalse(((Disposable)observer).isDisposed()); |
| 232 |
|
| 233 |
observer.onNext(1); |
| 234 |
|
| 235 |
assertTrue(((Disposable)observer).isDisposed()); |
| 236 |
} |
| 237 |
}.mergeWith(Single.<Integer>just(1)) |
| 238 |
.take(1) |
| 239 |
.test() |
| 240 |
.assertResult(1); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void onNextSlowPathCreateQueue() { |
| 245 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 246 |
final SingleSubject<Integer> cs = SingleSubject.create(); |
| 247 |
|
| 248 |
TestObserver<Integer> to = ps.mergeWith(cs).subscribeWith(new TestObserver<Integer>() { |
| 249 |
@Override |
| 250 |
public void onNext(Integer t) { |
| 251 |
super.onNext(t); |
| 252 |
if (t == 1) { |
| 253 |
ps.onNext(2); |
| 254 |
ps.onNext(3); |
| 255 |
} |
| 256 |
} |
| 257 |
}); |
| 258 |
|
| 259 |
cs.onSuccess(0); |
| 260 |
ps.onNext(1); |
| 261 |
|
| 262 |
ps.onNext(4); |
| 263 |
ps.onComplete(); |
| 264 |
|
| 265 |
to.assertResult(0, 1, 2, 3, 4); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void cancelOtherOnMainError() { |
| 270 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 271 |
SingleSubject<Integer> ss = SingleSubject.create(); |
| 272 |
|
| 273 |
TestObserver<Integer> to = ps.mergeWith(ss).test(); |
| 274 |
|
| 275 |
assertTrue(ps.hasObservers()); |
| 276 |
assertTrue(ss.hasObservers()); |
| 277 |
|
| 278 |
ps.onError(new TestException()); |
| 279 |
|
| 280 |
to.assertFailure(TestException.class); |
| 281 |
|
| 282 |
assertFalse("main has observers!", ps.hasObservers()); |
| 283 |
assertFalse("other has observers", ss.hasObservers()); |
| 284 |
} |
| 285 |
|
| 286 |
@Test |
| 287 |
public void cancelMainOnOtherError() { |
| 288 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 289 |
SingleSubject<Integer> ss = SingleSubject.create(); |
| 290 |
|
| 291 |
TestObserver<Integer> to = ps.mergeWith(ss).test(); |
| 292 |
|
| 293 |
assertTrue(ps.hasObservers()); |
| 294 |
assertTrue(ss.hasObservers()); |
| 295 |
|
| 296 |
ss.onError(new TestException()); |
| 297 |
|
| 298 |
to.assertFailure(TestException.class); |
| 299 |
|
| 300 |
assertFalse("main has observers!", ps.hasObservers()); |
| 301 |
assertFalse("other has observers", ss.hasObservers()); |
| 302 |
} |
| 303 |
|
| 304 |
@Test |
| 305 |
public void undeliverableUponCancel() { |
| 306 |
TestHelper.checkUndeliverableUponCancel(new ObservableConverter<Integer, Observable<Integer>>() { |
| 307 |
@Override |
| 308 |
public Observable<Integer> apply(Observable<Integer> upstream) { |
| 309 |
return upstream.mergeWith(Single.just(1).hide()); |
| 310 |
} |
| 311 |
}); |
| 312 |
} |
| 313 |
} |