From c50baa67cb8b557d17842cb333f4affef8908ca7 Mon Sep 17 00:00:00 2001 From: xenolf Date: Fri, 11 Mar 2016 03:52:46 +0100 Subject: [PATCH] Move WaitFor into new utils.go and switch timeout and interval to time.Duration. --- acme/dns_challenge.go | 24 ------------------------ acme/dns_challenge_test.go | 20 -------------------- acme/utils.go | 29 +++++++++++++++++++++++++++++ acme/utils_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 44 deletions(-) create mode 100644 acme/utils.go create mode 100644 acme/utils_test.go diff --git a/acme/dns_challenge.go b/acme/dns_challenge.go index 659d7082..b7be186f 100644 --- a/acme/dns_challenge.go +++ b/acme/dns_challenge.go @@ -8,7 +8,6 @@ import ( "log" "net" "strings" - "time" "github.com/miekg/dns" "golang.org/x/net/publicsuffix" @@ -256,26 +255,3 @@ func UnFqdn(name string) string { } return name } - -// WaitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds. -func WaitFor(timeout, interval int, f func() (bool, error)) error { - var lastErr string - timeup := time.After(time.Duration(timeout) * time.Second) - for { - select { - case <-timeup: - return fmt.Errorf("Time limit exceeded. Last error: %s", lastErr) - default: - } - - stop, err := f() - if stop { - return nil - } - if err != nil { - lastErr = err.Error() - } - - time.Sleep(time.Duration(interval) * time.Second) - } -} diff --git a/acme/dns_challenge_test.go b/acme/dns_challenge_test.go index 760c7991..bfc66561 100644 --- a/acme/dns_challenge_test.go +++ b/acme/dns_challenge_test.go @@ -163,23 +163,3 @@ func TestCheckAuthoritativeNssErr(t *testing.T) { } } } - -func TestWaitForTimeout(t *testing.T) { - c := make(chan error) - go func() { - err := WaitFor(3, 1, func() (bool, error) { - return false, nil - }) - c <- err - }() - - timeout := time.After(4 * time.Second) - select { - case <-timeout: - t.Fatal("timeout exceeded") - case err := <-c: - if err == nil { - t.Errorf("expected timeout error; got %v", err) - } - } -} diff --git a/acme/utils.go b/acme/utils.go new file mode 100644 index 00000000..937a8f2d --- /dev/null +++ b/acme/utils.go @@ -0,0 +1,29 @@ +package acme + +import ( + "fmt" + "time" +) + +// WaitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds. +func WaitFor(timeout, interval time.Duration, f func() (bool, error)) error { + var lastErr string + timeup := time.After(timeout * time.Second) + for { + select { + case <-timeup: + return fmt.Errorf("Time limit exceeded. Last error: %s", lastErr) + default: + } + + stop, err := f() + if stop { + return nil + } + if err != nil { + lastErr = err.Error() + } + + time.Sleep(interval * time.Second) + } +} diff --git a/acme/utils_test.go b/acme/utils_test.go new file mode 100644 index 00000000..cb837cd5 --- /dev/null +++ b/acme/utils_test.go @@ -0,0 +1,26 @@ +package acme + +import ( + "testing" + "time" +) + +func TestWaitForTimeout(t *testing.T) { + c := make(chan error) + go func() { + err := WaitFor(3, 1, func() (bool, error) { + return false, nil + }) + c <- err + }() + + timeout := time.After(4 * time.Second) + select { + case <-timeout: + t.Fatal("timeout exceeded") + case err := <-c: + if err == nil { + t.Errorf("expected timeout error; got %v", err) + } + } +}