...
1 package gleak
2
3 import (
4 "sort"
5
6 "github.com/onsi/gomega/format"
7 "github.com/onsi/gomega/types"
8 )
9
10
11
12
13
14
15 func IgnoringGoroutines(goroutines []Goroutine) types.GomegaMatcher {
16 m := &ignoringGoroutinesMatcher{
17 ignoreGoids: map[uint64]struct{}{},
18 }
19 for _, g := range goroutines {
20 m.ignoreGoids[g.ID] = struct{}{}
21 }
22 return m
23 }
24
25 type ignoringGoroutinesMatcher struct {
26 ignoreGoids map[uint64]struct{}
27 }
28
29
30
31 func (matcher *ignoringGoroutinesMatcher) Match(actual interface{}) (success bool, err error) {
32 g, err := G(actual, "IgnoringGoroutines")
33 if err != nil {
34 return false, err
35 }
36 _, ok := matcher.ignoreGoids[g.ID]
37 return ok, nil
38 }
39
40
41
42 func (matcher *ignoringGoroutinesMatcher) FailureMessage(actual interface{}) (message string) {
43 return format.Message(actual, "to be contained in the list of expected goroutine IDs", matcher.expectedGoids())
44 }
45
46
47
48 func (matcher *ignoringGoroutinesMatcher) NegatedFailureMessage(actual interface{}) (message string) {
49 return format.Message(actual, "not to be contained in the list of expected goroutine IDs", matcher.expectedGoids())
50 }
51
52
53 func (matcher *ignoringGoroutinesMatcher) expectedGoids() []uint64 {
54 ids := make([]uint64, 0, len(matcher.ignoreGoids))
55 for id := range matcher.ignoreGoids {
56 ids = append(ids, id)
57 }
58 sort.Sort(Uint64Slice(ids))
59 return ids
60 }
61
View as plain text