Move Until, Forever, NeverStop, ForeverTestTimeout from util to wait

This commit is contained in:
Jan Chaloupka
2016-02-02 10:28:44 +01:00
parent 59a05682dc
commit 43a47a8234
5 changed files with 80 additions and 82 deletions

View File

@@ -23,10 +23,39 @@ import (
"sync/atomic"
"testing"
"time"
"k8s.io/kubernetes/pkg/util"
)
func TestUntil(t *testing.T) {
ch := make(chan struct{})
close(ch)
Until(func() {
t.Fatal("should not have been invoked")
}, 0, ch)
ch = make(chan struct{})
called := make(chan struct{})
go func() {
Until(func() {
called <- struct{}{}
}, 0, ch)
close(called)
}()
<-called
close(ch)
<-called
}
func TestUntilReturnsImmediately(t *testing.T) {
now := time.Now()
ch := make(chan struct{})
Until(func() {
close(ch)
}, 30*time.Second, ch)
if now.Add(25 * time.Second).Before(time.Now()) {
t.Errorf("Until did not return immediately when the stop chan was closed inside the func")
}
}
func TestExponentialBackoff(t *testing.T) {
opts := Backoff{Factor: 1.0, Steps: 3}
@@ -87,7 +116,7 @@ DRAIN:
break DRAIN
}
count++
case <-time.After(util.ForeverTestTimeout):
case <-time.After(ForeverTestTimeout):
t.Errorf("unexpected timeout after poll")
}
}
@@ -233,7 +262,7 @@ func TestPollForever(t *testing.T) {
if !open {
t.Fatalf("did not expect channel to be closed")
}
case <-time.After(util.ForeverTestTimeout):
case <-time.After(ForeverTestTimeout):
t.Fatalf("channel did not return at least once within the poll interval")
}
}
@@ -313,14 +342,14 @@ func TestWaitFor(t *testing.T) {
func TestWaitForWithDelay(t *testing.T) {
done := make(chan struct{})
defer close(done)
WaitFor(poller(time.Millisecond, util.ForeverTestTimeout), func() (bool, error) {
WaitFor(poller(time.Millisecond, ForeverTestTimeout), func() (bool, error) {
time.Sleep(10 * time.Millisecond)
return true, nil
}, done)
// If polling goroutine doesn't see the done signal it will leak timers.
select {
case done <- struct{}{}:
case <-time.After(util.ForeverTestTimeout):
case <-time.After(ForeverTestTimeout):
t.Errorf("expected an ack of the done signal.")
}
}