| 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.List; |
| 19 |
import java.util.concurrent.*; |
| 20 |
import java.util.concurrent.atomic.*; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.Scheduler; |
| 25 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 26 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 27 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.schedulers.RxThreadFactory; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 32 |
|
| 33 |
public class ExecutorSchedulerInterruptibleTest extends AbstractSchedulerConcurrencyTests { |
| 34 |
|
| 35 |
static final Executor executor = Executors.newFixedThreadPool(2, new RxThreadFactory("TestCustomPool")); |
| 36 |
|
| 37 |
@Override |
| 38 |
protected Scheduler getScheduler() { |
| 39 |
return Schedulers.from(executor, true); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public final void handledErrorIsNotDeliveredToThreadHandler() throws InterruptedException { |
| 44 |
SchedulerTestHelper.handledErrorIsNotDeliveredToThreadHandler(getScheduler()); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void cancelledTaskRetention() throws InterruptedException { |
| 49 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 50 |
Scheduler s = Schedulers.from(exec, true); |
| 51 |
try { |
| 52 |
Scheduler.Worker w = s.createWorker(); |
| 53 |
try { |
| 54 |
ExecutorSchedulerTest.cancelledRetention(w, false); |
| 55 |
} finally { |
| 56 |
w.dispose(); |
| 57 |
} |
| 58 |
|
| 59 |
w = s.createWorker(); |
| 60 |
try { |
| 61 |
ExecutorSchedulerTest.cancelledRetention(w, true); |
| 62 |
} finally { |
| 63 |
w.dispose(); |
| 64 |
} |
| 65 |
} finally { |
| 66 |
exec.shutdownNow(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** A simple executor which queues tasks and executes them one-by-one if executeOne() is called. */ |
| 71 |
static final class TestExecutor implements Executor { |
| 72 |
final ConcurrentLinkedQueue<Runnable> queue = new ConcurrentLinkedQueue<>(); |
| 73 |
@Override |
| 74 |
public void execute(Runnable command) { |
| 75 |
queue.offer(command); |
| 76 |
} |
| 77 |
public void executeOne() { |
| 78 |
Runnable r = queue.poll(); |
| 79 |
if (r != null) { |
| 80 |
r.run(); |
| 81 |
} |
| 82 |
} |
| 83 |
public void executeAll() { |
| 84 |
Runnable r; |
| 85 |
while ((r = queue.poll()) != null) { |
| 86 |
r.run(); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void cancelledTasksDontRun() { |
| 93 |
final AtomicInteger calls = new AtomicInteger(); |
| 94 |
Runnable task = new Runnable() { |
| 95 |
@Override |
| 96 |
public void run() { |
| 97 |
calls.getAndIncrement(); |
| 98 |
} |
| 99 |
}; |
| 100 |
TestExecutor exec = new TestExecutor(); |
| 101 |
Scheduler custom = Schedulers.from(exec, true); |
| 102 |
Worker w = custom.createWorker(); |
| 103 |
try { |
| 104 |
Disposable d1 = w.schedule(task); |
| 105 |
Disposable d2 = w.schedule(task); |
| 106 |
Disposable d3 = w.schedule(task); |
| 107 |
|
| 108 |
d1.dispose(); |
| 109 |
d2.dispose(); |
| 110 |
d3.dispose(); |
| 111 |
|
| 112 |
exec.executeAll(); |
| 113 |
|
| 114 |
assertEquals(0, calls.get()); |
| 115 |
} finally { |
| 116 |
w.dispose(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void cancelledWorkerDoesntRunTasks() { |
| 122 |
final AtomicInteger calls = new AtomicInteger(); |
| 123 |
Runnable task = new Runnable() { |
| 124 |
@Override |
| 125 |
public void run() { |
| 126 |
calls.getAndIncrement(); |
| 127 |
} |
| 128 |
}; |
| 129 |
TestExecutor exec = new TestExecutor(); |
| 130 |
Scheduler custom = Schedulers.from(exec, true); |
| 131 |
Worker w = custom.createWorker(); |
| 132 |
try { |
| 133 |
w.schedule(task); |
| 134 |
w.schedule(task); |
| 135 |
w.schedule(task); |
| 136 |
} finally { |
| 137 |
w.dispose(); |
| 138 |
} |
| 139 |
exec.executeAll(); |
| 140 |
assertEquals(0, calls.get()); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void plainExecutor() throws Exception { |
| 145 |
Scheduler s = Schedulers.from(new Executor() { |
| 146 |
@Override |
| 147 |
public void execute(Runnable r) { |
| 148 |
r.run(); |
| 149 |
} |
| 150 |
}, true); |
| 151 |
|
| 152 |
final CountDownLatch cdl = new CountDownLatch(5); |
| 153 |
|
| 154 |
Runnable r = new Runnable() { |
| 155 |
@Override |
| 156 |
public void run() { |
| 157 |
cdl.countDown(); |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
s.scheduleDirect(r); |
| 162 |
|
| 163 |
s.scheduleDirect(r, 50, TimeUnit.MILLISECONDS); |
| 164 |
|
| 165 |
Disposable d = s.schedulePeriodicallyDirect(r, 10, 10, TimeUnit.MILLISECONDS); |
| 166 |
|
| 167 |
try { |
| 168 |
assertTrue(cdl.await(5, TimeUnit.SECONDS)); |
| 169 |
} finally { |
| 170 |
d.dispose(); |
| 171 |
} |
| 172 |
|
| 173 |
assertTrue(d.isDisposed()); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void rejectingExecutor() { |
| 178 |
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 179 |
exec.shutdown(); |
| 180 |
|
| 181 |
Scheduler s = Schedulers.from(exec, true); |
| 182 |
|
| 183 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 184 |
|
| 185 |
try { |
| 186 |
assertSame(EmptyDisposable.INSTANCE, s.scheduleDirect(Functions.EMPTY_RUNNABLE)); |
| 187 |
|
| 188 |
assertSame(EmptyDisposable.INSTANCE, s.scheduleDirect(Functions.EMPTY_RUNNABLE, 10, TimeUnit.MILLISECONDS)); |
| 189 |
|
| 190 |
assertSame(EmptyDisposable.INSTANCE, s.schedulePeriodicallyDirect(Functions.EMPTY_RUNNABLE, 10, 10, TimeUnit.MILLISECONDS)); |
| 191 |
|
| 192 |
TestHelper.assertUndeliverable(errors, 0, RejectedExecutionException.class); |
| 193 |
TestHelper.assertUndeliverable(errors, 1, RejectedExecutionException.class); |
| 194 |
TestHelper.assertUndeliverable(errors, 2, RejectedExecutionException.class); |
| 195 |
} finally { |
| 196 |
RxJavaPlugins.reset(); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void rejectingExecutorWorker() { |
| 202 |
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 203 |
exec.shutdown(); |
| 204 |
|
| 205 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 206 |
|
| 207 |
try { |
| 208 |
Worker s = Schedulers.from(exec, true).createWorker(); |
| 209 |
assertSame(EmptyDisposable.INSTANCE, s.schedule(Functions.EMPTY_RUNNABLE)); |
| 210 |
|
| 211 |
s = Schedulers.from(exec, true).createWorker(); |
| 212 |
assertSame(EmptyDisposable.INSTANCE, s.schedule(Functions.EMPTY_RUNNABLE, 10, TimeUnit.MILLISECONDS)); |
| 213 |
|
| 214 |
s = Schedulers.from(exec, true).createWorker(); |
| 215 |
assertSame(EmptyDisposable.INSTANCE, s.schedulePeriodically(Functions.EMPTY_RUNNABLE, 10, 10, TimeUnit.MILLISECONDS)); |
| 216 |
|
| 217 |
TestHelper.assertUndeliverable(errors, 0, RejectedExecutionException.class); |
| 218 |
TestHelper.assertUndeliverable(errors, 1, RejectedExecutionException.class); |
| 219 |
TestHelper.assertUndeliverable(errors, 2, RejectedExecutionException.class); |
| 220 |
} finally { |
| 221 |
RxJavaPlugins.reset(); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
@Test |
| 226 |
public void reuseScheduledExecutor() throws Exception { |
| 227 |
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 228 |
|
| 229 |
try { |
| 230 |
Scheduler s = Schedulers.from(exec, true); |
| 231 |
|
| 232 |
final CountDownLatch cdl = new CountDownLatch(8); |
| 233 |
|
| 234 |
Runnable r = new Runnable() { |
| 235 |
@Override |
| 236 |
public void run() { |
| 237 |
cdl.countDown(); |
| 238 |
} |
| 239 |
}; |
| 240 |
|
| 241 |
s.scheduleDirect(r); |
| 242 |
|
| 243 |
s.scheduleDirect(r, 10, TimeUnit.MILLISECONDS); |
| 244 |
|
| 245 |
Disposable d = s.schedulePeriodicallyDirect(r, 10, 10, TimeUnit.MILLISECONDS); |
| 246 |
|
| 247 |
try { |
| 248 |
assertTrue(cdl.await(5, TimeUnit.SECONDS)); |
| 249 |
} finally { |
| 250 |
d.dispose(); |
| 251 |
} |
| 252 |
} finally { |
| 253 |
exec.shutdown(); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
@Test |
| 258 |
public void reuseScheduledExecutorAsWorker() throws Exception { |
| 259 |
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); |
| 260 |
|
| 261 |
Worker s = Schedulers.from(exec, true).createWorker(); |
| 262 |
|
| 263 |
assertFalse(s.isDisposed()); |
| 264 |
try { |
| 265 |
|
| 266 |
final CountDownLatch cdl = new CountDownLatch(8); |
| 267 |
|
| 268 |
Runnable r = new Runnable() { |
| 269 |
@Override |
| 270 |
public void run() { |
| 271 |
cdl.countDown(); |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
s.schedule(r); |
| 276 |
|
| 277 |
s.schedule(r, 10, TimeUnit.MILLISECONDS); |
| 278 |
|
| 279 |
Disposable d = s.schedulePeriodically(r, 10, 10, TimeUnit.MILLISECONDS); |
| 280 |
|
| 281 |
try { |
| 282 |
assertTrue(cdl.await(5, TimeUnit.SECONDS)); |
| 283 |
} finally { |
| 284 |
d.dispose(); |
| 285 |
} |
| 286 |
} finally { |
| 287 |
s.dispose(); |
| 288 |
exec.shutdown(); |
| 289 |
} |
| 290 |
|
| 291 |
assertTrue(s.isDisposed()); |
| 292 |
} |
| 293 |
|
| 294 |
@Test |
| 295 |
public void disposeRace() { |
| 296 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 297 |
final Scheduler s = Schedulers.from(exec, true); |
| 298 |
try { |
| 299 |
for (int i = 0; i < 500; i++) { |
| 300 |
final Worker w = s.createWorker(); |
| 301 |
|
| 302 |
final AtomicInteger c = new AtomicInteger(2); |
| 303 |
|
| 304 |
w.schedule(new Runnable() { |
| 305 |
@Override |
| 306 |
public void run() { |
| 307 |
c.decrementAndGet(); |
| 308 |
while (c.get() != 0) { } |
| 309 |
} |
| 310 |
}); |
| 311 |
|
| 312 |
c.decrementAndGet(); |
| 313 |
while (c.get() != 0) { } |
| 314 |
w.dispose(); |
| 315 |
} |
| 316 |
} finally { |
| 317 |
exec.shutdownNow(); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void runnableDisposed() { |
| 323 |
final Scheduler s = Schedulers.from(new Executor() { |
| 324 |
@Override |
| 325 |
public void execute(Runnable r) { |
| 326 |
r.run(); |
| 327 |
} |
| 328 |
}, true); |
| 329 |
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE); |
| 330 |
|
| 331 |
assertTrue(d.isDisposed()); |
| 332 |
} |
| 333 |
|
| 334 |
@Test |
| 335 |
public void runnableDisposedAsync() throws Exception { |
| 336 |
final Scheduler s = Schedulers.from(new Executor() { |
| 337 |
@Override |
| 338 |
public void execute(Runnable r) { |
| 339 |
new Thread(r).start(); |
| 340 |
} |
| 341 |
}, true); |
| 342 |
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE); |
| 343 |
|
| 344 |
while (!d.isDisposed()) { |
| 345 |
Thread.sleep(1); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void runnableDisposedAsync2() throws Exception { |
| 351 |
final Scheduler s = Schedulers.from(executor, true); |
| 352 |
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE); |
| 353 |
|
| 354 |
while (!d.isDisposed()) { |
| 355 |
Thread.sleep(1); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
@Test |
| 360 |
public void runnableDisposedAsyncCrash() throws Exception { |
| 361 |
final Scheduler s = Schedulers.from(new Executor() { |
| 362 |
@Override |
| 363 |
public void execute(Runnable r) { |
| 364 |
new Thread(r).start(); |
| 365 |
} |
| 366 |
}, true); |
| 367 |
Disposable d = s.scheduleDirect(new Runnable() { |
| 368 |
@Override |
| 369 |
public void run() { |
| 370 |
throw new IllegalStateException(); |
| 371 |
} |
| 372 |
}); |
| 373 |
|
| 374 |
while (!d.isDisposed()) { |
| 375 |
Thread.sleep(1); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
@Test |
| 380 |
public void runnableDisposedAsyncTimed() throws Exception { |
| 381 |
final Scheduler s = Schedulers.from(new Executor() { |
| 382 |
@Override |
| 383 |
public void execute(Runnable r) { |
| 384 |
new Thread(r).start(); |
| 385 |
} |
| 386 |
}, true); |
| 387 |
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE, 1, TimeUnit.MILLISECONDS); |
| 388 |
|
| 389 |
while (!d.isDisposed()) { |
| 390 |
Thread.sleep(1); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
@Test |
| 395 |
public void runnableDisposedAsyncTimed2() throws Exception { |
| 396 |
ExecutorService executorScheduler = Executors.newScheduledThreadPool(1, new RxThreadFactory("TestCustomPoolTimed")); |
| 397 |
try { |
| 398 |
final Scheduler s = Schedulers.from(executorScheduler, true); |
| 399 |
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE, 1, TimeUnit.MILLISECONDS); |
| 400 |
|
| 401 |
while (!d.isDisposed()) { |
| 402 |
Thread.sleep(1); |
| 403 |
} |
| 404 |
} finally { |
| 405 |
executorScheduler.shutdownNow(); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
@Test |
| 410 |
public void unwrapScheduleDirectTaskAfterDispose() { |
| 411 |
Scheduler scheduler = getScheduler(); |
| 412 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 413 |
Runnable countDownRunnable = new Runnable() { |
| 414 |
@Override |
| 415 |
public void run() { |
| 416 |
cdl.countDown(); |
| 417 |
} |
| 418 |
}; |
| 419 |
Disposable disposable = scheduler.scheduleDirect(countDownRunnable, 100, TimeUnit.MILLISECONDS); |
| 420 |
SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) disposable; |
| 421 |
assertSame(countDownRunnable, wrapper.getWrappedRunnable()); |
| 422 |
disposable.dispose(); |
| 423 |
|
| 424 |
assertSame(Functions.EMPTY_RUNNABLE, wrapper.getWrappedRunnable()); |
| 425 |
} |
| 426 |
|
| 427 |
@Test |
| 428 |
public void interruptibleDirectTask() throws Exception { |
| 429 |
Scheduler scheduler = getScheduler(); |
| 430 |
|
| 431 |
final AtomicInteger sync = new AtomicInteger(2); |
| 432 |
|
| 433 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 434 |
|
| 435 |
Disposable d = scheduler.scheduleDirect(new Runnable() { |
| 436 |
@Override |
| 437 |
public void run() { |
| 438 |
if (sync.decrementAndGet() != 0) { |
| 439 |
while (sync.get() != 0) { } |
| 440 |
} |
| 441 |
try { |
| 442 |
Thread.sleep(1000); |
| 443 |
} catch (InterruptedException ex) { |
| 444 |
isInterrupted.set(true); |
| 445 |
} |
| 446 |
} |
| 447 |
}); |
| 448 |
|
| 449 |
if (sync.decrementAndGet() != 0) { |
| 450 |
while (sync.get() != 0) { } |
| 451 |
} |
| 452 |
|
| 453 |
Thread.sleep(500); |
| 454 |
|
| 455 |
d.dispose(); |
| 456 |
|
| 457 |
int i = 20; |
| 458 |
while (i-- > 0 && !isInterrupted.get()) { |
| 459 |
Thread.sleep(50); |
| 460 |
} |
| 461 |
|
| 462 |
assertTrue("Interruption did not propagate", isInterrupted.get()); |
| 463 |
} |
| 464 |
|
| 465 |
@Test |
| 466 |
public void interruptibleWorkerTask() throws Exception { |
| 467 |
Scheduler scheduler = getScheduler(); |
| 468 |
|
| 469 |
Worker worker = scheduler.createWorker(); |
| 470 |
|
| 471 |
try { |
| 472 |
final AtomicInteger sync = new AtomicInteger(2); |
| 473 |
|
| 474 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 475 |
|
| 476 |
Disposable d = worker.schedule(new Runnable() { |
| 477 |
@Override |
| 478 |
public void run() { |
| 479 |
if (sync.decrementAndGet() != 0) { |
| 480 |
while (sync.get() != 0) { } |
| 481 |
} |
| 482 |
try { |
| 483 |
Thread.sleep(1000); |
| 484 |
} catch (InterruptedException ex) { |
| 485 |
isInterrupted.set(true); |
| 486 |
} |
| 487 |
} |
| 488 |
}); |
| 489 |
|
| 490 |
if (sync.decrementAndGet() != 0) { |
| 491 |
while (sync.get() != 0) { } |
| 492 |
} |
| 493 |
|
| 494 |
Thread.sleep(500); |
| 495 |
|
| 496 |
d.dispose(); |
| 497 |
|
| 498 |
int i = 20; |
| 499 |
while (i-- > 0 && !isInterrupted.get()) { |
| 500 |
Thread.sleep(50); |
| 501 |
} |
| 502 |
|
| 503 |
assertTrue("Interruption did not propagate", isInterrupted.get()); |
| 504 |
} finally { |
| 505 |
worker.dispose(); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
@Test |
| 510 |
public void interruptibleDirectTaskScheduledExecutor() throws Exception { |
| 511 |
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); |
| 512 |
try { |
| 513 |
Scheduler scheduler = Schedulers.from(exec, true); |
| 514 |
|
| 515 |
final AtomicInteger sync = new AtomicInteger(2); |
| 516 |
|
| 517 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 518 |
|
| 519 |
Disposable d = scheduler.scheduleDirect(new Runnable() { |
| 520 |
@Override |
| 521 |
public void run() { |
| 522 |
if (sync.decrementAndGet() != 0) { |
| 523 |
while (sync.get() != 0) { } |
| 524 |
} |
| 525 |
try { |
| 526 |
Thread.sleep(1000); |
| 527 |
} catch (InterruptedException ex) { |
| 528 |
isInterrupted.set(true); |
| 529 |
} |
| 530 |
} |
| 531 |
}); |
| 532 |
|
| 533 |
if (sync.decrementAndGet() != 0) { |
| 534 |
while (sync.get() != 0) { } |
| 535 |
} |
| 536 |
|
| 537 |
Thread.sleep(500); |
| 538 |
|
| 539 |
d.dispose(); |
| 540 |
|
| 541 |
int i = 20; |
| 542 |
while (i-- > 0 && !isInterrupted.get()) { |
| 543 |
Thread.sleep(50); |
| 544 |
} |
| 545 |
|
| 546 |
assertTrue("Interruption did not propagate", isInterrupted.get()); |
| 547 |
} finally { |
| 548 |
exec.shutdown(); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
@Test |
| 553 |
public void interruptibleWorkerTaskScheduledExecutor() throws Exception { |
| 554 |
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); |
| 555 |
try { |
| 556 |
Scheduler scheduler = Schedulers.from(exec, true); |
| 557 |
|
| 558 |
Worker worker = scheduler.createWorker(); |
| 559 |
|
| 560 |
try { |
| 561 |
final AtomicInteger sync = new AtomicInteger(2); |
| 562 |
|
| 563 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 564 |
|
| 565 |
Disposable d = worker.schedule(new Runnable() { |
| 566 |
@Override |
| 567 |
public void run() { |
| 568 |
if (sync.decrementAndGet() != 0) { |
| 569 |
while (sync.get() != 0) { } |
| 570 |
} |
| 571 |
try { |
| 572 |
Thread.sleep(1000); |
| 573 |
} catch (InterruptedException ex) { |
| 574 |
isInterrupted.set(true); |
| 575 |
} |
| 576 |
} |
| 577 |
}); |
| 578 |
|
| 579 |
if (sync.decrementAndGet() != 0) { |
| 580 |
while (sync.get() != 0) { } |
| 581 |
} |
| 582 |
|
| 583 |
Thread.sleep(500); |
| 584 |
|
| 585 |
d.dispose(); |
| 586 |
|
| 587 |
int i = 20; |
| 588 |
while (i-- > 0 && !isInterrupted.get()) { |
| 589 |
Thread.sleep(50); |
| 590 |
} |
| 591 |
|
| 592 |
assertTrue("Interruption did not propagate", isInterrupted.get()); |
| 593 |
} finally { |
| 594 |
worker.dispose(); |
| 595 |
} |
| 596 |
} finally { |
| 597 |
exec.shutdown(); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
@Test |
| 602 |
public void nonInterruptibleDirectTask() throws Exception { |
| 603 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 604 |
try { |
| 605 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 606 |
|
| 607 |
final AtomicInteger sync = new AtomicInteger(2); |
| 608 |
|
| 609 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 610 |
|
| 611 |
Disposable d = scheduler.scheduleDirect(new Runnable() { |
| 612 |
@Override |
| 613 |
public void run() { |
| 614 |
if (sync.decrementAndGet() != 0) { |
| 615 |
while (sync.get() != 0) { } |
| 616 |
} |
| 617 |
try { |
| 618 |
Thread.sleep(1000); |
| 619 |
} catch (InterruptedException ex) { |
| 620 |
isInterrupted.set(true); |
| 621 |
} |
| 622 |
} |
| 623 |
}); |
| 624 |
|
| 625 |
if (sync.decrementAndGet() != 0) { |
| 626 |
while (sync.get() != 0) { } |
| 627 |
} |
| 628 |
|
| 629 |
Thread.sleep(500); |
| 630 |
|
| 631 |
d.dispose(); |
| 632 |
|
| 633 |
int i = 20; |
| 634 |
while (i-- > 0 && !isInterrupted.get()) { |
| 635 |
Thread.sleep(50); |
| 636 |
} |
| 637 |
|
| 638 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 639 |
} finally { |
| 640 |
exec.shutdown(); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
@Test |
| 645 |
public void nonInterruptibleWorkerTask() throws Exception { |
| 646 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 647 |
try { |
| 648 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 649 |
|
| 650 |
Worker worker = scheduler.createWorker(); |
| 651 |
|
| 652 |
try { |
| 653 |
final AtomicInteger sync = new AtomicInteger(2); |
| 654 |
|
| 655 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 656 |
|
| 657 |
Disposable d = worker.schedule(new Runnable() { |
| 658 |
@Override |
| 659 |
public void run() { |
| 660 |
if (sync.decrementAndGet() != 0) { |
| 661 |
while (sync.get() != 0) { } |
| 662 |
} |
| 663 |
try { |
| 664 |
Thread.sleep(1000); |
| 665 |
} catch (InterruptedException ex) { |
| 666 |
isInterrupted.set(true); |
| 667 |
} |
| 668 |
} |
| 669 |
}); |
| 670 |
|
| 671 |
if (sync.decrementAndGet() != 0) { |
| 672 |
while (sync.get() != 0) { } |
| 673 |
} |
| 674 |
|
| 675 |
Thread.sleep(500); |
| 676 |
|
| 677 |
d.dispose(); |
| 678 |
|
| 679 |
int i = 20; |
| 680 |
while (i-- > 0 && !isInterrupted.get()) { |
| 681 |
Thread.sleep(50); |
| 682 |
} |
| 683 |
|
| 684 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 685 |
} finally { |
| 686 |
worker.dispose(); |
| 687 |
} |
| 688 |
} finally { |
| 689 |
exec.shutdown(); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
@Test |
| 694 |
public void nonInterruptibleDirectTaskScheduledExecutor() throws Exception { |
| 695 |
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); |
| 696 |
try { |
| 697 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 698 |
|
| 699 |
final AtomicInteger sync = new AtomicInteger(2); |
| 700 |
|
| 701 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 702 |
|
| 703 |
Disposable d = scheduler.scheduleDirect(new Runnable() { |
| 704 |
@Override |
| 705 |
public void run() { |
| 706 |
if (sync.decrementAndGet() != 0) { |
| 707 |
while (sync.get() != 0) { } |
| 708 |
} |
| 709 |
try { |
| 710 |
Thread.sleep(1000); |
| 711 |
} catch (InterruptedException ex) { |
| 712 |
isInterrupted.set(true); |
| 713 |
} |
| 714 |
} |
| 715 |
}); |
| 716 |
|
| 717 |
if (sync.decrementAndGet() != 0) { |
| 718 |
while (sync.get() != 0) { } |
| 719 |
} |
| 720 |
|
| 721 |
Thread.sleep(500); |
| 722 |
|
| 723 |
d.dispose(); |
| 724 |
|
| 725 |
int i = 20; |
| 726 |
while (i-- > 0 && !isInterrupted.get()) { |
| 727 |
Thread.sleep(50); |
| 728 |
} |
| 729 |
|
| 730 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 731 |
} finally { |
| 732 |
exec.shutdown(); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
@Test |
| 737 |
public void nonInterruptibleWorkerTaskScheduledExecutor() throws Exception { |
| 738 |
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); |
| 739 |
try { |
| 740 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 741 |
|
| 742 |
Worker worker = scheduler.createWorker(); |
| 743 |
|
| 744 |
try { |
| 745 |
final AtomicInteger sync = new AtomicInteger(2); |
| 746 |
|
| 747 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 748 |
|
| 749 |
Disposable d = worker.schedule(new Runnable() { |
| 750 |
@Override |
| 751 |
public void run() { |
| 752 |
if (sync.decrementAndGet() != 0) { |
| 753 |
while (sync.get() != 0) { } |
| 754 |
} |
| 755 |
try { |
| 756 |
Thread.sleep(1000); |
| 757 |
} catch (InterruptedException ex) { |
| 758 |
isInterrupted.set(true); |
| 759 |
} |
| 760 |
} |
| 761 |
}); |
| 762 |
|
| 763 |
if (sync.decrementAndGet() != 0) { |
| 764 |
while (sync.get() != 0) { } |
| 765 |
} |
| 766 |
|
| 767 |
Thread.sleep(500); |
| 768 |
|
| 769 |
d.dispose(); |
| 770 |
|
| 771 |
int i = 20; |
| 772 |
while (i-- > 0 && !isInterrupted.get()) { |
| 773 |
Thread.sleep(50); |
| 774 |
} |
| 775 |
|
| 776 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 777 |
} finally { |
| 778 |
worker.dispose(); |
| 779 |
} |
| 780 |
} finally { |
| 781 |
exec.shutdown(); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
@Test |
| 786 |
public void nonInterruptibleDirectTaskTimed() throws Exception { |
| 787 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 788 |
try { |
| 789 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 790 |
|
| 791 |
final AtomicInteger sync = new AtomicInteger(2); |
| 792 |
|
| 793 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 794 |
|
| 795 |
Disposable d = scheduler.scheduleDirect(new Runnable() { |
| 796 |
@Override |
| 797 |
public void run() { |
| 798 |
if (sync.decrementAndGet() != 0) { |
| 799 |
while (sync.get() != 0) { } |
| 800 |
} |
| 801 |
try { |
| 802 |
Thread.sleep(1000); |
| 803 |
} catch (InterruptedException ex) { |
| 804 |
isInterrupted.set(true); |
| 805 |
} |
| 806 |
} |
| 807 |
}, 1, TimeUnit.MILLISECONDS); |
| 808 |
|
| 809 |
if (sync.decrementAndGet() != 0) { |
| 810 |
while (sync.get() != 0) { } |
| 811 |
} |
| 812 |
|
| 813 |
Thread.sleep(500); |
| 814 |
|
| 815 |
d.dispose(); |
| 816 |
|
| 817 |
int i = 20; |
| 818 |
while (i-- > 0 && !isInterrupted.get()) { |
| 819 |
Thread.sleep(50); |
| 820 |
} |
| 821 |
|
| 822 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 823 |
} finally { |
| 824 |
exec.shutdown(); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
@Test |
| 829 |
public void nonInterruptibleWorkerTaskTimed() throws Exception { |
| 830 |
ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 831 |
try { |
| 832 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 833 |
|
| 834 |
Worker worker = scheduler.createWorker(); |
| 835 |
|
| 836 |
try { |
| 837 |
final AtomicInteger sync = new AtomicInteger(2); |
| 838 |
|
| 839 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 840 |
|
| 841 |
Disposable d = worker.schedule(new Runnable() { |
| 842 |
@Override |
| 843 |
public void run() { |
| 844 |
if (sync.decrementAndGet() != 0) { |
| 845 |
while (sync.get() != 0) { } |
| 846 |
} |
| 847 |
try { |
| 848 |
Thread.sleep(1000); |
| 849 |
} catch (InterruptedException ex) { |
| 850 |
isInterrupted.set(true); |
| 851 |
} |
| 852 |
} |
| 853 |
}, 1, TimeUnit.MILLISECONDS); |
| 854 |
|
| 855 |
if (sync.decrementAndGet() != 0) { |
| 856 |
while (sync.get() != 0) { } |
| 857 |
} |
| 858 |
|
| 859 |
Thread.sleep(500); |
| 860 |
|
| 861 |
d.dispose(); |
| 862 |
|
| 863 |
int i = 20; |
| 864 |
while (i-- > 0 && !isInterrupted.get()) { |
| 865 |
Thread.sleep(50); |
| 866 |
} |
| 867 |
|
| 868 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 869 |
} finally { |
| 870 |
worker.dispose(); |
| 871 |
} |
| 872 |
} finally { |
| 873 |
exec.shutdown(); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
@Test |
| 878 |
public void nonInterruptibleDirectTaskScheduledExecutorTimed() throws Exception { |
| 879 |
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); |
| 880 |
try { |
| 881 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 882 |
|
| 883 |
final AtomicInteger sync = new AtomicInteger(2); |
| 884 |
|
| 885 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 886 |
|
| 887 |
Disposable d = scheduler.scheduleDirect(new Runnable() { |
| 888 |
@Override |
| 889 |
public void run() { |
| 890 |
if (sync.decrementAndGet() != 0) { |
| 891 |
while (sync.get() != 0) { } |
| 892 |
} |
| 893 |
try { |
| 894 |
Thread.sleep(1000); |
| 895 |
} catch (InterruptedException ex) { |
| 896 |
isInterrupted.set(true); |
| 897 |
} |
| 898 |
} |
| 899 |
}, 1, TimeUnit.MILLISECONDS); |
| 900 |
|
| 901 |
if (sync.decrementAndGet() != 0) { |
| 902 |
while (sync.get() != 0) { } |
| 903 |
} |
| 904 |
|
| 905 |
Thread.sleep(500); |
| 906 |
|
| 907 |
d.dispose(); |
| 908 |
|
| 909 |
int i = 20; |
| 910 |
while (i-- > 0 && !isInterrupted.get()) { |
| 911 |
Thread.sleep(50); |
| 912 |
} |
| 913 |
|
| 914 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 915 |
} finally { |
| 916 |
exec.shutdown(); |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
@Test |
| 921 |
public void nonInterruptibleWorkerTaskScheduledExecutorTimed() throws Exception { |
| 922 |
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); |
| 923 |
try { |
| 924 |
Scheduler scheduler = Schedulers.from(exec, false); |
| 925 |
|
| 926 |
Worker worker = scheduler.createWorker(); |
| 927 |
|
| 928 |
try { |
| 929 |
final AtomicInteger sync = new AtomicInteger(2); |
| 930 |
|
| 931 |
final AtomicBoolean isInterrupted = new AtomicBoolean(); |
| 932 |
|
| 933 |
Disposable d = worker.schedule(new Runnable() { |
| 934 |
@Override |
| 935 |
public void run() { |
| 936 |
if (sync.decrementAndGet() != 0) { |
| 937 |
while (sync.get() != 0) { } |
| 938 |
} |
| 939 |
try { |
| 940 |
Thread.sleep(1000); |
| 941 |
} catch (InterruptedException ex) { |
| 942 |
isInterrupted.set(true); |
| 943 |
} |
| 944 |
} |
| 945 |
}, 1, TimeUnit.MILLISECONDS); |
| 946 |
|
| 947 |
if (sync.decrementAndGet() != 0) { |
| 948 |
while (sync.get() != 0) { } |
| 949 |
} |
| 950 |
|
| 951 |
Thread.sleep(500); |
| 952 |
|
| 953 |
d.dispose(); |
| 954 |
|
| 955 |
int i = 20; |
| 956 |
while (i-- > 0 && !isInterrupted.get()) { |
| 957 |
Thread.sleep(50); |
| 958 |
} |
| 959 |
|
| 960 |
assertFalse("Interruption happened", isInterrupted.get()); |
| 961 |
} finally { |
| 962 |
worker.dispose(); |
| 963 |
} |
| 964 |
} finally { |
| 965 |
exec.shutdown(); |
| 966 |
} |
| 967 |
} |
| 968 |
} |