Atlas › Test

TestTruncate

Exact test identity: go.temporal.io/server/common/failure/TestTruncate

Package
go.temporal.io/server/common/failure
Suite / test hierarchy
TestTruncate
Test
TestTruncate
Introduced at
failure.go ×1 Frontier kind: Joint frontier
Covered ranges
14
Covered lines
35
Covered files
2

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/failure/failure.go 27 covered LOC · 8 ranges

Open complete file

46 }
47
48 > func Truncate(f *failurepb.Failure, maxSize int) *failurepb.Failure { failure.go
49 > return TruncateWithDepth(f, maxSize, 20)
50 > }
51
52 > func TruncateWithDepth(f *failurepb.Failure, maxSize, maxDepth int) *failurepb.Failure { failure.go
53 > if f == nil {
54 return nil
55 }
56
57 // note that bytes are given to earlier calls first, so call in order of importance
58 > trunc := func(s string) string { failure.go
59 > s = util.TruncateUTF8(s, maxSize)
60 > maxSize -= len(s)
61 > if s != "" {
62 > maxSize -= 4 // account for proto overhead
63 > }
64 > return s
65 }
66
67 > newFailure := &failurepb.Failure{} failure.go
68 >
69 > // Keep failure info for ApplicationFailureInfo and for ServerFailureInfo to persist NonRetryable flag.
70 > if i := f.GetApplicationFailureInfo(); i != nil {
71 newFailure.FailureInfo = &failurepb.Failure_ApplicationFailureInfo{ApplicationFailureInfo: &failurepb.ApplicationFailureInfo{
72 NonRetryable: i.NonRetryable,
74 }}
75 maxSize -= 8 // account for proto overhead
76 > } else if i := f.GetServerFailureInfo(); i != nil { failure.go
77 > newFailure.FailureInfo = &failurepb.Failure_ServerFailureInfo{ServerFailureInfo: &failurepb.ServerFailureInfo{ failure.go
78 > NonRetryable: i.NonRetryable,
79 > }}
80 > maxSize -= 4 // account for proto overhead
81 > }
82
83 > newFailure.Source = trunc(f.Source) failure.go
84 > newFailure.Message = trunc(f.Message)
85 > newFailure.StackTrace = trunc(f.StackTrace)
86 > if f.Cause != nil && maxSize > 4 && maxDepth > 0 {
87 newFailure.Cause = TruncateWithDepth(f.Cause, maxSize-4, maxDepth-1)
88 }
89
90 > return newFailure failure.go
91 }
go.temporal.io/server/common/util/strings.go 8 covered LOC · 6 ranges

Open complete file

7 // Note that truncation pays attention to codepoints only! This may truncate in the middle of a
8 // grapheme cluster.
9 > func TruncateUTF8(s string, n int) string { strings.go
10 > if len(s) <= n {
11 > return s strings.go
12 > } else if n <= 0 { strings.go
13 > return "" strings.go
14 > }
15 > for n > 0 && !utf8.RuneStart(s[n]) { strings.go
16 n--
17 }
18 > return s[:n] strings.go
19 }