Improve readability of test failures (#386)

This commit is contained in:
David Zhao
2022-01-28 21:34:21 -08:00
committed by GitHub
parent 5b57522c05
commit 6af6eefb8c
9 changed files with 198 additions and 147 deletions
+6 -8
View File
@@ -4,26 +4,24 @@ import (
"context"
"testing"
"time"
"github.com/livekit/protocol/logger"
)
var (
ConnectTimeout = 30 * time.Second
)
func WithTimeout(t *testing.T, description string, f func() bool) bool {
logger.Infow(description)
func WithTimeout(t *testing.T, f func() string) {
ctx, cancel := context.WithTimeout(context.Background(), ConnectTimeout)
defer cancel()
lastErr := ""
for {
select {
case <-ctx.Done():
t.Fatal("timed out: " + description)
return false
t.Fatal("timed out: " + lastErr)
case <-time.After(10 * time.Millisecond):
if f() {
return true
lastErr = f()
if lastErr == "" {
return
}
}
}