github.com/git-amp/amp-sdk-go@v0.7.5/stdlib/testutils/expectations.go (about)

     1  package testutils
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  type Awaiter chan struct{}
     9  
    10  func NewAwaiter() Awaiter { return make(Awaiter, 10) }
    11  
    12  func (a Awaiter) ItHappened() { a <- struct{}{} }
    13  
    14  func (a Awaiter) AwaitOrFail(t testing.TB, params ...interface{}) {
    15  	t.Helper()
    16  
    17  	duration := 10 * time.Second
    18  	msg := ""
    19  	for _, p := range params {
    20  		switch p := p.(type) {
    21  		case time.Duration:
    22  			duration = p
    23  		case string:
    24  			msg = p
    25  		}
    26  	}
    27  
    28  	select {
    29  	case <-a:
    30  	case <-time.After(duration):
    31  		t.Fatalf("Timed out waiting for Awaiter to get ItHappened: %v", msg)
    32  	}
    33  }
    34  
    35  func (a Awaiter) NeverHappenedOrFail(t testing.TB, params ...interface{}) {
    36  	t.Helper()
    37  
    38  	duration := 10 * time.Second
    39  	msg := ""
    40  	for _, p := range params {
    41  		switch p := p.(type) {
    42  		case time.Duration:
    43  			duration = p
    44  		case string:
    45  			msg = p
    46  		}
    47  	}
    48  
    49  	select {
    50  	case <-a:
    51  		t.Fatalf("should not happen: %v", msg)
    52  	case <-time.After(duration):
    53  	}
    54  }