Commit 151e021e by Bartłomiej Słota

formatting fixed + tests, fixes #1376

parent dd97b6a4
...@@ -89,7 +89,7 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl ...@@ -89,7 +89,7 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
final int instanceLeaseDuration = resolveInstanceLeaseDuration(info); final int instanceLeaseDuration = resolveInstanceLeaseDuration(info);
logRegistration(info, isReplication, instanceLeaseDuration); logRegistration(info, isReplication, instanceLeaseDuration);
publishEurekaInstanceRegisteredEvent(info, instanceLeaseDuration, isReplication); publishEurekaInstanceRegisteredEvent(info, instanceLeaseDuration, isReplication);
super.register(info, isReplication); superRegister(info, isReplication);
} }
@Override @Override
...@@ -128,29 +128,45 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl ...@@ -128,29 +128,45 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
protected boolean internalCancel(String appName, String id, boolean isReplication) { protected boolean internalCancel(String appName, String id, boolean isReplication) {
logCancelation(appName, id, isReplication); logCancelation(appName, id, isReplication);
publishEurekaInstanceCanceledEvent(appName, id, isReplication); publishEurekaInstanceCanceledEvent(appName, id, isReplication);
return superInternalCancel(appName, id, isReplication);
}
protected boolean superInternalCancel(String appName, String id,
boolean isReplication) {
return super.internalCancel(appName, id, isReplication); return super.internalCancel(appName, id, isReplication);
} }
private void logRegistration(InstanceInfo info, boolean isReplication, int instanceLeaseDuration) { protected void superRegister(InstanceInfo info, boolean isReplication) {
super.register(info, isReplication);
}
private void logRegistration(InstanceInfo info, boolean isReplication,
int instanceLeaseDuration) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("register " + info.getAppName() + ", vip " + info.getVIPAddress() log.debug("register " + info.getAppName() + ", vip " + info.getVIPAddress()
+ ", leaseDuration " + instanceLeaseDuration + ", isReplication " + isReplication); + ", leaseDuration " + instanceLeaseDuration + ", isReplication "
+ isReplication);
} }
} }
private void logCancelation(String appName, String serverId, boolean isReplication) { private void logCancelation(String appName, String serverId, boolean isReplication) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("cancel " + appName + " serverId " + serverId + ", isReplication " + isReplication); log.debug("cancel " + appName + " serverId " + serverId + ", isReplication "
+ isReplication);
} }
} }
private void publishEurekaInstanceRegisteredEvent(InstanceInfo info, int leaseDuration, boolean isReplication) { private void publishEurekaInstanceRegisteredEvent(InstanceInfo info,
int leaseDuration, boolean isReplication) {
// TODO: what to publish from info (whole object?) // TODO: what to publish from info (whole object?)
this.ctxt.publishEvent(new EurekaInstanceRegisteredEvent(this, info, leaseDuration, isReplication)); this.ctxt.publishEvent(new EurekaInstanceRegisteredEvent(this, info,
leaseDuration, isReplication));
} }
private void publishEurekaInstanceCanceledEvent(String appName, String serverId, boolean isReplication) { private void publishEurekaInstanceCanceledEvent(String appName, String serverId,
this.ctxt.publishEvent(new EurekaInstanceCanceledEvent(this, appName, serverId, isReplication)); boolean isReplication) {
this.ctxt.publishEvent(
new EurekaInstanceCanceledEvent(this, appName, serverId, isReplication));
} }
private int resolveInstanceLeaseDuration(final InstanceInfo info) { private int resolveInstanceLeaseDuration(final InstanceInfo info) {
......
package org.springframework.cloud.netflix.eureka.server;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.cloud.netflix.eureka.server.InstanceRegistryTest.Application;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.LeaseInfo;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
/**
* @author Bartlomiej Slota
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
value = {"spring.application.name=eureka", "logging.level.org.springframework."
+ "cloud.netflix.eureka.server.InstanceRegistry=DEBUG"})
public class InstanceRegistryTest {
@SpyBean(PeerAwareInstanceRegistry.class)
private InstanceRegistry instanceRegistry;
@Autowired
private Listener eurekaEventListener;
@Before
public void setup() {
eurekaEventListener.getApplicationEvents().clear();
}
@Test
public void testRegister() throws Exception {
// stubbing superclass method invocation
doNothing().when(instanceRegistry)
.superRegister(any(InstanceInfo.class), anyBoolean());
// creating instance info
LeaseInfo leaseInfo = getLeaseInfo();
InstanceInfo instanceInfo = getInstanceInfo(leaseInfo);
// calling tested method
instanceRegistry.register(instanceInfo, false);
// event of proper type is registered
assertEquals(1, eurekaEventListener.getApplicationEvents().size());
assertTrue(eurekaEventListener.getApplicationEvents().get(0)
instanceof EurekaInstanceRegisteredEvent);
// event details are correct
EurekaInstanceRegisteredEvent registeredEvent = (EurekaInstanceRegisteredEvent)
(eurekaEventListener.getApplicationEvents().get(0));
assertEquals(instanceInfo, registeredEvent.getInstanceInfo());
assertEquals(leaseInfo.getDurationInSecs(), registeredEvent.getLeaseDuration());
assertEquals(instanceRegistry, registeredEvent.getSource());
assertFalse(registeredEvent.isReplication());
// superclass method wrapper was successfully invoked
verify(instanceRegistry).superRegister(instanceInfo, false);
}
@Test
public void testDefaultLeaseDurationRegisterEvent() throws Exception {
// stubbing superclass method invocation
doNothing().when(instanceRegistry)
.superRegister(any(InstanceInfo.class), anyBoolean());
// creating instance info
InstanceInfo instanceInfo = getInstanceInfo(null);
// calling tested method
instanceRegistry.register(instanceInfo, false);
// instance info duration is set to default
EurekaInstanceRegisteredEvent registeredEvent = (EurekaInstanceRegisteredEvent)
(eurekaEventListener.getApplicationEvents().get(0));
assertEquals(LeaseInfo.DEFAULT_LEASE_DURATION, registeredEvent.getLeaseDuration());
}
@Test
public void testInternalCancel() throws Exception {
// stubbing superclass method invocation
doReturn(Boolean.TRUE).when(instanceRegistry)
.superInternalCancel(anyString(), anyString(), anyBoolean());
// calling tested method
boolean cancellationResult = instanceRegistry
.internalCancel("my-app", "appId", false);
// event of proper type is registered
assertEquals(1, eurekaEventListener.getApplicationEvents().size());
assertTrue(eurekaEventListener.getApplicationEvents().get(0)
instanceof EurekaInstanceCanceledEvent);
// event details are correct
EurekaInstanceCanceledEvent registeredEvent = (EurekaInstanceCanceledEvent)
(eurekaEventListener.getApplicationEvents().get(0));
assertEquals("my-app", registeredEvent.getAppName());
assertEquals("appId", registeredEvent.getServerId());
assertEquals(instanceRegistry, registeredEvent.getSource());
assertFalse(registeredEvent.isReplication());
// superclass method wrapper was successfully invoked
verify(instanceRegistry).superInternalCancel("my-app", "appId", false);
assertTrue(cancellationResult);
}
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
protected static class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.properties("spring.application.name=eureka").run(args);
}
@Bean
public Listener eurekaEventsListener() {
return new Listener();
}
}
private LeaseInfo getLeaseInfo() {
LeaseInfo.Builder leaseBuilder = LeaseInfo.Builder.newBuilder();
leaseBuilder.setRenewalIntervalInSecs(10);
leaseBuilder.setDurationInSecs(15);
return leaseBuilder.build();
}
private InstanceInfo getInstanceInfo(LeaseInfo leaseInfo) {
InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder();
builder.setAppName("my-app-name");
builder.setHostName("my-host-name");
builder.setPort(8008);
builder.setLeaseInfo(leaseInfo);
return builder.build();
}
private static class Listener implements ApplicationListener {
private final List<ApplicationEvent> applicationEvents = new LinkedList<>();
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EurekaInstanceCanceledEvent ||
event instanceof EurekaInstanceRegisteredEvent) {
applicationEvents.add(event);
}
}
public List<ApplicationEvent> getApplicationEvents() {
return applicationEvents;
}
}
}
\ No newline at end of file
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