Skip to content

Commit

Permalink
GEODE-8659: Consolidating locator-wait-time-tests
Browse files Browse the repository at this point in the history
We had two different tests of this functionality that were testing almost the
same thing. The first test had a slightly odd configuration - only one of the
locators knew about the other.

Combining these into a single test that locators wait for locator-wait-time and
stop waiting when all locators are found.
  • Loading branch information
upthewaterspout authored Oct 30, 2020
1 parent 08e9e96 commit a00f18d
Showing 1 changed file with 18 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,41 +187,26 @@ public void secondMembershipCanJoinUsingTheSecondLocatorToStart()
}

@Test
public void secondMembershipPausesForLocatorWaitTime()
throws IOException, MemberStartupException, InterruptedException {
public void locatorWaitsForLocatorWaitTimeUntilAllLocatorsContacted()
throws InterruptedException, TimeoutException, ExecutionException {

/*
* Start a locator for the coordinator (membership) so we have a port for it.
*
* Its locator-wait-time is set to 0 so it eventually (soon after membership is started) forms a
* distributed system and becomes a coordinator.
*/

final MembershipLocator<MemberIdentifier> coordinatorLocator = createLocator(0);
coordinatorLocator.start();
final int coordinatorLocatorPort = coordinatorLocator.getPort();
final Supplier<ExecutorService> executorServiceSupplier =
() -> LoggingExecutors.newCachedThreadPool("membership", false);

final Membership<MemberIdentifier> coordinatorMembership =
createMembership(coordinatorLocator, coordinatorLocatorPort);
int[] locatorPorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);

/*
* We have not even started the membership yet — connection attempts will certainly fail until
* we do. This is a bit like the locator (host) not being present in DNS (yet).
*/
int locatorWaitTime = (int) Duration.ofMinutes(5).getSeconds();
final MembershipConfig config =
createMembershipConfig(true, locatorWaitTime, locatorPorts[0], locatorPorts[1]);

/*
* Start a second locator and membership trying to join via the coordinator (membership) that
* hasn't yet started behind the port.
* Start a locator trying to contact the locator that hasn't started it's port
*
* Set its locator-wait-time so it'll not become a coordinator right away, allowing time for the
* other member to start and become a coordinator.
*/

final MembershipLocator<MemberIdentifier> lateJoiningLocator = createLocator(0);
lateJoiningLocator.start();
final int lateJoiningLocatorPort = lateJoiningLocator.getPort();

final int[] locatorPorts = new int[] {coordinatorLocatorPort, lateJoiningLocatorPort};
CompletableFuture<Membership<MemberIdentifier>> createMembership0 =
launchLocator(executorServiceSupplier, locatorPorts[0], config);

// minimum duration a locator waits to become the coordinator, regardless of locatorWaitTime
final Duration minimumJoinWaitTime = Duration
Expand All @@ -230,27 +215,6 @@ public void secondMembershipPausesForLocatorWaitTime()
// expected number of retries in GMSJoinLeave.join()
.multipliedBy(getMinimumRetriesBeforeBecomingCoordinator(locatorPorts.length));

/*
* By setting locatorWaitTime to 10x the minimumJoinWaitTime, we are trying to make sure the
* locatorWaitTime is sufficiently larger than the minimum so we can reliably detect whether
* the lateJoiningMembership is waiting for the full locatorWaitTime and not just the minimum
* wait time.
*/
final int locatorWaitTime = (int) (10 * minimumJoinWaitTime.getSeconds());

final MembershipConfig lateJoiningMembershipConfig =
createMembershipConfig(true, locatorWaitTime, locatorPorts);
final Membership<MemberIdentifier> lateJoiningMembership =
createMembership(lateJoiningMembershipConfig, lateJoiningLocator);

CompletableFuture<Void> lateJoiningMembershipStartup = executorServiceRule.runAsync(() -> {
try {
start(lateJoiningMembership);
} catch (MemberStartupException e) {
throw new RuntimeException(e);
}
});

/*
* By sleeping for 2x the minimumJoinWaitTime, we are trying to make sure we sleep for
* longer than the minimum but shorter than the locatorWaitTime so we can detect whether the
Expand All @@ -259,56 +223,28 @@ public void secondMembershipPausesForLocatorWaitTime()
*/
Thread.sleep(2 * minimumJoinWaitTime.toMillis());

assertThat(createMembership0.getNow(null)).isNull();

/*
* Now start the coordinator (membership), after waiting longer than the minimum wait time for
* Now start the other locator, after waiting longer than the minimum wait time for
* connecting to a locator but shorter than the locator-wait-time.
*/
start(coordinatorMembership);

await().untilAsserted(() -> assertThat(lateJoiningMembershipStartup).isCompleted());

await().untilAsserted(
() -> assertThat(coordinatorMembership.getView().getMembers()).hasSize(2));
await().untilAsserted(
() -> assertThat(lateJoiningMembership.getView().getMembers()).hasSize(2));

stop(coordinatorMembership, lateJoiningMembership);
stop(coordinatorLocator, lateJoiningLocator);
}

@Test
public void locatorsStopWaitingForLocatorWaitTimeIfAllLocatorsContacted()
throws IOException, MemberStartupException, InterruptedException, TimeoutException,
ExecutionException {

final Supplier<ExecutorService> executorServiceSupplier =
() -> LoggingExecutors.newCachedThreadPool("membership", false);

int[] locatorPorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);

int locatorWaitTime = (int) Duration.ofMinutes(5).getSeconds();
final MembershipConfig config =
createMembershipConfig(true, locatorWaitTime, locatorPorts[0], locatorPorts[1]);

CompletableFuture<Membership<MemberIdentifier>> createMembership0 =
launchLocator(executorServiceSupplier, locatorPorts[0], config);

// Assert that membership 0 is waiting for the other locator to start
Thread.sleep(5000);
assertThat(createMembership0.getNow(null)).isNull();

CompletableFuture<Membership<MemberIdentifier>> createMembership1 =
launchLocator(executorServiceSupplier, locatorPorts[1], config);


// Make sure the members are created in less than the locator-wait-time
Membership<MemberIdentifier> membership0 = createMembership0.get(2, TimeUnit.MINUTES);
Membership<MemberIdentifier> membership1 = createMembership1.get(2, TimeUnit.MINUTES);

// Make sure the members see each other in the view
await().untilAsserted(() -> assertThat(membership0.getView().getMembers()).hasSize(2));
await().untilAsserted(() -> assertThat(membership1.getView().getMembers()).hasSize(2));

stop(membership0, membership1);
}


private CompletableFuture<Membership<MemberIdentifier>> launchLocator(
Supplier<ExecutorService> executorServiceSupplier, int locatorPort, MembershipConfig config) {
return executorServiceRule.supplyAsync(() -> {
Expand Down

0 comments on commit a00f18d

Please sign in to comment.