InstanceUpdater implements StateEvaluator<Optional<IScheduledTask>> { @Override public synchronized StateEvaluator.Result evaluate(Optional<IScheduledTask> actualState) { boolean desiredPresent = desiredState.isPresent(); boolean actualPresent = isTaskPresent(actualState); if (desiredPresent && actualPresent) { return handleActualAndDesiredPresent(actualState.get()); } else if (desiredPresent) { return REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE; } else if (actualPresent) { return isKillable(actualState.get().getStatus()) ? KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE : EVALUATE_ON_STATE_CHANGE; } else { return SUCCEEDED; } } InstanceUpdater(
      Optional<ITaskConfig> desiredState,
      int toleratedFailures,
      Amount<Long, Time> minRunningTime,
      Clock clock); @Override synchronized StateEvaluator.Result evaluate(Optional<IScheduledTask> actualState);  }
@Test public void testSuccessfulUpdate() { TestFixture f = new TestFixture(NEW, 1); f.setActualState(OLD); f.evaluate(KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, KILLING); f.evaluate(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE, FINISHED); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING, ASSIGNED, STARTING); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.advanceTime(MIN_RUNNING_TIME); f.evaluateCurrentState(SUCCEEDED); }
@Test public void testUpdateRetryOnTaskExit() { TestFixture f = new TestFixture(NEW, 1); f.setActualState(OLD); f.evaluate(KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, KILLING); f.evaluate(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE, FINISHED); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING, ASSIGNED, STARTING); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, FAILED); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING, ASSIGNED, STARTING); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.advanceTime(MIN_RUNNING_TIME); f.evaluateCurrentState(SUCCEEDED); }
@Test public void testUpdateRetryFailure() { TestFixture f = new TestFixture(NEW, 0); f.setActualState(OLD); f.evaluate(KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, KILLING); f.evaluate(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE, FINISHED); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING, ASSIGNED, STARTING); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.evaluate(Result.FAILED_TERMINATED, FAILED); }
@Test public void testNoopUpdate() { TestFixture f = new TestFixture(NEW, 1); f.setActualState(NEW); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.advanceTime(MIN_RUNNING_TIME); f.evaluate(SUCCEEDED, RUNNING); }
@Test public void testNoOldConfig() { TestFixture f = new TestFixture(NEW, 1); f.setActualStateAbsent(); f.evaluateCurrentState(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING, ASSIGNED, STARTING); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.advanceTime(MIN_RUNNING_TIME); f.evaluateCurrentState(SUCCEEDED); }
@Test public void testNoNewConfig() { TestFixture f = new TestFixture(NO_CONFIG, 1); f.setActualState(OLD); f.evaluate(KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, KILLING); f.evaluate(SUCCEEDED, FINISHED); f.evaluateCurrentState(SUCCEEDED); }
@Test public void testStuckInPending() { TestFixture f = new TestFixture(NEW, 1); f.setActualState(OLD); f.evaluate(KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, KILLING); f.evaluate(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE, FINISHED); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING); f.advanceTime(A_LONG_TIME); f.evaluateCurrentState(EVALUATE_ON_STATE_CHANGE); }
@Test public void testSlowToKill() { TestFixture f = new TestFixture(NEW, 1); f.setActualState(OLD); f.evaluate(KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, KILLING); f.evaluate(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE, FINISHED); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING); f.advanceTime(A_LONG_TIME); f.evaluateCurrentState(EVALUATE_ON_STATE_CHANGE); f.setActualStateAbsent(); f.evaluateCurrentState(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING); f.evaluate(EVALUATE_ON_STATE_CHANGE, ASSIGNED, STARTING); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.evaluate(EVALUATE_ON_STATE_CHANGE, KILLING); f.advanceTime(A_LONG_TIME); f.evaluateCurrentState(EVALUATE_ON_STATE_CHANGE); }
@Test(expected = IllegalArgumentException.class) public void testInvalidInput() { TestFixture f = new TestFixture(NEW, 1); ScheduledTask noEvents = new TaskUtil(new FakeClock()) .makeTask(OLD, RUNNING).newBuilder().setTaskEvents(ImmutableList.of()); f.updater.evaluate(Optional.of(IScheduledTask.build(noEvents))); }
@Test public void testOldTaskDies() { TestFixture f = new TestFixture(NEW, 1); f.setActualState(OLD); f.evaluate(EVALUATE_ON_STATE_CHANGE, FINISHED); }
@Test public void testOldTaskDiesAndRescheduled() { TestFixture f = new TestFixture(NEW, 1); f.setActualState(OLD); f.evaluate(EVALUATE_ON_STATE_CHANGE, FINISHED); f.evaluate(KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE, PENDING); f.setActualStateAbsent(); f.evaluateCurrentState(REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE); f.setActualState(NEW); f.evaluate(EVALUATE_ON_STATE_CHANGE, PENDING, ASSIGNED, STARTING); f.evaluate(EVALUATE_AFTER_MIN_RUNNING_MS, RUNNING); f.advanceTime(MIN_RUNNING_TIME); f.evaluateCurrentState(SUCCEEDED); }