...

Source file src/github.com/onsi/gomega/gleak/ignoring_goroutines.go

Documentation: github.com/onsi/gomega/gleak

     1  package gleak
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/onsi/gomega/format"
     7  	"github.com/onsi/gomega/types"
     8  )
     9  
    10  // IgnoringGoroutines succeeds if an actual goroutine, identified by its ID, is
    11  // in a slice of expected goroutines. A typical use of the IgnoringGoroutines
    12  // matcher is to take a snapshot of the current goroutines just right before a
    13  // test and then at the end of a test filtering out these "good" and known
    14  // goroutines.
    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  // Match succeeds if actual is a Goroutine and its ID is in the set of
    30  // goroutine IDs to expect and thus to ignore in leak checks.
    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  // FailureMessage returns a failure message if the actual goroutine isn't in the
    41  // set of goroutines to be ignored.
    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  // NegatedFailureMessage returns a negated failure message if the actual
    47  // goroutine actually is in the set of goroutines to be ignored.
    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  // expectedGoids returns the sorted list of expected goroutine IDs.
    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