forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourceTest.go
448 lines (362 loc) · 13.6 KB
/
resourceTest.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
// 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 (
"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"
"github.com/uber/cadence/.gen/go/admin/adminservicetest"
"github.com/uber/cadence/.gen/go/cadence/workflowservicetest"
"github.com/uber/cadence/.gen/go/history/historyservicetest"
"github.com/uber/cadence/.gen/go/matching/matchingservicetest"
"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"
"github.com/uber/cadence/common/archiver"
"github.com/uber/cadence/common/archiver/provider"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/loggerimpl"
"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/persistence"
persistenceClient "github.com/uber/cadence/common/persistence/client"
"go.uber.org/yarpc"
"go.uber.org/zap"
)
type (
// Test is the test implementation used for testing
Test struct {
MetricsScope tally.Scope
ClusterMetadata *cluster.MockMetadata
// other common resources
DomainCache *cache.MockDomainCache
TimeSource clock.TimeSource
PayloadSerializer persistence.PayloadSerializer
MetricsClient metrics.Client
ArchivalMetadata *archiver.MockArchivalMetadata
ArchiverProvider *provider.MockArchiverProvider
// membership infos
MembershipMonitor *membership.MockMonitor
FrontendServiceResolver *membership.MockServiceResolver
MatchingServiceResolver *membership.MockServiceResolver
HistoryServiceResolver *membership.MockServiceResolver
WorkerServiceResolver *membership.MockServiceResolver
// internal services clients
SDKClient *publicservicetest.MockClient
FrontendClient *workflowservicetest.MockClient
MatchingClient *matchingservicetest.MockClient
HistoryClient *historyservicetest.MockClient
RemoteAdminClient *adminservicetest.MockClient
RemoteFrontendClient *workflowservicetest.MockClient
ClientBean *client.MockBean
// persistence clients
MetadataMgr *mocks.MetadataManager
TaskMgr *mocks.TaskManager
VisibilityMgr *mocks.VisibilityManager
DomainReplicationQueue persistence.DomainReplicationQueue
ShardMgr *mocks.ShardManager
HistoryMgr *mocks.HistoryV2Manager
ExecutionMgr *mocks.ExecutionManager
PersistenceBean *persistenceClient.MockBean
Logger log.Logger
}
)
var _ Resource = (*Test)(nil)
const (
testHostName = "test_host"
)
var (
testHostInfo = membership.NewHostInfo(testHostName, nil)
)
// NewTest returns a new test resource instance
func NewTest(
controller *gomock.Controller,
serviceMetricsIndex metrics.ServiceIdx,
) *Test {
zapLogger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
logger := loggerimpl.NewLogger(zapLogger)
frontendClient := workflowservicetest.NewMockClient(controller)
matchingClient := matchingservicetest.NewMockClient(controller)
historyClient := historyservicetest.NewMockClient(controller)
remoteFrontendClient := workflowservicetest.NewMockClient(controller)
remoteAdminClient := adminservicetest.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 := persistence.NewMockDomainReplicationQueue(controller)
domainReplicationQueue.EXPECT().Start().AnyTimes()
domainReplicationQueue.EXPECT().Stop().AnyTimes()
persistenceBean := persistenceClient.NewMockBean(controller)
persistenceBean.EXPECT().GetMetadataManager().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()
persistenceBean.EXPECT().GetDomainReplicationQueue().Return(domainReplicationQueue).AnyTimes()
membershipMonitor := membership.NewMockMonitor(controller)
frontendServiceResolver := membership.NewMockServiceResolver(controller)
matchingServiceResolver := membership.NewMockServiceResolver(controller)
historyServiceResolver := membership.NewMockServiceResolver(controller)
workerServiceResolver := membership.NewMockServiceResolver(controller)
membershipMonitor.EXPECT().GetResolver(common.FrontendServiceName).Return(frontendServiceResolver, nil).AnyTimes()
membershipMonitor.EXPECT().GetResolver(common.MatchingServiceName).Return(matchingServiceResolver, nil).AnyTimes()
membershipMonitor.EXPECT().GetResolver(common.HistoryServiceName).Return(historyServiceResolver, nil).AnyTimes()
membershipMonitor.EXPECT().GetResolver(common.WorkerServiceName).Return(workerServiceResolver, nil).AnyTimes()
scope := tally.NewTestScope("test", nil)
return &Test{
MetricsScope: scope,
ClusterMetadata: cluster.NewMockMetadata(controller),
// other common resources
DomainCache: cache.NewMockDomainCache(controller),
TimeSource: clock.NewRealTimeSource(),
PayloadSerializer: persistence.NewPayloadSerializer(),
MetricsClient: metrics.NewClient(scope, serviceMetricsIndex),
ArchivalMetadata: &archiver.MockArchivalMetadata{},
ArchiverProvider: &provider.MockArchiverProvider{},
// membership infos
MembershipMonitor: membershipMonitor,
FrontendServiceResolver: frontendServiceResolver,
MatchingServiceResolver: matchingServiceResolver,
HistoryServiceResolver: historyServiceResolver,
WorkerServiceResolver: workerServiceResolver,
// 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,
DomainReplicationQueue: domainReplicationQueue,
ShardMgr: shardMgr,
HistoryMgr: historyMgr,
ExecutionMgr: executionMgr,
PersistenceBean: persistenceBean,
// logger
Logger: logger,
}
}
// 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")
}
// GetHostName for testing
func (s *Test) GetHostName() string {
return testHostInfo.Identity()
}
// 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
}
// GetTimeSource for testing
func (s *Test) GetTimeSource() clock.TimeSource {
return s.TimeSource
}
// GetPayloadSerializer for testing
func (s *Test) GetPayloadSerializer() persistence.PayloadSerializer {
return s.PayloadSerializer
}
// 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")
}
// GetArchivalMetadata for testing
func (s *Test) GetArchivalMetadata() archiver.ArchivalMetadata {
return s.ArchivalMetadata
}
// GetArchiverProvider for testing
func (s *Test) GetArchiverProvider() provider.ArchiverProvider {
return s.ArchiverProvider
}
// membership infos
// GetMembershipMonitor for testing
func (s *Test) GetMembershipMonitor() membership.Monitor {
return s.MembershipMonitor
}
// GetFrontendServiceResolver for testing
func (s *Test) GetFrontendServiceResolver() membership.ServiceResolver {
return s.FrontendServiceResolver
}
// GetMatchingServiceResolver for testing
func (s *Test) GetMatchingServiceResolver() membership.ServiceResolver {
return s.MatchingServiceResolver
}
// GetHistoryServiceResolver for testing
func (s *Test) GetHistoryServiceResolver() membership.ServiceResolver {
return s.HistoryServiceResolver
}
// GetWorkerServiceResolver for testing
func (s *Test) GetWorkerServiceResolver() membership.ServiceResolver {
return s.WorkerServiceResolver
}
// 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) GetMetadataManager() persistence.MetadataManager {
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
}
// GetDomainReplicationQueue for testing
func (s *Test) GetDomainReplicationQueue() persistence.DomainReplicationQueue {
// user should implement this method for test
return s.DomainReplicationQueue
}
// 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
}
// 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")
}
// 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)
}