Search in sources :

Example 1 with Command

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();
}
Also used : Command(com.twitter.common.base.Command) Membership(com.twitter.common.zookeeper.Group.Membership) CountDownLatch(java.util.concurrent.CountDownLatch) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 2 with Command

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);
}
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)

Example 3 with Command

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();
}
Also used : Command(com.twitter.common.base.Command) Membership(com.twitter.common.zookeeper.Group.Membership) CountDownLatch(java.util.concurrent.CountDownLatch) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 4 with Command

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Command(com.twitter.common.base.Command) Test(org.junit.Test)

Example 5 with Command

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");
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Command(com.twitter.common.base.Command) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

Command (com.twitter.common.base.Command)23 Test (org.junit.Test)15 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)6 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Membership (com.twitter.common.zookeeper.Group.Membership)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 AbstractModule (com.google.inject.AbstractModule)2 Injector (com.google.inject.Injector)2 Module (com.google.inject.Module)2 ShutdownRegistryImpl (com.twitter.common.application.ShutdownRegistry.ShutdownRegistryImpl)2 ExceptionalCommand (com.twitter.common.base.ExceptionalCommand)2 ZooKeeperConnectionException (com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 KeeperException (org.apache.zookeeper.KeeperException)2 Watcher (org.apache.zookeeper.Watcher)2 Supplier (com.google.common.base.Supplier)1