More phase tests

This time for when setting the new action fails
This commit is contained in:
Colin Goodheart-Smithe 2017-12-15 11:20:59 +00:00
parent 33a1a92e3d
commit b12f1a9526
2 changed files with 248 additions and 1 deletions

View File

@ -26,7 +26,7 @@ public class MockAction implements LifecycleAction {
public static final ParseField EXECUTED_COUNT_FIELD = new ParseField("executed_count");
public static final ParseField NAME_FIELD = new ParseField("name");
public static final String NAME = "TEST_ACTION";
private final SetOnce<Boolean> completed = new SetOnce<>();
private SetOnce<Boolean> completed = new SetOnce<>();
private final AtomicLong executedCount;
private Exception exceptionToThrow = null;
private boolean completeOnExecute = false;
@ -89,6 +89,10 @@ public class MockAction implements LifecycleAction {
return completed.get() != null && completed.get();
}
public void resetCompleted() {
completed = new SetOnce<>();
}
public long getExecutedCount() {
return executedCount.get();
}

View File

@ -783,4 +783,247 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
assertEquals(1L, thirdAction.getExecutedCount());
}
public void testExecuteActionFailedSetAction() throws Exception {
String indexName = randomAlphaOfLengthBetween(1, 20);
String phaseName = randomAlphaOfLengthBetween(1, 20);
TimeValue after = TimeValue.timeValueSeconds(randomIntBetween(10, 100));
Map<String, LifecycleAction> actions = new HashMap<>();
MockAction firstAction = new MockAction() {
@Override
public String getWriteableName() {
return "first_action";
}
};
firstAction.setCompleteOnExecute(false);
actions.put(firstAction.getWriteableName(), firstAction);
MockAction secondAction = new MockAction() {
@Override
public String getWriteableName() {
return "second_action";
}
};
secondAction.setCompleteOnExecute(true);
actions.put(secondAction.getWriteableName(), secondAction);
MockAction thirdAction = new MockAction() {
@Override
public String getWriteableName() {
return "third_action";
}
};
thirdAction.setCompleteOnExecute(false);
actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(), 0) {
@Override
public boolean canExecute(Phase phase) {
throw new AssertionError("canExecute should not have been called");
}
};
// First check that if setting the new action fails we don't execute the
// next phase
Exception exception = new RuntimeException();
context.failOnSetters(exception);
phase.execute(context, current -> {
if (current == null) {
return firstAction;
} else if ("first_action".equals(current.getWriteableName())) {
return secondAction;
} else if ("second_action".equals(current.getWriteableName())) {
return thirdAction;
}
return null;
});
assertEquals(indexName, context.getLifecycleTarget());
assertEquals(phaseName, context.getPhase());
assertEquals(secondAction.getWriteableName(), context.getAction());
assertFalse(firstAction.wasCompleted());
assertEquals(0L, firstAction.getExecutedCount());
assertTrue(secondAction.wasCompleted());
assertEquals(1L, secondAction.getExecutedCount());
assertFalse(thirdAction.wasCompleted());
assertEquals(0L, thirdAction.getExecutedCount());
// Now check that if we execute the phase again the current action is
// re-executed and if setting the new action fails again we still don't
// execute the next action
secondAction.setCompleteOnExecute(true);
secondAction.resetCompleted();
phase.execute(context, current -> {
if (current == null) {
return firstAction;
} else if ("first_action".equals(current.getWriteableName())) {
return secondAction;
} else if ("second_action".equals(current.getWriteableName())) {
return thirdAction;
}
return null;
});
assertEquals(indexName, context.getLifecycleTarget());
assertEquals(phaseName, context.getPhase());
assertEquals(secondAction.getWriteableName(), context.getAction());
assertFalse(firstAction.wasCompleted());
assertEquals(0L, firstAction.getExecutedCount());
assertTrue(secondAction.wasCompleted());
assertEquals(2L, secondAction.getExecutedCount());
assertFalse(thirdAction.wasCompleted());
assertEquals(0L, thirdAction.getExecutedCount());
// Now check that if we execute the phase again the current action is
// re-executed and if setting the new action suceeds now the next action
// is executed
secondAction.setCompleteOnExecute(true);
context.failOnSetters(null);
secondAction.resetCompleted();
phase.execute(context, current -> {
if (current == null) {
return firstAction;
} else if ("first_action".equals(current.getWriteableName())) {
return secondAction;
} else if ("second_action".equals(current.getWriteableName())) {
return thirdAction;
}
return null;
});
assertEquals(indexName, context.getLifecycleTarget());
assertEquals(phaseName, context.getPhase());
assertEquals(thirdAction.getWriteableName(), context.getAction());
assertFalse(firstAction.wasCompleted());
assertEquals(0L, firstAction.getExecutedCount());
assertTrue(secondAction.wasCompleted());
assertEquals(3L, secondAction.getExecutedCount());
assertFalse(thirdAction.wasCompleted());
assertEquals(1L, thirdAction.getExecutedCount());
}
public void testExecuteLastActionFailedSetAction() throws Exception {
String indexName = randomAlphaOfLengthBetween(1, 20);
String phaseName = randomAlphaOfLengthBetween(1, 20);
TimeValue after = TimeValue.timeValueSeconds(randomIntBetween(10, 100));
Map<String, LifecycleAction> actions = new HashMap<>();
MockAction firstAction = new MockAction() {
@Override
public String getWriteableName() {
return "first_action";
}
};
firstAction.setCompleteOnExecute(false);
actions.put(firstAction.getWriteableName(), firstAction);
MockAction secondAction = new MockAction() {
@Override
public String getWriteableName() {
return "second_action";
}
};
secondAction.setCompleteOnExecute(false);
actions.put(secondAction.getWriteableName(), secondAction);
MockAction thirdAction = new MockAction() {
@Override
public String getWriteableName() {
return "third_action";
}
};
thirdAction.setCompleteOnExecute(true);
actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, thirdAction.getWriteableName(), 0) {
@Override
public boolean canExecute(Phase phase) {
throw new AssertionError("canExecute should not have been called");
}
};
// First check setting the ACTION_COMPLETED fails
Exception exception = new RuntimeException();
context.failOnSetters(exception);
phase.execute(context, current -> {
if (current == null) {
return firstAction;
} else if ("first_action".equals(current.getWriteableName())) {
return secondAction;
} else if ("second_action".equals(current.getWriteableName())) {
return thirdAction;
}
return null;
});
assertEquals(indexName, context.getLifecycleTarget());
assertEquals(phaseName, context.getPhase());
assertEquals(thirdAction.getWriteableName(), context.getAction());
assertFalse(firstAction.wasCompleted());
assertEquals(0L, firstAction.getExecutedCount());
assertFalse(secondAction.wasCompleted());
assertEquals(0L, secondAction.getExecutedCount());
assertTrue(thirdAction.wasCompleted());
assertEquals(1L, thirdAction.getExecutedCount());
// Now check the same happens if it fails again
thirdAction.setCompleteOnExecute(true);
thirdAction.resetCompleted();
phase.execute(context, current -> {
if (current == null) {
return firstAction;
} else if ("first_action".equals(current.getWriteableName())) {
return secondAction;
} else if ("second_action".equals(current.getWriteableName())) {
return thirdAction;
}
return null;
});
assertEquals(indexName, context.getLifecycleTarget());
assertEquals(phaseName, context.getPhase());
assertEquals(thirdAction.getWriteableName(), context.getAction());
assertFalse(firstAction.wasCompleted());
assertEquals(0L, firstAction.getExecutedCount());
assertFalse(secondAction.wasCompleted());
assertEquals(0L, secondAction.getExecutedCount());
assertTrue(thirdAction.wasCompleted());
assertEquals(2L, thirdAction.getExecutedCount());
// Now check the action updates if we try again and setting the
// PHASE_COMPLETED succeeds
context.failOnSetters(null);
thirdAction.resetCompleted();
phase.execute(context, current -> {
if (current == null) {
return firstAction;
} else if ("first_action".equals(current.getWriteableName())) {
return secondAction;
} else if ("second_action".equals(current.getWriteableName())) {
return thirdAction;
}
return null;
});
assertEquals(indexName, context.getLifecycleTarget());
assertEquals(phaseName, context.getPhase());
assertEquals(Phase.PHASE_COMPLETED, context.getAction());
assertFalse(firstAction.wasCompleted());
assertEquals(0L, firstAction.getExecutedCount());
assertFalse(secondAction.wasCompleted());
assertEquals(0L, secondAction.getExecutedCount());
assertTrue(thirdAction.wasCompleted());
assertEquals(3L, thirdAction.getExecutedCount());
}
}