-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook_test.go
More file actions
55 lines (48 loc) · 1.42 KB
/
Copy pathhook_test.go
File metadata and controls
55 lines (48 loc) · 1.42 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
package drops_test
import (
"context"
"reflect"
"testing"
"github.com/bernardoforcillo/drops"
)
func TestCallHookNilIsNoop(t *testing.T) {
// Must not panic.
drops.CallHook(nil, context.Background(), drops.QueryEvent{})
}
func TestCallHookRecoversFromPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("a panicking hook leaked: %v", r)
}
}()
panicHook := func(context.Context, drops.QueryEvent) {
panic("user code is buggy")
}
drops.CallHook(panicHook, context.Background(), drops.QueryEvent{})
}
func TestChainHooksContinuesAfterPanic(t *testing.T) {
var ran []string
a := func(context.Context, drops.QueryEvent) { ran = append(ran, "a") }
b := func(context.Context, drops.QueryEvent) { panic("boom") }
c := func(context.Context, drops.QueryEvent) { ran = append(ran, "c") }
chained := drops.ChainHooks(a, b, c)
defer func() {
if r := recover(); r != nil {
t.Fatalf("chain leaked panic: %v", r)
}
}()
chained(context.Background(), drops.QueryEvent{Kind: "test"})
if len(ran) != 2 || ran[0] != "a" || ran[1] != "c" {
t.Errorf("expected [a c], got %v", ran)
}
}
func TestCallHookForwardsEventVerbatim(t *testing.T) {
want := drops.QueryEvent{Kind: "exec", SQL: "SELECT 1"}
var got drops.QueryEvent
drops.CallHook(func(_ context.Context, e drops.QueryEvent) {
got = e
}, context.Background(), want)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}