OneWayJobUpdater { synchronized EvaluationResult<K> evaluate( Map<K, T> instancesNeedingUpdate, InstanceStateProvider<K, T> stateProvider) { if (stateMachine.getState() == OneWayStatus.IDLE) { stateMachine.transition(OneWayStatus.WORKING); } Preconditions.checkState( stateMachine.getState() == OneWayStatus.WORKING, "Attempted to evaluate an inactive job updater."); ImmutableMap.Builder<K, SideEffect> actions = ImmutableMap.<K, SideEffect>builder() .putAll(evaluateInstances(instancesNeedingUpdate)); if (computeJobUpdateStatus() == OneWayStatus.WORKING) { actions.putAll(startNextInstanceGroup(stateProvider)); } return new EvaluationResult<>(computeJobUpdateStatus(), actions.build()); } OneWayJobUpdater(
      UpdateStrategy<K> strategy,
      int maxFailedInstances,
      Map<K, StateEvaluator<T>> instanceEvaluators);   }
@Test public void testSuccessfulUpdate() { expect(strategy.getNextGroup(ImmutableSet.of(0, 1, 2, 3), EMPTY)) .andReturn(ImmutableSet.of(0, 2)); String s0 = "0"; String s1 = "1"; String s2 = "2"; String s3 = "3"; expectFetchAndEvaluate( 0, instance0, s0, KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE); expectFetchAndEvaluate( 2, instance2, s2, REPLACE_TASK_AND_EVALUATE_ON_STATE_CHANGE); expectEvaluate(instance0, s0, EVALUATE_ON_STATE_CHANGE); expect(strategy.getNextGroup(ImmutableSet.of(1, 3), ImmutableSet.of(0, 2))).andReturn(EMPTY); expectEvaluate(instance0, s0, SUCCEEDED); expect(strategy.getNextGroup(ImmutableSet.of(1, 3), ImmutableSet.of(2))).andReturn(EMPTY); expectEvaluate(instance2, s2, SUCCEEDED); expect(strategy.getNextGroup(ImmutableSet.of(1, 3), EMPTY)) .andReturn(ImmutableSet.of(1, 3)); expectFetchAndEvaluate(1, instance1, s1, SUCCEEDED); expectEvaluate(instance3, s3, EVALUATE_AFTER_MIN_RUNNING_MS); expectFetchAndEvaluate(3, instance3, s3, SUCCEEDED); control.replay(); jobUpdater = new OneWayJobUpdater<>(strategy, 0, allInstances); evaluate( OneWayStatus.WORKING, ImmutableMap.of( 0, sideEffect(KILL_TASK, InstanceUpdateStatus.WORKING), 2, sideEffect(ADD_TASK, InstanceUpdateStatus.WORKING))); evaluate( 0, s0, OneWayStatus.WORKING, ImmutableMap.of(0, sideEffect(AWAIT_STATE_CHANGE))); evaluate( 0, s0, OneWayStatus.WORKING, ImmutableMap.of(0, sideEffect(InstanceUpdateStatus.SUCCEEDED))); evaluate( 2, s2, OneWayStatus.WORKING, ImmutableMap.of( 1, sideEffect(InstanceUpdateStatus.WORKING, InstanceUpdateStatus.SUCCEEDED), 2, sideEffect(InstanceUpdateStatus.SUCCEEDED), 3, sideEffect(WATCH_TASK, InstanceUpdateStatus.WORKING))); evaluate( 3, s3, OneWayStatus.SUCCEEDED, ImmutableMap.of(3, sideEffect(InstanceUpdateStatus.SUCCEEDED))); }
@Test public void testFailedUpdate() { expect(strategy.getNextGroup(ImmutableSet.of(0, 1, 2, 3), EMPTY)) .andReturn(ImmutableSet.of(0, 1)); String s0 = "0"; String s1 = "1"; expectFetchAndEvaluate( 0, instance0, s0, FAILED_TERMINATED); expectFetchAndEvaluate( 1, instance1, s1, KILL_TASK_AND_EVALUATE_ON_STATE_CHANGE); control.replay(); jobUpdater = new OneWayJobUpdater<>(strategy, 0, allInstances); evaluate( OneWayStatus.FAILED, ImmutableMap.of( 0, sideEffect(InstanceUpdateStatus.WORKING, InstanceUpdateStatus.FAILED), 1, sideEffect(KILL_TASK, InstanceUpdateStatus.WORKING))); try { jobUpdater.evaluate(ImmutableMap.of(), stateProvider); fail(); } catch (IllegalStateException e) { } }
@Test public void testEvaluateCompletedInstance() { expect(strategy.getNextGroup(ImmutableSet.of(0, 1, 2, 3), EMPTY)) .andReturn(ImmutableSet.of(0)); expect(strategy.getNextGroup(ImmutableSet.of(1, 2, 3), EMPTY)) .andReturn(ImmutableSet.of(1)); expect(strategy.getNextGroup(ImmutableSet.of(2, 3), ImmutableSet.of(1))) .andReturn(ImmutableSet.of()); String s0 = "0"; String s1 = "1"; expectFetchAndEvaluate( 0, instance0, s0, SUCCEEDED); expectFetchAndEvaluate( 1, instance1, s1, EVALUATE_ON_STATE_CHANGE); control.replay(); jobUpdater = new OneWayJobUpdater<>(strategy, 0, allInstances); evaluate( OneWayStatus.WORKING, ImmutableMap.of( 0, sideEffect(InstanceUpdateStatus.WORKING, InstanceUpdateStatus.SUCCEEDED), 1, sideEffect(AWAIT_STATE_CHANGE, InstanceUpdateStatus.WORKING))); evaluate( 0, s0, OneWayStatus.WORKING, ImmutableMap.of()); }