import ch.qos.logback.classic.Logger;
import java.time.Instant;
+import java.util.ArrayDeque;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
private static final Logger logger = (Logger) LoggerFactory.getLogger(OperationPartial.class);
private static final ExtractAppender appender = new ExtractAppender();
+ private static final List<String> PROP_NAMES = List.of("hello", "world");
+
@Mock
private ActorService service;
@Mock
private OperationOutcome opstart;
private OperationOutcome opend;
+ private Deque<OperationOutcome> starts;
+ private Deque<OperationOutcome> ends;
+
private OperatorConfig config;
/**
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- /**
+ /*
* Attach appender to the logger.
*/
appender.setContext(logger.getLoggerContext());
opstart = null;
opend = null;
+
+ starts = new ArrayDeque<>(10);
+ ends = new ArrayDeque<>(10);
}
@Test
assertNull(future.get(5, TimeUnit.SECONDS));
}
+ @Test
+ public void testGetPropertyNames() {
+ assertThat(oper.getPropertyNames()).isEqualTo(PROP_NAMES);
+ }
+
+ @Test
+ public void testGetProperty_testSetProperty() {
+ oper.setProperty("propertyA", "valueA");
+ oper.setProperty("propertyB", "valueB");
+ oper.setProperty("propertyC", 20);
+
+ assertEquals("valueA", oper.getProperty("propertyA"));
+ assertEquals("valueB", oper.getProperty("propertyB"));
+ assertEquals(Integer.valueOf(20), oper.getProperty("propertyC"));
+ }
+
@Test
public void testStart() {
verifyRun("testStart", 1, 1, PolicyResult.SUCCESS);
Map<String, Object> payload = params.getPayload();
assertNotNull(payload);
- @SuppressWarnings("unchecked")
- Map<String, Object> resource = (Map<String, Object>) payload.get("resource");
- assertNotNull(resource);
+ assertEquals(oper.makeGuardPayload(), payload);
+ }
- @SuppressWarnings("unchecked")
- Map<String, Object> guard = (Map<String, Object>) resource.get("guard");
- assertEquals(oper.makeGuardPayload(), guard);
+ /**
+ * Tests startGuardAsync() when preprocessing is disabled.
+ */
+ @Test
+ public void testStartGuardAsyncDisabled() {
+ params = params.toBuilder().preprocessed(true).build();
+ assertNull(new MyOper().startGuardAsync());
}
@Test
// request id changes, so remove it
payload.remove("requestId");
- assertEquals("{actor=my-actor, recipe=my-operation, target=my-entity}", payload.toString());
+ assertEquals("{actor=my-actor, operation=my-operation, target=my-entity}", payload.toString());
// repeat, but with closed loop name
event.setClosedLoopControlName("my-loop");
payload = oper.makeGuardPayload();
payload.remove("requestId");
- assertEquals("{actor=my-actor, recipe=my-operation, target=my-entity, clname=my-loop}", payload.toString());
+ assertEquals("{actor=my-actor, operation=my-operation, target=my-entity, clname=my-loop}", payload.toString());
}
@Test
@Test
public void testIsSuccess() {
+ assertFalse(oper.isSuccess(null));
+
OperationOutcome outcome = new OperationOutcome();
outcome.setResult(PolicyResult.SUCCESS);
/*
* Use an operation that doesn't override doOperation().
*/
- OperationPartial oper2 = new OperationPartial(params, config) {};
+ OperationPartial oper2 = new OperationPartial(params, config, Collections.emptyList()) {};
oper2.start();
assertTrue(executor.runAll(MAX_REQUESTS));
// arrange to return null from doOperation()
oper = new MyOper() {
@Override
- protected OperationOutcome doOperation(int attempt, OperationOutcome operation) {
+ protected OperationOutcome doOperation(int attempt, OperationOutcome outcome) {
// update counters
- super.doOperation(attempt, operation);
+ super.doOperation(attempt, outcome);
return null;
}
};
- verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, null, noop());
+ verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, noop());
}
@Test
* Tests handleFailure() when the outcome is a success.
*/
@Test
- public void testHandlePreprocessorFailureTrue() {
+ public void testHandlePreprocessorFailureSuccess() {
oper.setPreProc(CompletableFuture.completedFuture(makeSuccess()));
verifyRun("testHandlePreprocessorFailureTrue", 1, 1, PolicyResult.SUCCESS);
}
* Tests handleFailure() when the outcome is <i>not</i> a success.
*/
@Test
- public void testHandlePreprocessorFailureFalse() throws Exception {
+ public void testHandlePreprocessorFailureFailed() throws Exception {
oper.setPreProc(CompletableFuture.completedFuture(makeFailure()));
verifyRun("testHandlePreprocessorFailureFalse", 1, 0, PolicyResult.FAILURE_GUARD);
}
assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+ oper.setOutcome(outcome, PolicyResult.SUCCESS);
+ assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+ assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+
for (PolicyResult result : FAILURE_RESULTS) {
outcome = new OperationOutcome();
oper.setOutcome(outcome, result);
@Test
public void testGetRetryWait() {
// need an operator that doesn't override the retry time
- OperationPartial oper2 = new OperationPartial(params, config) {};
+ OperationPartial oper2 = new OperationPartial(params, config, Collections.emptyList()) {};
assertEquals(OperationPartial.DEFAULT_RETRY_WAIT_MS, oper2.getRetryWaitMs());
}
++numStart;
tstart = oper.getStart();
opstart = oper;
+ starts.add(oper);
}
private void completer(OperationOutcome oper) {
++numEnd;
opend = oper;
+ ends.add(oper);
}
/**
private void verifyRun(String testName, int expectedCallbacks, int expectedOperations,
PolicyResult expectedResult) {
- String expectedSubRequestId =
- (expectedResult == PolicyResult.FAILURE_EXCEPTION ? null : String.valueOf(expectedOperations));
-
- verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, expectedSubRequestId, noop());
+ verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, noop());
}
/**
* @param expectedCallbacks number of callbacks expected
* @param expectedOperations number of operation invocations expected
* @param expectedResult expected outcome
- * @param expectedSubRequestId expected sub request ID
* @param manipulator function to modify the future returned by
* {@link OperationPartial#start(ControlLoopOperationParams)} before the tasks
* in the executor are run
*/
private void verifyRun(String testName, int expectedCallbacks, int expectedOperations, PolicyResult expectedResult,
- String expectedSubRequestId, Consumer<CompletableFuture<OperationOutcome>> manipulator) {
+ Consumer<CompletableFuture<OperationOutcome>> manipulator) {
+
+ tstart = null;
+ opstart = null;
+ opend = null;
+ starts.clear();
+ ends.clear();
CompletableFuture<OperationOutcome> future = oper.start();
try {
assertTrue(future.isDone());
- assertSame(testName, opend, future.get());
+ assertEquals(testName, opend, future.get());
+
+ // "start" is never final
+ for (OperationOutcome outcome : starts) {
+ assertFalse(testName, outcome.isFinalOutcome());
+ }
+
+ // only the last "complete" is final
+ assertTrue(testName, ends.removeLast().isFinalOutcome());
+
+ for (OperationOutcome outcome : ends) {
+ assertFalse(outcome.isFinalOutcome());
+ }
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
if (expectedOperations > 0) {
- assertEquals(testName, expectedSubRequestId, opend.getSubRequestId());
+ assertNotNull(testName, oper.getSubRequestId());
+ assertEquals(testName + " op start", oper.getSubRequestId(), opstart.getSubRequestId());
+ assertEquals(testName + " op end", oper.getSubRequestId(), opend.getSubRequestId());
}
}
private void setOperCoderException() {
oper = new MyOper() {
@Override
- protected Coder makeCoder() {
+ protected Coder getCoder() {
return new StandardCoder() {
@Override
public String encode(Object object, boolean pretty) throws CoderException {
public MyOper() {
- super(OperationPartialTest.this.params, config);
+ super(OperationPartialTest.this.params, config, PROP_NAMES);
}
@Override