Search in sources :

Example 1 with TestTask

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class DataCollectingWorker method execute.

@Override
public void execute(Task aTask, WorkerContext ctl) {
    boolean finished = false;
    try {
        TestTask task = (TestTask) aTask;
        Command command = task.getCommands().peekFirst();
        if (command != null && command.done()) {
            task.getCommands().removeFirst();
            command = task.getCommands().peekFirst();
        }
        if (command != null) {
            command.execute(ctl, task);
        } else {
            // Do not record stop caused by end of commands
            finished = true;
            ctl.stop();
        }
        data.recordSuccess();
    } catch (RetryExecutionControlException ex) {
        if (ex.isForce())
            data.recordForceRetry();
        else
            data.recordRetry();
        throw ex;
    } catch (StopExecutionControlException ex) {
        if (!finished)
            data.recordStop();
        throw ex;
    } catch (Exception ex) {
        data.recordException();
        throw ex;
    } finally {
        if (!finished)
            data.recordExecution();
    }
}
Also used : RetryExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException) Command(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command) StopExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.StopExecutionControlException) TestTask(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask) StopExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.StopExecutionControlException) RetryExecutionControlException(org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException)

Example 2 with TestTask

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class TaskManagerTest method testInputs.

@Test
void testInputs() {
    TestTask task = null;
    // Zero period is OK, but not recommended
    task = TestTask.builder().schedule(TaskSchedule.builder().minRetries(1).interval(Duration.ZERO).build()).build().andThen(BasicCommand.builder().increment(true).build()).andThen(BasicCommand.builder().increment(true).build());
    tasks.submit(task);
    data.awaitFinished();
    assertThat(data.getExecutions(), equalTo(2));
    assertThat(data.getCounter(), equalTo(2));
    data.reset();
    try {
        TaskSchedule.builder().minRetries(-1).build();
        fail("Illegal argument.");
    } catch (IllegalArgumentException ex) {
    // OK
    }
    try {
        TaskSchedule.builder().interval(Duration.ofSeconds(-1)).build();
        fail("Illegal argument.");
    } catch (IllegalArgumentException ex) {
    // OK
    }
}
Also used : TestTask(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 3 with TestTask

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class TaskManagerTest method testDelay.

@Test
void testDelay() {
    TestTask task = null;
    // Throw an exception after delaying an increment
    // It will fails since delay is executed only on success
    task = TestTask.builder().schedule(TaskSchedule.builder().minRetries(1).build()).build().andThen(RetryCommand.builder().build()).andThen(// Throw but delay
    BasicCommand.builder().throwNPE(true).delayIncrement(true).build()).andThen(BasicCommand.builder().increment(true).build());
    tasks.submit(task);
    data.awaitFinished();
    assertThat(data.getExecutions(), equalTo(2));
    assertThat(data.getCounter(), equalTo(0));
    data.reset();
    // Increment normally + delayed
    task = TestTask.builder().schedule(TaskSchedule.builder().minRetries(1).build()).build().andThen(RetryCommand.builder().build()).andThen(BasicCommand.builder().increment(true).delayIncrement(true).build());
    tasks.submit(task);
    data.awaitFinished();
    assertThat(data.getExecutions(), equalTo(2));
    assertThat(data.getCounter(), equalTo(2));
    data.reset();
}
Also used : TestTask(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 4 with TestTask

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class TaskManagerTest method testFinallyExecute.

@Test
void testFinallyExecute() {
    TestTask task = null;
    // Normally fail
    // Finally is always executed
    task = TestTask.builder().schedule(TaskSchedule.builder().minRetries(1).build()).build().andThen(RetryCommand.builder().build()).andThen(BasicCommand.builder().throwNPE(true).build());
    tasks.submit(task);
    data.awaitFinished();
    assertThat(data.getExecutions(), equalTo(2));
    assertThat(data.getCounter(), equalTo(0));
    assertThat(data.getFinallyExecuteAttempts(), equalTo(1));
    data.reset();
    // Throw an exception finally execute an increment
    task = TestTask.builder().schedule(TaskSchedule.builder().minRetries(1).build()).build().andThen(RetryCommand.builder().build()).andThen(// Throw
    BasicCommand.builder().throwNPE(true).build()).finallyExecute(// But increment in finally
    BasicCommand.builder().increment(true).build());
    tasks.submit(task);
    data.awaitFinished();
    assertThat(data.getExecutions(), equalTo(2));
    assertThat(data.getCounter(), equalTo(1));
    assertThat(data.getFinallyExecuteAttempts(), equalTo(1));
    assertThat(data.getFinallyExecuteSuccess(), equalTo(1));
    data.reset();
    // Throw in finally
    task = TestTask.builder().schedule(TaskSchedule.builder().minRetries(1).build()).build().andThen(RetryCommand.builder().build()).andThen(// Throw
    BasicCommand.builder().throwNPE(true).build()).finallyExecute(// Throw again
    BasicCommand.builder().throwNPE(true).build());
    tasks.submit(task);
    data.awaitFinished();
    assertThat(data.getExecutions(), equalTo(2));
    assertThat(data.getCounter(), equalTo(0));
    assertThat(data.getFinallyExecuteAttempts(), equalTo(1));
    assertThat(data.getFinallyExecuteSuccess(), equalTo(0));
    data.reset();
}
Also used : TestTask(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with TestTask

use of org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class DataCollectingWorker method finallyExecute.

@Override
public void finallyExecute(Task aTask, WorkerContext ctl, Optional<Exception> error) {
    TestTask task = (TestTask) aTask;
    try {
        Command command = task.getFinalCommand();
        if (command != null)
            command.execute(ctl, task);
        data.recordFinallyExecuteSuccess();
    } finally {
        data.recordCounter(task.getCounter());
        data.recordFinallyExecuteAttempt();
        data.signalFinished();
    }
}
Also used : Command(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command) TestTask(org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask)

Aggregations

TestTask (org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask)10 QuarkusTest (io.quarkus.test.junit.QuarkusTest)8 Test (org.junit.jupiter.api.Test)8 Command (org.bf2.srs.fleetmanager.execution.impl.tasks.TestTask.Command)2 Duration (java.time.Duration)1 Instant (java.time.Instant)1 RetryExecutionControlException (org.bf2.srs.fleetmanager.execution.manager.impl.RetryExecutionControlException)1 StopExecutionControlException (org.bf2.srs.fleetmanager.execution.manager.impl.StopExecutionControlException)1 Tag (org.junit.jupiter.api.Tag)1