|
| 1 | +// Copyright (c) 2017-2021 Uber Technologies Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package internal |
| 22 | + |
| 23 | +import ( |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "go.uber.org/cadence/.gen/go/shared" |
| 26 | + "go.uber.org/cadence/internal/common" |
| 27 | + "testing" |
| 28 | +) |
| 29 | + |
| 30 | +type matchReplayWithHistoryTestCase struct { |
| 31 | + name string |
| 32 | + replayDecisions []*shared.Decision |
| 33 | + historyEvents []*shared.HistoryEvent |
| 34 | + expectError bool |
| 35 | +} |
| 36 | + |
| 37 | +func TestMatchReplayWithHistory(t *testing.T) { |
| 38 | + testCases := []matchReplayWithHistoryTestCase{ |
| 39 | + { |
| 40 | + name: "Success matching", |
| 41 | + replayDecisions: []*shared.Decision{ |
| 42 | + mockDecision(shared.DecisionTypeScheduleActivityTask), |
| 43 | + }, |
| 44 | + historyEvents: []*shared.HistoryEvent{ |
| 45 | + mockHistoryEvent(shared.EventTypeActivityTaskScheduled), |
| 46 | + }, |
| 47 | + expectError: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Mismatch error", |
| 51 | + replayDecisions: []*shared.Decision{ |
| 52 | + mockDecision(shared.DecisionTypeStartTimer), |
| 53 | + }, |
| 54 | + historyEvents: []*shared.HistoryEvent{ |
| 55 | + mockHistoryEvent(shared.EventTypeTimerCanceled), |
| 56 | + }, |
| 57 | + expectError: true, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tc := range testCases { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + info := &WorkflowInfo{} // Assuming WorkflowInfo is a struct you have defined |
| 64 | + err := matchReplayWithHistory(info, tc.replayDecisions, tc.historyEvents) |
| 65 | + if tc.expectError { |
| 66 | + assert.Error(t, err) |
| 67 | + } else { |
| 68 | + assert.NoError(t, err) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func mockHistoryEvent(eventType shared.EventType) *shared.HistoryEvent { |
| 75 | + event := &shared.HistoryEvent{ |
| 76 | + EventType: &eventType, |
| 77 | + } |
| 78 | + |
| 79 | + switch eventType { |
| 80 | + case shared.EventTypeActivityTaskScheduled: |
| 81 | + event.ActivityTaskScheduledEventAttributes = &shared.ActivityTaskScheduledEventAttributes{ |
| 82 | + ActivityId: common.StringPtr("mockActivityId"), |
| 83 | + ActivityType: &shared.ActivityType{Name: common.StringPtr("mockActivityType")}, |
| 84 | + TaskList: &shared.TaskList{Name: common.StringPtr("mockTaskList")}, |
| 85 | + Input: []byte("mockInput"), |
| 86 | + } |
| 87 | + case shared.EventTypeTimerCanceled: // Newly added case |
| 88 | + event.TimerCanceledEventAttributes = &shared.TimerCanceledEventAttributes{ |
| 89 | + TimerId: common.StringPtr("mockTimerId"), |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return event |
| 94 | +} |
| 95 | + |
| 96 | +func mockDecision(decisionType shared.DecisionType) *shared.Decision { |
| 97 | + decision := &shared.Decision{ |
| 98 | + DecisionType: &decisionType, |
| 99 | + } |
| 100 | + |
| 101 | + switch decisionType { |
| 102 | + case shared.DecisionTypeScheduleActivityTask: |
| 103 | + decision.ScheduleActivityTaskDecisionAttributes = &shared.ScheduleActivityTaskDecisionAttributes{ |
| 104 | + ActivityId: common.StringPtr("mockActivityId"), |
| 105 | + ActivityType: &shared.ActivityType{Name: common.StringPtr("mockActivityType")}, |
| 106 | + TaskList: &shared.TaskList{Name: common.StringPtr("mockTaskList")}, |
| 107 | + Input: []byte("mockInput"), |
| 108 | + } |
| 109 | + case shared.DecisionTypeStartTimer: // Newly added case |
| 110 | + decision.StartTimerDecisionAttributes = &shared.StartTimerDecisionAttributes{ |
| 111 | + TimerId: common.StringPtr("mockTimerId"), |
| 112 | + StartToFireTimeoutSeconds: common.Int64Ptr(60), // Example value |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return decision |
| 117 | +} |
0 commit comments