use of com.netflix.appinfo.InstanceInfo in project coffeenet-starter by coffeenet.
the class IntegrationCoffeeNetAppServiceTest method getServiceInstance.
private EurekaServiceInstance getServiceInstance(String name, String roles) {
InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName(name).setVIPAddress(name).build();
if (roles != null) {
instanceInfo.getMetadata().put("allowedAuthorities", roles);
}
EurekaServiceInstance eurekaServiceInstance = mock(EurekaServiceInstance.class);
when(eurekaServiceInstance.getInstanceInfo()).thenReturn(instanceInfo);
return eurekaServiceInstance;
}
use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.
the class EurekaVipAddressRoundRobinService method getActiveInstanceInfoForVipAddressBlocking.
/**
* Round-robins the instances returned by {@link #getActiveInstanceInfosForVipAddressBlocking(String)} to determine
* the *next* active {@link InstanceInfo} that should be called for the given VIP name
*
* @return The *next* active {@link InstanceInfo} that should be called for the given VIP name (using a round-robin
* strategy), or an empty Optional if no active instances were found.
*/
public Optional<InstanceInfo> getActiveInstanceInfoForVipAddressBlocking(String vipName) {
List<InstanceInfo> instancesByVipAddress = getActiveInstanceInfosForVipAddressBlocking(vipName);
if (instancesByVipAddress.isEmpty())
return Optional.empty();
// Found at least one "up" instance at this VIP. Grab the AtomicInteger associated with this VIP
// (map a new one if necessary).
AtomicInteger roundRobinCounter = vipRoundRobinCounterMap.computeIfAbsent(vipName, vip -> new AtomicInteger(0));
// Atomically get-and-increment the atomic int associated with this VIP, then mod it against the number of
// instances available. This effectively round robins the use of all the instances associated with the VIP.
int instanceIndexToUse = roundRobinCounter.getAndIncrement() % instancesByVipAddress.size();
if (instanceIndexToUse < 0) {
// The counter went high enough to do an integer overflow. Fix the index so we don't blow up this call,
// and reset the counter to 0.
instanceIndexToUse = Math.abs(instanceIndexToUse);
roundRobinCounter.set(0);
}
return Optional.of(instancesByVipAddress.get(instanceIndexToUse));
}
use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.
the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfoForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfoForVipAddressBlocking.
@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getActiveInstanceInfoForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfoForVipAddressBlocking(boolean useExecutor) {
// given
InstanceInfo iiMock = mock(InstanceInfo.class);
doReturn(Optional.of(iiMock)).when(serviceSpy).getActiveInstanceInfoForVipAddressBlocking(vip);
Optional<Executor> executorOpt = useExecutor ? Optional.of(Executors.newSingleThreadExecutor()) : Optional.empty();
// when
CompletableFuture<Optional<InstanceInfo>> result = serviceSpy.getActiveInstanceInfoForVipAddress(vip, executorOpt);
// then
assertThat(result.join()).isEqualTo(Optional.of(iiMock));
}
use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.
the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfoForVipAddressBlocking_returns_empty_if_no_instances_are_active.
@Test
public void getActiveInstanceInfoForVipAddressBlocking_returns_empty_if_no_instances_are_active() {
// given
InstanceInfo down = mock(InstanceInfo.class);
doReturn(InstanceInfo.InstanceStatus.DOWN).when(down).getStatus();
List<InstanceInfo> allInstancesForVip = Collections.singletonList(down);
doReturn(allInstancesForVip).when(discoveryClientMock).getInstancesByVipAddress(vip, false);
// when
Optional<InstanceInfo> result = serviceSpy.getActiveInstanceInfoForVipAddressBlocking(vip);
// then
assertThat(result).isEmpty();
}
use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.
the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfosForVipAddress_uses_provided_executor_for_future_if_provided_one.
@Test
public void getActiveInstanceInfosForVipAddress_uses_provided_executor_for_future_if_provided_one() {
// given
InstanceInfo iiMock = mock(InstanceInfo.class);
List<InstanceInfo> expectedInstances = Collections.singletonList(iiMock);
doReturn(expectedInstances).when(serviceSpy).getActiveInstanceInfosForVipAddressBlocking(vip);
Executor executorSpy = spy(new SpyableExecutor());
// when
CompletableFuture<List<InstanceInfo>> result = serviceSpy.getActiveInstanceInfosForVipAddress(vip, Optional.of(executorSpy));
// then
assertThat(result.join()).isEqualTo(expectedInstances);
verify(executorSpy).execute(any(Runnable.class));
}
Aggregations