github.com/network-quality/goresponsiveness@v0.0.0-20240129151524-343954285090/timeoutat/timeoutat_test.go (about)

     1  package timeoutat
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/network-quality/goresponsiveness/debug"
     9  )
    10  
    11  func TestTimeoutAt(t *testing.T) {
    12  	testTime := 5 * time.Second
    13  	testTimeLimit := 6 * time.Second
    14  
    15  	now := time.Now()
    16  	select {
    17  	case <-TimeoutAt(context.Background(), time.Now().Add(testTime), debug.NoDebug):
    18  
    19  	}
    20  	then := time.Now()
    21  
    22  	actualTime := then.Sub(now)
    23  
    24  	if actualTime >= testTimeLimit {
    25  		t.Fatalf("Should have taken 5 seconds but it really took %v!", actualTime)
    26  	}
    27  }