|
| 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.rxjava4.internal.subscriptions; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.parallel.Isolated; |
| 18 | + |
| 19 | +import io.reactivex.rxjava4.core.RxJavaTest; |
| 20 | +import io.reactivex.rxjava4.subscribers.TestSubscriber; |
| 21 | +import io.reactivex.rxjava4.testsupport.TestHelper; |
| 22 | + |
| 23 | +@Isolated |
| 24 | +public class DeferredScalarSubscriptionIsolatedTest extends RxJavaTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + public void completeCancelRace() { |
| 28 | + for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 29 | + final DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(new TestSubscriber<>()); |
| 30 | + |
| 31 | + Runnable r1 = () -> ds.complete(1); |
| 32 | + |
| 33 | + Runnable r2 = ds::cancel; |
| 34 | + |
| 35 | + TestHelper.race(r1, r2); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void requestClearRace() { |
| 41 | + for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 42 | + TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 43 | + |
| 44 | + final DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(ts); |
| 45 | + ts.onSubscribe(ds); |
| 46 | + ds.complete(1); |
| 47 | + |
| 48 | + Runnable r1 = () -> ds.request(1); |
| 49 | + |
| 50 | + Runnable r2 = () -> ds.value = null; |
| 51 | + |
| 52 | + TestHelper.race(r1, r2); |
| 53 | + |
| 54 | + if (!ts.values().isEmpty()) { |
| 55 | + ts.assertValue(1); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void requestCancelRace() { |
| 62 | + for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 63 | + TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 64 | + |
| 65 | + final DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(ts); |
| 66 | + ts.onSubscribe(ds); |
| 67 | + ds.complete(1); |
| 68 | + |
| 69 | + Runnable r1 = () -> ds.request(1); |
| 70 | + |
| 71 | + Runnable r2 = ds::cancel; |
| 72 | + |
| 73 | + TestHelper.race(r1, r2); |
| 74 | + |
| 75 | + if (!ts.values().isEmpty()) { |
| 76 | + ts.assertValue(1); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments