|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.uber.cadence.internal.replay; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.uber.cadence.Decision; |
| 22 | +import com.uber.cadence.DecisionType; |
| 23 | +import com.uber.cadence.HistoryEvent; |
| 24 | +import com.uber.cadence.ScheduleActivityTaskDecisionAttributes; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | +import junit.framework.TestCase; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +public class DecisionsHelperTest extends TestCase { |
| 31 | + @Test |
| 32 | + public void testGetDecisionsSimple() { |
| 33 | + // Start the decision |
| 34 | + DecisionsHelper decisionsHelper = new DecisionsHelper(null, null); |
| 35 | + |
| 36 | + HistoryHelper.DecisionEvents decisionEvents = |
| 37 | + new HistoryHelper.DecisionEvents( |
| 38 | + Collections.emptyList(), ImmutableList.of(new HistoryEvent()), false, 123, 456); |
| 39 | + |
| 40 | + decisionsHelper.handleDecisionTaskStartedEvent(decisionEvents); |
| 41 | + |
| 42 | + // Schedule 2 activity tasks |
| 43 | + ScheduleActivityTaskDecisionAttributes scheduleActivityTaskDecisionAttributes = |
| 44 | + new ScheduleActivityTaskDecisionAttributes(); |
| 45 | + scheduleActivityTaskDecisionAttributes.setActivityId("testActivityId"); |
| 46 | + |
| 47 | + decisionsHelper.scheduleActivityTask(scheduleActivityTaskDecisionAttributes); |
| 48 | + decisionsHelper.scheduleActivityTask(scheduleActivityTaskDecisionAttributes); |
| 49 | + |
| 50 | + // Act |
| 51 | + List<Decision> decisions = decisionsHelper.getDecisions(); |
| 52 | + |
| 53 | + // Assert |
| 54 | + assertEquals(2, decisions.size()); |
| 55 | + } |
| 56 | + |
| 57 | + private static final int MAXIMUM_DECISIONS_PER_COMPLETION = 10000; |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testGetDecisionsManyDecisions() { |
| 61 | + // Start the decision |
| 62 | + DecisionsHelper decisionsHelper = new DecisionsHelper(null, null); |
| 63 | + |
| 64 | + HistoryHelper.DecisionEvents decisionEvents = |
| 65 | + new HistoryHelper.DecisionEvents( |
| 66 | + Collections.emptyList(), ImmutableList.of(new HistoryEvent()), false, 123, 456); |
| 67 | + |
| 68 | + decisionsHelper.handleDecisionTaskStartedEvent(decisionEvents); |
| 69 | + |
| 70 | + // Schedule more than MAXIMUM_DECISIONS_PER_COMPLETION activity tasks |
| 71 | + ScheduleActivityTaskDecisionAttributes scheduleActivityTaskDecisionAttributes = |
| 72 | + new ScheduleActivityTaskDecisionAttributes(); |
| 73 | + scheduleActivityTaskDecisionAttributes.setActivityId("testActivityId"); |
| 74 | + |
| 75 | + for (int i = 0; i < MAXIMUM_DECISIONS_PER_COMPLETION * 2; i++) { |
| 76 | + decisionsHelper.scheduleActivityTask(scheduleActivityTaskDecisionAttributes); |
| 77 | + } |
| 78 | + |
| 79 | + // Act |
| 80 | + List<Decision> decisions = decisionsHelper.getDecisions(); |
| 81 | + |
| 82 | + // Assert |
| 83 | + assertEquals(MAXIMUM_DECISIONS_PER_COMPLETION, decisions.size()); |
| 84 | + |
| 85 | + // Check that the last decision is a FORCE_IMMEDIATE_DECISION_TIMER |
| 86 | + Decision lastDecision = decisions.get(decisions.size() - 1); |
| 87 | + assertEquals(DecisionType.StartTimer, lastDecision.getDecisionType()); |
| 88 | + String timerId = lastDecision.getStartTimerDecisionAttributes().getTimerId(); |
| 89 | + assertEquals(timerId, DecisionsHelper.FORCE_IMMEDIATE_DECISION_TIMER); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testNotifyDecisionSent() { |
| 94 | + DecisionsHelper decisionsHelper = new DecisionsHelper(null, null); |
| 95 | + |
| 96 | + // Start the decision |
| 97 | + HistoryHelper.DecisionEvents decisionEvents = |
| 98 | + new HistoryHelper.DecisionEvents( |
| 99 | + Collections.emptyList(), ImmutableList.of(new HistoryEvent()), false, 123, 456); |
| 100 | + |
| 101 | + decisionsHelper.handleDecisionTaskStartedEvent(decisionEvents); |
| 102 | + |
| 103 | + // Schedule activity task |
| 104 | + ScheduleActivityTaskDecisionAttributes scheduleActivityTaskDecisionAttributes = |
| 105 | + new ScheduleActivityTaskDecisionAttributes(); |
| 106 | + scheduleActivityTaskDecisionAttributes.setActivityId("testActivityId"); |
| 107 | + |
| 108 | + decisionsHelper.scheduleActivityTask(scheduleActivityTaskDecisionAttributes); |
| 109 | + |
| 110 | + // Act |
| 111 | + decisionsHelper.notifyDecisionSent(); |
| 112 | + |
| 113 | + // The decision is sent so it's not returned anymore |
| 114 | + assertEquals(0, decisionsHelper.getDecisions().size()); |
| 115 | + } |
| 116 | +} |
0 commit comments