Search in sources :

Example 1 with EurekaClient

use of com.netflix.discovery.EurekaClient 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 2 with EurekaClient

use of com.netflix.discovery.EurekaClient 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)

Example 3 with EurekaClient

use of com.netflix.discovery.EurekaClient 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 EurekaClient

use of com.netflix.discovery.EurekaClient in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method testStopWithCommonExecutor.

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

        @Override
        public EurekaClient get() {
            return eurekaClientMock;
        }
    }, testExecutor);
    EurekaNotificationServerListUpdater serverListUpdater2 = new EurekaNotificationServerListUpdater(new Provider<EurekaClient>() {

        @Override
        public EurekaClient get() {
            return eurekaClientMock2;
        }
    }, testExecutor);
    Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
    eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));
    Capture<EurekaEventListener> eventListenerCapture2 = new Capture<EurekaEventListener>();
    eurekaClientMock2.registerEventListener(EasyMock.capture(eventListenerCapture2));
    EasyMock.replay(eurekaClientMock);
    EasyMock.replay(eurekaClientMock2);
    final CountDownLatch updateCountLatch = new CountDownLatch(2);
    serverListUpdater1.start(new ServerListUpdater.UpdateAction() {

        @Override
        public void doUpdate() {
            updateCountLatch.countDown();
        }
    });
    serverListUpdater2.start(new ServerListUpdater.UpdateAction() {

        @Override
        public void doUpdate() {
            updateCountLatch.countDown();
        }
    });
    eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
    eventListenerCapture2.getValue().onEvent(new CacheRefreshedEvent());
    // latch is for both
    Assert.assertTrue(updateCountLatch.await(2, TimeUnit.SECONDS));
    serverListUpdater1.stop();
    serverListUpdater2.stop();
    EasyMock.verify(eurekaClientMock);
    EasyMock.verify(eurekaClientMock2);
}
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) Test(org.junit.Test)

Example 5 with EurekaClient

use of com.netflix.discovery.EurekaClient in project ribbon by Netflix.

the class EurekaNotificationServerListUpdaterTest method setUpEurekaClientMock.

private EurekaClient setUpEurekaClientMock() {
    final EurekaClient eurekaClientMock = EasyMock.createMock(EurekaClient.class);
    EasyMock.expect(eurekaClientMock.unregisterEventListener(EasyMock.isA(EurekaEventListener.class))).andReturn(true).times(1);
    return eurekaClientMock;
}
Also used : EurekaClient(com.netflix.discovery.EurekaClient)

Aggregations

EurekaClient (com.netflix.discovery.EurekaClient)21 Test (org.junit.Test)13 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)7 DiscoveryClient (com.netflix.discovery.DiscoveryClient)6 ServerListUpdater (com.netflix.loadbalancer.ServerListUpdater)6 EurekaInstanceConfig (com.netflix.appinfo.EurekaInstanceConfig)5 InstanceInfo (com.netflix.appinfo.InstanceInfo)5 CacheRefreshedEvent (com.netflix.discovery.CacheRefreshedEvent)5 EurekaEventListener (com.netflix.discovery.EurekaEventListener)5 Capture (org.easymock.Capture)5 MyDataCenterInstanceConfig (com.netflix.appinfo.MyDataCenterInstanceConfig)4 DefaultEurekaClientConfig (com.netflix.discovery.DefaultEurekaClientConfig)4 EurekaClientConfig (com.netflix.discovery.EurekaClientConfig)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Archaius2VipAddressResolver (com.netflix.appinfo.providers.Archaius2VipAddressResolver)2 VipAddressResolver (com.netflix.appinfo.providers.VipAddressResolver)2 TransportClientFactories (com.netflix.discovery.shared.transport.jersey.TransportClientFactories)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 Injector (com.google.inject.Injector)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1