Search in sources :

Example 6 with CommandHandler

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);
        }
    }
}
Also used : ServerController(org.wildfly.core.testrunner.ServerController) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) LsHandler(org.jboss.as.cli.handlers.LsHandler) CliInitializationException(org.jboss.as.cli.CliInitializationException) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) EmptySubsystemParser(org.jboss.as.test.integration.management.extension.EmptySubsystemParser) ExtensionUtils(org.jboss.as.test.integration.management.extension.ExtensionUtils) WildflyTestRunner(org.wildfly.core.testrunner.WildflyTestRunner) Future(java.util.concurrent.Future) CommandExecutor(org.jboss.as.cli.impl.CommandExecutor) After(org.junit.After) CommandArgument(org.jboss.as.cli.CommandArgument) Before(org.junit.Before) CommandHandler(org.jboss.as.cli.CommandHandler) Collection(java.util.Collection) IOException(java.io.IOException) Test(org.junit.Test) BlockerExtension(org.jboss.as.test.integration.management.extension.blocker.BlockerExtension) Util(org.jboss.as.cli.Util) TestSuiteEnvironment(org.jboss.as.test.shared.TestSuiteEnvironment) CommandLineException(org.jboss.as.cli.CommandLineException) TimeUnit(java.util.concurrent.TimeUnit) TimeoutCommandContext(org.jboss.as.cli.impl.CommandExecutor.TimeoutCommandContext) List(java.util.List) CommandContext(org.jboss.as.cli.CommandContext) TimeoutUtil(org.jboss.as.test.shared.TimeoutUtil) ModelNode(org.jboss.dmr.ModelNode) ServerControl(org.wildfly.core.testrunner.ServerControl) CommandContextFactory(org.jboss.as.cli.CommandContextFactory) CommandContextConfiguration(org.jboss.as.cli.impl.CommandContextConfiguration) Assert.assertEquals(org.junit.Assert.assertEquals) CommandExecutor(org.jboss.as.cli.impl.CommandExecutor) CommandHandler(org.jboss.as.cli.CommandHandler) TimeoutCommandContext(org.jboss.as.cli.impl.CommandExecutor.TimeoutCommandContext) CommandLineException(org.jboss.as.cli.CommandLineException) TimeoutException(java.util.concurrent.TimeoutException) CliInitializationException(org.jboss.as.cli.CliInitializationException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) LsHandler(org.jboss.as.cli.handlers.LsHandler) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

CommandHandler (org.jboss.as.cli.CommandHandler)6 CommandLineException (org.jboss.as.cli.CommandLineException)4 CommandFormatException (org.jboss.as.cli.CommandFormatException)3 CommandCommandHandler (org.jboss.as.cli.handlers.CommandCommandHandler)3 ModelNode (org.jboss.dmr.ModelNode)3 IOException (java.io.IOException)2 CommandNotFoundException (org.aesh.command.CommandNotFoundException)2 CommandContext (org.jboss.as.cli.CommandContext)2 Batch (org.jboss.as.cli.batch.Batch)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 AccessDeniedException (java.nio.file.AccessDeniedException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Inject (javax.inject.Inject)1 CommandException (org.aesh.command.CommandException)1 CommandLineParser (org.aesh.command.impl.parser.CommandLineParser)1