...
1
2
3
4
5
6
7
8
9
10 package assert
11
12 import (
13 "context"
14 "fmt"
15 "reflect"
16 "time"
17 "unsafe"
18 )
19
20
21
22 func DifferentAddressRanges(t TestingT, a, b []byte) (ok bool) {
23 if h, ok := t.(tHelper); ok {
24 h.Helper()
25 }
26
27 if len(a) == 0 || len(b) == 0 {
28 return true
29 }
30
31
32
33 sliceAddrRange := func(b []byte) (uintptr, uintptr) {
34 sh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
35 return sh.Data, sh.Data + uintptr(sh.Cap-1)
36 }
37 aStart, aEnd := sliceAddrRange(a)
38 bStart, bEnd := sliceAddrRange(b)
39
40
41
42 if bStart > aEnd || aStart > bEnd {
43 return true
44 }
45
46
47
48 min := func(a, b uintptr) uintptr {
49 if a < b {
50 return a
51 }
52 return b
53 }
54 max := func(a, b uintptr) uintptr {
55 if a > b {
56 return a
57 }
58 return b
59 }
60 overlapLow := max(aStart, bStart)
61 overlapHigh := min(aEnd, bEnd)
62
63 t.Errorf("Byte slices point to the same underlying byte array:\n"+
64 "\ta addresses:\t%d ... %d\n"+
65 "\tb addresses:\t%d ... %d\n"+
66 "\toverlap:\t%d ... %d",
67 aStart, aEnd,
68 bStart, bEnd,
69 overlapLow, overlapHigh)
70
71 return false
72 }
73
74
75
76
77
78 func EqualBSON(t TestingT, expected, actual interface{}) bool {
79 if h, ok := t.(tHelper); ok {
80 h.Helper()
81 }
82
83 return Equal(t,
84 expected,
85 actual,
86 `expected and actual BSON values do not match
87 As Extended JSON:
88 Expected: %s
89 Actual : %s`,
90 expected.(fmt.Stringer).String(),
91 actual.(fmt.Stringer).String())
92 }
93
94
95
96
97
98
99
100 func Soon(t TestingT, callback func(ctx context.Context), timeout time.Duration) {
101 if h, ok := t.(tHelper); ok {
102 h.Helper()
103 }
104
105
106 callbackCtx, cancel := context.WithCancel(context.Background())
107 defer cancel()
108
109 done := make(chan struct{})
110 fullCallback := func() {
111 callback(callbackCtx)
112 done <- struct{}{}
113 }
114
115 timer := time.NewTimer(timeout)
116 defer timer.Stop()
117
118 go fullCallback()
119
120 select {
121 case <-done:
122 return
123 case <-timer.C:
124 t.Errorf("timed out in %s waiting for callback", timeout)
125 }
126 }
127
View as plain text