|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Uber Technologies, Inc. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +package workflow |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "errors" |
| 30 | + "testing" |
| 31 | + "time" |
| 32 | + |
| 33 | + "github.com/golang/mock/gomock" |
| 34 | + "github.com/stretchr/testify/assert" |
| 35 | + "github.com/stretchr/testify/require" |
| 36 | + "go.temporal.io/api/common/v1" |
| 37 | + "go.temporal.io/api/workflow/v1" |
| 38 | + |
| 39 | + "go.temporal.io/server/api/persistence/v1" |
| 40 | + "go.temporal.io/server/common/persistence/visibility/manager" |
| 41 | + "go.temporal.io/server/service/history/tests" |
| 42 | +) |
| 43 | + |
| 44 | +func TestRelocatableAttributesFetcher_Fetch(t *testing.T) { |
| 45 | + mutableStateAttributes := &RelocatableAttributes{ |
| 46 | + Memo: &common.Memo{Fields: map[string]*common.Payload{ |
| 47 | + "memoLocation": {Data: []byte("mutableState")}, |
| 48 | + }}, |
| 49 | + SearchAttributes: &common.SearchAttributes{IndexedFields: map[string]*common.Payload{ |
| 50 | + "searchAttributesLocation": {Data: []byte("mutableState")}, |
| 51 | + }}, |
| 52 | + } |
| 53 | + persistenceAttributes := &RelocatableAttributes{ |
| 54 | + Memo: &common.Memo{Fields: map[string]*common.Payload{ |
| 55 | + "memoLocation": {Data: []byte("persistence")}, |
| 56 | + }}, |
| 57 | + SearchAttributes: &common.SearchAttributes{IndexedFields: map[string]*common.Payload{ |
| 58 | + "searchAttributesLocation": {Data: []byte("persistence")}, |
| 59 | + }}, |
| 60 | + } |
| 61 | + require.NotEqual(t, mutableStateAttributes.Memo, persistenceAttributes.Memo) |
| 62 | + require.NotEqual(t, mutableStateAttributes.SearchAttributes, persistenceAttributes.SearchAttributes) |
| 63 | + testErr := errors.New("test error") |
| 64 | + for _, c := range []*struct { |
| 65 | + Name string |
| 66 | + CloseVisibilityTaskCompleted bool |
| 67 | + GetWorkflowExecutionErr error |
| 68 | + |
| 69 | + ExpectedInfo *RelocatableAttributes |
| 70 | + ExpectedErr error |
| 71 | + }{ |
| 72 | + { |
| 73 | + Name: "CloseVisibilityTaskNotComplete", |
| 74 | + CloseVisibilityTaskCompleted: false, |
| 75 | + |
| 76 | + ExpectedInfo: mutableStateAttributes, |
| 77 | + }, |
| 78 | + { |
| 79 | + Name: "CloseVisibilityTaskCompleted", |
| 80 | + CloseVisibilityTaskCompleted: true, |
| 81 | + |
| 82 | + ExpectedInfo: persistenceAttributes, |
| 83 | + }, |
| 84 | + { |
| 85 | + Name: "GetWorkflowExecutionErr", |
| 86 | + CloseVisibilityTaskCompleted: true, |
| 87 | + GetWorkflowExecutionErr: testErr, |
| 88 | + |
| 89 | + ExpectedErr: testErr, |
| 90 | + }, |
| 91 | + } { |
| 92 | + c := c |
| 93 | + t.Run(c.Name, func(t *testing.T) { |
| 94 | + t.Parallel() |
| 95 | + closeTime := time.Unix(100, 0) |
| 96 | + executionInfo := &persistence.WorkflowExecutionInfo{ |
| 97 | + Memo: mutableStateAttributes.Memo.Fields, |
| 98 | + SearchAttributes: mutableStateAttributes.SearchAttributes.IndexedFields, |
| 99 | + CloseVisibilityTaskCompleted: c.CloseVisibilityTaskCompleted, |
| 100 | + CloseTime: &closeTime, |
| 101 | + WorkflowId: tests.WorkflowID, |
| 102 | + } |
| 103 | + executionState := &persistence.WorkflowExecutionState{ |
| 104 | + RunId: tests.RunID, |
| 105 | + } |
| 106 | + namespaceEntry := tests.GlobalNamespaceEntry |
| 107 | + ctrl := gomock.NewController(t) |
| 108 | + visibilityManager := manager.NewMockVisibilityManager(ctrl) |
| 109 | + mutableState := NewMockMutableState(ctrl) |
| 110 | + mutableState.EXPECT().GetExecutionInfo().Return(executionInfo).AnyTimes() |
| 111 | + mutableState.EXPECT().GetNamespaceEntry().Return(namespaceEntry).AnyTimes() |
| 112 | + mutableState.EXPECT().GetExecutionState().Return(executionState).AnyTimes() |
| 113 | + if c.CloseVisibilityTaskCompleted { |
| 114 | + visibilityManager.EXPECT().GetWorkflowExecution(gomock.Any(), &manager.GetWorkflowExecutionRequest{ |
| 115 | + NamespaceID: namespaceEntry.ID(), |
| 116 | + Namespace: namespaceEntry.Name(), |
| 117 | + RunID: tests.RunID, |
| 118 | + WorkflowID: tests.WorkflowID, |
| 119 | + CloseTime: &closeTime, |
| 120 | + }).Return(&manager.GetWorkflowExecutionResponse{ |
| 121 | + Execution: &workflow.WorkflowExecutionInfo{ |
| 122 | + Memo: persistenceAttributes.Memo, |
| 123 | + SearchAttributes: persistenceAttributes.SearchAttributes, |
| 124 | + }, |
| 125 | + }, c.GetWorkflowExecutionErr) |
| 126 | + } |
| 127 | + ctx := context.Background() |
| 128 | + |
| 129 | + fetcher := NewRelocatableAttributesFetcher(visibilityManager) |
| 130 | + info, err := fetcher.Fetch(ctx, mutableState) |
| 131 | + |
| 132 | + if c.ExpectedErr != nil { |
| 133 | + require.Error(t, err) |
| 134 | + assert.ErrorIs(t, err, c.ExpectedErr) |
| 135 | + } else { |
| 136 | + require.NoError(t, err) |
| 137 | + assert.Equal(t, c.ExpectedInfo, info) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments