pkg/wait: don't expect time.Now() to be strict increasing in WaitTime tests

This commit is contained in:
Anthony Romano 2016-08-17 11:53:34 -07:00
parent 30cf8b7f0f
commit f9d122066e
2 changed files with 16 additions and 21 deletions

View File

@ -42,16 +42,11 @@ func NewTimeList() *timeList {
func (tl *timeList) Wait(deadline time.Time) <-chan struct{} {
tl.l.Lock()
defer tl.l.Unlock()
ch := make(chan struct{}, 1)
// The given deadline SHOULD be unique but CI manages to get
// the same nano time in the unit tests.
nano := deadline.UnixNano()
for {
if tl.m[nano] == nil {
tl.m[nano] = ch
break
}
nano++
ch := tl.m[nano]
if ch == nil {
ch = make(chan struct{})
tl.m[nano] = ch
}
return ch
}

View File

@ -21,28 +21,28 @@ import (
func TestWaitTime(t *testing.T) {
wt := NewTimeList()
ch1 := wt.Wait(time.Now())
t1 := time.Now()
wt.Trigger(t1)
ch1 := wt.Wait(t1)
wt.Trigger(time.Unix(0, t1.UnixNano()+1))
select {
case <-ch1:
case <-time.After(100 * time.Millisecond):
default:
t.Fatalf("cannot receive from ch as expected")
}
ch2 := wt.Wait(time.Now())
t2 := time.Now()
wt.Trigger(t1)
select {
case <-ch2:
t.Fatalf("unexpected to receive from ch")
case <-time.After(10 * time.Millisecond):
}
ch2 := wt.Wait(t2)
wt.Trigger(t2)
select {
case <-ch2:
case <-time.After(10 * time.Millisecond):
t.Fatalf("cannot receive from ch as expected")
t.Fatalf("unexpected to receive from ch2")
default:
}
wt.Trigger(time.Unix(0, t2.UnixNano()+1))
select {
case <-ch2:
default:
t.Fatalf("cannot receive from ch2 as expected")
}
}