Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/subscriptions/SubscriptionArbiterTest.java

raw
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.subscriptions;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.RxJavaTest;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24 import io.reactivex.rxjava3.testsupport.TestHelper;
25
26 public class SubscriptionArbiterTest extends RxJavaTest {
27
28 @Test
29 public void setSubscriptionMissed() {
30 SubscriptionArbiter sa = new SubscriptionArbiter(true);
31
32 sa.getAndIncrement();
33
34 BooleanSubscription bs1 = new BooleanSubscription();
35
36 sa.setSubscription(bs1);
37
38 BooleanSubscription bs2 = new BooleanSubscription();
39
40 sa.setSubscription(bs2);
41
42 assertTrue(bs1.isCancelled());
43
44 assertFalse(bs2.isCancelled());
45 }
46
47 @Test
48 public void invalidDeferredRequest() {
49 SubscriptionArbiter sa = new SubscriptionArbiter(true);
50
51 List<Throwable> errors = TestHelper.trackPluginErrors();
52 try {
53 sa.request(-99);
54
55 TestHelper.assertError(errors, 0, IllegalArgumentException.class, "n > 0 required but it was -99");
56 } finally {
57 RxJavaPlugins.reset();
58 }
59 }
60
61 @Test
62 public void unbounded() {
63 SubscriptionArbiter sa = new SubscriptionArbiter(true);
64
65 sa.request(Long.MAX_VALUE);
66
67 assertEquals(Long.MAX_VALUE, sa.requested);
68
69 assertTrue(sa.isUnbounded());
70
71 sa.unbounded = false;
72
73 sa.request(Long.MAX_VALUE);
74
75 assertEquals(Long.MAX_VALUE, sa.requested);
76
77 sa.produced(1);
78
79 assertEquals(Long.MAX_VALUE, sa.requested);
80
81 sa.unbounded = false;
82
83 sa.produced(Long.MAX_VALUE);
84
85 assertEquals(Long.MAX_VALUE, sa.requested);
86 }
87
88 @Test
89 public void cancelled() {
90 SubscriptionArbiter sa = new SubscriptionArbiter(true);
91 sa.cancelled = true;
92
93 BooleanSubscription bs1 = new BooleanSubscription();
94
95 sa.missedSubscription.set(bs1);
96
97 sa.getAndIncrement();
98
99 sa.drainLoop();
100
101 assertTrue(bs1.isCancelled());
102 }
103
104 @Test
105 public void drainUnbounded() {
106 SubscriptionArbiter sa = new SubscriptionArbiter(true);
107
108 sa.getAndIncrement();
109
110 sa.requested = Long.MAX_VALUE;
111
112 sa.drainLoop();
113 }
114
115 @Test
116 public void drainMissedRequested() {
117 SubscriptionArbiter sa = new SubscriptionArbiter(true);
118
119 sa.getAndIncrement();
120
121 sa.requested = 0;
122
123 sa.missedRequested.set(1);
124
125 sa.drainLoop();
126
127 assertEquals(1, sa.requested);
128 }
129
130 @Test
131 public void drainMissedRequestedProduced() {
132 SubscriptionArbiter sa = new SubscriptionArbiter(true);
133
134 sa.getAndIncrement();
135
136 sa.requested = 0;
137
138 sa.missedRequested.set(Long.MAX_VALUE);
139
140 sa.missedProduced.set(1);
141
142 sa.drainLoop();
143
144 assertEquals(Long.MAX_VALUE, sa.requested);
145 }
146
147 @Test
148 public void drainMissedRequestedMoreProduced() {
149 List<Throwable> errors = TestHelper.trackPluginErrors();
150 try {
151 SubscriptionArbiter sa = new SubscriptionArbiter(true);
152
153 sa.getAndIncrement();
154
155 sa.requested = 0;
156
157 sa.missedRequested.set(1);
158
159 sa.missedProduced.set(2);
160
161 sa.drainLoop();
162
163 assertEquals(0, sa.requested);
164
165 TestHelper.assertError(errors, 0, IllegalStateException.class, "More produced than requested: -1");
166 } finally {
167 RxJavaPlugins.reset();
168 }
169 }
170
171 @Test
172 public void missedSubscriptionNoPrior() {
173 SubscriptionArbiter sa = new SubscriptionArbiter(true);
174
175 sa.getAndIncrement();
176
177 BooleanSubscription bs1 = new BooleanSubscription();
178
179 sa.missedSubscription.set(bs1);
180
181 sa.drainLoop();
182
183 assertSame(bs1, sa.actual);
184 }
185
186 @Test
187 public void noCancelFastPath() {
188 SubscriptionArbiter sa = new SubscriptionArbiter(false);
189
190 BooleanSubscription bs1 = new BooleanSubscription();
191 BooleanSubscription bs2 = new BooleanSubscription();
192
193 sa.setSubscription(bs1);
194 sa.setSubscription(bs2);
195
196 assertFalse(bs1.isCancelled());
197 assertFalse(bs2.isCancelled());
198 }
199
200 @Test
201 public void cancelFastPath() {
202 SubscriptionArbiter sa = new SubscriptionArbiter(true);
203
204 BooleanSubscription bs1 = new BooleanSubscription();
205 BooleanSubscription bs2 = new BooleanSubscription();
206
207 sa.setSubscription(bs1);
208 sa.setSubscription(bs2);
209
210 assertTrue(bs1.isCancelled());
211 assertFalse(bs2.isCancelled());
212 }
213
214 @Test
215 public void noCancelSlowPathReplace() {
216 SubscriptionArbiter sa = new SubscriptionArbiter(false);
217
218 BooleanSubscription bs1 = new BooleanSubscription();
219 BooleanSubscription bs2 = new BooleanSubscription();
220 BooleanSubscription bs3 = new BooleanSubscription();
221
222 sa.setSubscription(bs1);
223
224 sa.getAndIncrement();
225
226 sa.setSubscription(bs2);
227 sa.setSubscription(bs3);
228
229 sa.drainLoop();
230
231 assertFalse(bs1.isCancelled());
232 assertFalse(bs2.isCancelled());
233 assertFalse(bs3.isCancelled());
234 }
235
236 @Test
237 public void cancelSlowPathReplace() {
238 SubscriptionArbiter sa = new SubscriptionArbiter(true);
239
240 BooleanSubscription bs1 = new BooleanSubscription();
241 BooleanSubscription bs2 = new BooleanSubscription();
242 BooleanSubscription bs3 = new BooleanSubscription();
243
244 sa.setSubscription(bs1);
245
246 sa.getAndIncrement();
247
248 sa.setSubscription(bs2);
249 sa.setSubscription(bs3);
250
251 sa.drainLoop();
252
253 assertTrue(bs1.isCancelled());
254 assertTrue(bs2.isCancelled());
255 assertFalse(bs3.isCancelled());
256 }
257
258 @Test
259 public void noCancelSlowPath() {
260 SubscriptionArbiter sa = new SubscriptionArbiter(false);
261
262 BooleanSubscription bs1 = new BooleanSubscription();
263 BooleanSubscription bs2 = new BooleanSubscription();
264
265 sa.setSubscription(bs1);
266
267 sa.getAndIncrement();
268
269 sa.setSubscription(bs2);
270
271 sa.drainLoop();
272
273 assertFalse(bs1.isCancelled());
274 assertFalse(bs2.isCancelled());
275 }
276
277 @Test
278 public void cancelSlowPath() {
279 SubscriptionArbiter sa = new SubscriptionArbiter(true);
280
281 BooleanSubscription bs1 = new BooleanSubscription();
282 BooleanSubscription bs2 = new BooleanSubscription();
283
284 sa.setSubscription(bs1);
285
286 sa.getAndIncrement();
287
288 sa.setSubscription(bs2);
289
290 sa.drainLoop();
291
292 assertTrue(bs1.isCancelled());
293 assertFalse(bs2.isCancelled());
294 }
295
296 @Test
297 public void moreProducedViolationFastPath() {
298 List<Throwable> errors = TestHelper.trackPluginErrors();
299 try {
300 SubscriptionArbiter sa = new SubscriptionArbiter(true);
301
302 sa.produced(2);
303
304 assertEquals(0, sa.requested);
305
306 TestHelper.assertError(errors, 0, IllegalStateException.class, "More produced than requested: -2");
307 } finally {
308 RxJavaPlugins.reset();
309 }
310 }
311 }