Search in sources :

Example 16 with Command

use of com.twitter.common.base.Command in project commons by twitter.

the class ArgScannerTest method testStandardArgs.

@Test
public void testStandardArgs() {
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.ENUM_VAL.get(), is(Optimizations.ALL));
        }
    }, "enum", "ALL");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.STRING_VAL.get(), is("newstring"));
        }
    }, "string", "newstring");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.CHAR_VAL.get(), is('x'));
        }
    }, "char", "x");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.BYTE_VAL.get(), is((byte) 10));
        }
    }, "byte", "10");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.SHORT_VAL.get(), is((short) 10));
        }
    }, "short", "10");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.INT_VAL.get(), is(10));
        }
    }, "int", "10");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.LONG_VAL.get(), is(10L));
        }
    }, "long", "10");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.FLOAT_VAL.get(), is(10f));
        }
    }, "float", "10.0");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.DOUBLE_VAL.get(), is(10d));
        }
    }, "double", "10.0");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.BOOL.get(), is(true));
        }
    }, "bool", "true");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.BOOL.get(), is(true));
        }
    }, "bool", "");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.REGEX.get().matcher("jack").matches(), is(true));
        }
    }, "regex", ".*ack$");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.BOOL.get(), is(false));
        }
    }, "no_bool", "");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.BOOL.get(), is(true));
        }
    }, "no_bool", "false");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.TIME_AMOUNT.get(), is(Amount.of(100L, Time.SECONDS)));
        }
    }, "time_amount", "100secs");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.DATA_AMOUNT.get(), is(Amount.of(1L, Data.Gb)));
        }
    }, "data_amount", "1Gb");
    test(StandardArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(StandardArgs.RANGE.get(), is(com.google.common.collect.Range.closed(1, 5)));
        }
    }, "range", "1-5");
    resetArgs(StandardArgs.class);
    assertTrue(parse(StandardArgs.class, "1mins", "2secs"));
    assertEquals(ImmutableList.builder().add(Amount.of(60L, Time.SECONDS)).add(Amount.of(2L, Time.SECONDS)).build(), StandardArgs.POSITIONAL.get());
}
Also used : Command(com.twitter.common.base.Command) Test(org.junit.Test)

Example 17 with Command

use of com.twitter.common.base.Command in project commons by twitter.

the class ArgScannerTest method testCustomArgs.

@Test
public void testCustomArgs() {
    test(CustomArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(CustomArgs.NAME_VAL.get(), is(new Name("jane")));
        }
    }, "custom1", "jane");
    test(CustomArgs.class, new Command() {

        @Override
        public void execute() {
            assertThat(CustomArgs.MEANING_VAL.get(), is(new MeaningOfLife(42L)));
        }
    }, "custom2", "jim");
}
Also used : Command(com.twitter.common.base.Command) Test(org.junit.Test)

Example 18 with Command

use of com.twitter.common.base.Command in project commons by twitter.

the class DynamicHostSetUtil method getSnapshot.

/**
   * Gets a snapshot of a set of dynamic hosts (e.g. a ServerSet) and returns a readable copy of
   * the underlying actual endpoints.
   *
   * @param hostSet The hostSet to snapshot.
   * @throws MonitorException if there was a problem obtaining the snapshot.
   */
public static <T> ImmutableSet<T> getSnapshot(DynamicHostSet<T> hostSet) throws MonitorException {
    final ImmutableSet.Builder<T> snapshot = ImmutableSet.builder();
    Command unwatch = hostSet.watch(new HostChangeMonitor<T>() {

        @Override
        public void onChange(ImmutableSet<T> hostSet) {
            snapshot.addAll(hostSet);
        }
    });
    unwatch.execute();
    return snapshot.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Command(com.twitter.common.base.Command)

Example 19 with Command

use of com.twitter.common.base.Command in project commons by twitter.

the class MetaPool method startDeadBackendRestorer.

private Command startDeadBackendRestorer(final Amount<Long, Time> restoreInterval) {
    final AtomicBoolean shouldRestore = new AtomicBoolean(true);
    Runnable restoreDeadBackends = new Runnable() {

        @Override
        public void run() {
            if (shouldRestore.get()) {
                restoreDeadBackends(restoreInterval);
            }
        }
    };
    final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("MTCP-DeadBackendRestorer[%s]").build());
    long restoreDelay = restoreInterval.getValue();
    scheduledExecutorService.scheduleWithFixedDelay(restoreDeadBackends, restoreDelay, restoreDelay, restoreInterval.getUnit().getTimeUnit());
    return new Command() {

        @Override
        public void execute() {
            shouldRestore.set(false);
            scheduledExecutorService.shutdownNow();
            LOG.info("Backend restorer shut down");
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Command(com.twitter.common.base.Command) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Example 20 with Command

use of com.twitter.common.base.Command in project commons by twitter.

the class NumericStatExporter method start.

/**
   * Starts the stat exporter.
   *
   * @param shutdownRegistry Shutdown hook registry to allow the exporter to cleanly halt.
   */
public void start(ShutdownRegistry shutdownRegistry) {
    long intervalSecs = exportInterval.as(Time.SECONDS);
    executor.scheduleAtFixedRate(exporter, intervalSecs, intervalSecs, TimeUnit.SECONDS);
    shutdownRegistry.addAction(new Command() {

        @Override
        public void execute() {
            stop();
            exportSink.execute(Maps.transformValues(Maps.uniqueIndex(Stats.getNumericVariables(), GET_NAME), SAMPLE_AND_READ_STAT));
        }
    });
}
Also used : Command(com.twitter.common.base.Command)

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