mirror of
https://github.com/go-acme/lego
synced 2026-03-14 22:45:48 +01:00
chore: wait.For stop with error (#2665)
This commit is contained in:
parent
a3f3c620e9
commit
213d7b8fa3
2 changed files with 43 additions and 2 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue