| 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 |
import static org.mockito.ArgumentMatchers.anyLong; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.List; |
| 21 |
import java.util.concurrent.atomic.*; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.reactivestreams.Subscription; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 27 |
import io.reactivex.rxjava3.exceptions.ProtocolViolationException; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class SubscriptionHelperTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void checkEnum() { |
| 35 |
TestHelper.checkEnum(SubscriptionHelper.class); |
| 36 |
} |
| 37 |
|
| 38 |
@Test |
| 39 |
public void validateNullThrows() { |
| 40 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 41 |
try { |
| 42 |
SubscriptionHelper.validate(null, null); |
| 43 |
|
| 44 |
TestHelper.assertError(errors, 0, NullPointerException.class, "next is null"); |
| 45 |
} finally { |
| 46 |
RxJavaPlugins.reset(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void cancelNoOp() { |
| 52 |
SubscriptionHelper.CANCELLED.cancel(); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void set() { |
| 57 |
AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 58 |
|
| 59 |
BooleanSubscription bs1 = new BooleanSubscription(); |
| 60 |
|
| 61 |
assertTrue(SubscriptionHelper.set(atomicSubscription, bs1)); |
| 62 |
|
| 63 |
BooleanSubscription bs2 = new BooleanSubscription(); |
| 64 |
|
| 65 |
assertTrue(SubscriptionHelper.set(atomicSubscription, bs2)); |
| 66 |
|
| 67 |
assertTrue(bs1.isCancelled()); |
| 68 |
|
| 69 |
assertFalse(bs2.isCancelled()); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void replace() { |
| 74 |
AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 75 |
|
| 76 |
BooleanSubscription bs1 = new BooleanSubscription(); |
| 77 |
|
| 78 |
assertTrue(SubscriptionHelper.replace(atomicSubscription, bs1)); |
| 79 |
|
| 80 |
BooleanSubscription bs2 = new BooleanSubscription(); |
| 81 |
|
| 82 |
assertTrue(SubscriptionHelper.replace(atomicSubscription, bs2)); |
| 83 |
|
| 84 |
assertFalse(bs1.isCancelled()); |
| 85 |
|
| 86 |
assertFalse(bs2.isCancelled()); |
| 87 |
} |
| 88 |
|
| 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 = new Runnable() { |
| 95 |
@Override |
| 96 |
public void run() { |
| 97 |
SubscriptionHelper.cancel(atomicSubscription); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
TestHelper.race(r, r); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void setRace() { |
| 107 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 108 |
final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 109 |
|
| 110 |
final BooleanSubscription bs1 = new BooleanSubscription(); |
| 111 |
final BooleanSubscription bs2 = new BooleanSubscription(); |
| 112 |
|
| 113 |
Runnable r1 = new Runnable() { |
| 114 |
@Override |
| 115 |
public void run() { |
| 116 |
SubscriptionHelper.set(atomicSubscription, bs1); |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
Runnable r2 = new Runnable() { |
| 121 |
@Override |
| 122 |
public void run() { |
| 123 |
SubscriptionHelper.set(atomicSubscription, bs2); |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
TestHelper.race(r1, r2); |
| 128 |
|
| 129 |
assertTrue(bs1.isCancelled() ^ bs2.isCancelled()); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void replaceRace() { |
| 135 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 136 |
final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 137 |
|
| 138 |
final BooleanSubscription bs1 = new BooleanSubscription(); |
| 139 |
final BooleanSubscription bs2 = new BooleanSubscription(); |
| 140 |
|
| 141 |
Runnable r1 = new Runnable() { |
| 142 |
@Override |
| 143 |
public void run() { |
| 144 |
SubscriptionHelper.replace(atomicSubscription, bs1); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
Runnable r2 = new Runnable() { |
| 149 |
@Override |
| 150 |
public void run() { |
| 151 |
SubscriptionHelper.replace(atomicSubscription, bs2); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
TestHelper.race(r1, r2); |
| 156 |
|
| 157 |
assertFalse(bs1.isCancelled()); |
| 158 |
assertFalse(bs2.isCancelled()); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void cancelAndChange() { |
| 164 |
AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 165 |
|
| 166 |
SubscriptionHelper.cancel(atomicSubscription); |
| 167 |
|
| 168 |
BooleanSubscription bs1 = new BooleanSubscription(); |
| 169 |
assertFalse(SubscriptionHelper.set(atomicSubscription, bs1)); |
| 170 |
assertTrue(bs1.isCancelled()); |
| 171 |
|
| 172 |
assertFalse(SubscriptionHelper.set(atomicSubscription, null)); |
| 173 |
|
| 174 |
BooleanSubscription bs2 = new BooleanSubscription(); |
| 175 |
assertFalse(SubscriptionHelper.replace(atomicSubscription, bs2)); |
| 176 |
assertTrue(bs2.isCancelled()); |
| 177 |
|
| 178 |
assertFalse(SubscriptionHelper.replace(atomicSubscription, null)); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void invalidDeferredRequest() { |
| 183 |
AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 184 |
AtomicLong r = new AtomicLong(); |
| 185 |
|
| 186 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 187 |
try { |
| 188 |
SubscriptionHelper.deferredRequest(atomicSubscription, r, -99); |
| 189 |
|
| 190 |
TestHelper.assertError(errors, 0, IllegalArgumentException.class, "n > 0 required but it was -99"); |
| 191 |
} finally { |
| 192 |
RxJavaPlugins.reset(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void deferredRace() { |
| 198 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 199 |
final AtomicReference<Subscription> atomicSubscription = new AtomicReference<>(); |
| 200 |
final AtomicLong r = new AtomicLong(); |
| 201 |
|
| 202 |
final AtomicLong q = new AtomicLong(); |
| 203 |
|
| 204 |
final Subscription a = new Subscription() { |
| 205 |
@Override |
| 206 |
public void request(long n) { |
| 207 |
q.addAndGet(n); |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void cancel() { |
| 212 |
|
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
Runnable r1 = new Runnable() { |
| 217 |
@Override |
| 218 |
public void run() { |
| 219 |
SubscriptionHelper.deferredSetOnce(atomicSubscription, r, a); |
| 220 |
} |
| 221 |
}; |
| 222 |
|
| 223 |
Runnable r2 = new Runnable() { |
| 224 |
@Override |
| 225 |
public void run() { |
| 226 |
SubscriptionHelper.deferredRequest(atomicSubscription, r, 1); |
| 227 |
} |
| 228 |
}; |
| 229 |
|
| 230 |
TestHelper.race(r1, r2); |
| 231 |
|
| 232 |
assertSame(a, atomicSubscription.get()); |
| 233 |
assertEquals(1, q.get()); |
| 234 |
assertEquals(0, r.get()); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void setOnceAndRequest() { |
| 240 |
AtomicReference<Subscription> ref = new AtomicReference<>(); |
| 241 |
|
| 242 |
Subscription sub = mock(Subscription.class); |
| 243 |
|
| 244 |
assertTrue(SubscriptionHelper.setOnce(ref, sub, 1)); |
| 245 |
|
| 246 |
verify(sub).request(1); |
| 247 |
verify(sub, never()).cancel(); |
| 248 |
|
| 249 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 250 |
try { |
| 251 |
sub = mock(Subscription.class); |
| 252 |
|
| 253 |
assertFalse(SubscriptionHelper.setOnce(ref, sub, 1)); |
| 254 |
|
| 255 |
verify(sub, never()).request(anyLong()); |
| 256 |
verify(sub).cancel(); |
| 257 |
|
| 258 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 259 |
} finally { |
| 260 |
RxJavaPlugins.reset(); |
| 261 |
} |
| 262 |
} |
| 263 |
} |