Browse Source

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

Anthony Romano 9 years ago
parent
commit
f9d122066e
2 changed files with 14 additions and 19 deletions
  1. 4 9
      pkg/wait/wait_time.go
  2. 10 10
      pkg/wait/wait_time_test.go

+ 4 - 9
pkg/wait/wait_time.go

@@ -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
 }

+ 10 - 10
pkg/wait/wait_time_test.go

@@ -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)
+	ch2 := wt.Wait(t2)
+	wt.Trigger(t2)
 	select {
 	case <-ch2:
-		t.Fatalf("unexpected to receive from ch")
-	case <-time.After(10 * time.Millisecond):
+		t.Fatalf("unexpected to receive from ch2")
+	default:
 	}
-	wt.Trigger(t2)
+	wt.Trigger(time.Unix(0, t2.UnixNano()+1))
 	select {
 	case <-ch2:
-	case <-time.After(10 * time.Millisecond):
-		t.Fatalf("cannot receive from ch as expected")
+	default:
+		t.Fatalf("cannot receive from ch2 as expected")
 	}
 }