com.netflix.discovery.EurekaEventListener Java Examples

The following examples show how to use com.netflix.discovery.EurekaEventListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EurekaContainerHealthService.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Inject
public EurekaContainerHealthService(ReadOnlyJobOperations jobOperations, EurekaClient eurekaClient, TitusRuntime titusRuntime) {
    this.jobOperations = jobOperations;
    this.eurekaClient = eurekaClient;
    this.titusRuntime = titusRuntime;

    Flux<EurekaEvent> eurekaCallbacks = ReactorExt.fromListener(
            EurekaEventListener.class,
            eurekaClient::registerEventListener,
            eurekaClient::unregisterEventListener
    );

    this.healthStatuses = Flux.defer(() -> {
        ConcurrentMap<String, ContainerHealthEvent> current = new ConcurrentHashMap<>();
        return Flux.merge(eurekaCallbacks, ReactorExt.toFlux(jobOperations.observeJobs()))
                .flatMap(event -> handleJobManagerOrEurekaStatusUpdate(event, current));
    }).share().compose(ReactorExt.badSubscriberHandler(logger));

}
 
Example #2
Source File: EurekaDynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private EurekaClient setUpEurekaClientMock(List<InstanceInfo> servers) {
    final EurekaClient eurekaClientMock = PowerMock.createMock(EurekaClient.class);

    EasyMock.expect(eurekaClientMock.getEurekaClientConfig()).andReturn(new DefaultEurekaClientConfig()).anyTimes();

    EasyMock
            .expect(eurekaClientMock.getInstancesByVipAddress(EasyMock.anyString(), EasyMock.anyBoolean(), EasyMock.anyString()))
            .andReturn(servers.subList(0, initialServerListSize)).times(1)
            .andReturn(servers.subList(initialServerListSize, servers.size())).anyTimes();

    EasyMock
            .expectLastCall();

    EasyMock
            .expect(eurekaClientMock.unregisterEventListener(EasyMock.isA(EurekaEventListener.class)))
            .andReturn(true).anyTimes();

    return eurekaClientMock;
}
 
Example #3
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@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);
    }

}
 
Example #4
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private EurekaClient setUpEurekaClientMock() {
    final EurekaClient eurekaClientMock = EasyMock.createMock(EurekaClient.class);

    EasyMock
            .expect(eurekaClientMock.unregisterEventListener(EasyMock.isA(EurekaEventListener.class)))
            .andReturn(true).times(1);

    return eurekaClientMock;
}
 
Example #5
Source File: EurekaDynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadBalancerHappyCase() throws Exception {
    Assert.assertNotEquals("the two test server list counts should be different",
            secondServerListSize, initialServerListSize);

    DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb = null;
    try {
        Capture<EurekaEventListener> eventListenerCapture = new Capture<EurekaEventListener>();
        eurekaClientMock.registerEventListener(EasyMock.capture(eventListenerCapture));

        PowerMock.replay(DiscoveryClient.class);
        PowerMock.replay(eurekaClientMock);

        // actual testing
        // initial creation and loading of the first serverlist
        lb = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        Assert.assertEquals(initialServerListSize, lb.getServerCount(false));

        // trigger an eureka CacheRefreshEvent
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());

        Assert.assertTrue(verifyFinalServerListCount(secondServerListSize, lb));

    } finally {
        if (lb != null) {
            lb.shutdown();

            PowerMock.verify(eurekaClientMock);
            PowerMock.verify(DiscoveryClient.class);
        }
    }
}
 
Example #6
Source File: EurekaServerStub.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Override
public void registerEventListener(EurekaEventListener eventListener) {
    eventListeners.add(eventListener);
}
 
Example #7
Source File: EurekaServerStub.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Override
public boolean unregisterEventListener(EurekaEventListener eventListener) {
    return eventListeners.remove(eventListener);
}
 
Example #8
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example #9
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@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());

    Assert.assertTrue(updateCountLatch.await(2, TimeUnit.SECONDS));  // latch is for both

    serverListUpdater1.stop();
    serverListUpdater2.stop();

    EasyMock.verify(eurekaClientMock);
    EasyMock.verify(eurekaClientMock2);
}
 
Example #10
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testTaskAlreadyQueued() 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 CountDownLatch countDownLatch = new CountDownLatch(1);
        serverListUpdater.start(new ServerListUpdater.UpdateAction() {
            @Override
            public void doUpdate() {
                if (countDownLatch.getCount() == 0) {
                    Assert.fail("should only countdown once");
                }
                countDownLatch.countDown();
            }
        });

        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());
        eventListenerCapture.getValue().onEvent(new CacheRefreshedEvent());

        Assert.assertTrue(countDownLatch.await(2, TimeUnit.SECONDS));
        Thread.sleep(100);  // sleep a bit more

        Assert.assertFalse(serverListUpdater.updateQueued.get());
    } finally {
        serverListUpdater.stop();

        EasyMock.verify(eurekaClientMock);
    }
}
 
Example #11
Source File: EurekaNotificationServerListUpdaterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@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);
    }

}
 
Example #12
Source File: EurekaDynamicServerListLoadBalancerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testShutdownMultiple() {
    try {
        eurekaClientMock.registerEventListener(EasyMock.anyObject(EurekaEventListener.class));
        EasyMock.expectLastCall().anyTimes();

        PowerMock.replay(DiscoveryClient.class);
        PowerMock.replay(eurekaClientMock);

        DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb1 = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb2 = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        DynamicServerListLoadBalancer<DiscoveryEnabledServer> lb3 = new DynamicServerListLoadBalancer<DiscoveryEnabledServer>(
                config,
                new AvailabilityFilteringRule(),
                new DummyPing(),
                new DiscoveryEnabledNIWSServerList(vipAddress, eurekaClientProvider),
                new ZoneAffinityServerListFilter<DiscoveryEnabledServer>(),
                new EurekaNotificationServerListUpdater(eurekaClientProvider)
        );

        lb3.shutdown();
        lb1.shutdown();
        lb2.shutdown();
    } finally {
        PowerMock.verify(eurekaClientMock);
        PowerMock.verify(DiscoveryClient.class);
    }
}