|
26 | 26 | import static org.junit.Assert.assertNotSame; |
27 | 27 | import static org.junit.Assert.assertNull; |
28 | 28 | import static org.junit.Assert.assertSame; |
| 29 | +import static org.junit.Assert.assertThrows; |
29 | 30 | import static org.junit.Assert.assertTrue; |
30 | 31 | import static org.junit.Assert.fail; |
31 | 32 |
|
|
49 | 50 | import java.util.concurrent.TimeoutException; |
50 | 51 | import java.util.concurrent.atomic.AtomicBoolean; |
51 | 52 | import java.util.concurrent.atomic.AtomicReference; |
| 53 | +import java.util.function.Supplier; |
52 | 54 | import java.util.logging.Handler; |
53 | 55 | import java.util.logging.Level; |
54 | 56 | import java.util.logging.LogRecord; |
@@ -553,6 +555,75 @@ public Object call() { |
553 | 555 | current.detach(Context.ROOT); |
554 | 556 | } |
555 | 557 |
|
| 558 | + @Test |
| 559 | + public void supply_executesSupplierUnderTargetContextAndRestoresPrevious() { |
| 560 | + Context base = Context.current().withValue(PET, "cat"); |
| 561 | + Context current = Context.current().withValue(PET, "fish"); |
| 562 | + Context toRestore = current.attach(); |
| 563 | + |
| 564 | + final Object ret = new Object(); |
| 565 | + Supplier<Object> supplier = new Supplier<Object>() { |
| 566 | + @Override |
| 567 | + public Object get() { |
| 568 | + runner.run(); |
| 569 | + return ret; |
| 570 | + } |
| 571 | + }; |
| 572 | + |
| 573 | + Object result = base.supply(supplier); |
| 574 | + |
| 575 | + assertSame(ret, result); |
| 576 | + assertSame(base, observed); |
| 577 | + assertSame(current, Context.current()); |
| 578 | + |
| 579 | + current.detach(toRestore); |
| 580 | + } |
| 581 | + |
| 582 | + @Test |
| 583 | + public void supply_whenContextIsAlreadyCurrent_executesAndMaintainsContext() { |
| 584 | + Context current = Context.current().withValue(PET, "fish"); |
| 585 | + Context toRestore = current.attach(); |
| 586 | + |
| 587 | + final Object ret = new Object(); |
| 588 | + Supplier<Object> supplier = new Supplier<Object>() { |
| 589 | + @Override |
| 590 | + public Object get() { |
| 591 | + runner.run(); |
| 592 | + return ret; |
| 593 | + } |
| 594 | + }; |
| 595 | + |
| 596 | + Object result = current.supply(supplier); |
| 597 | + |
| 598 | + assertSame(ret, result); |
| 599 | + assertSame(current, observed); |
| 600 | + assertSame(current, Context.current()); |
| 601 | + |
| 602 | + current.detach(toRestore); |
| 603 | + } |
| 604 | + |
| 605 | + @Test |
| 606 | + public void supply_whenSupplierThrows_propagatesExceptionAndRestoresPreviousContext() { |
| 607 | + Context base = Context.current().withValue(PET, "cat"); |
| 608 | + Context current = Context.current().withValue(PET, "fish"); |
| 609 | + Context toRestore = current.attach(); |
| 610 | + |
| 611 | + final TestError err = new TestError(); |
| 612 | + Supplier<Object> supplier = new Supplier<Object>() { |
| 613 | + @Override |
| 614 | + public Object get() { |
| 615 | + throw err; |
| 616 | + } |
| 617 | + }; |
| 618 | + |
| 619 | + TestError thrown = assertThrows(TestError.class, () -> base.supply(supplier)); |
| 620 | + |
| 621 | + assertSame(err, thrown); |
| 622 | + assertSame(current, Context.current()); |
| 623 | + |
| 624 | + current.detach(toRestore); |
| 625 | + } |
| 626 | + |
556 | 627 | @Test |
557 | 628 | public void currentContextExecutor() { |
558 | 629 | QueuedExecutor queuedExecutor = new QueuedExecutor(); |
|
0 commit comments