github.com/onsi/gomega@v1.32.0/gleak/util.go (about) 1 package gleak 2 3 import ( 4 "fmt" 5 "sort" 6 "strconv" 7 "strings" 8 9 "github.com/onsi/gomega/format" 10 ) 11 12 // G takes an actual "any" untyped value and returns it as a typed Goroutine, if 13 // possible. It returns an error if actual isn't of either type Goroutine or a 14 // pointer to it. G is intended to be mainly used by goroutine-related Gomega 15 // matchers, such as IgnoringTopFunction, et cetera. 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 // goids returns a (sorted) list of Goroutine IDs in textual format. 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 // Uint64Slice implements the sort.Interface for a []uint64 to sort in 48 // increasing order. 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] }