Commit 4aaa7d05 by Bartłomiej Słota

InstanceRegistry class refactor + renew method test #1376

parent 0985c8f0
...@@ -36,6 +36,7 @@ import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl; ...@@ -36,6 +36,7 @@ import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl;
import com.netflix.eureka.resources.ServerCodecs; import com.netflix.eureka.resources.ServerCodecs;
import lombok.extern.apachecommons.CommonsLog; import lombok.extern.apachecommons.CommonsLog;
import org.springframework.context.ApplicationEvent;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
...@@ -79,33 +80,27 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl ...@@ -79,33 +80,27 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
@Override @Override
public void register(InstanceInfo info, int leaseDuration, boolean isReplication) { public void register(InstanceInfo info, int leaseDuration, boolean isReplication) {
logRegistration(info, isReplication, leaseDuration); handleRegistration(info, leaseDuration, isReplication);
publishEurekaInstanceRegisteredEvent(info, leaseDuration, isReplication);
super.register(info, leaseDuration, isReplication); super.register(info, leaseDuration, isReplication);
} }
@Override @Override
public void register(final InstanceInfo info, final boolean isReplication) { public void register(final InstanceInfo info, final boolean isReplication) {
final int instanceLeaseDuration = resolveInstanceLeaseDuration(info); handleRegistration(info, resolveInstanceLeaseDuration(info), isReplication);
logRegistration(info, isReplication, instanceLeaseDuration);
publishEurekaInstanceRegisteredEvent(info, instanceLeaseDuration, isReplication);
super.register(info, isReplication); super.register(info, isReplication);
} }
@Override @Override
public boolean cancel(String appName, String serverId, boolean isReplication) { public boolean cancel(String appName, String serverId, boolean isReplication) {
logCancelation(appName, serverId, isReplication); handleCancelation(appName, serverId, isReplication);
publishEurekaInstanceCanceledEvent(appName, serverId, isReplication);
return super.cancel(appName, serverId, isReplication); return super.cancel(appName, serverId, isReplication);
} }
@Override @Override
public boolean renew(final String appName, final String serverId, public boolean renew(final String appName, final String serverId,
boolean isReplication) { boolean isReplication) {
if (log.isDebugEnabled()) { log("renew " + appName + " serverId " + serverId + ", isReplication {}"
log.debug("renew " + appName + " serverId " + serverId + ", isReplication {}" + isReplication);
+ isReplication);
}
List<Application> applications = getSortedApplications(); List<Application> applications = getSortedApplications();
for (Application input : applications) { for (Application input : applications) {
if (input.getName().equals(appName)) { if (input.getName().equals(appName)) {
...@@ -116,8 +111,8 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl ...@@ -116,8 +111,8 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
break; break;
} }
} }
this.ctxt.publishEvent(new EurekaInstanceRenewedEvent(this, appName, publishEvent(new EurekaInstanceRenewedEvent(this, appName, serverId,
serverId, instance, isReplication)); instance, isReplication));
break; break;
} }
} }
...@@ -126,38 +121,32 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl ...@@ -126,38 +121,32 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
@Override @Override
protected boolean internalCancel(String appName, String id, boolean isReplication) { protected boolean internalCancel(String appName, String id, boolean isReplication) {
logCancelation(appName, id, isReplication); handleCancelation(appName, id, isReplication);
publishEurekaInstanceCanceledEvent(appName, id, isReplication);
return super.internalCancel(appName, id, isReplication); return super.internalCancel(appName, id, isReplication);
} }
private void logRegistration(InstanceInfo info, boolean isReplication, private void handleCancelation(String appName, String id, boolean isReplication) {
int instanceLeaseDuration) { log("cancel " + appName + ", serverId " + id + ", isReplication " + isReplication);
if (log.isDebugEnabled()) { publishEvent(new EurekaInstanceCanceledEvent(this, appName, id, isReplication));
log.debug("register " + info.getAppName() + ", vip " + info.getVIPAddress()
+ ", leaseDuration " + instanceLeaseDuration + ", isReplication "
+ isReplication);
}
} }
private void logCancelation(String appName, String serverId, boolean isReplication) { private void handleRegistration(InstanceInfo info, int leaseDuration,
boolean isReplication) {
log("register " + info.getAppName() + ", vip " + info.getVIPAddress()
+ ", leaseDuration " + leaseDuration + ", isReplication "
+ isReplication);
publishEvent(new EurekaInstanceRegisteredEvent(this, info, leaseDuration,
isReplication));
}
private void log(String message) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("cancel " + appName + " serverId " + serverId + ", isReplication " log.debug(message);
+ isReplication);
} }
} }
private void publishEurekaInstanceRegisteredEvent(InstanceInfo info, private void publishEvent(ApplicationEvent applicationEvent) {
int leaseDuration, boolean isReplication) { this.ctxt.publishEvent(applicationEvent);
// TODO: what to publish from info (whole object?)
this.ctxt.publishEvent(new EurekaInstanceRegisteredEvent(this, info,
leaseDuration, isReplication));
}
private void publishEurekaInstanceCanceledEvent(String appName, String serverId,
boolean isReplication) {
this.ctxt.publishEvent(
new EurekaInstanceCanceledEvent(this, appName, serverId, isReplication));
} }
private int resolveInstanceLeaseDuration(final InstanceInfo info) { private int resolveInstanceLeaseDuration(final InstanceInfo info) {
......
...@@ -3,10 +3,13 @@ package org.springframework.cloud.netflix.eureka.server; ...@@ -3,10 +3,13 @@ package org.springframework.cloud.netflix.eureka.server;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Matchers.isA; import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import com.netflix.discovery.shared.Application;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -17,9 +20,10 @@ import org.springframework.boot.builder.SpringApplicationBuilder; ...@@ -17,9 +20,10 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.cloud.netflix.eureka.server.InstanceRegistryTest.Application; import org.springframework.cloud.netflix.eureka.server.InstanceRegistryTest.TestApplication;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent; import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent; import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -33,13 +37,15 @@ import com.netflix.eureka.registry.PeerAwareInstanceRegistry; ...@@ -33,13 +37,15 @@ import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
* @author Bartlomiej Slota * @author Bartlomiej Slota
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, @SpringBootTest(classes = TestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
value = {"spring.application.name=eureka", "logging.level.org.springframework." value = {"spring.application.name=eureka", "logging.level.org.springframework."
+ "cloud.netflix.eureka.server.InstanceRegistry=DEBUG"}) + "cloud.netflix.eureka.server.InstanceRegistry=DEBUG"})
public class InstanceRegistryTest { public class InstanceRegistryTest {
private final List<ApplicationEvent> applicationEvents = new LinkedList<>(); private final List<ApplicationEvent> applicationEvents = new LinkedList<>();
private static final String APP_NAME = "MY-APP-NAME";
private static final String HOST_NAME = "my-host-name";
@SpyBean(PeerAwareInstanceRegistry.class) @SpyBean(PeerAwareInstanceRegistry.class)
private InstanceRegistry instanceRegistry; private InstanceRegistry instanceRegistry;
...@@ -52,6 +58,9 @@ public class InstanceRegistryTest { ...@@ -52,6 +58,9 @@ public class InstanceRegistryTest {
private ApplicationListener<EurekaInstanceCanceledEvent> private ApplicationListener<EurekaInstanceCanceledEvent>
instanceCanceledEventListenerMock; instanceCanceledEventListenerMock;
@MockBean
private ApplicationListener<EurekaInstanceRenewedEvent> instanceRenewedEventListener;
@Before @Before
public void setup() { public void setup() {
applicationEvents.clear(); applicationEvents.clear();
...@@ -60,21 +69,23 @@ public class InstanceRegistryTest { ...@@ -60,21 +69,23 @@ public class InstanceRegistryTest {
.onApplicationEvent(isA(EurekaInstanceRegisteredEvent.class)); .onApplicationEvent(isA(EurekaInstanceRegisteredEvent.class));
doAnswer(applicationListenerAnswer).when(instanceCanceledEventListenerMock) doAnswer(applicationListenerAnswer).when(instanceCanceledEventListenerMock)
.onApplicationEvent(isA(EurekaInstanceCanceledEvent.class)); .onApplicationEvent(isA(EurekaInstanceCanceledEvent.class));
doAnswer(applicationListenerAnswer).when(instanceRenewedEventListener)
.onApplicationEvent(isA(EurekaInstanceRenewedEvent.class));
} }
@Test @Test
public void testRegister() throws Exception { public void testRegister() throws Exception {
// creating instance info // creating instance info
LeaseInfo leaseInfo = getLeaseInfo(); final LeaseInfo leaseInfo = getLeaseInfo();
InstanceInfo instanceInfo = getInstanceInfo(leaseInfo); final InstanceInfo instanceInfo = getInstanceInfo(leaseInfo);
// calling tested method // calling tested method
instanceRegistry.register(instanceInfo, false); instanceRegistry.register(instanceInfo, false);
// event of proper type is registered // event of proper type is registered
assertEquals(1, applicationEvents.size()); assertEquals(1, applicationEvents.size());
assertTrue(applicationEvents.get(0) instanceof EurekaInstanceRegisteredEvent); assertTrue(applicationEvents.get(0) instanceof EurekaInstanceRegisteredEvent);
// event details are correct // event details are correct
EurekaInstanceRegisteredEvent registeredEvent = final EurekaInstanceRegisteredEvent registeredEvent =
(EurekaInstanceRegisteredEvent) (applicationEvents.get(0)); (EurekaInstanceRegisteredEvent) (applicationEvents.get(0));
assertEquals(instanceInfo, registeredEvent.getInstanceInfo()); assertEquals(instanceInfo, registeredEvent.getInstanceInfo());
assertEquals(leaseInfo.getDurationInSecs(), registeredEvent.getLeaseDuration()); assertEquals(leaseInfo.getDurationInSecs(), registeredEvent.getLeaseDuration());
...@@ -85,11 +96,11 @@ public class InstanceRegistryTest { ...@@ -85,11 +96,11 @@ public class InstanceRegistryTest {
@Test @Test
public void testDefaultLeaseDurationRegisterEvent() throws Exception { public void testDefaultLeaseDurationRegisterEvent() throws Exception {
// creating instance info // creating instance info
InstanceInfo instanceInfo = getInstanceInfo(null); final InstanceInfo instanceInfo = getInstanceInfo(null);
// calling tested method // calling tested method
instanceRegistry.register(instanceInfo, false); instanceRegistry.register(instanceInfo, false);
// instance info duration is set to default // instance info duration is set to default
EurekaInstanceRegisteredEvent registeredEvent = final EurekaInstanceRegisteredEvent registeredEvent =
(EurekaInstanceRegisteredEvent) (applicationEvents.get(0)); (EurekaInstanceRegisteredEvent) (applicationEvents.get(0));
assertEquals(LeaseInfo.DEFAULT_LEASE_DURATION, assertEquals(LeaseInfo.DEFAULT_LEASE_DURATION,
registeredEvent.getLeaseDuration()); registeredEvent.getLeaseDuration());
...@@ -98,25 +109,52 @@ public class InstanceRegistryTest { ...@@ -98,25 +109,52 @@ public class InstanceRegistryTest {
@Test @Test
public void testInternalCancel() throws Exception { public void testInternalCancel() throws Exception {
// calling tested method // calling tested method
instanceRegistry.internalCancel("my-app", "appId", false); instanceRegistry.internalCancel(APP_NAME, HOST_NAME, false);
// event of proper type is registered // event of proper type is registered
assertEquals(1, applicationEvents.size()); assertEquals(1, applicationEvents.size());
assertTrue(applicationEvents.get(0) instanceof EurekaInstanceCanceledEvent); assertTrue(applicationEvents.get(0) instanceof EurekaInstanceCanceledEvent);
// event details are correct // event details are correct
EurekaInstanceCanceledEvent registeredEvent = final EurekaInstanceCanceledEvent registeredEvent =
(EurekaInstanceCanceledEvent) (applicationEvents.get(0)); (EurekaInstanceCanceledEvent) (applicationEvents.get(0));
assertEquals("my-app", registeredEvent.getAppName()); assertEquals(APP_NAME, registeredEvent.getAppName());
assertEquals("appId", registeredEvent.getServerId()); assertEquals(HOST_NAME, registeredEvent.getServerId());
assertEquals(instanceRegistry, registeredEvent.getSource());
assertFalse(registeredEvent.isReplication());
}
@Test
public void testRenew() throws Exception {
// creating application list
final LeaseInfo leaseInfo = getLeaseInfo();
final InstanceInfo instanceInfo = getInstanceInfo(leaseInfo);
final List<InstanceInfo> instances = new ArrayList<>();
instances.add(instanceInfo);
final Application application = new Application(APP_NAME, instances);
final List<Application> applications = new ArrayList<>();
applications.add(application);
// stubbing applications list
doReturn(applications).when(instanceRegistry).getSortedApplications();
// calling tested method
instanceRegistry.renew(APP_NAME, HOST_NAME, false);
// event of proper type is registered
assertEquals(1, applicationEvents.size());
assertTrue(applicationEvents.get(0) instanceof EurekaInstanceRenewedEvent);
// event details are correct
final EurekaInstanceRenewedEvent registeredEvent = (EurekaInstanceRenewedEvent)
(applicationEvents.get(0));
assertEquals(APP_NAME, registeredEvent.getAppName());
assertEquals(HOST_NAME, registeredEvent.getServerId());
assertEquals(instanceRegistry, registeredEvent.getSource()); assertEquals(instanceRegistry, registeredEvent.getSource());
assertEquals(instanceInfo, registeredEvent.getInstanceInfo());
assertFalse(registeredEvent.isReplication()); assertFalse(registeredEvent.isReplication());
} }
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@EnableEurekaServer @EnableEurekaServer
protected static class Application { protected static class TestApplication {
public static void main(String[] args) { public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args); new SpringApplicationBuilder(TestApplication.class).run(args);
} }
} }
...@@ -129,8 +167,8 @@ public class InstanceRegistryTest { ...@@ -129,8 +167,8 @@ public class InstanceRegistryTest {
private InstanceInfo getInstanceInfo(LeaseInfo leaseInfo) { private InstanceInfo getInstanceInfo(LeaseInfo leaseInfo) {
InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder(); InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder();
builder.setAppName("my-app-name"); builder.setAppName(APP_NAME);
builder.setHostName("my-host-name"); builder.setHostName(HOST_NAME);
builder.setPort(8008); builder.setPort(8008);
builder.setLeaseInfo(leaseInfo); builder.setLeaseInfo(leaseInfo);
return builder.build(); return builder.build();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment