Search in sources :

Example 1 with HostChangeMonitor

use of com.twitter.common.net.pool.DynamicHostSet.HostChangeMonitor in project commons by twitter.

the class DynamicHostSetUtilTest method testSnapshot.

@Test
public void testSnapshot() throws MonitorException {
    DynamicHostSet<String> hostSet = createMock(new Clazz<DynamicHostSet<String>>() {
    });
    final Capture<HostChangeMonitor<String>> monitorCapture = createCapture();
    final Command unwatchCommand = createMock(Command.class);
    expect(hostSet.watch(capture(monitorCapture))).andAnswer(new IAnswer<Command>() {

        @Override
        public Command answer() throws Throwable {
            // Simulate the 1st blocking onChange callback.
            HostChangeMonitor<String> monitor = monitorCapture.getValue();
            monitor.onChange(ImmutableSet.of("jack", "jill"));
            return unwatchCommand;
        }
    });
    // Confirm we clean up our watch.
    unwatchCommand.execute();
    expectLastCall();
    control.replay();
    ImmutableSet<String> snapshot = DynamicHostSetUtil.getSnapshot(hostSet);
    assertEquals(ImmutableSet.of("jack", "jill"), snapshot);
}
Also used : Command(com.twitter.common.base.Command) HostChangeMonitor(com.twitter.common.net.pool.DynamicHostSet.HostChangeMonitor) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test)

Aggregations

Command (com.twitter.common.base.Command)1 HostChangeMonitor (com.twitter.common.net.pool.DynamicHostSet.HostChangeMonitor)1 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)1 Test (org.junit.Test)1