chore: wait.For stop with error (#2665)

This commit is contained in:
Ludovic Fernandez 2025-10-13 12:15:13 +02:00 committed by GitHub
commit 213d7b8fa3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 2 deletions

View file

@ -25,8 +25,9 @@ func For(msg string, timeout, interval time.Duration, f func() (bool, error)) er
stop, err := f()
if stop {
return nil
return err
}
if err != nil {
lastErr = err
}

View file

@ -1,11 +1,12 @@
package wait
import (
"errors"
"testing"
"time"
)
func TestForTimeout(t *testing.T) {
func TestFor_timeout(t *testing.T) {
c := make(chan error)
go func() {
c <- For("", 3*time.Second, 1*time.Second, func() (bool, error) {
@ -24,3 +25,42 @@ func TestForTimeout(t *testing.T) {
t.Logf("%v", err)
}
}
func TestFor_stop(t *testing.T) {
c := make(chan error)
go func() {
c <- For("", 3*time.Second, 1*time.Second, func() (bool, error) {
return true, nil
})
}()
timeout := time.After(6 * time.Second)
select {
case <-timeout:
t.Fatal("timeout exceeded")
case err := <-c:
if err != nil {
t.Errorf("expected no timeout error; got %v", err)
}
}
}
func TestFor_stop_error(t *testing.T) {
c := make(chan error)
go func() {
c <- For("", 3*time.Second, 1*time.Second, func() (bool, error) {
return true, errors.New("oops")
})
}()
timeout := time.After(6 * time.Second)
select {
case <-timeout:
t.Fatal("timeout exceeded")
case err := <-c:
if err == nil {
t.Errorf("expected error; got %v", err)
}
t.Logf("%v", err)
}
}