| 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.schedulers; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.*; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.subscribers.*; |
| 28 |
|
| 29 |
/** |
| 30 |
* Base tests for schedulers that involve threads (concurrency). |
| 31 |
* |
| 32 |
* These can only run on Schedulers that launch threads since they expect async/concurrent behavior. |
| 33 |
* |
| 34 |
* The Current/Immediate schedulers will not work with these tests. |
| 35 |
*/ |
| 36 |
public abstract class AbstractSchedulerConcurrencyTests extends AbstractSchedulerTests { |
| 37 |
|
| 38 |
/** |
| 39 |
* Make sure canceling through {@code subscribeOn} works. |
| 40 |
* Bug report: https://github.com/ReactiveX/RxJava/issues/431 |
| 41 |
* @throws InterruptedException if the test is interrupted |
| 42 |
*/ |
| 43 |
@Test |
| 44 |
public final void unSubscribeForScheduler() throws InterruptedException { |
| 45 |
final AtomicInteger countReceived = new AtomicInteger(); |
| 46 |
final AtomicInteger countGenerated = new AtomicInteger(); |
| 47 |
final CountDownLatch latch = new CountDownLatch(1); |
| 48 |
|
| 49 |
Flowable.interval(50, TimeUnit.MILLISECONDS) |
| 50 |
.map(new Function<Long, Long>() { |
| 51 |
@Override |
| 52 |
public Long apply(Long aLong) { |
| 53 |
countGenerated.incrementAndGet(); |
| 54 |
return aLong; |
| 55 |
} |
| 56 |
}) |
| 57 |
.subscribeOn(getScheduler()) |
| 58 |
.observeOn(getScheduler()) |
| 59 |
.subscribe(new DefaultSubscriber<Long>() { |
| 60 |
@Override |
| 61 |
public void onComplete() { |
| 62 |
System.out.println("--- completed"); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void onError(Throwable e) { |
| 67 |
System.out.println("--- onError"); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onNext(Long args) { |
| 72 |
if (countReceived.incrementAndGet() == 2) { |
| 73 |
cancel(); |
| 74 |
latch.countDown(); |
| 75 |
} |
| 76 |
System.out.println("==> Received " + args); |
| 77 |
} |
| 78 |
}); |
| 79 |
|
| 80 |
latch.await(1000, TimeUnit.MILLISECONDS); |
| 81 |
|
| 82 |
System.out.println("----------- it thinks it is finished ------------------ "); |
| 83 |
|
| 84 |
int timeout = 10; |
| 85 |
|
| 86 |
while (timeout-- > 0 && countGenerated.get() != 2) { |
| 87 |
Thread.sleep(100); |
| 88 |
} |
| 89 |
|
| 90 |
assertEquals(2, countGenerated.get()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void unsubscribeRecursiveScheduleFromOutside() throws InterruptedException { |
| 95 |
final CountDownLatch latch = new CountDownLatch(1); |
| 96 |
final CountDownLatch unsubscribeLatch = new CountDownLatch(1); |
| 97 |
final AtomicInteger counter = new AtomicInteger(); |
| 98 |
final Worker inner = getScheduler().createWorker(); |
| 99 |
try { |
| 100 |
inner.schedule(new Runnable() { |
| 101 |
|
| 102 |
@Override |
| 103 |
public void run() { |
| 104 |
inner.schedule(new Runnable() { |
| 105 |
|
| 106 |
int i; |
| 107 |
|
| 108 |
@Override |
| 109 |
public void run() { |
| 110 |
System.out.println("Run: " + i++); |
| 111 |
if (i == 10) { |
| 112 |
latch.countDown(); |
| 113 |
try { |
| 114 |
// wait for unsubscribe to finish so we are not racing it |
| 115 |
unsubscribeLatch.await(); |
| 116 |
} catch (InterruptedException e) { |
| 117 |
// we expect the countDown if unsubscribe is not working |
| 118 |
// or to be interrupted if unsubscribe is successful since |
| 119 |
// the unsubscribe will interrupt it as it is calling Future.cancel(true) |
| 120 |
// so we will ignore the stacktrace |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
counter.incrementAndGet(); |
| 125 |
inner.schedule(this); |
| 126 |
} |
| 127 |
}); |
| 128 |
} |
| 129 |
|
| 130 |
}); |
| 131 |
|
| 132 |
latch.await(); |
| 133 |
inner.dispose(); |
| 134 |
unsubscribeLatch.countDown(); |
| 135 |
Thread.sleep(200); // let time pass to see if the scheduler is still doing work |
| 136 |
assertEquals(10, counter.get()); |
| 137 |
} finally { |
| 138 |
inner.dispose(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void unsubscribeRecursiveScheduleFromInside() throws InterruptedException { |
| 144 |
final CountDownLatch unsubscribeLatch = new CountDownLatch(1); |
| 145 |
final AtomicInteger counter = new AtomicInteger(); |
| 146 |
final Worker inner = getScheduler().createWorker(); |
| 147 |
try { |
| 148 |
inner.schedule(new Runnable() { |
| 149 |
|
| 150 |
@Override |
| 151 |
public void run() { |
| 152 |
inner.schedule(new Runnable() { |
| 153 |
|
| 154 |
int i; |
| 155 |
|
| 156 |
@Override |
| 157 |
public void run() { |
| 158 |
System.out.println("Run: " + i++); |
| 159 |
if (i == 10) { |
| 160 |
inner.dispose(); |
| 161 |
} |
| 162 |
|
| 163 |
counter.incrementAndGet(); |
| 164 |
inner.schedule(this); |
| 165 |
} |
| 166 |
}); |
| 167 |
} |
| 168 |
|
| 169 |
}); |
| 170 |
|
| 171 |
unsubscribeLatch.countDown(); |
| 172 |
Thread.sleep(200); // let time pass to see if the scheduler is still doing work |
| 173 |
assertEquals(10, counter.get()); |
| 174 |
} finally { |
| 175 |
inner.dispose(); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
@Test |
| 180 |
public void unsubscribeRecursiveScheduleWithDelay() throws InterruptedException { |
| 181 |
final CountDownLatch latch = new CountDownLatch(1); |
| 182 |
final CountDownLatch unsubscribeLatch = new CountDownLatch(1); |
| 183 |
final AtomicInteger counter = new AtomicInteger(); |
| 184 |
final Worker inner = getScheduler().createWorker(); |
| 185 |
|
| 186 |
try { |
| 187 |
inner.schedule(new Runnable() { |
| 188 |
|
| 189 |
@Override |
| 190 |
public void run() { |
| 191 |
inner.schedule(new Runnable() { |
| 192 |
|
| 193 |
long i = 1L; |
| 194 |
|
| 195 |
@Override |
| 196 |
public void run() { |
| 197 |
if (i++ == 10) { |
| 198 |
latch.countDown(); |
| 199 |
try { |
| 200 |
// wait for unsubscribe to finish so we are not racing it |
| 201 |
unsubscribeLatch.await(); |
| 202 |
} catch (InterruptedException e) { |
| 203 |
// we expect the countDown if unsubscribe is not working |
| 204 |
// or to be interrupted if unsubscribe is successful since |
| 205 |
// the unsubscribe will interrupt it as it is calling Future.cancel(true) |
| 206 |
// so we will ignore the stacktrace |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
counter.incrementAndGet(); |
| 211 |
inner.schedule(this, 10, TimeUnit.MILLISECONDS); |
| 212 |
} |
| 213 |
}, 10, TimeUnit.MILLISECONDS); |
| 214 |
} |
| 215 |
}); |
| 216 |
|
| 217 |
latch.await(); |
| 218 |
inner.dispose(); |
| 219 |
unsubscribeLatch.countDown(); |
| 220 |
Thread.sleep(200); // let time pass to see if the scheduler is still doing work |
| 221 |
assertEquals(10, counter.get()); |
| 222 |
} finally { |
| 223 |
inner.dispose(); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
@Test |
| 228 |
public void recursionFromOuterActionAndUnsubscribeInside() throws InterruptedException { |
| 229 |
final CountDownLatch latch = new CountDownLatch(1); |
| 230 |
final Worker inner = getScheduler().createWorker(); |
| 231 |
try { |
| 232 |
inner.schedule(new Runnable() { |
| 233 |
|
| 234 |
int i; |
| 235 |
|
| 236 |
@Override |
| 237 |
public void run() { |
| 238 |
i++; |
| 239 |
if (i % 100000 == 0) { |
| 240 |
System.out.println(i + " Total Memory: " + Runtime.getRuntime().totalMemory() + " Free: " + Runtime.getRuntime().freeMemory()); |
| 241 |
} |
| 242 |
if (i < 1000000L) { |
| 243 |
inner.schedule(this); |
| 244 |
} else { |
| 245 |
latch.countDown(); |
| 246 |
} |
| 247 |
} |
| 248 |
}); |
| 249 |
|
| 250 |
latch.await(); |
| 251 |
} finally { |
| 252 |
inner.dispose(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void recursion() throws InterruptedException { |
| 258 |
final CountDownLatch latch = new CountDownLatch(1); |
| 259 |
final Worker inner = getScheduler().createWorker(); |
| 260 |
try { |
| 261 |
inner.schedule(new Runnable() { |
| 262 |
|
| 263 |
private long i; |
| 264 |
|
| 265 |
@Override |
| 266 |
public void run() { |
| 267 |
i++; |
| 268 |
if (i % 100000 == 0) { |
| 269 |
System.out.println(i + " Total Memory: " + Runtime.getRuntime().totalMemory() + " Free: " + Runtime.getRuntime().freeMemory()); |
| 270 |
} |
| 271 |
if (i < 1000000L) { |
| 272 |
inner.schedule(this); |
| 273 |
} else { |
| 274 |
latch.countDown(); |
| 275 |
} |
| 276 |
} |
| 277 |
}); |
| 278 |
|
| 279 |
latch.await(); |
| 280 |
} finally { |
| 281 |
inner.dispose(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
@Test |
| 286 |
public void recursionAndOuterUnsubscribe() throws InterruptedException { |
| 287 |
// use latches instead of Thread.sleep |
| 288 |
final CountDownLatch latch = new CountDownLatch(10); |
| 289 |
final CountDownLatch completionLatch = new CountDownLatch(1); |
| 290 |
final Worker inner = getScheduler().createWorker(); |
| 291 |
try { |
| 292 |
Flowable<Integer> obs = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 293 |
@Override |
| 294 |
public void subscribe(final Subscriber<? super Integer> subscriber) { |
| 295 |
inner.schedule(new Runnable() { |
| 296 |
@Override |
| 297 |
public void run() { |
| 298 |
subscriber.onNext(42); |
| 299 |
latch.countDown(); |
| 300 |
|
| 301 |
// this will recursively schedule this task for execution again |
| 302 |
inner.schedule(this); |
| 303 |
} |
| 304 |
}); |
| 305 |
|
| 306 |
subscriber.onSubscribe(new Subscription() { |
| 307 |
|
| 308 |
@Override |
| 309 |
public void cancel() { |
| 310 |
inner.dispose(); |
| 311 |
subscriber.onComplete(); |
| 312 |
completionLatch.countDown(); |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public void request(long n) { |
| 317 |
|
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
} |
| 322 |
}); |
| 323 |
|
| 324 |
final AtomicInteger count = new AtomicInteger(); |
| 325 |
final AtomicBoolean completed = new AtomicBoolean(false); |
| 326 |
ResourceSubscriber<Integer> s = new ResourceSubscriber<Integer>() { |
| 327 |
@Override |
| 328 |
public void onComplete() { |
| 329 |
System.out.println("Completed"); |
| 330 |
completed.set(true); |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public void onError(Throwable e) { |
| 335 |
System.out.println("Error"); |
| 336 |
} |
| 337 |
|
| 338 |
@Override |
| 339 |
public void onNext(Integer args) { |
| 340 |
count.incrementAndGet(); |
| 341 |
System.out.println(args); |
| 342 |
} |
| 343 |
}; |
| 344 |
obs.subscribe(s); |
| 345 |
|
| 346 |
if (!latch.await(5000, TimeUnit.MILLISECONDS)) { |
| 347 |
fail("Timed out waiting on onNext latch"); |
| 348 |
} |
| 349 |
|
| 350 |
// now unsubscribe and ensure it stops the recursive loop |
| 351 |
s.dispose(); |
| 352 |
System.out.println("unsubscribe"); |
| 353 |
|
| 354 |
if (!completionLatch.await(5000, TimeUnit.MILLISECONDS)) { |
| 355 |
fail("Timed out waiting on completion latch"); |
| 356 |
} |
| 357 |
|
| 358 |
// the count can be 10 or higher due to thread scheduling of the unsubscribe vs the scheduler looping to emit the count |
| 359 |
assertTrue(count.get() >= 10); |
| 360 |
assertTrue(completed.get()); |
| 361 |
} finally { |
| 362 |
inner.dispose(); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
@Test |
| 367 |
public final void subscribeWithScheduler() throws InterruptedException { |
| 368 |
final Scheduler scheduler = getScheduler(); |
| 369 |
|
| 370 |
final AtomicInteger count = new AtomicInteger(); |
| 371 |
|
| 372 |
Flowable<Integer> f1 = Flowable.<Integer> just(1, 2, 3, 4, 5); |
| 373 |
|
| 374 |
f1.subscribe(new Consumer<Integer>() { |
| 375 |
|
| 376 |
@Override |
| 377 |
public void accept(Integer t) { |
| 378 |
System.out.println("Thread: " + Thread.currentThread().getName()); |
| 379 |
System.out.println("t: " + t); |
| 380 |
count.incrementAndGet(); |
| 381 |
} |
| 382 |
}); |
| 383 |
|
| 384 |
// the above should be blocking so we should see a count of 5 |
| 385 |
assertEquals(5, count.get()); |
| 386 |
|
| 387 |
count.set(0); |
| 388 |
|
| 389 |
// now we'll subscribe with a scheduler and it should be async |
| 390 |
|
| 391 |
final String currentThreadName = Thread.currentThread().getName(); |
| 392 |
|
| 393 |
// latches for deterministically controlling the test below across threads |
| 394 |
final CountDownLatch latch = new CountDownLatch(5); |
| 395 |
final CountDownLatch first = new CountDownLatch(1); |
| 396 |
|
| 397 |
f1.subscribeOn(scheduler).subscribe(new Consumer<Integer>() { |
| 398 |
|
| 399 |
@Override |
| 400 |
public void accept(Integer t) { |
| 401 |
try { |
| 402 |
// we block the first one so we can assert this executes asynchronously with a count |
| 403 |
first.await(1000, TimeUnit.SECONDS); |
| 404 |
} catch (InterruptedException e) { |
| 405 |
throw new RuntimeException("The latch should have released if we are async.", e); |
| 406 |
} |
| 407 |
|
| 408 |
assertNotEquals(Thread.currentThread().getName(), currentThreadName); |
| 409 |
System.out.println("Thread: " + Thread.currentThread().getName()); |
| 410 |
System.out.println("t: " + t); |
| 411 |
count.incrementAndGet(); |
| 412 |
latch.countDown(); |
| 413 |
} |
| 414 |
}); |
| 415 |
|
| 416 |
// assert we are async |
| 417 |
assertEquals(0, count.get()); |
| 418 |
// release the latch so it can go forward |
| 419 |
first.countDown(); |
| 420 |
|
| 421 |
// wait for all 5 responses |
| 422 |
latch.await(); |
| 423 |
assertEquals(5, count.get()); |
| 424 |
} |
| 425 |
|
| 426 |
} |