| 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.processors; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 27 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 28 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 29 |
import io.reactivex.rxjava3.testsupport.*; |
| 30 |
|
| 31 |
public class SerializedProcessorTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void basic() { |
| 35 |
SerializedProcessor<String> processor = new SerializedProcessor<>(PublishProcessor.<String>create()); |
| 36 |
TestSubscriber<String> ts = new TestSubscriber<>(); |
| 37 |
processor.subscribe(ts); |
| 38 |
processor.onNext("hello"); |
| 39 |
processor.onComplete(); |
| 40 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 41 |
ts.assertValue("hello"); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void asyncSubjectValueRelay() { |
| 46 |
AsyncProcessor<Integer> async = AsyncProcessor.create(); |
| 47 |
async.onNext(1); |
| 48 |
async.onComplete(); |
| 49 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 50 |
|
| 51 |
assertFalse(serial.hasSubscribers()); |
| 52 |
assertTrue(serial.hasComplete()); |
| 53 |
assertFalse(serial.hasThrowable()); |
| 54 |
assertNull(serial.getThrowable()); |
| 55 |
assertEquals((Integer)1, async.getValue()); |
| 56 |
assertTrue(async.hasValue()); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void asyncSubjectValueEmpty() { |
| 61 |
AsyncProcessor<Integer> async = AsyncProcessor.create(); |
| 62 |
async.onComplete(); |
| 63 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 64 |
|
| 65 |
assertFalse(serial.hasSubscribers()); |
| 66 |
assertTrue(serial.hasComplete()); |
| 67 |
assertFalse(serial.hasThrowable()); |
| 68 |
assertNull(serial.getThrowable()); |
| 69 |
assertNull(async.getValue()); |
| 70 |
assertFalse(async.hasValue()); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void asyncSubjectValueError() { |
| 75 |
AsyncProcessor<Integer> async = AsyncProcessor.create(); |
| 76 |
TestException te = new TestException(); |
| 77 |
async.onError(te); |
| 78 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 79 |
|
| 80 |
assertFalse(serial.hasSubscribers()); |
| 81 |
assertFalse(serial.hasComplete()); |
| 82 |
assertTrue(serial.hasThrowable()); |
| 83 |
assertSame(te, serial.getThrowable()); |
| 84 |
assertNull(async.getValue()); |
| 85 |
assertFalse(async.hasValue()); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void publishSubjectValueRelay() { |
| 90 |
PublishProcessor<Integer> async = PublishProcessor.create(); |
| 91 |
async.onNext(1); |
| 92 |
async.onComplete(); |
| 93 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 94 |
|
| 95 |
assertFalse(serial.hasSubscribers()); |
| 96 |
assertTrue(serial.hasComplete()); |
| 97 |
assertFalse(serial.hasThrowable()); |
| 98 |
assertNull(serial.getThrowable()); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void publishSubjectValueEmpty() { |
| 103 |
PublishProcessor<Integer> async = PublishProcessor.create(); |
| 104 |
async.onComplete(); |
| 105 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 106 |
|
| 107 |
assertFalse(serial.hasSubscribers()); |
| 108 |
assertTrue(serial.hasComplete()); |
| 109 |
assertFalse(serial.hasThrowable()); |
| 110 |
assertNull(serial.getThrowable()); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void publishSubjectValueError() { |
| 115 |
PublishProcessor<Integer> async = PublishProcessor.create(); |
| 116 |
TestException te = new TestException(); |
| 117 |
async.onError(te); |
| 118 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 119 |
|
| 120 |
assertFalse(serial.hasSubscribers()); |
| 121 |
assertFalse(serial.hasComplete()); |
| 122 |
assertTrue(serial.hasThrowable()); |
| 123 |
assertSame(te, serial.getThrowable()); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void behaviorSubjectValueRelay() { |
| 128 |
BehaviorProcessor<Integer> async = BehaviorProcessor.create(); |
| 129 |
async.onNext(1); |
| 130 |
async.onComplete(); |
| 131 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 132 |
|
| 133 |
assertFalse(serial.hasSubscribers()); |
| 134 |
assertTrue(serial.hasComplete()); |
| 135 |
assertFalse(serial.hasThrowable()); |
| 136 |
assertNull(serial.getThrowable()); |
| 137 |
assertNull(async.getValue()); |
| 138 |
assertFalse(async.hasValue()); |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void behaviorSubjectValueRelayIncomplete() { |
| 143 |
BehaviorProcessor<Integer> async = BehaviorProcessor.create(); |
| 144 |
async.onNext(1); |
| 145 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 146 |
|
| 147 |
assertFalse(serial.hasSubscribers()); |
| 148 |
assertFalse(serial.hasComplete()); |
| 149 |
assertFalse(serial.hasThrowable()); |
| 150 |
assertNull(serial.getThrowable()); |
| 151 |
assertEquals((Integer)1, async.getValue()); |
| 152 |
assertTrue(async.hasValue()); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void behaviorSubjectIncompleteEmpty() { |
| 157 |
BehaviorProcessor<Integer> async = BehaviorProcessor.create(); |
| 158 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 159 |
|
| 160 |
assertFalse(serial.hasSubscribers()); |
| 161 |
assertFalse(serial.hasComplete()); |
| 162 |
assertFalse(serial.hasThrowable()); |
| 163 |
assertNull(serial.getThrowable()); |
| 164 |
assertNull(async.getValue()); |
| 165 |
assertFalse(async.hasValue()); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void behaviorSubjectEmpty() { |
| 170 |
BehaviorProcessor<Integer> async = BehaviorProcessor.create(); |
| 171 |
async.onComplete(); |
| 172 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 173 |
|
| 174 |
assertFalse(serial.hasSubscribers()); |
| 175 |
assertTrue(serial.hasComplete()); |
| 176 |
assertFalse(serial.hasThrowable()); |
| 177 |
assertNull(serial.getThrowable()); |
| 178 |
assertNull(async.getValue()); |
| 179 |
assertFalse(async.hasValue()); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void behaviorSubjectError() { |
| 184 |
BehaviorProcessor<Integer> async = BehaviorProcessor.create(); |
| 185 |
TestException te = new TestException(); |
| 186 |
async.onError(te); |
| 187 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 188 |
|
| 189 |
assertFalse(serial.hasSubscribers()); |
| 190 |
assertFalse(serial.hasComplete()); |
| 191 |
assertTrue(serial.hasThrowable()); |
| 192 |
assertSame(te, serial.getThrowable()); |
| 193 |
assertNull(async.getValue()); |
| 194 |
assertFalse(async.hasValue()); |
| 195 |
} |
| 196 |
|
| 197 |
@Test |
| 198 |
public void replaySubjectValueRelay() { |
| 199 |
ReplayProcessor<Integer> async = ReplayProcessor.create(); |
| 200 |
async.onNext(1); |
| 201 |
async.onComplete(); |
| 202 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 203 |
|
| 204 |
assertFalse(serial.hasSubscribers()); |
| 205 |
assertTrue(serial.hasComplete()); |
| 206 |
assertFalse(serial.hasThrowable()); |
| 207 |
assertNull(serial.getThrowable()); |
| 208 |
assertEquals((Integer)1, async.getValue()); |
| 209 |
assertTrue(async.hasValue()); |
| 210 |
assertArrayEquals(new Object[] { 1 }, async.getValues()); |
| 211 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[0])); |
| 212 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[] { 0 })); |
| 213 |
assertArrayEquals(new Integer[] { 1, null }, async.getValues(new Integer[] { 0, 0 })); |
| 214 |
} |
| 215 |
|
| 216 |
@Test |
| 217 |
public void replaySubjectValueRelayIncomplete() { |
| 218 |
ReplayProcessor<Integer> async = ReplayProcessor.create(); |
| 219 |
async.onNext(1); |
| 220 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 221 |
|
| 222 |
assertFalse(serial.hasSubscribers()); |
| 223 |
assertFalse(serial.hasComplete()); |
| 224 |
assertFalse(serial.hasThrowable()); |
| 225 |
assertNull(serial.getThrowable()); |
| 226 |
assertEquals((Integer)1, async.getValue()); |
| 227 |
assertTrue(async.hasValue()); |
| 228 |
assertArrayEquals(new Object[] { 1 }, async.getValues()); |
| 229 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[0])); |
| 230 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[] { 0 })); |
| 231 |
assertArrayEquals(new Integer[] { 1, null }, async.getValues(new Integer[] { 0, 0 })); |
| 232 |
} |
| 233 |
|
| 234 |
@Test |
| 235 |
public void replaySubjectValueRelayBounded() { |
| 236 |
ReplayProcessor<Integer> async = ReplayProcessor.createWithSize(1); |
| 237 |
async.onNext(0); |
| 238 |
async.onNext(1); |
| 239 |
async.onComplete(); |
| 240 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 241 |
|
| 242 |
assertFalse(serial.hasSubscribers()); |
| 243 |
assertTrue(serial.hasComplete()); |
| 244 |
assertFalse(serial.hasThrowable()); |
| 245 |
assertNull(serial.getThrowable()); |
| 246 |
assertEquals((Integer)1, async.getValue()); |
| 247 |
assertTrue(async.hasValue()); |
| 248 |
assertArrayEquals(new Object[] { 1 }, async.getValues()); |
| 249 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[0])); |
| 250 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[] { 0 })); |
| 251 |
assertArrayEquals(new Integer[] { 1, null }, async.getValues(new Integer[] { 0, 0 })); |
| 252 |
} |
| 253 |
|
| 254 |
@Test |
| 255 |
public void replaySubjectValueRelayBoundedIncomplete() { |
| 256 |
ReplayProcessor<Integer> async = ReplayProcessor.createWithSize(1); |
| 257 |
async.onNext(0); |
| 258 |
async.onNext(1); |
| 259 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 260 |
|
| 261 |
assertFalse(serial.hasSubscribers()); |
| 262 |
assertFalse(serial.hasComplete()); |
| 263 |
assertFalse(serial.hasThrowable()); |
| 264 |
assertNull(serial.getThrowable()); |
| 265 |
assertEquals((Integer)1, async.getValue()); |
| 266 |
assertTrue(async.hasValue()); |
| 267 |
assertArrayEquals(new Object[] { 1 }, async.getValues()); |
| 268 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[0])); |
| 269 |
assertArrayEquals(new Integer[] { 1 }, async.getValues(new Integer[] { 0 })); |
| 270 |
assertArrayEquals(new Integer[] { 1, null }, async.getValues(new Integer[] { 0, 0 })); |
| 271 |
} |
| 272 |
|
| 273 |
@Test |
| 274 |
public void replaySubjectValueRelayBoundedEmptyIncomplete() { |
| 275 |
ReplayProcessor<Integer> async = ReplayProcessor.createWithSize(1); |
| 276 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 277 |
|
| 278 |
assertFalse(serial.hasSubscribers()); |
| 279 |
assertFalse(serial.hasComplete()); |
| 280 |
assertFalse(serial.hasThrowable()); |
| 281 |
assertNull(serial.getThrowable()); |
| 282 |
assertNull(async.getValue()); |
| 283 |
assertFalse(async.hasValue()); |
| 284 |
assertArrayEquals(new Object[] { }, async.getValues()); |
| 285 |
assertArrayEquals(new Integer[] { }, async.getValues(new Integer[0])); |
| 286 |
assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 })); |
| 287 |
assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 })); |
| 288 |
} |
| 289 |
|
| 290 |
@Test |
| 291 |
public void replaySubjectValueRelayEmptyIncomplete() { |
| 292 |
ReplayProcessor<Integer> async = ReplayProcessor.create(); |
| 293 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 294 |
|
| 295 |
assertFalse(serial.hasSubscribers()); |
| 296 |
assertFalse(serial.hasComplete()); |
| 297 |
assertFalse(serial.hasThrowable()); |
| 298 |
assertNull(serial.getThrowable()); |
| 299 |
assertNull(async.getValue()); |
| 300 |
assertFalse(async.hasValue()); |
| 301 |
assertArrayEquals(new Object[] { }, async.getValues()); |
| 302 |
assertArrayEquals(new Integer[] { }, async.getValues(new Integer[0])); |
| 303 |
assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 })); |
| 304 |
assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 })); |
| 305 |
} |
| 306 |
|
| 307 |
@Test |
| 308 |
public void replaySubjectEmpty() { |
| 309 |
ReplayProcessor<Integer> async = ReplayProcessor.create(); |
| 310 |
async.onComplete(); |
| 311 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 312 |
|
| 313 |
assertFalse(serial.hasSubscribers()); |
| 314 |
assertTrue(serial.hasComplete()); |
| 315 |
assertFalse(serial.hasThrowable()); |
| 316 |
assertNull(serial.getThrowable()); |
| 317 |
assertNull(async.getValue()); |
| 318 |
assertFalse(async.hasValue()); |
| 319 |
assertArrayEquals(new Object[] { }, async.getValues()); |
| 320 |
assertArrayEquals(new Integer[] { }, async.getValues(new Integer[0])); |
| 321 |
assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 })); |
| 322 |
assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 })); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void replaySubjectError() { |
| 327 |
ReplayProcessor<Integer> async = ReplayProcessor.create(); |
| 328 |
TestException te = new TestException(); |
| 329 |
async.onError(te); |
| 330 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 331 |
|
| 332 |
assertFalse(serial.hasSubscribers()); |
| 333 |
assertFalse(serial.hasComplete()); |
| 334 |
assertTrue(serial.hasThrowable()); |
| 335 |
assertSame(te, serial.getThrowable()); |
| 336 |
assertNull(async.getValue()); |
| 337 |
assertFalse(async.hasValue()); |
| 338 |
assertArrayEquals(new Object[] { }, async.getValues()); |
| 339 |
assertArrayEquals(new Integer[] { }, async.getValues(new Integer[0])); |
| 340 |
assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 })); |
| 341 |
assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 })); |
| 342 |
} |
| 343 |
|
| 344 |
@Test |
| 345 |
public void replaySubjectBoundedEmpty() { |
| 346 |
ReplayProcessor<Integer> async = ReplayProcessor.createWithSize(1); |
| 347 |
async.onComplete(); |
| 348 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 349 |
|
| 350 |
assertFalse(serial.hasSubscribers()); |
| 351 |
assertTrue(serial.hasComplete()); |
| 352 |
assertFalse(serial.hasThrowable()); |
| 353 |
assertNull(serial.getThrowable()); |
| 354 |
assertNull(async.getValue()); |
| 355 |
assertFalse(async.hasValue()); |
| 356 |
assertArrayEquals(new Object[] { }, async.getValues()); |
| 357 |
assertArrayEquals(new Integer[] { }, async.getValues(new Integer[0])); |
| 358 |
assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 })); |
| 359 |
assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 })); |
| 360 |
} |
| 361 |
|
| 362 |
@Test |
| 363 |
public void replaySubjectBoundedError() { |
| 364 |
ReplayProcessor<Integer> async = ReplayProcessor.createWithSize(1); |
| 365 |
TestException te = new TestException(); |
| 366 |
async.onError(te); |
| 367 |
FlowableProcessor<Integer> serial = async.toSerialized(); |
| 368 |
|
| 369 |
assertFalse(serial.hasSubscribers()); |
| 370 |
assertFalse(serial.hasComplete()); |
| 371 |
assertTrue(serial.hasThrowable()); |
| 372 |
assertSame(te, serial.getThrowable()); |
| 373 |
assertNull(async.getValue()); |
| 374 |
assertFalse(async.hasValue()); |
| 375 |
assertArrayEquals(new Object[] { }, async.getValues()); |
| 376 |
assertArrayEquals(new Integer[] { }, async.getValues(new Integer[0])); |
| 377 |
assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 })); |
| 378 |
assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 })); |
| 379 |
} |
| 380 |
|
| 381 |
@Test |
| 382 |
public void dontWrapSerializedSubjectAgain() { |
| 383 |
PublishProcessor<Object> s = PublishProcessor.create(); |
| 384 |
FlowableProcessor<Object> s1 = s.toSerialized(); |
| 385 |
FlowableProcessor<Object> s2 = s1.toSerialized(); |
| 386 |
assertSame(s1, s2); |
| 387 |
} |
| 388 |
|
| 389 |
@Test |
| 390 |
public void normal() { |
| 391 |
FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 392 |
|
| 393 |
TestSubscriber<Integer> ts = s.test(); |
| 394 |
|
| 395 |
Flowable.range(1, 10).subscribe(s); |
| 396 |
|
| 397 |
ts.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 398 |
|
| 399 |
assertFalse(s.hasSubscribers()); |
| 400 |
|
| 401 |
s.onNext(11); |
| 402 |
|
| 403 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 404 |
try { |
| 405 |
s.onError(new TestException()); |
| 406 |
|
| 407 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 408 |
} finally { |
| 409 |
RxJavaPlugins.reset(); |
| 410 |
} |
| 411 |
s.onComplete(); |
| 412 |
|
| 413 |
BooleanSubscription bs = new BooleanSubscription(); |
| 414 |
s.onSubscribe(bs); |
| 415 |
assertTrue(bs.isCancelled()); |
| 416 |
} |
| 417 |
|
| 418 |
@Test |
| 419 |
public void onNextOnNextRace() { |
| 420 |
Set<Integer> expectedSet = new HashSet<>(Arrays.asList(1, 2)); |
| 421 |
|
| 422 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 423 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 424 |
|
| 425 |
TestSubscriberEx<Integer> ts = s.to(TestHelper.<Integer>testConsumer()); |
| 426 |
|
| 427 |
Runnable r1 = new Runnable() { |
| 428 |
@Override |
| 429 |
public void run() { |
| 430 |
s.onNext(1); |
| 431 |
} |
| 432 |
}; |
| 433 |
|
| 434 |
Runnable r2 = new Runnable() { |
| 435 |
@Override |
| 436 |
public void run() { |
| 437 |
s.onNext(2); |
| 438 |
} |
| 439 |
}; |
| 440 |
|
| 441 |
TestHelper.race(r1, r2); |
| 442 |
|
| 443 |
ts.assertSubscribed() |
| 444 |
.assertNoErrors() |
| 445 |
.assertNotComplete() |
| 446 |
.assertValueCount(2) |
| 447 |
; |
| 448 |
|
| 449 |
Set<Integer> actualSet = new HashSet<>(ts.values()); |
| 450 |
assertEquals("" + actualSet, expectedSet, actualSet); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
@Test |
| 455 |
public void onNextOnErrorRace() { |
| 456 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 457 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 458 |
|
| 459 |
TestSubscriber<Integer> ts = s.test(); |
| 460 |
|
| 461 |
final TestException ex = new TestException(); |
| 462 |
|
| 463 |
Runnable r1 = new Runnable() { |
| 464 |
@Override |
| 465 |
public void run() { |
| 466 |
s.onNext(1); |
| 467 |
} |
| 468 |
}; |
| 469 |
|
| 470 |
Runnable r2 = new Runnable() { |
| 471 |
@Override |
| 472 |
public void run() { |
| 473 |
s.onError(ex); |
| 474 |
} |
| 475 |
}; |
| 476 |
|
| 477 |
TestHelper.race(r1, r2); |
| 478 |
|
| 479 |
ts.assertError(ex).assertNotComplete(); |
| 480 |
|
| 481 |
if (ts.values().size() != 0) { |
| 482 |
ts.assertValue(1); |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
@Test |
| 488 |
public void onNextOnCompleteRace() { |
| 489 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 490 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 491 |
|
| 492 |
TestSubscriber<Integer> ts = s.test(); |
| 493 |
|
| 494 |
Runnable r1 = new Runnable() { |
| 495 |
@Override |
| 496 |
public void run() { |
| 497 |
s.onNext(1); |
| 498 |
} |
| 499 |
}; |
| 500 |
|
| 501 |
Runnable r2 = new Runnable() { |
| 502 |
@Override |
| 503 |
public void run() { |
| 504 |
s.onComplete(); |
| 505 |
} |
| 506 |
}; |
| 507 |
|
| 508 |
TestHelper.race(r1, r2); |
| 509 |
|
| 510 |
ts.assertComplete().assertNoErrors(); |
| 511 |
|
| 512 |
if (ts.values().size() != 0) { |
| 513 |
ts.assertValue(1); |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
@Test |
| 519 |
public void onNextOnSubscribeRace() { |
| 520 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 521 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 522 |
|
| 523 |
TestSubscriber<Integer> ts = s.test(); |
| 524 |
|
| 525 |
final BooleanSubscription bs = new BooleanSubscription(); |
| 526 |
|
| 527 |
Runnable r1 = new Runnable() { |
| 528 |
@Override |
| 529 |
public void run() { |
| 530 |
s.onNext(1); |
| 531 |
} |
| 532 |
}; |
| 533 |
|
| 534 |
Runnable r2 = new Runnable() { |
| 535 |
@Override |
| 536 |
public void run() { |
| 537 |
s.onSubscribe(bs); |
| 538 |
} |
| 539 |
}; |
| 540 |
|
| 541 |
TestHelper.race(r1, r2); |
| 542 |
|
| 543 |
ts.assertValue(1).assertNotComplete().assertNoErrors(); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
@Test |
| 548 |
public void onCompleteOnSubscribeRace() { |
| 549 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 550 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 551 |
|
| 552 |
TestSubscriber<Integer> ts = s.test(); |
| 553 |
|
| 554 |
final BooleanSubscription bs = new BooleanSubscription(); |
| 555 |
|
| 556 |
Runnable r1 = new Runnable() { |
| 557 |
@Override |
| 558 |
public void run() { |
| 559 |
s.onComplete(); |
| 560 |
} |
| 561 |
}; |
| 562 |
|
| 563 |
Runnable r2 = new Runnable() { |
| 564 |
@Override |
| 565 |
public void run() { |
| 566 |
s.onSubscribe(bs); |
| 567 |
} |
| 568 |
}; |
| 569 |
|
| 570 |
TestHelper.race(r1, r2); |
| 571 |
|
| 572 |
ts.assertResult(); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
@Test |
| 577 |
public void onCompleteOnCompleteRace() { |
| 578 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 579 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 580 |
|
| 581 |
TestSubscriber<Integer> ts = s.test(); |
| 582 |
|
| 583 |
Runnable r1 = new Runnable() { |
| 584 |
@Override |
| 585 |
public void run() { |
| 586 |
s.onComplete(); |
| 587 |
} |
| 588 |
}; |
| 589 |
|
| 590 |
Runnable r2 = new Runnable() { |
| 591 |
@Override |
| 592 |
public void run() { |
| 593 |
s.onComplete(); |
| 594 |
} |
| 595 |
}; |
| 596 |
|
| 597 |
TestHelper.race(r1, r2); |
| 598 |
|
| 599 |
ts.assertResult(); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
@Test |
| 604 |
public void onErrorOnErrorRace() { |
| 605 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 606 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 607 |
|
| 608 |
TestSubscriber<Integer> ts = s.test(); |
| 609 |
|
| 610 |
final TestException ex = new TestException(); |
| 611 |
|
| 612 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 613 |
try { |
| 614 |
Runnable r1 = new Runnable() { |
| 615 |
@Override |
| 616 |
public void run() { |
| 617 |
s.onError(ex); |
| 618 |
} |
| 619 |
}; |
| 620 |
|
| 621 |
Runnable r2 = new Runnable() { |
| 622 |
@Override |
| 623 |
public void run() { |
| 624 |
s.onError(ex); |
| 625 |
} |
| 626 |
}; |
| 627 |
|
| 628 |
TestHelper.race(r1, r2); |
| 629 |
|
| 630 |
ts.assertFailure(TestException.class); |
| 631 |
|
| 632 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 633 |
} finally { |
| 634 |
RxJavaPlugins.reset(); |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
@Test |
| 640 |
public void onSubscribeOnSubscribeRace() { |
| 641 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 642 |
final FlowableProcessor<Integer> s = PublishProcessor.<Integer>create().toSerialized(); |
| 643 |
|
| 644 |
TestSubscriber<Integer> ts = s.test(); |
| 645 |
|
| 646 |
final BooleanSubscription bs1 = new BooleanSubscription(); |
| 647 |
final BooleanSubscription bs2 = new BooleanSubscription(); |
| 648 |
|
| 649 |
Runnable r1 = new Runnable() { |
| 650 |
@Override |
| 651 |
public void run() { |
| 652 |
s.onSubscribe(bs1); |
| 653 |
} |
| 654 |
}; |
| 655 |
|
| 656 |
Runnable r2 = new Runnable() { |
| 657 |
@Override |
| 658 |
public void run() { |
| 659 |
s.onSubscribe(bs2); |
| 660 |
} |
| 661 |
}; |
| 662 |
|
| 663 |
TestHelper.race(r1, r2); |
| 664 |
|
| 665 |
ts.assertEmpty(); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
@Test |
| 670 |
public void onErrorQueued() { |
| 671 |
FlowableProcessor<Integer> sp = PublishProcessor.<Integer>create().toSerialized(); |
| 672 |
|
| 673 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 674 |
@Override |
| 675 |
public void onNext(@NonNull Integer t) { |
| 676 |
super.onNext(t); |
| 677 |
if (t == 1) { |
| 678 |
sp.onNext(2); |
| 679 |
sp.onSubscribe(new BooleanSubscription()); |
| 680 |
sp.onError(new TestException()); |
| 681 |
} |
| 682 |
} |
| 683 |
}; |
| 684 |
|
| 685 |
sp.subscribe(ts); |
| 686 |
|
| 687 |
sp.onNext(1); |
| 688 |
|
| 689 |
ts.assertFailure(TestException.class, 1); // errors skip ahead |
| 690 |
} |
| 691 |
} |