-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathstack_propagation_test.go
More file actions
79 lines (63 loc) · 1.83 KB
/
stack_propagation_test.go
File metadata and controls
79 lines (63 loc) · 1.83 KB
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
package terrors_test
import (
_ "embed"
"encoding/json"
"errors"
pe "github.com/monzo/terrors/proto"
"github.com/stretchr/testify/assert"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/monzo/terrors"
)
// there are two main cases here.
//
// a) we have an error in a child goroutine and handle it in a parent (eg: from a waitgroup)
// b) we have an error in a different process (eg: via RPC) and it gets propagated to the child
func TestStackTraceCrossGoRoutinePropagation(t *testing.T) {
err := errorCanaryParentA()
var terr *terrors.Error
require.True(t, errors.As(err, &terr))
stack := terr.StackString()
t.Logf("Stack: %s", stack)
assert.Contains(t, stack, "errorCanaryChild")
assert.Contains(t, stack, "errorCanaryParentA")
}
func TestStackTraceCrossProcessPropagation(t *testing.T) {
err := rpcCaller()
var terr *terrors.Error
require.True(t, errors.As(err, &terr))
stack := terr.StackString()
t.Logf("Stack: %s", stack)
assert.Contains(t, stack, "rpcCaller")
assert.Contains(t, stack, "rpcCallee")
assert.Contains(t, stack, "rpcHandler")
}
func errorCanaryParentA() error {
// Simulate a handler that uses a wait-group or similar
err := errorCanaryChild()
return terrors.Augment(err, "a", nil)
}
func errorCanaryChild() error {
var err error
// This is relatively common, and shows that wait-groups can often result in stack traces that aren't entirely helpful
g := sync.WaitGroup{}
g.Add(1)
go func() {
err = terrors.InternalService("test", "test", nil)
g.Done()
}()
g.Wait()
return err
}
//go:embed stack_propagation_sample.json
var rpcError []byte
func rpcCaller() error {
var representation *pe.Error
jsonErr := json.Unmarshal(rpcError, &representation)
if jsonErr != nil {
panic(jsonErr)
}
err := terrors.Unmarshal(representation)
return terrors.Augment(err, "calling callee", nil)
}