Skip to content

Commit e31564b

Browse files
authored
4.x: Isolate SubscriptionHelper race tests (#8391)
1 parent e1346fe commit e31564b

2 files changed

Lines changed: 109 additions & 80 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

‎src/test/java/io/reactivex/rxjava4/internal/subscriptions/SubscriptionHelperTest.java‎

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -86,54 +86,6 @@ public void replace() {
8686
assertFalse(bs2.isCancelled());
8787
}
8888

89-
@Test
90-
public void cancelRace() {
91-
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
92-
final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>();
93-
94-
Runnable r = () -> SubscriptionHelper.cancel(atomicSubscription);
95-
96-
TestHelper.race(r, r);
97-
}
98-
}
99-
100-
@Test
101-
public void setRace() {
102-
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
103-
final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>();
104-
105-
final BooleanSubscription bs1 = new BooleanSubscription();
106-
final BooleanSubscription bs2 = new BooleanSubscription();
107-
108-
Runnable r1 = () -> SubscriptionHelper.set(atomicSubscription, bs1);
109-
110-
Runnable r2 = () -> SubscriptionHelper.set(atomicSubscription, bs2);
111-
112-
TestHelper.race(r1, r2);
113-
114-
assertTrue(bs1.isCancelled() ^ bs2.isCancelled());
115-
}
116-
}
117-
118-
@Test
119-
public void replaceRace() {
120-
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
121-
final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>();
122-
123-
final BooleanSubscription bs1 = new BooleanSubscription();
124-
final BooleanSubscription bs2 = new BooleanSubscription();
125-
126-
Runnable r1 = () -> SubscriptionHelper.replace(atomicSubscription, bs1);
127-
128-
Runnable r2 = () -> SubscriptionHelper.replace(atomicSubscription, bs2);
129-
130-
TestHelper.race(r1, r2);
131-
132-
assertFalse(bs1.isCancelled());
133-
assertFalse(bs2.isCancelled());
134-
}
135-
}
136-
13789
@Test
13890
public void cancelAndChange() {
13991
AtomicReference<Subscription> atomicSubscription = new AtomicReference<>();
@@ -168,38 +120,6 @@ public void invalidDeferredRequest() {
168120
}
169121
}
170122

171-
@Test
172-
public void deferredRace() {
173-
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
174-
final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>();
175-
final AtomicLong r = new AtomicLong();
176-
177-
final AtomicLong q = new AtomicLong();
178-
179-
final Subscription a = new Subscription() /* NFI */ {
180-
@Override
181-
public void request(long n) {
182-
q.addAndGet(n);
183-
}
184-
185-
@Override
186-
public void cancel() {
187-
188-
}
189-
};
190-
191-
Runnable r1 = () -> SubscriptionHelper.deferredSetOnce(atomicSubscription, r, a);
192-
193-
Runnable r2 = () -> SubscriptionHelper.deferredRequest(atomicSubscription, r, 1);
194-
195-
TestHelper.race(r1, r2);
196-
197-
assertSame(a, atomicSubscription.get());
198-
assertEquals(1, q.get());
199-
assertEquals(0, r.get());
200-
}
201-
}
202-
203123
@Test
204124
public void setOnceAndRequest() {
205125
AtomicReference<Subscription> ref = new AtomicReference<>();

0 commit comments

Comments
 (0)