|
| 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 static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +import java.util.concurrent.Flow.Subscription; |
| 19 | +import java.util.concurrent.atomic.*; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.parallel.Isolated; |
| 23 | + |
| 24 | +import io.reactivex.rxjava4.core.RxJavaTest; |
| 25 | +import io.reactivex.rxjava4.testsupport.TestHelper; |
| 26 | + |
| 27 | +@Isolated |
| 28 | +public class SubscriptionHelperIsolatedTest extends RxJavaTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void cancelRace() { |
| 32 | + for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 33 | + final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 34 | + |
| 35 | + Runnable r = () -> SubscriptionHelper.cancel(atomicSubscription); |
| 36 | + |
| 37 | + TestHelper.race(r, r); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void setRace() { |
| 43 | + for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 44 | + final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 45 | + |
| 46 | + final BooleanSubscription bs1 = new BooleanSubscription(); |
| 47 | + final BooleanSubscription bs2 = new BooleanSubscription(); |
| 48 | + |
| 49 | + Runnable r1 = () -> SubscriptionHelper.set(atomicSubscription, bs1); |
| 50 | + |
| 51 | + Runnable r2 = () -> SubscriptionHelper.set(atomicSubscription, bs2); |
| 52 | + |
| 53 | + TestHelper.race(r1, r2); |
| 54 | + |
| 55 | + assertTrue(bs1.isCancelled() ^ bs2.isCancelled()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void replaceRace() { |
| 61 | + for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 62 | + final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 63 | + |
| 64 | + final BooleanSubscription bs1 = new BooleanSubscription(); |
| 65 | + final BooleanSubscription bs2 = new BooleanSubscription(); |
| 66 | + |
| 67 | + Runnable r1 = () -> SubscriptionHelper.replace(atomicSubscription, bs1); |
| 68 | + |
| 69 | + Runnable r2 = () -> SubscriptionHelper.replace(atomicSubscription, bs2); |
| 70 | + |
| 71 | + TestHelper.race(r1, r2); |
| 72 | + |
| 73 | + assertFalse(bs1.isCancelled()); |
| 74 | + assertFalse(bs2.isCancelled()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void deferredRace() { |
| 80 | + for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 81 | + final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 82 | + final AtomicLong r = new AtomicLong(); |
| 83 | + |
| 84 | + final AtomicLong q = new AtomicLong(); |
| 85 | + |
| 86 | + final Subscription a = new Subscription() /* NFI */ { |
| 87 | + @Override |
| 88 | + public void request(long n) { |
| 89 | + q.addAndGet(n); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void cancel() { |
| 94 | + |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + Runnable r1 = () -> SubscriptionHelper.deferredSetOnce(atomicSubscription, r, a); |
| 99 | + |
| 100 | + Runnable r2 = () -> SubscriptionHelper.deferredRequest(atomicSubscription, r, 1); |
| 101 | + |
| 102 | + TestHelper.race(r1, r2); |
| 103 | + |
| 104 | + assertSame(a, atomicSubscription.get()); |
| 105 | + assertEquals(1, q.get()); |
| 106 | + assertEquals(0, r.get()); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments