...
1 package gleak
2
3 import (
4 "fmt"
5 "sort"
6 "strconv"
7 "strings"
8
9 "github.com/onsi/gomega/format"
10 )
11
12
13
14
15
16 func G(actual interface{}, matchername string) (Goroutine, error) {
17 if actual != nil {
18 switch actual := actual.(type) {
19 case Goroutine:
20 return actual, nil
21 case *Goroutine:
22 return *actual, nil
23 }
24 }
25 return Goroutine{},
26 fmt.Errorf("%s matcher expects a Goroutine or *Goroutine. Got:\n%s",
27 matchername, format.Object(actual, 1))
28 }
29
30
31 func goids(gs []Goroutine) string {
32 ids := make([]uint64, len(gs))
33 for idx, g := range gs {
34 ids[idx] = g.ID
35 }
36 sort.Sort(Uint64Slice(ids))
37 var buff strings.Builder
38 for idx, id := range ids {
39 if idx > 0 {
40 buff.WriteString(", ")
41 }
42 buff.WriteString(strconv.FormatInt(int64(id), 10))
43 }
44 return buff.String()
45 }
46
47
48
49 type Uint64Slice []uint64
50
51 func (s Uint64Slice) Len() int { return len(s) }
52 func (s Uint64Slice) Less(a, b int) bool { return s[a] < s[b] }
53 func (s Uint64Slice) Swap(a, b int) { s[a], s[b] = s[b], s[a] }
54
View as plain text