-
Notifications
You must be signed in to change notification settings - Fork 787
/
resource_test_utils.go
466 lines (376 loc) · 13.7 KB
/
resource_test_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package resource
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/mock"
"github.com/uber-go/tally"
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
publicservicetest "go.uber.org/cadence/.gen/go/cadence/workflowservicetest"
"go.uber.org/yarpc"
"github.com/uber/cadence/client"
"github.com/uber/cadence/client/admin"
"github.com/uber/cadence/client/frontend"
"github.com/uber/cadence/client/history"
"github.com/uber/cadence/client/matching"
"github.com/uber/cadence/common/archiver"
"github.com/uber/cadence/common/archiver/provider"
"github.com/uber/cadence/common/asyncworkflow/queue"
"github.com/uber/cadence/common/blobstore"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/domain"
"github.com/uber/cadence/common/dynamicconfig/configstore"
"github.com/uber/cadence/common/isolationgroup"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/testlogger"
"github.com/uber/cadence/common/membership"
"github.com/uber/cadence/common/messaging"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/mocks"
"github.com/uber/cadence/common/partition"
"github.com/uber/cadence/common/persistence"
persistenceClient "github.com/uber/cadence/common/persistence/client"
"github.com/uber/cadence/common/taskvalidator"
)
type (
// Test is the test implementation used for testing
Test struct {
MetricsScope tally.TestScope
ClusterMetadata cluster.Metadata
// other common resources
DomainCache *cache.MockDomainCache
DomainMetricsScopeCache cache.DomainMetricsScopeCache
DomainReplicationQueue *domain.MockReplicationQueue
TimeSource clock.TimeSource
PayloadSerializer persistence.PayloadSerializer
MetricsClient metrics.Client
ArchivalMetadata *archiver.MockArchivalMetadata
ArchiverProvider *provider.MockArchiverProvider
BlobstoreClient *blobstore.MockClient
MockPayloadSerializer *persistence.MockPayloadSerializer
// membership infos
MembershipResolver *membership.MockResolver
// internal services clients
SDKClient *publicservicetest.MockClient
FrontendClient *frontend.MockClient
MatchingClient *matching.MockClient
HistoryClient *history.MockClient
RemoteAdminClient *admin.MockClient
RemoteFrontendClient *frontend.MockClient
ClientBean *client.MockBean
// persistence clients
MetadataMgr *mocks.MetadataManager
TaskMgr *mocks.TaskManager
VisibilityMgr *mocks.VisibilityManager
ShardMgr *mocks.ShardManager
HistoryMgr *mocks.HistoryV2Manager
ExecutionMgr *mocks.ExecutionManager
PersistenceBean *persistenceClient.MockBean
IsolationGroups *isolationgroup.MockState
IsolationGroupStore *configstore.MockClient
Partitioner *partition.MockPartitioner
HostName string
Logger log.Logger
taskvalidator taskvalidator.Checker
AsyncWorkflowQueueProvider *queue.MockProvider
}
)
var _ Resource = (*Test)(nil)
const (
testHostName = "test_host"
)
var (
testHostInfo = membership.NewHostInfo(testHostName)
)
// NewTest returns a new test resource instance
func NewTest(
t *testing.T,
controller *gomock.Controller,
serviceMetricsIndex metrics.ServiceIdx,
) *Test {
logger := testlogger.New(t)
frontendClient := frontend.NewMockClient(controller)
matchingClient := matching.NewMockClient(controller)
historyClient := history.NewMockClient(controller)
remoteAdminClient := admin.NewMockClient(controller)
remoteFrontendClient := frontend.NewMockClient(controller)
clientBean := client.NewMockBean(controller)
clientBean.EXPECT().GetFrontendClient().Return(frontendClient).AnyTimes()
clientBean.EXPECT().GetMatchingClient(gomock.Any()).Return(matchingClient, nil).AnyTimes()
clientBean.EXPECT().GetHistoryClient().Return(historyClient).AnyTimes()
clientBean.EXPECT().GetRemoteAdminClient(gomock.Any()).Return(remoteAdminClient).AnyTimes()
clientBean.EXPECT().GetRemoteFrontendClient(gomock.Any()).Return(remoteFrontendClient).AnyTimes()
metadataMgr := &mocks.MetadataManager{}
taskMgr := &mocks.TaskManager{}
visibilityMgr := &mocks.VisibilityManager{}
shardMgr := &mocks.ShardManager{}
historyMgr := &mocks.HistoryV2Manager{}
executionMgr := &mocks.ExecutionManager{}
domainReplicationQueue := domain.NewMockReplicationQueue(controller)
domainReplicationQueue.EXPECT().Start().AnyTimes()
domainReplicationQueue.EXPECT().Stop().AnyTimes()
persistenceBean := persistenceClient.NewMockBean(controller)
persistenceBean.EXPECT().GetDomainManager().Return(metadataMgr).AnyTimes()
persistenceBean.EXPECT().GetTaskManager().Return(taskMgr).AnyTimes()
persistenceBean.EXPECT().GetVisibilityManager().Return(visibilityMgr).AnyTimes()
persistenceBean.EXPECT().GetHistoryManager().Return(historyMgr).AnyTimes()
persistenceBean.EXPECT().GetShardManager().Return(shardMgr).AnyTimes()
persistenceBean.EXPECT().GetExecutionManager(gomock.Any()).Return(executionMgr, nil).AnyTimes()
isolationGroupMock := isolationgroup.NewMockState(controller)
isolationGroupMock.EXPECT().Stop().AnyTimes()
partitionMock := partition.NewMockPartitioner(controller)
mockZone := "zone1"
partitionMock.EXPECT().GetIsolationGroupByDomainID(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(mockZone, nil)
scope := tally.NewTestScope("test", nil)
asyncWorkflowQueueProvider := queue.NewMockProvider(controller)
return &Test{
MetricsScope: scope,
// By default tests will run on active cluster unless overridden otherwise
ClusterMetadata: cluster.TestActiveClusterMetadata,
// other common resources
DomainCache: cache.NewMockDomainCache(controller),
DomainMetricsScopeCache: cache.NewDomainMetricsScopeCache(),
DomainReplicationQueue: domainReplicationQueue,
TimeSource: clock.NewRealTimeSource(),
PayloadSerializer: persistence.NewPayloadSerializer(),
MetricsClient: metrics.NewClient(scope, serviceMetricsIndex),
ArchivalMetadata: &archiver.MockArchivalMetadata{},
ArchiverProvider: &provider.MockArchiverProvider{},
BlobstoreClient: &blobstore.MockClient{},
MockPayloadSerializer: persistence.NewMockPayloadSerializer(controller),
// membership infos
MembershipResolver: membership.NewMockResolver(controller),
// internal services clients
SDKClient: publicservicetest.NewMockClient(controller),
FrontendClient: frontendClient,
MatchingClient: matchingClient,
HistoryClient: historyClient,
RemoteAdminClient: remoteAdminClient,
RemoteFrontendClient: remoteFrontendClient,
ClientBean: clientBean,
// persistence clients
MetadataMgr: metadataMgr,
TaskMgr: taskMgr,
VisibilityMgr: visibilityMgr,
ShardMgr: shardMgr,
HistoryMgr: historyMgr,
ExecutionMgr: executionMgr,
PersistenceBean: persistenceBean,
IsolationGroups: isolationGroupMock,
Partitioner: partitionMock,
// logger
Logger: logger,
AsyncWorkflowQueueProvider: asyncWorkflowQueueProvider,
}
}
// Start for testing
func (s *Test) Start() {
}
// Stop for testing
func (s *Test) Stop() {
}
// static infos
// GetServiceName for testing
func (s *Test) GetServiceName() string {
panic("user should implement this method for test")
}
// GetHostInfo for testing
func (s *Test) GetHostInfo() membership.HostInfo {
return testHostInfo
}
// GetClusterMetadata for testing
func (s *Test) GetClusterMetadata() cluster.Metadata {
return s.ClusterMetadata
}
// other common resources
// GetDomainCache for testing
func (s *Test) GetDomainCache() cache.DomainCache {
return s.DomainCache
}
// GetDomainMetricsScopeCache for testing
func (s *Test) GetDomainMetricsScopeCache() cache.DomainMetricsScopeCache {
return s.DomainMetricsScopeCache
}
// GetDomainReplicationQueue for testing
func (s *Test) GetDomainReplicationQueue() domain.ReplicationQueue {
// user should implement this method for test
return s.DomainReplicationQueue
}
// GetTimeSource for testing
func (s *Test) GetTimeSource() clock.TimeSource {
return s.TimeSource
}
// GetPayloadSerializer for testing
func (s *Test) GetPayloadSerializer() persistence.PayloadSerializer {
return s.PayloadSerializer
}
// GetPayloadSerializer for testing
func (s *Test) GetTaskValidator() taskvalidator.Checker {
return s.taskvalidator
}
// GetMetricsClient for testing
func (s *Test) GetMetricsClient() metrics.Client {
return s.MetricsClient
}
// GetMessagingClient for testing
func (s *Test) GetMessagingClient() messaging.Client {
panic("user should implement this method for test")
}
// GetBlobstoreClient for testing
func (s *Test) GetBlobstoreClient() blobstore.Client {
return s.BlobstoreClient
}
// GetArchivalMetadata for testing
func (s *Test) GetArchivalMetadata() archiver.ArchivalMetadata {
return s.ArchivalMetadata
}
// GetArchiverProvider for testing
func (s *Test) GetArchiverProvider() provider.ArchiverProvider {
return s.ArchiverProvider
}
// GetMembershipResolver for testing
func (s *Test) GetMembershipResolver() membership.Resolver {
return s.MembershipResolver
}
// internal services clients
// GetSDKClient for testing
func (s *Test) GetSDKClient() workflowserviceclient.Interface {
return s.SDKClient
}
// GetFrontendRawClient for testing
func (s *Test) GetFrontendRawClient() frontend.Client {
return s.FrontendClient
}
// GetFrontendClient for testing
func (s *Test) GetFrontendClient() frontend.Client {
return s.FrontendClient
}
// GetMatchingRawClient for testing
func (s *Test) GetMatchingRawClient() matching.Client {
return s.MatchingClient
}
// GetMatchingClient for testing
func (s *Test) GetMatchingClient() matching.Client {
return s.MatchingClient
}
// GetHistoryRawClient for testing
func (s *Test) GetHistoryRawClient() history.Client {
return s.HistoryClient
}
// GetHistoryClient for testing
func (s *Test) GetHistoryClient() history.Client {
return s.HistoryClient
}
// GetRemoteAdminClient for testing
func (s *Test) GetRemoteAdminClient(
cluster string,
) admin.Client {
return s.RemoteAdminClient
}
// GetRemoteFrontendClient for testing
func (s *Test) GetRemoteFrontendClient(
cluster string,
) frontend.Client {
return s.RemoteFrontendClient
}
// GetClientBean for testing
func (s *Test) GetClientBean() client.Bean {
return s.ClientBean
}
// persistence clients
// GetMetadataManager for testing
func (s *Test) GetDomainManager() persistence.DomainManager {
return s.MetadataMgr
}
// GetTaskManager for testing
func (s *Test) GetTaskManager() persistence.TaskManager {
return s.TaskMgr
}
// GetVisibilityManager for testing
func (s *Test) GetVisibilityManager() persistence.VisibilityManager {
return s.VisibilityMgr
}
// GetShardManager for testing
func (s *Test) GetShardManager() persistence.ShardManager {
return s.ShardMgr
}
// GetHistoryManager for testing
func (s *Test) GetHistoryManager() persistence.HistoryManager {
return s.HistoryMgr
}
// GetExecutionManager for testing
func (s *Test) GetExecutionManager(
shardID int,
) (persistence.ExecutionManager, error) {
return s.ExecutionMgr, nil
}
// GetPersistenceBean for testing
func (s *Test) GetPersistenceBean() persistenceClient.Bean {
return s.PersistenceBean
}
// GetHostName for testing
func (s *Test) GetHostName() string {
return s.HostName
}
// loggers
// GetLogger for testing
func (s *Test) GetLogger() log.Logger {
return s.Logger
}
// GetThrottledLogger for testing
func (s *Test) GetThrottledLogger() log.Logger {
return s.Logger
}
// GetDispatcher for testing
func (s *Test) GetDispatcher() *yarpc.Dispatcher {
panic("user should implement this method for test")
}
// GetIsolationGroupState returns the isolationGroupState for testing
func (s *Test) GetIsolationGroupState() isolationgroup.State {
return s.IsolationGroups
}
// GetPartitioner returns the partitioner
func (s *Test) GetPartitioner() partition.Partitioner {
return s.Partitioner
}
// GetIsolationGroupStore returns the config store for their
// isolation-group stores
func (s *Test) GetIsolationGroupStore() configstore.Client {
return s.IsolationGroupStore
}
func (s *Test) GetAsyncWorkflowQueueProvider() queue.Provider {
return s.AsyncWorkflowQueueProvider
}
// Finish checks whether expectations are met
func (s *Test) Finish(
t mock.TestingT,
) {
s.ArchivalMetadata.AssertExpectations(t)
s.ArchiverProvider.AssertExpectations(t)
s.MetadataMgr.AssertExpectations(t)
s.TaskMgr.AssertExpectations(t)
s.VisibilityMgr.AssertExpectations(t)
s.ShardMgr.AssertExpectations(t)
s.HistoryMgr.AssertExpectations(t)
s.ExecutionMgr.AssertExpectations(t)
}