use of com.twitter.common.base.Command in project commons by twitter.
the class GroupTest method testNodeDeleteTriggersOnLoseMembership.
@Test
public void testNodeDeleteTriggersOnLoseMembership() throws Exception {
final CountDownLatch lostMembership = new CountDownLatch(1);
Command onLoseMembership = new Command() {
@Override
public void execute() throws RuntimeException {
lostMembership.countDown();
}
};
assertEmptyMembershipObserved();
Membership membership = joinGroup.join(onLoseMembership);
assertMembershipObserved(membership.getMemberId());
membership.cancel();
// Will hang this test if onLoseMembership event is not propagated.
lostMembership.await();
}
use of com.twitter.common.base.Command 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);
}
use of com.twitter.common.base.Command in project commons by twitter.
the class GroupTest method testSessionExpirationTriggersOnLoseMembership.
@Test
public void testSessionExpirationTriggersOnLoseMembership() throws Exception {
final CountDownLatch lostMembership = new CountDownLatch(1);
Command onLoseMembership = new Command() {
@Override
public void execute() throws RuntimeException {
lostMembership.countDown();
}
};
assertEmptyMembershipObserved();
Membership membership = joinGroup.join(onLoseMembership);
assertMembershipObserved(membership.getMemberId());
expireSession(zkClient);
// Will hang this test if onLoseMembership event is not propagated.
lostMembership.await();
}
use of com.twitter.common.base.Command in project commons by twitter.
the class TearDownRegistryTest method testTearDown.
@Test
public void testTearDown() {
TearDownRegistry tearDownRegistry = new TearDownRegistry(this);
final AtomicBoolean actionExecuted = new AtomicBoolean(false);
tearDownRegistry.addAction(new Command() {
@Override
public void execute() {
actionExecuted.set(true);
}
});
assertFalse(actionExecuted.get());
tearDown();
assertTrue(actionExecuted.get());
}
use of com.twitter.common.base.Command in project commons by twitter.
the class TimeSeriesRepositoryImpl method start.
/**
* Starts the variable sampler, which will fetch variables {@link Stats} on the given period.
*
*/
@Override
public void start(ShutdownRegistry shutdownRegistry) {
checkNotNull(shutdownRegistry);
checkNotNull(samplePeriod);
Preconditions.checkArgument(samplePeriod.getValue() > 0);
final ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, /* One thread. */
new ThreadFactoryBuilder().setNameFormat("VariableSampler-%d").setDaemon(true).build());
final AtomicBoolean shouldSample = new AtomicBoolean(true);
final Runnable sampler = new Runnable() {
@Override
public void run() {
if (shouldSample.get()) {
try {
runSampler(Clock.SYSTEM_CLOCK);
} catch (Exception e) {
LOG.log(Level.SEVERE, "ignoring runSampler failure", e);
}
}
}
};
executor.scheduleAtFixedRate(sampler, samplePeriod.getValue(), samplePeriod.getValue(), samplePeriod.getUnit().getTimeUnit());
shutdownRegistry.addAction(new Command() {
@Override
public void execute() throws RuntimeException {
shouldSample.set(false);
executor.shutdown();
LOG.info("Variable sampler shut down");
}
});
}
Aggregations