use of org.jboss.as.cli.CommandHandler in project wildfly-core by wildfly.
the class CommandTimeoutHandlerTestCase method testComandExecutor.
@Test
public void testComandExecutor() throws Exception {
CommandExecutor executor = new CommandExecutor(ctx);
CommandHandler ls = new LsHandler(ctx);
ctx.setCommandTimeout(100);
// Required in order for the ctx to be in sync when calling the handler directly.
ctx.handle("ls");
{
List<Boolean> holder = new ArrayList<>();
CommandHandlerWrapper wrapper = new CommandHandlerWrapper(ctx, ls, (context) -> {
holder.add(true);
TimeoutCommandContext tc = (TimeoutCommandContext) context;
tc.setLastHandlerTask(null);
ls.handle(context);
Future<?> future = tc.getLastTask();
if (future == null || !future.isDone()) {
throw new CommandLineException("Future is not done " + future);
}
});
executor.execute(wrapper, 100, TimeUnit.SECONDS);
if (holder.size() != 1) {
throw new Exception("Handler not called");
}
}
{
List<Object> holder = new ArrayList<>();
CommandHandlerWrapper wrapper = new CommandHandlerWrapper(ctx, ls, (context) -> {
TimeoutCommandContext tc = (TimeoutCommandContext) context;
tc.setLastHandlerTask(null);
try {
long sleep = TimeoutUtil.adjust(1000);
Thread.sleep(sleep);
holder.add(null);
} catch (InterruptedException ex) {
holder.add(ex);
Thread.currentThread().interrupt();
}
try {
ls.handle(context);
holder.add(null);
} catch (Exception ex) {
// Expecting a timeout exception,
// the task has already been canceled.
holder.add(ex);
}
Future<?> future = tc.getLastTask();
holder.add(future);
});
try {
executor.execute(wrapper, 100, TimeUnit.MILLISECONDS);
throw new RuntimeException("Should have failed");
} catch (TimeoutException ex) {
// XXX OK expected.
}
// Wait for the task to terminate and check the various steps.
int waitTime = 1000;
while (holder.size() != 3 && waitTime > 0) {
Thread.sleep(100);
waitTime -= 100;
}
if (holder.size() != 3) {
throw new Exception("Task didn't terminate");
}
if (holder.get(0) == null) {
throw new Exception("Task thread not interrupted");
}
if (holder.get(1) == null) {
throw new Exception("Ls has not timeouted");
}
if (holder.get(2) != null) {
throw new Exception("Future task is not null. Steps: " + holder);
}
}
}
Aggregations