• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uber / cadence / 019008aa-36ee-48dc-947b-b3bb36a4eda0

11 Jun 2024 06:58PM UTC coverage: 71.411% (-0.008%) from 71.419%
019008aa-36ee-48dc-947b-b3bb36a4eda0

push

buildkite

web-flow
Unit tests for part of applyEvents (#6120)

* part of test for applyEvents

* more tests to cover all applyEvents function

* refactor to delete redundant code

* add assertions in a function affordance func

106469 of 149093 relevant lines covered (71.41%)

2575.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.21
/common/persistence/statsComputer.go
1
// Copyright (c) 2017-2020 Uber Technologies, Inc.
2
// Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a copy
5
// of this software and associated documentation files (the "Software"), to deal
6
// in the Software without restriction, including without limitation the rights
7
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
// copies of the Software, and to permit persons to whom the Software is
9
// furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
// THE SOFTWARE.
21

22
package persistence
23

24
type (
25
        // statsComputer is to computing struct sizes after serialization
26
        statsComputer struct{}
27
)
28

29
func (sc *statsComputer) computeMutableStateStats(req *InternalGetWorkflowExecutionResponse) *MutableStateStats {
739✔
30
        executionInfoSize := computeExecutionInfoSize(req.State.ExecutionInfo)
739✔
31

739✔
32
        activityInfoCount := 0
739✔
33
        activityInfoSize := 0
739✔
34
        for _, ai := range req.State.ActivityInfos {
857✔
35
                activityInfoCount++
118✔
36
                activityInfoSize += computeActivityInfoSize(ai)
118✔
37
        }
118✔
38

39
        timerInfoCount := 0
739✔
40
        timerInfoSize := 0
739✔
41
        for _, ti := range req.State.TimerInfos {
740✔
42
                timerInfoCount++
1✔
43
                timerInfoSize += computeTimerInfoSize(ti)
1✔
44
        }
1✔
45

46
        childExecutionInfoCount := 0
739✔
47
        childExecutionInfoSize := 0
739✔
48
        for _, ci := range req.State.ChildExecutionInfos {
742✔
49
                childExecutionInfoCount++
3✔
50
                childExecutionInfoSize += computeChildInfoSize(ci)
3✔
51
        }
3✔
52

53
        signalInfoCount := 0
739✔
54
        signalInfoSize := 0
739✔
55
        for _, si := range req.State.SignalInfos {
739✔
56
                signalInfoCount++
×
57
                signalInfoSize += computeSignalInfoSize(si)
×
58
        }
×
59

60
        bufferedEventsCount := 0
739✔
61
        bufferedEventsSize := 0
739✔
62

739✔
63
        for _, be := range req.State.BufferedEvents {
742✔
64
                bufferedEventsCount++
3✔
65
                bufferedEventsSize += len(be.Data)
3✔
66
        }
3✔
67

68
        requestCancelInfoCount := len(req.State.RequestCancelInfos)
739✔
69

739✔
70
        totalSize := executionInfoSize
739✔
71
        totalSize += activityInfoSize
739✔
72
        totalSize += timerInfoSize
739✔
73
        totalSize += childExecutionInfoSize
739✔
74
        totalSize += signalInfoSize
739✔
75
        totalSize += bufferedEventsSize
739✔
76

739✔
77
        return &MutableStateStats{
739✔
78
                MutableStateSize:       totalSize,
739✔
79
                ExecutionInfoSize:      executionInfoSize,
739✔
80
                ActivityInfoSize:       activityInfoSize,
739✔
81
                TimerInfoSize:          timerInfoSize,
739✔
82
                ChildInfoSize:          childExecutionInfoSize,
739✔
83
                SignalInfoSize:         signalInfoSize,
739✔
84
                BufferedEventsSize:     bufferedEventsSize,
739✔
85
                ActivityInfoCount:      activityInfoCount,
739✔
86
                TimerInfoCount:         timerInfoCount,
739✔
87
                ChildInfoCount:         childExecutionInfoCount,
739✔
88
                SignalInfoCount:        signalInfoCount,
739✔
89
                BufferedEventsCount:    bufferedEventsCount,
739✔
90
                RequestCancelInfoCount: requestCancelInfoCount,
739✔
91
        }
739✔
92
}
93

94
func (sc *statsComputer) computeMutableStateUpdateStats(req *InternalUpdateWorkflowExecutionRequest) *MutableStateUpdateSessionStats {
4,466✔
95
        if req.NewWorkflowSnapshot != nil {
4,641✔
96
                return mergeMutableStateUpdateSessionStats(sc.computeWorkflowMutationStats(&req.UpdateWorkflowMutation), sc.computeWorkflowSnapshotStats(req.NewWorkflowSnapshot))
175✔
97
        }
175✔
98
        return sc.computeWorkflowMutationStats(&req.UpdateWorkflowMutation)
4,294✔
99
}
100

101
func (sc *statsComputer) computeMutableStateCreateStats(req *InternalCreateWorkflowExecutionRequest) *MutableStateUpdateSessionStats {
528✔
102
        return sc.computeWorkflowSnapshotStats(&req.NewWorkflowSnapshot)
528✔
103
}
528✔
104

105
func (sc *statsComputer) computeMutableStateConflictResolveStats(req *InternalConflictResolveWorkflowExecutionRequest) *MutableStateUpdateSessionStats {
5✔
106
        mss := sc.computeWorkflowSnapshotStats(&req.ResetWorkflowSnapshot)
5✔
107
        if req.NewWorkflowSnapshot != nil {
5✔
108
                mss = mergeMutableStateUpdateSessionStats(mss, sc.computeWorkflowSnapshotStats(req.NewWorkflowSnapshot))
×
109
        }
×
110
        if req.CurrentWorkflowMutation != nil {
6✔
111
                mss = mergeMutableStateUpdateSessionStats(mss, sc.computeWorkflowMutationStats(req.CurrentWorkflowMutation))
1✔
112
        }
1✔
113
        return mss
5✔
114
}
115

116
func (sc *statsComputer) computeWorkflowMutationStats(req *InternalWorkflowMutation) *MutableStateUpdateSessionStats {
4,468✔
117
        executionInfoSize := computeExecutionInfoSize(req.ExecutionInfo)
4,468✔
118

4,468✔
119
        activityInfoCount := 0
4,468✔
120
        activityInfoSize := 0
4,468✔
121
        for _, ai := range req.UpsertActivityInfos {
5,748✔
122
                activityInfoCount++
1,280✔
123
                activityInfoSize += computeActivityInfoSize(ai)
1,280✔
124
        }
1,280✔
125

126
        timerInfoCount := 0
4,468✔
127
        timerInfoSize := 0
4,468✔
128
        for _, ti := range req.UpsertTimerInfos {
4,506✔
129
                timerInfoCount++
38✔
130
                timerInfoSize += computeTimerInfoSize(ti)
38✔
131
        }
38✔
132

133
        childExecutionInfoCount := 0
4,468✔
134
        childExecutionInfoSize := 0
4,468✔
135
        for _, ci := range req.UpsertChildExecutionInfos {
4,513✔
136
                childExecutionInfoCount++
45✔
137
                childExecutionInfoSize += computeChildInfoSize(ci)
45✔
138
        }
45✔
139

140
        signalInfoCount := 0
4,468✔
141
        signalInfoSize := 0
4,468✔
142
        for _, si := range req.UpsertSignalInfos {
4,484✔
143
                signalInfoCount++
16✔
144
                signalInfoSize += computeSignalInfoSize(si)
16✔
145
        }
16✔
146

147
        bufferedEventsSize := 0
4,468✔
148
        if req.NewBufferedEvents != nil {
5,111✔
149
                bufferedEventsSize = len(req.NewBufferedEvents.Data)
643✔
150
        }
643✔
151

152
        requestCancelInfoCount := len(req.UpsertRequestCancelInfos)
4,468✔
153

4,468✔
154
        deleteActivityInfoCount := len(req.DeleteActivityInfos)
4,468✔
155

4,468✔
156
        deleteTimerInfoCount := len(req.DeleteTimerInfos)
4,468✔
157

4,468✔
158
        deleteChildInfoCount := len(req.DeleteChildExecutionInfos)
4,468✔
159

4,468✔
160
        deleteSignalInfoCount := len(req.DeleteSignalInfos)
4,468✔
161

4,468✔
162
        deleteRequestCancelInfoCount := len(req.DeleteRequestCancelInfos)
4,468✔
163

4,468✔
164
        transferTasksCount := len(req.TransferTasks)
4,468✔
165
        crossClusterTasksCount := len(req.CrossClusterTasks)
4,468✔
166
        timerTasksCount := len(req.TimerTasks)
4,468✔
167
        replicationTasksCount := len(req.ReplicationTasks)
4,468✔
168

4,468✔
169
        totalSize := executionInfoSize
4,468✔
170
        totalSize += activityInfoSize
4,468✔
171
        totalSize += timerInfoSize
4,468✔
172
        totalSize += childExecutionInfoSize
4,468✔
173
        totalSize += signalInfoSize
4,468✔
174
        totalSize += bufferedEventsSize
4,468✔
175

4,468✔
176
        return &MutableStateUpdateSessionStats{
4,468✔
177
                MutableStateSize:             totalSize,
4,468✔
178
                ExecutionInfoSize:            executionInfoSize,
4,468✔
179
                ActivityInfoSize:             activityInfoSize,
4,468✔
180
                TimerInfoSize:                timerInfoSize,
4,468✔
181
                ChildInfoSize:                childExecutionInfoSize,
4,468✔
182
                SignalInfoSize:               signalInfoSize,
4,468✔
183
                BufferedEventsSize:           bufferedEventsSize,
4,468✔
184
                ActivityInfoCount:            activityInfoCount,
4,468✔
185
                TimerInfoCount:               timerInfoCount,
4,468✔
186
                ChildInfoCount:               childExecutionInfoCount,
4,468✔
187
                SignalInfoCount:              signalInfoCount,
4,468✔
188
                RequestCancelInfoCount:       requestCancelInfoCount,
4,468✔
189
                DeleteActivityInfoCount:      deleteActivityInfoCount,
4,468✔
190
                DeleteTimerInfoCount:         deleteTimerInfoCount,
4,468✔
191
                DeleteChildInfoCount:         deleteChildInfoCount,
4,468✔
192
                DeleteSignalInfoCount:        deleteSignalInfoCount,
4,468✔
193
                DeleteRequestCancelInfoCount: deleteRequestCancelInfoCount,
4,468✔
194
                TransferTasksCount:           transferTasksCount,
4,468✔
195
                CrossClusterTaskCount:        crossClusterTasksCount,
4,468✔
196
                TimerTasksCount:              timerTasksCount,
4,468✔
197
                ReplicationTasksCount:        replicationTasksCount,
4,468✔
198
        }
4,468✔
199
}
200

201
func (sc *statsComputer) computeWorkflowSnapshotStats(req *InternalWorkflowSnapshot) *MutableStateUpdateSessionStats {
703✔
202
        executionInfoSize := computeExecutionInfoSize(req.ExecutionInfo)
703✔
203

703✔
204
        activityInfoCount := 0
703✔
205
        activityInfoSize := 0
703✔
206
        for _, ai := range req.ActivityInfos {
730✔
207
                activityInfoCount++
27✔
208
                activityInfoSize += computeActivityInfoSize(ai)
27✔
209
        }
27✔
210

211
        timerInfoCount := 0
703✔
212
        timerInfoSize := 0
703✔
213
        for _, ti := range req.TimerInfos {
714✔
214
                timerInfoCount++
11✔
215
                timerInfoSize += computeTimerInfoSize(ti)
11✔
216
        }
11✔
217

218
        childExecutionInfoCount := 0
703✔
219
        childExecutionInfoSize := 0
703✔
220
        for _, ci := range req.ChildExecutionInfos {
711✔
221
                childExecutionInfoCount++
8✔
222
                childExecutionInfoSize += computeChildInfoSize(ci)
8✔
223
        }
8✔
224

225
        signalInfoCount := 0
703✔
226
        signalInfoSize := 0
703✔
227
        for _, si := range req.SignalInfos {
704✔
228
                signalInfoCount++
1✔
229
                signalInfoSize += computeSignalInfoSize(si)
1✔
230
        }
1✔
231

232
        requestCancelInfoCount := len(req.RequestCancelInfos)
703✔
233

703✔
234
        transferTasksCount := len(req.TransferTasks)
703✔
235
        crossClusterTasksCount := len(req.CrossClusterTasks)
703✔
236
        timerTasksCount := len(req.TimerTasks)
703✔
237
        replicationTasksCount := len(req.ReplicationTasks)
703✔
238

703✔
239
        totalSize := executionInfoSize
703✔
240
        totalSize += activityInfoSize
703✔
241
        totalSize += timerInfoSize
703✔
242
        totalSize += childExecutionInfoSize
703✔
243
        totalSize += signalInfoSize
703✔
244

703✔
245
        return &MutableStateUpdateSessionStats{
703✔
246
                MutableStateSize:       totalSize,
703✔
247
                ExecutionInfoSize:      executionInfoSize,
703✔
248
                ActivityInfoSize:       activityInfoSize,
703✔
249
                TimerInfoSize:          timerInfoSize,
703✔
250
                ChildInfoSize:          childExecutionInfoSize,
703✔
251
                SignalInfoSize:         signalInfoSize,
703✔
252
                ActivityInfoCount:      activityInfoCount,
703✔
253
                TimerInfoCount:         timerInfoCount,
703✔
254
                ChildInfoCount:         childExecutionInfoCount,
703✔
255
                SignalInfoCount:        signalInfoCount,
703✔
256
                RequestCancelInfoCount: requestCancelInfoCount,
703✔
257
                TransferTasksCount:     transferTasksCount,
703✔
258
                CrossClusterTaskCount:  crossClusterTasksCount,
703✔
259
                TimerTasksCount:        timerTasksCount,
703✔
260
                ReplicationTasksCount:  replicationTasksCount,
703✔
261
        }
703✔
262
}
263

264
func mergeMutableStateUpdateSessionStats(stats ...*MutableStateUpdateSessionStats) *MutableStateUpdateSessionStats {
176✔
265
        result := &MutableStateUpdateSessionStats{}
176✔
266
        for _, s := range stats {
525✔
267
                result.MutableStateSize += s.MutableStateSize
349✔
268

349✔
269
                result.ExecutionInfoSize += s.ExecutionInfoSize
349✔
270
                result.ActivityInfoSize += s.ActivityInfoSize
349✔
271
                result.TimerInfoSize += s.TimerInfoSize
349✔
272
                result.ChildInfoSize += s.ChildInfoSize
349✔
273
                result.SignalInfoSize += s.SignalInfoSize
349✔
274
                result.BufferedEventsSize += s.BufferedEventsSize
349✔
275

349✔
276
                result.ActivityInfoCount += s.ActivityInfoCount
349✔
277
                result.TimerInfoCount += s.TimerInfoCount
349✔
278
                result.ChildInfoCount += s.ChildInfoCount
349✔
279
                result.SignalInfoCount += s.SignalInfoCount
349✔
280
                result.RequestCancelInfoCount += s.RequestCancelInfoCount
349✔
281

349✔
282
                result.DeleteActivityInfoCount += s.DeleteActivityInfoCount
349✔
283
                result.DeleteTimerInfoCount += s.DeleteTimerInfoCount
349✔
284
                result.DeleteChildInfoCount += s.DeleteChildInfoCount
349✔
285
                result.DeleteSignalInfoCount += s.DeleteSignalInfoCount
349✔
286
                result.DeleteRequestCancelInfoCount += s.DeleteRequestCancelInfoCount
349✔
287

349✔
288
                result.TransferTasksCount += s.TransferTasksCount
349✔
289
                result.CrossClusterTaskCount += s.CrossClusterTaskCount
349✔
290
                result.TimerInfoCount += s.TimerInfoCount
349✔
291
                result.ReplicationTasksCount += s.ReplicationTasksCount
349✔
292
        }
349✔
293
        return result
176✔
294
}
295

296
func computeExecutionInfoSize(executionInfo *InternalWorkflowExecutionInfo) int {
5,906✔
297
        size := len(executionInfo.WorkflowID)
5,906✔
298
        size += len(executionInfo.TaskList)
5,906✔
299
        size += len(executionInfo.WorkflowTypeName)
5,906✔
300
        size += len(executionInfo.ParentWorkflowID)
5,906✔
301

5,906✔
302
        return size
5,906✔
303
}
5,906✔
304

305
func computeActivityInfoSize(ai *InternalActivityInfo) int {
1,421✔
306
        size := len(ai.ActivityID)
1,421✔
307
        if ai.ScheduledEvent != nil {
1,429✔
308
                size += len(ai.ScheduledEvent.Data)
8✔
309
        }
8✔
310
        if ai.StartedEvent != nil {
1,429✔
311
                size += len(ai.StartedEvent.Data)
8✔
312
        }
8✔
313
        size += len(ai.Details)
1,421✔
314

1,421✔
315
        return size
1,421✔
316
}
317

318
func computeTimerInfoSize(ti *TimerInfo) int {
52✔
319
        size := len(ti.TimerID)
52✔
320

52✔
321
        return size
52✔
322
}
52✔
323

324
func computeChildInfoSize(ci *InternalChildExecutionInfo) int {
58✔
325
        size := 0
58✔
326
        if ci.InitiatedEvent != nil {
64✔
327
                size += len(ci.InitiatedEvent.Data)
6✔
328
        }
6✔
329
        if ci.StartedEvent != nil {
64✔
330
                size += len(ci.StartedEvent.Data)
6✔
331
        }
6✔
332
        return size
58✔
333
}
334

335
func computeSignalInfoSize(si *SignalInfo) int {
19✔
336
        size := len(si.SignalName)
19✔
337
        size += len(si.Input)
19✔
338
        size += len(si.Control)
19✔
339

19✔
340
        return size
19✔
341
}
19✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc