Skip to content

Commit d9c859b

Browse files
authored
GEODE-8650: Support multiple instances of DistributedReference (apache#5664)
* GEODE-8650: Support multiple instances of DistributedReference Includes additional improvements for usage of DistributedReference in tests. * * Rename all CompletionUtils to close * Annotate all overridden methods * Suppress false warnings
1 parent 4ceabc8 commit d9c859b

File tree

4 files changed

+341
-29
lines changed

4 files changed

+341
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3+
* agreements. See the NOTICE file distributed with this work for additional information regarding
4+
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
5+
* "License"); you may not use this file except in compliance with the License. You may obtain a
6+
* copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the License
11+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12+
* or implied. See the License for the specific language governing permissions and limitations under
13+
* the License.
14+
*/
15+
package org.apache.geode.util.internal;
16+
17+
import java.util.concurrent.CountDownLatch;
18+
import java.util.concurrent.atomic.AtomicBoolean;
19+
import java.util.concurrent.atomic.AtomicReference;
20+
21+
/**
22+
* Collection of utilities for changing simple Java util structures to a state of completion or
23+
* default.
24+
*
25+
* <p>
26+
* All utilities herein throw checked exceptions wrapped within a runtime exception.
27+
*/
28+
public class CompletionUtils {
29+
30+
/**
31+
* Closes the {@code AutoCloseable}.
32+
*/
33+
public static void close(AutoCloseable autoCloseable) {
34+
try {
35+
autoCloseable.close();
36+
} catch (Exception e) {
37+
throw new RuntimeException(e);
38+
}
39+
}
40+
41+
/**
42+
* Opens the {@code CountDownLatch} by counting it down.
43+
*/
44+
public static void close(CountDownLatch countDownLatch) {
45+
while (countDownLatch.getCount() > 0) {
46+
countDownLatch.countDown();
47+
}
48+
}
49+
50+
/**
51+
* Sets the {@code AtomicBoolean} to false.
52+
*/
53+
public static void close(AtomicBoolean atomicBoolean) {
54+
atomicBoolean.set(false);
55+
}
56+
57+
/**
58+
* Sets the {@code AtomicReference} to null.
59+
*/
60+
public static void close(AtomicReference<?> atomicReference) {
61+
atomicReference.set(null);
62+
}
63+
}

geode-dunit/src/distributedTest/java/org/apache/geode/test/dunit/rules/tests/DistributedReferenceTest.java

+205-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.io.Closeable;
3030
import java.io.IOException;
3131
import java.io.Serializable;
32+
import java.util.concurrent.CountDownLatch;
33+
import java.util.concurrent.atomic.AtomicBoolean;
3234
import java.util.concurrent.atomic.AtomicReference;
3335

3436
import org.junit.Before;
@@ -212,6 +214,69 @@ public void disconnectsWithDisconnectInEachVm() {
212214
}
213215
}
214216

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+
215280
public static class SetAutoCloseableInLocalVm implements Serializable {
216281

217282
private static final AtomicReference<AutoCloseable> autoCloseable = new AtomicReference<>();
@@ -587,6 +652,145 @@ public void hasWithStopInEachVm() {
587652
}
588653
}
589654

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+
590794
@SuppressWarnings("WeakerAccess")
591795
public static class WithClose {
592796

@@ -633,7 +837,7 @@ public String toString() {
633837
}
634838
}
635839

636-
@SuppressWarnings("unused")
840+
@SuppressWarnings({"unused", "WeakerAccess"})
637841
public static class WithStop {
638842

639843
private final String value;

geode-dunit/src/main/java/org/apache/geode/test/dunit/VM.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,9 @@ public synchronized void makeAvailable() {
533533
*
534534
* @throws RMIException if an exception occurs while bouncing this {@code VM}
535535
*/
536-
public void bounce() {
536+
public VM bounce() {
537537
bounce(version, false);
538+
return this;
538539
}
539540

540541
/**
@@ -550,12 +551,14 @@ public void bounce() {
550551
*
551552
* @throws RMIException if an exception occurs while bouncing this {@code VM}
552553
*/
553-
public void bounceForcibly() {
554+
public VM bounceForcibly() {
554555
bounce(version, true);
556+
return this;
555557
}
556558

557-
public void bounce(final String targetVersion) {
559+
public VM bounce(final String targetVersion) {
558560
bounce(targetVersion, false);
561+
return this;
559562
}
560563

561564
private synchronized void bounce(final String targetVersion, boolean force) {

0 commit comments

Comments
 (0)