| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.*; |
| 20 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.BiConsumer; |
| 27 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.subjects.*; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
public class SingleAmbTest extends RxJavaTest { |
| 35 |
@Test |
| 36 |
public void ambWithFirstFires() { |
| 37 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 38 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 39 |
|
| 40 |
TestObserver<Integer> to = pp1.single(-99).ambWith(pp2.single(-99)).test(); |
| 41 |
|
| 42 |
assertTrue(pp1.hasSubscribers()); |
| 43 |
assertTrue(pp2.hasSubscribers()); |
| 44 |
|
| 45 |
pp1.onNext(1); |
| 46 |
pp1.onComplete(); |
| 47 |
|
| 48 |
assertFalse(pp1.hasSubscribers()); |
| 49 |
assertFalse(pp2.hasSubscribers()); |
| 50 |
|
| 51 |
to.assertResult(1); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void ambWithSecondFires() { |
| 57 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 58 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 59 |
|
| 60 |
TestObserver<Integer> to = pp1.single(-99).ambWith(pp2.single(-99)).test(); |
| 61 |
|
| 62 |
assertTrue(pp1.hasSubscribers()); |
| 63 |
assertTrue(pp2.hasSubscribers()); |
| 64 |
|
| 65 |
pp2.onNext(2); |
| 66 |
pp2.onComplete(); |
| 67 |
|
| 68 |
assertFalse(pp1.hasSubscribers()); |
| 69 |
assertFalse(pp2.hasSubscribers()); |
| 70 |
|
| 71 |
to.assertResult(2); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void ambIterableWithFirstFires() { |
| 76 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 77 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 78 |
|
| 79 |
List<Single<Integer>> singles = Arrays.asList(pp1.single(-99), pp2.single(-99)); |
| 80 |
TestObserver<Integer> to = Single.amb(singles).test(); |
| 81 |
|
| 82 |
assertTrue(pp1.hasSubscribers()); |
| 83 |
assertTrue(pp2.hasSubscribers()); |
| 84 |
|
| 85 |
pp1.onNext(1); |
| 86 |
pp1.onComplete(); |
| 87 |
|
| 88 |
assertFalse(pp1.hasSubscribers()); |
| 89 |
assertFalse(pp2.hasSubscribers()); |
| 90 |
|
| 91 |
to.assertResult(1); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void ambIterableWithSecondFires() { |
| 97 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 98 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 99 |
|
| 100 |
List<Single<Integer>> singles = Arrays.asList(pp1.single(-99), pp2.single(-99)); |
| 101 |
TestObserver<Integer> to = Single.amb(singles).test(); |
| 102 |
|
| 103 |
assertTrue(pp1.hasSubscribers()); |
| 104 |
assertTrue(pp2.hasSubscribers()); |
| 105 |
|
| 106 |
pp2.onNext(2); |
| 107 |
pp2.onComplete(); |
| 108 |
|
| 109 |
assertFalse(pp1.hasSubscribers()); |
| 110 |
assertFalse(pp2.hasSubscribers()); |
| 111 |
|
| 112 |
to.assertResult(2); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void ambArrayEmpty() { |
| 117 |
Single.ambArray() |
| 118 |
.test() |
| 119 |
.assertFailure(NoSuchElementException.class); |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void ambSingleSource() { |
| 124 |
assertSame(Single.never(), Single.ambArray(Single.never())); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void error() { |
| 129 |
Single.ambArray(Single.error(new TestException()), Single.just(1)) |
| 130 |
.test() |
| 131 |
.assertFailure(TestException.class); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void nullSourceSuccessRace() { |
| 136 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 137 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 138 |
|
| 139 |
try { |
| 140 |
|
| 141 |
final Subject<Integer> ps = ReplaySubject.create(); |
| 142 |
ps.onNext(1); |
| 143 |
|
| 144 |
final Single<Integer> source = Single.ambArray(ps.singleOrError(), Single.<Integer>never(), Single.<Integer>never(), null); |
| 145 |
|
| 146 |
Runnable r1 = new Runnable() { |
| 147 |
@Override |
| 148 |
public void run() { |
| 149 |
source.test(); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
Runnable r2 = new Runnable() { |
| 154 |
@Override |
| 155 |
public void run() { |
| 156 |
ps.onComplete(); |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
TestHelper.race(r1, r2); |
| 161 |
|
| 162 |
if (!errors.isEmpty()) { |
| 163 |
TestHelper.assertError(errors, 0, NullPointerException.class); |
| 164 |
} |
| 165 |
} finally { |
| 166 |
RxJavaPlugins.reset(); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void multipleErrorRace() { |
| 173 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 174 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 175 |
|
| 176 |
try { |
| 177 |
|
| 178 |
final Subject<Integer> ps1 = PublishSubject.create(); |
| 179 |
final Subject<Integer> ps2 = PublishSubject.create(); |
| 180 |
|
| 181 |
Single.ambArray(ps1.singleOrError(), ps2.singleOrError()).test(); |
| 182 |
|
| 183 |
final TestException ex = new TestException(); |
| 184 |
|
| 185 |
Runnable r1 = new Runnable() { |
| 186 |
@Override |
| 187 |
public void run() { |
| 188 |
ps1.onError(ex); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
Runnable r2 = new Runnable() { |
| 193 |
@Override |
| 194 |
public void run() { |
| 195 |
ps2.onError(ex); |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
TestHelper.race(r1, r2); |
| 200 |
|
| 201 |
if (!errors.isEmpty()) { |
| 202 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 203 |
} |
| 204 |
} finally { |
| 205 |
RxJavaPlugins.reset(); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void successErrorRace() { |
| 212 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 213 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 214 |
|
| 215 |
try { |
| 216 |
|
| 217 |
final Subject<Integer> ps1 = PublishSubject.create(); |
| 218 |
final Subject<Integer> ps2 = PublishSubject.create(); |
| 219 |
|
| 220 |
Single.ambArray(ps1.singleOrError(), ps2.singleOrError()).test(); |
| 221 |
|
| 222 |
final TestException ex = new TestException(); |
| 223 |
|
| 224 |
Runnable r1 = new Runnable() { |
| 225 |
@Override |
| 226 |
public void run() { |
| 227 |
ps1.onNext(1); |
| 228 |
ps1.onComplete(); |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
Runnable r2 = new Runnable() { |
| 233 |
@Override |
| 234 |
public void run() { |
| 235 |
ps2.onError(ex); |
| 236 |
} |
| 237 |
}; |
| 238 |
|
| 239 |
TestHelper.race(r1, r2); |
| 240 |
|
| 241 |
if (!errors.isEmpty()) { |
| 242 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 243 |
} |
| 244 |
} finally { |
| 245 |
RxJavaPlugins.reset(); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void manySources() { |
| 252 |
Single<?>[] sources = new Single[32]; |
| 253 |
Arrays.fill(sources, Single.never()); |
| 254 |
sources[31] = Single.just(31); |
| 255 |
|
| 256 |
Single.amb(Arrays.asList(sources)) |
| 257 |
.test() |
| 258 |
.assertResult(31); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void ambWithOrder() { |
| 263 |
Single<Integer> error = Single.error(new RuntimeException()); |
| 264 |
Single.just(1).ambWith(error).test().assertValue(1); |
| 265 |
} |
| 266 |
|
| 267 |
@Test |
| 268 |
public void ambIterableOrder() { |
| 269 |
Single<Integer> error = Single.error(new RuntimeException()); |
| 270 |
Single.amb(Arrays.asList(Single.just(1), error)).test().assertValue(1); |
| 271 |
} |
| 272 |
|
| 273 |
@Test |
| 274 |
public void ambArrayOrder() { |
| 275 |
Single<Integer> error = Single.error(new RuntimeException()); |
| 276 |
Single.ambArray(Single.just(1), error).test().assertValue(1); |
| 277 |
} |
| 278 |
|
| 279 |
@Test |
| 280 |
public void noWinnerSuccessDispose() throws Exception { |
| 281 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 282 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 283 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 284 |
|
| 285 |
Single.ambArray( |
| 286 |
Single.just(1) |
| 287 |
.subscribeOn(Schedulers.single()) |
| 288 |
.observeOn(Schedulers.computation()), |
| 289 |
Single.never() |
| 290 |
) |
| 291 |
.subscribe(new BiConsumer<Object, Throwable>() { |
| 292 |
@Override |
| 293 |
public void accept(Object v, Throwable e) throws Exception { |
| 294 |
assertNotNull(v); |
| 295 |
assertNull(e); |
| 296 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 297 |
cdl.countDown(); |
| 298 |
} |
| 299 |
}); |
| 300 |
|
| 301 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 302 |
assertFalse("Interrupted!", interrupted.get()); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
@Test |
| 307 |
public void noWinnerErrorDispose() throws Exception { |
| 308 |
final TestException ex = new TestException(); |
| 309 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 310 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 311 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 312 |
|
| 313 |
Single.ambArray( |
| 314 |
Single.error(ex) |
| 315 |
.subscribeOn(Schedulers.single()) |
| 316 |
.observeOn(Schedulers.computation()), |
| 317 |
Single.never() |
| 318 |
) |
| 319 |
.subscribe(new BiConsumer<Object, Throwable>() { |
| 320 |
@Override |
| 321 |
public void accept(Object v, Throwable e) throws Exception { |
| 322 |
assertNull(v); |
| 323 |
assertNotNull(e); |
| 324 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 325 |
cdl.countDown(); |
| 326 |
} |
| 327 |
}); |
| 328 |
|
| 329 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 330 |
assertFalse("Interrupted!", interrupted.get()); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
@Test |
| 335 |
public void singleSourcesInIterable() { |
| 336 |
SingleSource<Integer> source = new SingleSource<Integer>() { |
| 337 |
@Override |
| 338 |
public void subscribe(SingleObserver<? super Integer> observer) { |
| 339 |
Single.just(1).subscribe(observer); |
| 340 |
} |
| 341 |
}; |
| 342 |
|
| 343 |
Single.amb(Arrays.asList(source, source)) |
| 344 |
.test() |
| 345 |
.assertResult(1); |
| 346 |
} |
| 347 |
} |