Skip to content

Commit

Permalink
Improve test coverage for PeerAwareInstanceRegistry.
Browse files Browse the repository at this point in the history
In preparation for refactoring the getOverriddenInstanceStatus()
method to remove duplicate code and enable composition of different
"status overriding rules".
  • Loading branch information
Nikos Michalakis committed Jul 13, 2016
1 parent 5cf3c37 commit a5f39ae
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Thumbs.db
*/build
/bin
*/bin
classes

#
# # IntelliJ specific files/directories

Expand Down
13 changes: 13 additions & 0 deletions eureka-core/src/test/java/com/netflix/eureka/AbstractTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,25 @@ protected static InstanceInfo createRemoteInstance(String instanceHostName) {
}

protected static InstanceInfo createLocalInstance(String hostname) {
return createLocalInstanceWithStatus(hostname, InstanceInfo.InstanceStatus.UP);
}

protected static InstanceInfo createLocalStartingInstance(String hostname) {
return createLocalInstanceWithStatus(hostname, InstanceInfo.InstanceStatus.STARTING);
}

protected static InstanceInfo createLocalOutOfServiceInstance(String hostname) {
return createLocalInstanceWithStatus(hostname, InstanceInfo.InstanceStatus.OUT_OF_SERVICE);
}

private static InstanceInfo createLocalInstanceWithStatus(String hostname, InstanceInfo.InstanceStatus status) {
InstanceInfo.Builder instanceBuilder = InstanceInfo.Builder.newBuilder();
instanceBuilder.setAppName(LOCAL_REGION_APP_NAME);
instanceBuilder.setHostName(hostname);
instanceBuilder.setIPAddr("10.10.101.1");
instanceBuilder.setDataCenterInfo(getAmazonInfo(null, hostname));
instanceBuilder.setLeaseInfo(LeaseInfo.Builder.newBuilder().build());
instanceBuilder.setStatus(status);
return instanceBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,63 @@ public void testStatusOverrideSetAndRemoval() throws Exception {
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.UP);
}

@Test
public void testStatusOverrideStartingStatus() throws Exception {
// Regular registration first
InstanceInfo myInstance = createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registerInstanceLocally(myInstance);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.UP);

// Override status
boolean statusResult = registry.statusUpdate(LOCAL_REGION_APP_NAME, myInstance.getId(), InstanceStatus.OUT_OF_SERVICE, "0", false);
assertThat("Couldn't override instance status", statusResult, is(true));
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.OUT_OF_SERVICE);

// If we are not UP or OUT_OF_SERVICE, the OUT_OF_SERVICE override does not apply. It gets trumped by the current
// status (STARTING or DOWN). Here we test with STARTING.
myInstance = createLocalStartingInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registerInstanceLocally(myInstance);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.STARTING);
}

@Test
public void testStatusOverrideWithExistingLeaseUp() throws Exception {
// Without an override we expect to get the existing UP lease when we re-register with OUT_OF_SERVICE.
// First, we are "up".
InstanceInfo myInstance = createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registerInstanceLocally(myInstance);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.UP);

// Then, we re-register with "out of service".
InstanceInfo sameInstance = createLocalOutOfServiceInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registry.register(sameInstance, 10000000, false);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.UP);

// Let's try again. We shouldn't see a difference.
sameInstance = createLocalOutOfServiceInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registry.register(sameInstance, 10000000, false);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.UP);
}

@Test
public void testStatusOverrideWithExistingLeaseOutOfService() throws Exception {
// Without an override we expect to get the existing OUT_OF_SERVICE lease when we re-register with UP.
// First, we are "out of service".
InstanceInfo myInstance = createLocalOutOfServiceInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registerInstanceLocally(myInstance);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.OUT_OF_SERVICE);

// Then, we re-register with "out of service".
InstanceInfo sameInstance = createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registry.register(sameInstance, 10000000, false);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.OUT_OF_SERVICE);

// Let's try again. We shouldn't see a difference.
sameInstance = createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME);
registry.register(sameInstance, 10000000, false);
verifyLocalInstanceStatus(myInstance.getId(), InstanceStatus.OUT_OF_SERVICE);
}

@Test
public void testEvictionTaskCompensationTime() throws Exception {
long evictionTaskPeriodNanos = serverConfig.getEvictionIntervalTimerInMs() * 1000000;
Expand Down

0 comments on commit a5f39ae

Please sign in to comment.