|
29 | 29 | import java.io.Closeable;
|
30 | 30 | import java.io.IOException;
|
31 | 31 | import java.io.Serializable;
|
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.atomic.AtomicBoolean; |
32 | 34 | import java.util.concurrent.atomic.AtomicReference;
|
33 | 35 |
|
34 | 36 | import org.junit.Before;
|
@@ -212,6 +214,69 @@ public void disconnectsWithDisconnectInEachVm() {
|
212 | 214 | }
|
213 | 215 | }
|
214 | 216 |
|
| 217 | + @Test |
| 218 | + public void accessesAtomicBooleanInEachVm() { |
| 219 | + runTestWithValidation(SetAtomicBooleanInLocalVm.class); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void setsAtomicBooleanToFalseInEachVm() { |
| 224 | + runTestWithValidation(SetAtomicBooleanInLocalVm.class); |
| 225 | + |
| 226 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 227 | + vm.invoke(() -> { |
| 228 | + assertThat(SetAtomicBooleanInLocalVm.atomicBoolean.get()).isFalse(); |
| 229 | + }); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + public void accessesCountDownLatchInEachVm() { |
| 235 | + runTestWithValidation(SetCountDownLatchInLocalVm.class); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void opensCountDownLatchInEachVm() { |
| 240 | + runTestWithValidation(SetCountDownLatchInLocalVm.class); |
| 241 | + |
| 242 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 243 | + vm.invoke(() -> { |
| 244 | + assertThat(SetCountDownLatchInLocalVm.latch.get().getCount()).isZero(); |
| 245 | + }); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + @Test |
| 250 | + public void accessesTwoReferencesInEachVm() { |
| 251 | + runTestWithValidation(SetTwoCloseablesInEachVm.class); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + public void closesTwoReferencesInEachVm() { |
| 256 | + runTestWithValidation(SetTwoCloseablesInEachVm.class); |
| 257 | + |
| 258 | + getController().invoke(() -> { |
| 259 | + verify(SetTwoCloseablesInEachVm.withClose1.get()).close(); |
| 260 | + verify(SetTwoCloseablesInEachVm.withClose2.get()).close(); |
| 261 | + }); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + public void accessesManyReferencesInEachVm() { |
| 266 | + runTestWithValidation(SetManyReferencesInEachVm.class); |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + public void closesManyReferencesInEachVm() { |
| 271 | + runTestWithValidation(SetManyReferencesInEachVm.class); |
| 272 | + |
| 273 | + getController().invoke(() -> { |
| 274 | + verify(SetManyReferencesInEachVm.withClose.get()).close(); |
| 275 | + verify(SetManyReferencesInEachVm.withDisconnect.get()).disconnect(); |
| 276 | + verify(SetManyReferencesInEachVm.withStop.get()).stop(); |
| 277 | + }); |
| 278 | + } |
| 279 | + |
215 | 280 | public static class SetAutoCloseableInLocalVm implements Serializable {
|
216 | 281 |
|
217 | 282 | private static final AtomicReference<AutoCloseable> autoCloseable = new AtomicReference<>();
|
@@ -587,6 +652,145 @@ public void hasWithStopInEachVm() {
|
587 | 652 | }
|
588 | 653 | }
|
589 | 654 |
|
| 655 | + public static class SetAtomicBooleanInLocalVm implements Serializable { |
| 656 | + |
| 657 | + private static final AtomicReference<AtomicBoolean> atomicBoolean = new AtomicReference<>(); |
| 658 | + |
| 659 | + @Rule |
| 660 | + public DistributedReference<AtomicBoolean> reference = new DistributedReference<>(); |
| 661 | + |
| 662 | + @Before |
| 663 | + public void setUp() { |
| 664 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 665 | + vm.invoke(() -> { |
| 666 | + atomicBoolean.set(new AtomicBoolean(true)); |
| 667 | + reference.set(atomicBoolean.get()); |
| 668 | + }); |
| 669 | + } |
| 670 | + } |
| 671 | + |
| 672 | + @Test |
| 673 | + public void hasAtomicBooleanInEachVm() { |
| 674 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 675 | + vm.invoke(() -> { |
| 676 | + assertThat(reference.get()) |
| 677 | + .isSameAs(atomicBoolean.get()) |
| 678 | + .isTrue(); |
| 679 | + }); |
| 680 | + } |
| 681 | + } |
| 682 | + } |
| 683 | + |
| 684 | + public static class SetCountDownLatchInLocalVm implements Serializable { |
| 685 | + |
| 686 | + private static final AtomicReference<CountDownLatch> latch = new AtomicReference<>(); |
| 687 | + |
| 688 | + @Rule |
| 689 | + public DistributedReference<CountDownLatch> reference = new DistributedReference<>(); |
| 690 | + |
| 691 | + @Before |
| 692 | + public void setUp() { |
| 693 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 694 | + vm.invoke(() -> { |
| 695 | + latch.set(new CountDownLatch(2)); |
| 696 | + reference.set(latch.get()); |
| 697 | + }); |
| 698 | + } |
| 699 | + } |
| 700 | + |
| 701 | + @Test |
| 702 | + public void hasReferenceInLocalVm() { |
| 703 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 704 | + vm.invoke(() -> { |
| 705 | + assertThat(reference.get()).isSameAs(latch.get()); |
| 706 | + assertThat(latch.get().getCount()).isEqualTo(2); |
| 707 | + }); |
| 708 | + } |
| 709 | + } |
| 710 | + } |
| 711 | + |
| 712 | + public static class SetTwoCloseablesInEachVm implements Serializable { |
| 713 | + |
| 714 | + private static final AtomicReference<WithClose> withClose1 = new AtomicReference<>(); |
| 715 | + private static final AtomicReference<WithClose> withClose2 = new AtomicReference<>(); |
| 716 | + |
| 717 | + @Rule |
| 718 | + public DistributedReference<WithClose> reference1 = new DistributedReference<>(); |
| 719 | + @Rule |
| 720 | + public DistributedReference<WithClose> reference2 = new DistributedReference<>(); |
| 721 | + |
| 722 | + @Before |
| 723 | + public void setUp() { |
| 724 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 725 | + vm.invoke(() -> { |
| 726 | + withClose1.set(spy(new WithClose("WithClose1 in VM-" + vm.getId()))); |
| 727 | + reference1.set(withClose1.get()); |
| 728 | + |
| 729 | + withClose2.set(spy(new WithClose("WithClose2 in VM-" + vm.getId()))); |
| 730 | + reference2.set(withClose2.get()); |
| 731 | + }); |
| 732 | + } |
| 733 | + } |
| 734 | + |
| 735 | + @Test |
| 736 | + public void hasTwoWithCloseInEachVm() { |
| 737 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 738 | + vm.invoke(() -> { |
| 739 | + assertThat(reference1.get()).isSameAs(withClose1.get()); |
| 740 | + assertThat(reference2.get()).isSameAs(withClose2.get()); |
| 741 | + |
| 742 | + assertThat(reference1.get().toString()).isEqualTo("WithClose1 in VM-" + vm.getId()); |
| 743 | + assertThat(reference2.get().toString()).isEqualTo("WithClose2 in VM-" + vm.getId()); |
| 744 | + }); |
| 745 | + } |
| 746 | + } |
| 747 | + } |
| 748 | + |
| 749 | + public static class SetManyReferencesInEachVm implements Serializable { |
| 750 | + |
| 751 | + private static final AtomicReference<WithClose> withClose = new AtomicReference<>(); |
| 752 | + private static final AtomicReference<WithDisconnect> withDisconnect = new AtomicReference<>(); |
| 753 | + private static final AtomicReference<WithStop> withStop = new AtomicReference<>(); |
| 754 | + |
| 755 | + @Rule |
| 756 | + public DistributedReference<WithClose> refWithClose = new DistributedReference<>(); |
| 757 | + @Rule |
| 758 | + public DistributedReference<WithDisconnect> refWithDisconnect = new DistributedReference<>(); |
| 759 | + @Rule |
| 760 | + public DistributedReference<WithStop> refWithStop = new DistributedReference<>(); |
| 761 | + |
| 762 | + @Before |
| 763 | + public void setUp() { |
| 764 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 765 | + vm.invoke(() -> { |
| 766 | + withClose.set(spy(new WithClose("WithClose in VM-" + vm.getId()))); |
| 767 | + withDisconnect.set(spy(new WithDisconnect("WithDisconnect in VM-" + vm.getId()))); |
| 768 | + withStop.set(spy(new WithStop("WithStop in VM-" + vm.getId()))); |
| 769 | + |
| 770 | + refWithClose.set(withClose.get()); |
| 771 | + refWithDisconnect.set(withDisconnect.get()); |
| 772 | + refWithStop.set(withStop.get()); |
| 773 | + }); |
| 774 | + } |
| 775 | + } |
| 776 | + |
| 777 | + @Test |
| 778 | + public void hasManyReferencesInEachVm() { |
| 779 | + for (VM vm : asList(getVM(0), getVM(1), getVM(2), getVM(3), getController())) { |
| 780 | + vm.invoke(() -> { |
| 781 | + assertThat(refWithClose.get()).isSameAs(withClose.get()); |
| 782 | + assertThat(refWithDisconnect.get()).isSameAs(withDisconnect.get()); |
| 783 | + assertThat(refWithStop.get()).isSameAs(withStop.get()); |
| 784 | + |
| 785 | + assertThat(refWithClose.get().toString()).isEqualTo("WithClose in VM-" + vm.getId()); |
| 786 | + assertThat(refWithDisconnect.get().toString()) |
| 787 | + .isEqualTo("WithDisconnect in VM-" + vm.getId()); |
| 788 | + assertThat(refWithStop.get().toString()).isEqualTo("WithStop in VM-" + vm.getId()); |
| 789 | + }); |
| 790 | + } |
| 791 | + } |
| 792 | + } |
| 793 | + |
590 | 794 | @SuppressWarnings("WeakerAccess")
|
591 | 795 | public static class WithClose {
|
592 | 796 |
|
@@ -633,7 +837,7 @@ public String toString() {
|
633 | 837 | }
|
634 | 838 | }
|
635 | 839 |
|
636 |
| - @SuppressWarnings("unused") |
| 840 | + @SuppressWarnings({"unused", "WeakerAccess"}) |
637 | 841 | public static class WithStop {
|
638 | 842 |
|
639 | 843 | private final String value;
|
|
0 commit comments