wait.go 447 B

12345678910111213141516171819202122232425262728
  1. package debugging
  2. import (
  3. "time"
  4. )
  5. // PossibleBlocking runs the function f in a goroutine and returns the result.
  6. // If the function f is blocking, the test will fail.
  7. func PossibleBlocking[T any](f func() T, timeout time.Duration, trigger func()) T {
  8. d := make(chan T)
  9. go func() {
  10. d <- f()
  11. }()
  12. timer := time.NewTimer(timeout)
  13. defer timer.Stop()
  14. for {
  15. select {
  16. case <-timer.C:
  17. trigger()
  18. case v := <-d:
  19. return v
  20. }
  21. }
  22. }