47
47
testName string
48
48
testHash uint32
49
49
an Any
50
- kv sync.Map
50
+ values sync.Map
51
+ ns sync.Map
51
52
}
52
53
testNamer interface {
53
54
Name () string
@@ -67,10 +68,26 @@ func newFromName(testName string) *TestVars {
67
68
}
68
69
}
69
70
70
- func getOrCreate [T any ](tv * TestVars , key string , initialValGen func (key string ) T ) T {
71
- v , _ := tv .kv .LoadOrStore (key , initialValGen (key ))
71
+ func getOrCreate [T any ](tv * TestVars , key string , initialValGen func (key string ) T , valNSetter func (val T , n int ) T ) T {
72
+ v , _ := tv .values .LoadOrStore (key , initialValGen (key ))
73
+
74
+ n , ok := tv .ns .Load (key )
75
+ if ! ok {
76
+ //revive:disable-next-line:unchecked-type-assertion
77
+ return v .(T )
78
+ }
79
+
72
80
//revive:disable-next-line:unchecked-type-assertion
73
- return v .(T )
81
+ return valNSetter (v .(T ), n .(int ))
82
+ }
83
+
84
+ func (tv * TestVars ) stringNSetter (v string , n int ) string {
85
+ return fmt .Sprintf ("%s_%d" , v , n )
86
+ }
87
+
88
+ // Use this setter for entities that don't support setting n (like uuid).
89
+ func unsupportedNSetter [T any ](v T , _ int ) T {
90
+ panic (fmt .Sprintf ("setting n on type %T is not supported" , v ))
74
91
}
75
92
76
93
func (tv * TestVars ) uniqueString (key string ) string {
@@ -83,75 +100,66 @@ func (tv *TestVars) uuidString(_ string) string {
83
100
84
101
func (tv * TestVars ) clone () * TestVars {
85
102
tv2 := newFromName (tv .testName )
86
- tv .kv .Range (func (key , value any ) bool {
87
- tv2 .kv .Store (key , value )
103
+ tv .values .Range (func (key , value any ) bool {
104
+ tv2 .values .Store (key , value )
105
+ return true
106
+ })
107
+ tv .ns .Range (func (key , value any ) bool {
108
+ tv2 .ns .Store (key , value )
88
109
return true
89
110
})
90
111
91
112
return tv2
92
113
}
93
114
func (tv * TestVars ) cloneSetVal (key string , val any ) * TestVars {
94
115
tv2 := tv .clone ()
95
- tv2 .kv .Store (key , val )
116
+ tv2 .values .Store (key , val )
96
117
return tv2
97
118
}
98
119
99
- func (tv * TestVars ) cloneSetN (key string , defaultValGen func ( key string ) string , n int ) * TestVars {
120
+ func (tv * TestVars ) cloneSetN (key string , n int ) * TestVars {
100
121
tv2 := tv .clone ()
101
-
102
- v , ok := tv .kv .Load (key )
103
- if ! ok {
104
- v = defaultValGen (key )
105
- }
106
-
107
- vStr := valString (key , v )
108
- tv2 .kv .Store (key , fmt .Sprintf ("%s_%d" , vStr , n ))
109
-
122
+ tv2 .ns .Store (key , n )
110
123
return tv2
111
124
}
112
125
113
- func valString (key string , v any ) string {
114
- vString , vIsString := v .(string )
115
- if ! vIsString {
116
- vStringer , vIsStringer := v .(fmt.Stringer )
117
- if ! vIsStringer {
118
- panic (fmt .Sprintf ("value of key %s is of type %T but must be of type %T or implement fmt.Stringer" , key , v , "" ))
119
- }
120
- vString = vStringer .String ()
121
- }
122
- return vString
123
- }
124
-
125
126
// ----------- Methods for every entity ------------
126
127
// Add more as you need them following the pattern below.
127
128
// Replace "Entity" with the name of the entity, i.e., UpdateID, ActivityType, etc.
128
- // Add only the necessary methods (in most cases only getter).
129
+ // Add only the necessary methods (in most cases only first getter).
129
130
/*
130
131
func (tv *TestVars) Entity() string {
131
- return getOrCreate(tv, "entity", tv.uniqueString)
132
+ return getOrCreate(tv, "entity", tv.uniqueString, tv.stringNSetter )
132
133
}
133
134
func (tv *TestVars) WithEntity(entity string) *TestVars {
134
135
return tv.cloneSetVal("entity", entity)
135
136
}
136
137
func (tv *TestVars) WithEntityN(n int) *TestVars {
137
- return tv.cloneSetN("entity", tv.uniqueString, n)
138
+ return tv.cloneSetN("entity", n)
138
139
}
139
140
*/
140
141
141
142
func (tv * TestVars ) NamespaceID () namespace.ID {
142
143
return getOrCreate (tv , "namespace_id" , func (key string ) namespace.ID {
143
144
return namespace .ID (tv .uuidString (key ))
144
- })
145
+ },
146
+ unsupportedNSetter ,
147
+ )
145
148
}
146
149
147
150
func (tv * TestVars ) WithNamespaceID (namespaceID namespace.ID ) * TestVars {
148
151
return tv .cloneSetVal ("namespace_id" , namespaceID )
149
152
}
150
153
151
154
func (tv * TestVars ) NamespaceName () namespace.Name {
152
- return getOrCreate (tv , "namespace_name" , func (key string ) namespace.Name {
153
- return namespace .Name (tv .uniqueString (key ))
154
- })
155
+ return getOrCreate (tv , "namespace_name" ,
156
+ func (key string ) namespace.Name {
157
+ return namespace .Name (tv .uniqueString (key ))
158
+ },
159
+ func (val namespace.Name , n int ) namespace.Name {
160
+ return namespace .Name (tv .stringNSetter (val .String (), n ))
161
+ },
162
+ )
155
163
}
156
164
157
165
func (tv * TestVars ) WithNamespaceName (namespaceName namespace.Name ) * TestVars {
@@ -177,15 +185,15 @@ func (tv *TestVars) Namespace() *namespace.Namespace {
177
185
}
178
186
179
187
func (tv * TestVars ) WorkflowID () string {
180
- return getOrCreate (tv , "workflow_id" , tv .uniqueString )
188
+ return getOrCreate (tv , "workflow_id" , tv .uniqueString , tv . stringNSetter )
181
189
}
182
190
183
191
func (tv * TestVars ) WithWorkflowIDN (n int ) * TestVars {
184
- return tv .cloneSetN ("workflow_id" , tv . uniqueString , n )
192
+ return tv .cloneSetN ("workflow_id" , n )
185
193
}
186
194
187
195
func (tv * TestVars ) RunID () string {
188
- return getOrCreate (tv , "run_id" , tv .uuidString )
196
+ return getOrCreate (tv , "run_id" , tv .uuidString , unsupportedNSetter )
189
197
}
190
198
191
199
func (tv * TestVars ) WithRunID (runID string ) * TestVars {
@@ -200,23 +208,23 @@ func (tv *TestVars) WorkflowExecution() *commonpb.WorkflowExecution {
200
208
}
201
209
202
210
func (tv * TestVars ) RequestID () string {
203
- return getOrCreate (tv , "request_id" , tv .uuidString )
211
+ return getOrCreate (tv , "request_id" , tv .uuidString , unsupportedNSetter )
204
212
}
205
213
206
214
func (tv * TestVars ) BuildID () string {
207
- return getOrCreate (tv , "build_id" , tv .uniqueString )
215
+ return getOrCreate (tv , "build_id" , tv .uniqueString , tv . stringNSetter )
208
216
}
209
217
210
218
func (tv * TestVars ) WithBuildID (buildId string ) * TestVars {
211
219
return tv .cloneSetVal ("build_id" , buildId )
212
220
}
213
221
214
222
func (tv * TestVars ) WithBuildIDN (n int ) * TestVars {
215
- return tv .cloneSetN ("build_id" , tv . uniqueString , n )
223
+ return tv .cloneSetN ("build_id" , n )
216
224
}
217
225
218
226
func (tv * TestVars ) DeploymentSeries () string {
219
- return getOrCreate (tv , "deployment_series" , tv .uniqueString )
227
+ return getOrCreate (tv , "deployment_series" , tv .uniqueString , tv . stringNSetter )
220
228
}
221
229
222
230
func (tv * TestVars ) WithDeploymentSeries (series string ) * TestVars {
@@ -232,7 +240,7 @@ func (tv *TestVars) Deployment() *deploymentpb.Deployment {
232
240
233
241
func (tv * TestVars ) TaskQueue () * taskqueuepb.TaskQueue {
234
242
return & taskqueuepb.TaskQueue {
235
- Name : getOrCreate (tv , "task_queue" , tv .uniqueString ),
243
+ Name : getOrCreate (tv , "task_queue" , tv .uniqueString , tv . stringNSetter ),
236
244
Kind : enumspb .TASK_QUEUE_KIND_NORMAL ,
237
245
}
238
246
}
@@ -242,12 +250,12 @@ func (tv *TestVars) WithTaskQueue(taskQueue string) *TestVars {
242
250
}
243
251
244
252
func (tv * TestVars ) WithTaskQueueN (n int ) * TestVars {
245
- return tv .cloneSetN ("task_queue" , tv . uniqueString , n )
253
+ return tv .cloneSetN ("task_queue" , n )
246
254
}
247
255
248
256
func (tv * TestVars ) StickyTaskQueue () * taskqueuepb.TaskQueue {
249
257
return & taskqueuepb.TaskQueue {
250
- Name : getOrCreate (tv , "sticky_task_queue" , tv .uniqueString ),
258
+ Name : getOrCreate (tv , "sticky_task_queue" , tv .uniqueString , tv . stringNSetter ),
251
259
Kind : enumspb .TASK_QUEUE_KIND_STICKY ,
252
260
NormalName : tv .TaskQueue ().Name ,
253
261
}
@@ -262,38 +270,38 @@ func (tv *TestVars) StickyExecutionAttributes(timeout time.Duration) *taskqueuep
262
270
263
271
func (tv * TestVars ) WorkflowType () * commonpb.WorkflowType {
264
272
return & commonpb.WorkflowType {
265
- Name : getOrCreate (tv , "workflow_type" , tv .uniqueString ),
273
+ Name : getOrCreate (tv , "workflow_type" , tv .uniqueString , tv . stringNSetter ),
266
274
}
267
275
}
268
276
269
277
func (tv * TestVars ) ActivityID () string {
270
- return getOrCreate (tv , "activity_id" , tv .uniqueString )
278
+ return getOrCreate (tv , "activity_id" , tv .uniqueString , tv . stringNSetter )
271
279
}
272
280
273
281
func (tv * TestVars ) WithActivityIDN (n int ) * TestVars {
274
- return tv .cloneSetN ("activity_id" , tv . uniqueString , n )
282
+ return tv .cloneSetN ("activity_id" , n )
275
283
}
276
284
277
285
func (tv * TestVars ) ActivityType () * commonpb.ActivityType {
278
286
return & commonpb.ActivityType {
279
- Name : getOrCreate (tv , "activity_type" , tv .uniqueString ),
287
+ Name : getOrCreate (tv , "activity_type" , tv .uniqueString , tv . stringNSetter ),
280
288
}
281
289
}
282
290
283
291
func (tv * TestVars ) MessageID () string {
284
- return getOrCreate (tv , "message_id" , tv .uniqueString )
292
+ return getOrCreate (tv , "message_id" , tv .uniqueString , tv . stringNSetter )
285
293
}
286
294
287
295
func (tv * TestVars ) WithMessageIDN (n int ) * TestVars {
288
- return tv .cloneSetN ("message_id" , tv . uniqueString , n )
296
+ return tv .cloneSetN ("message_id" , n )
289
297
}
290
298
291
299
func (tv * TestVars ) UpdateID () string {
292
- return getOrCreate (tv , "update_id" , tv .uniqueString )
300
+ return getOrCreate (tv , "update_id" , tv .uniqueString , tv . stringNSetter )
293
301
}
294
302
295
303
func (tv * TestVars ) WithUpdateIDN (n int ) * TestVars {
296
- return tv .cloneSetN ("update_id" , tv . uniqueString , n )
304
+ return tv .cloneSetN ("update_id" , n )
297
305
}
298
306
299
307
func (tv * TestVars ) UpdateRef () * updatepb.UpdateRef {
@@ -304,35 +312,35 @@ func (tv *TestVars) UpdateRef() *updatepb.UpdateRef {
304
312
}
305
313
306
314
func (tv * TestVars ) HandlerName () string {
307
- return getOrCreate (tv , "handler_name" , tv .uniqueString )
315
+ return getOrCreate (tv , "handler_name" , tv .uniqueString , tv . stringNSetter )
308
316
}
309
317
310
318
func (tv * TestVars ) ClientIdentity () string {
311
- return getOrCreate (tv , "client_identity" , tv .uniqueString )
319
+ return getOrCreate (tv , "client_identity" , tv .uniqueString , tv . stringNSetter )
312
320
}
313
321
314
322
func (tv * TestVars ) WorkerIdentity () string {
315
- return getOrCreate (tv , "worker_identity" , tv .uniqueString )
323
+ return getOrCreate (tv , "worker_identity" , tv .uniqueString , tv . stringNSetter )
316
324
}
317
325
318
326
func (tv * TestVars ) TimerID () string {
319
- return getOrCreate (tv , "timer_id" , tv .uniqueString )
327
+ return getOrCreate (tv , "timer_id" , tv .uniqueString , tv . stringNSetter )
320
328
}
321
329
322
330
func (tv * TestVars ) WithTimerIDN (n int ) * TestVars {
323
- return tv .cloneSetN ("timer_id" , tv . uniqueString , n )
331
+ return tv .cloneSetN ("timer_id" , n )
324
332
}
325
333
326
334
func (tv * TestVars ) QueryType () string {
327
- return getOrCreate (tv , "query_type" , tv .uniqueString )
335
+ return getOrCreate (tv , "query_type" , tv .uniqueString , tv . stringNSetter )
328
336
}
329
337
330
338
func (tv * TestVars ) SignalName () string {
331
- return getOrCreate (tv , "signal_name" , tv .uniqueString )
339
+ return getOrCreate (tv , "signal_name" , tv .uniqueString , tv . stringNSetter )
332
340
}
333
341
334
342
func (tv * TestVars ) IndexName () string {
335
- return getOrCreate (tv , "index_name" , tv .uniqueString )
343
+ return getOrCreate (tv , "index_name" , tv .uniqueString , tv . stringNSetter )
336
344
}
337
345
338
346
// ----------- Generic methods ------------
0 commit comments