Search in sources :

Example 1 with LoadBalancerStateListenerCallback

use of com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testListenToService.

@Test(groups = { "small", "back-end" })
public void testListenToService() throws InterruptedException {
    reset();
    assertFalse(_state.isListeningToService("service-1"));
    assertNull(_state.getServiceProperties("service-1"));
    final CountDownLatch latch = new CountDownLatch(1);
    LoadBalancerStateListenerCallback callback = new LoadBalancerStateListenerCallback() {

        @Override
        public void done(int type, String name) {
            latch.countDown();
        }
    };
    _state.listenToService("service-1", callback);
    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("didn't get callback when listenToService was called");
    }
    assertTrue(_state.isListeningToService("service-1"));
    assertNotNull(_state.getServiceProperties("service-1"));
    assertNull(_state.getServiceProperties("service-1").getProperty());
    ServiceProperties property = new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"));
    _serviceRegistry.put("service-1", property);
    assertTrue(_state.isListeningToService("service-1"));
    assertNotNull(_state.getServiceProperties("service-1"));
    assertEquals(_state.getServiceProperties("service-1").getProperty(), property);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) LoadBalancerStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 2 with LoadBalancerStateListenerCallback

use of com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testListenToCluster.

@Test(groups = { "small", "back-end" })
public void testListenToCluster() throws URISyntaxException, InterruptedException {
    reset();
    List<String> schemes = new ArrayList<>();
    schemes.add("http");
    assertFalse(_state.isListeningToCluster("cluster-1"));
    assertNull(_state.getClusterProperties("cluster-1"));
    final CountDownLatch latch = new CountDownLatch(1);
    LoadBalancerStateListenerCallback callback = new LoadBalancerStateListenerCallback() {

        @Override
        public void done(int type, String name) {
            latch.countDown();
        }
    };
    _state.listenToCluster("cluster-1", callback);
    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("didn't get callback when listenToCluster was called");
    }
    assertTrue(_state.isListeningToCluster("cluster-1"));
    assertNotNull(_state.getClusterProperties("cluster-1"));
    assertNull(_state.getClusterProperties("cluster-1").getProperty());
    ClusterProperties property = new ClusterProperties("cluster-1", schemes);
    _clusterRegistry.put("cluster-1", property);
    assertTrue(_state.isListeningToCluster("cluster-1"));
    assertNotNull(_state.getClusterProperties("cluster-1"));
    assertEquals(_state.getClusterProperties("cluster-1").getProperty(), property);
}
Also used : LoadBalancerStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback) ArrayList(java.util.ArrayList) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 3 with LoadBalancerStateListenerCallback

use of com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback in project rest.li by linkedin.

the class SimpleLoadBalancer method listenToCluster.

public void listenToCluster(String clusterName, boolean waitForUpdatedValue, LoadBalancerStateListenerCallback callback) {
    if (waitForUpdatedValue) {
        _state.listenToCluster(clusterName, callback);
    } else {
        _state.listenToCluster(clusterName, new NullStateListenerCallback());
        callback.done(0, null);
    }
}
Also used : NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)

Aggregations

LoadBalancerStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback)2 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.testng.annotations.Test)2 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)1 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)1 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)1 ArrayList (java.util.ArrayList)1