testutils.go 421 B

1234567891011121314151617181920212223
  1. package testutils
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. // Timeout calls f and waits for 100ms for it to complete.
  7. // If it doesn't, it causes the tests to fail.
  8. // t must be a valid testing context.
  9. func Timeout(t *testing.T, f func()) {
  10. onTimeout := time.After(100 * time.Millisecond)
  11. onDone := make(chan bool)
  12. go func() {
  13. f()
  14. close(onDone)
  15. }()
  16. select {
  17. case <-onTimeout:
  18. t.Fatalf("timeout")
  19. case <-onDone:
  20. }
  21. }