|
| 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 scanner |
| 26 | + |
| 27 | +import ( |
| 28 | + "testing" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/golang/mock/gomock" |
| 32 | + "github.com/stretchr/testify/suite" |
| 33 | + |
| 34 | + "go.temporal.io/server/api/historyservicemock/v1" |
| 35 | + "go.temporal.io/server/common/config" |
| 36 | + "go.temporal.io/server/common/log" |
| 37 | + "go.temporal.io/server/common/metrics" |
| 38 | + p "go.temporal.io/server/common/persistence" |
| 39 | + "go.temporal.io/server/common/sdk" |
| 40 | + "go.temporal.io/server/common/testing/mocksdk" |
| 41 | +) |
| 42 | + |
| 43 | +type scannerTestSuite struct { |
| 44 | + suite.Suite |
| 45 | +} |
| 46 | + |
| 47 | +func TestScanner(t *testing.T) { |
| 48 | + suite.Run(t, new(scannerTestSuite)) |
| 49 | +} |
| 50 | + |
| 51 | +func (s *scannerTestSuite) TestScannerEnabled() { |
| 52 | + defer func(originalDelay time.Duration) { |
| 53 | + scannerStartUpDelay = originalDelay |
| 54 | + }(scannerStartUpDelay) |
| 55 | + scannerStartUpDelay = 0 |
| 56 | + |
| 57 | + type expectedScanner struct { |
| 58 | + WFTypeName string |
| 59 | + TaskQueueName string |
| 60 | + } |
| 61 | + executionScanner := expectedScanner{ |
| 62 | + WFTypeName: executionsScannerWFTypeName, |
| 63 | + TaskQueueName: executionsScannerTaskQueueName, |
| 64 | + } |
| 65 | + _ = executionScanner |
| 66 | + taskQueueScanner := expectedScanner{ |
| 67 | + WFTypeName: tqScannerWFTypeName, |
| 68 | + TaskQueueName: tqScannerTaskQueueName, |
| 69 | + } |
| 70 | + historyScanner := expectedScanner{ |
| 71 | + WFTypeName: historyScannerWFTypeName, |
| 72 | + TaskQueueName: historyScannerTaskQueueName, |
| 73 | + } |
| 74 | + |
| 75 | + type testCase struct { |
| 76 | + Name string |
| 77 | + ExecutionsScannerEnabled bool |
| 78 | + TaskQueueScannerEnabled bool |
| 79 | + HistoryScannerEnabled bool |
| 80 | + DefaultStore string |
| 81 | + ExpectedScanners []expectedScanner |
| 82 | + } |
| 83 | + |
| 84 | + for _, c := range []testCase{ |
| 85 | + { |
| 86 | + Name: "NothingEnabledNoSQL", |
| 87 | + ExecutionsScannerEnabled: false, |
| 88 | + TaskQueueScannerEnabled: false, |
| 89 | + HistoryScannerEnabled: false, |
| 90 | + DefaultStore: config.StoreTypeNoSQL, |
| 91 | + ExpectedScanners: []expectedScanner{}, |
| 92 | + }, |
| 93 | + { |
| 94 | + Name: "NothingEnabledSQL", |
| 95 | + ExecutionsScannerEnabled: false, |
| 96 | + TaskQueueScannerEnabled: false, |
| 97 | + HistoryScannerEnabled: false, |
| 98 | + DefaultStore: config.StoreTypeSQL, |
| 99 | + ExpectedScanners: []expectedScanner{}, |
| 100 | + }, |
| 101 | + { |
| 102 | + Name: "HistoryScannerNoSQL", |
| 103 | + ExecutionsScannerEnabled: false, |
| 104 | + TaskQueueScannerEnabled: false, |
| 105 | + HistoryScannerEnabled: true, |
| 106 | + DefaultStore: config.StoreTypeNoSQL, |
| 107 | + ExpectedScanners: []expectedScanner{historyScanner}, |
| 108 | + }, |
| 109 | + { |
| 110 | + Name: "HistoryScannerSQL", |
| 111 | + ExecutionsScannerEnabled: false, |
| 112 | + TaskQueueScannerEnabled: false, |
| 113 | + HistoryScannerEnabled: true, |
| 114 | + DefaultStore: config.StoreTypeSQL, |
| 115 | + ExpectedScanners: []expectedScanner{historyScanner}, |
| 116 | + }, |
| 117 | + { |
| 118 | + Name: "TaskQueueScannerNoSQL", |
| 119 | + ExecutionsScannerEnabled: false, |
| 120 | + TaskQueueScannerEnabled: true, |
| 121 | + HistoryScannerEnabled: false, |
| 122 | + DefaultStore: config.StoreTypeNoSQL, |
| 123 | + ExpectedScanners: []expectedScanner{}, // TODO: enable task queue scanner for NoSQL? |
| 124 | + }, |
| 125 | + { |
| 126 | + Name: "TaskQueueScannerSQL", |
| 127 | + ExecutionsScannerEnabled: false, |
| 128 | + TaskQueueScannerEnabled: true, |
| 129 | + HistoryScannerEnabled: false, |
| 130 | + DefaultStore: config.StoreTypeSQL, |
| 131 | + ExpectedScanners: []expectedScanner{taskQueueScanner}, |
| 132 | + }, |
| 133 | + { |
| 134 | + Name: "ExecutionsScannerNoSQL", |
| 135 | + ExecutionsScannerEnabled: true, |
| 136 | + TaskQueueScannerEnabled: false, |
| 137 | + HistoryScannerEnabled: false, |
| 138 | + DefaultStore: config.StoreTypeNoSQL, |
| 139 | + ExpectedScanners: []expectedScanner{executionScanner}, |
| 140 | + }, |
| 141 | + { |
| 142 | + Name: "ExecutionsScannerSQL", |
| 143 | + ExecutionsScannerEnabled: true, |
| 144 | + TaskQueueScannerEnabled: false, |
| 145 | + HistoryScannerEnabled: false, |
| 146 | + DefaultStore: config.StoreTypeSQL, |
| 147 | + ExpectedScanners: []expectedScanner{executionScanner}, |
| 148 | + }, |
| 149 | + } { |
| 150 | + s.Run(c.Name, func() { |
| 151 | + ctrl := gomock.NewController(s.T()) |
| 152 | + mockWorkerFactory := sdk.NewMockWorkerFactory(ctrl) |
| 153 | + mockSdkClient := mocksdk.NewMockClient(ctrl) |
| 154 | + scanner := New( |
| 155 | + log.NewNoopLogger(), |
| 156 | + &Config{ |
| 157 | + MaxConcurrentActivityExecutionSize: func() int { |
| 158 | + return 1 |
| 159 | + }, |
| 160 | + MaxConcurrentWorkflowTaskExecutionSize: func() int { |
| 161 | + return 1 |
| 162 | + }, |
| 163 | + MaxConcurrentActivityTaskPollers: func() int { |
| 164 | + return 1 |
| 165 | + }, |
| 166 | + MaxConcurrentWorkflowTaskPollers: func() int { |
| 167 | + return 1 |
| 168 | + }, |
| 169 | + ExecutionsScannerEnabled: func() bool { |
| 170 | + return c.ExecutionsScannerEnabled |
| 171 | + }, |
| 172 | + HistoryScannerEnabled: func() bool { |
| 173 | + return c.HistoryScannerEnabled |
| 174 | + }, |
| 175 | + TaskQueueScannerEnabled: func() bool { |
| 176 | + return c.TaskQueueScannerEnabled |
| 177 | + }, |
| 178 | + Persistence: &config.Persistence{ |
| 179 | + DefaultStore: c.DefaultStore, |
| 180 | + DataStores: map[string]config.DataStore{ |
| 181 | + config.StoreTypeNoSQL: {}, |
| 182 | + config.StoreTypeSQL: { |
| 183 | + SQL: &config.SQL{}, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + mockSdkClient, |
| 189 | + metrics.NoopClient, |
| 190 | + p.NewMockExecutionManager(ctrl), |
| 191 | + p.NewMockTaskManager(ctrl), |
| 192 | + historyservicemock.NewMockHistoryServiceClient(ctrl), |
| 193 | + mockWorkerFactory, |
| 194 | + ) |
| 195 | + for _, sc := range c.ExpectedScanners { |
| 196 | + worker := mocksdk.NewMockWorker(ctrl) |
| 197 | + worker.EXPECT().RegisterActivityWithOptions(gomock.Any(), gomock.Any()).AnyTimes() |
| 198 | + worker.EXPECT().RegisterWorkflowWithOptions(gomock.Any(), gomock.Any()).AnyTimes() |
| 199 | + worker.EXPECT().Start() |
| 200 | + mockWorkerFactory.EXPECT().New(gomock.Any(), sc.TaskQueueName, gomock.Any()).Return(worker) |
| 201 | + mockSdkClient.EXPECT().ExecuteWorkflow(gomock.Any(), gomock.Any(), sc.WFTypeName, gomock.Any()) |
| 202 | + } |
| 203 | + err := scanner.Start() |
| 204 | + s.NoError(err) |
| 205 | + scanner.Stop() |
| 206 | + }) |
| 207 | + } |
| 208 | +} |
0 commit comments