Search in sources :

Example 1 with ServerListUpdater

use of com.netflix.loadbalancer.ServerListUpdater in project spring-cloud-netflix by spring-cloud.

the class RibbonClientConfigurationIntegrationTests method testLoadBalancerConstruction.

@Test
public void testLoadBalancerConstruction() {
    ILoadBalancer loadBalancer = clientFactory.getInstance("test", ILoadBalancer.class);
    assertThat(loadBalancer, is(instanceOf(ZoneAwareLoadBalancer.class)));
    ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) loadBalancer;
    ServerListUpdater serverListUpdater = (PollingServerListUpdater) ReflectionTestUtils.getField(loadBalancer, "serverListUpdater");
    Long refreshIntervalMs = (Long) ReflectionTestUtils.getField(serverListUpdater, "refreshIntervalMs");
    // assertThat(refreshIntervalMs, equalTo(999L));
    ServerListUpdater updater = clientFactory.getInstance("test", ServerListUpdater.class);
    assertThat(updater, is(sameInstance(serverListUpdater)));
}
Also used : PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ServerListUpdater

use of com.netflix.loadbalancer.ServerListUpdater in project ribbon by Netflix.

the class LBBuilderTest method testBuildWithDiscoveryEnabledNIWSServerListAndUpdater.

@Test
public void testBuildWithDiscoveryEnabledNIWSServerListAndUpdater() {
    IRule rule = new AvailabilityFilteringRule();
    ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
    ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<>();
    ServerListUpdater updater = new PollingServerListUpdater();
    ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder().withDynamicServerList(list).withRule(rule).withServerListFilter(filter).withServerListUpdater(updater).buildDynamicServerListLoadBalancerWithUpdater();
    assertNotNull(lb);
    assertEquals(Lists.newArrayList(expected), lb.getAllServers());
    assertSame(filter, lb.getFilter());
    assertSame(list, lb.getServerListImpl());
    assertSame(updater, lb.getServerListUpdater());
    Server server = lb.chooseServer();
    // make sure load balancer does not recreate the server instance
    assertTrue(server instanceof DiscoveryEnabledServer);
}
Also used : Server(com.netflix.loadbalancer.Server) PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ZoneAffinityServerListFilter(com.netflix.loadbalancer.ZoneAffinityServerListFilter) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) IRule(com.netflix.loadbalancer.IRule) PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ServerListUpdater

use of com.netflix.loadbalancer.ServerListUpdater in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testUpdating.

@Test
public void testUpdating() throws Exception {
    EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, testExecutor);
    try {
        Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
        EasyMock.replay(eurekaClientMock);
        final AtomicBoolean firstTime = new AtomicBoolean(false);
        final CountDownLatch firstLatch = new CountDownLatch(1);
        final CountDownLatch secondLatch = new CountDownLatch(1);
        serverListUpdater.start(new ServerListUpdater.UpdateAction() {

            @Override
            public void doUpdate() {
                if (firstTime.compareAndSet(false, true)) {
                    firstLatch.countDown();
                } else {
                    secondLatch.countDown();
                }
            }
        });
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertTrue(firstLatch.await(2, TimeUnit.SECONDS));
        // wait a bit for the updateQueued flag to be reset
        for (int i = 1; i < 10; i++) {
            if (serverListUpdater.updateQueued.get()) {
                Thread.sleep(i * 100);
            } else {
                break;
            }
        }
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertTrue(secondLatch.await(2, TimeUnit.SECONDS));
    } finally {
        serverListUpdater.stop();
        EasyMock.verify(eurekaClientMock);
    }
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) EurekaEventListener(com.netflix.discovery.EurekaEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 4 with ServerListUpdater

use of com.netflix.loadbalancer.ServerListUpdater in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testSubmitExceptionClearQueued.

@Test
public void testSubmitExceptionClearQueued() {
    ThreadPoolExecutor executorMock = EasyMock.createMock(ThreadPoolExecutor.class);
    EasyMock.expect(executorMock.submit(EasyMock.isA(Runnable.class))).andThrow(new RejectedExecutionException("test exception"));
    EasyMock.expect(executorMock.isShutdown()).andReturn(Boolean.FALSE);
    EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, executorMock);
    try {
        Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
        EasyMock.replay(eurekaClientMock);
        EasyMock.replay(executorMock);
        serverListUpdater.start(new ServerListUpdater.UpdateAction() {

            @Override
            public void doUpdate() {
                Assert.fail("should not reach here");
            }
        });
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        Assert.assertFalse(serverListUpdater.updateQueued.get());
    } finally {
        serverListUpdater.stop();
        EasyMock.verify(executorMock);
        EasyMock.verify(eurekaClientMock);
    }
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) EurekaEventListener(com.netflix.discovery.EurekaEventListener) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 5 with ServerListUpdater

use of com.netflix.loadbalancer.ServerListUpdater in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testEurekaClientUnregister.

@Test
public void testEurekaClientUnregister() {
    ThreadPoolExecutor executorMock = EasyMock.createMock(ThreadPoolExecutor.class);
    EasyMock.expect(executorMock.isShutdown()).andReturn(Boolean.TRUE);
    EurekaNotificationServerListUpdater serverListUpdater = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, executorMock);
    try {
        Capture<EurekaEventListener> registeredListener = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(registeredListener));
        EasyMock.replay(eurekaClientMock);
        EasyMock.replay(executorMock);
        serverListUpdater.start(new ServerListUpdater.UpdateAction() {

            @Override
            public void doUpdate() {
                Assert.fail("should not reach here");
            }
        });
        registeredListener.getValue().onEvent(new CacheRefreshedEvent());
    } finally {
        EasyMock.verify(executorMock);
        EasyMock.verify(eurekaClientMock);
    }
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) EurekaEventListener(com.netflix.discovery.EurekaEventListener) Capture(org.easymock.Capture) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) CacheRefreshedEvent(com.netflix.discovery.CacheRefreshedEvent) Test(org.junit.Test)

Aggregations

ServerListUpdater (com.netflix.loadbalancer.ServerListUpdater)7 Test (org.junit.Test)6 EurekaClient (com.netflix.discovery.EurekaClient)5 CacheRefreshedEvent (com.netflix.discovery.CacheRefreshedEvent)4 EurekaEventListener (com.netflix.discovery.EurekaEventListener)4 Capture (org.easymock.Capture)4 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)2 IRule (com.netflix.loadbalancer.IRule)2 PollingServerListUpdater (com.netflix.loadbalancer.PollingServerListUpdater)2 ZoneAffinityServerListFilter (com.netflix.loadbalancer.ZoneAffinityServerListFilter)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)1 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)1 RandomRule (com.netflix.loadbalancer.RandomRule)1 Server (com.netflix.loadbalancer.Server)1 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)1 DiscoveryEnabledNIWSServerList (com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList)1 DiscoveryEnabledServer (com.netflix.niws.loadbalancer.DiscoveryEnabledServer)1 EurekaNotificationServerListUpdater (com.netflix.niws.loadbalancer.EurekaNotificationServerListUpdater)1