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());
}
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");
}
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();
}
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");
}
};
}
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));
}
});
}
Aggregations