summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/timer_test.go
diff options
context:
space:
mode:
authorTamir Duberstein <tamird@google.com>2021-05-27 05:04:04 -0700
committergVisor bot <gvisor-bot@google.com>2021-05-27 05:06:04 -0700
commit2c1df1f445df67b7481e6ee3deee0007a576fefe (patch)
treebc4b4668610a3bb547f95446f74f01e1f6bdd092 /pkg/tcpip/timer_test.go
parent93d98b874b0687f53a508f3733c3fa8d687091cb (diff)
Use fake clocks in all tests
...except TCP tests and NDP tests that mutate globals. These will be undertaken later. Updates #5940. PiperOrigin-RevId: 376145608
Diffstat (limited to 'pkg/tcpip/timer_test.go')
-rw-r--r--pkg/tcpip/timer_test.go53
1 files changed, 31 insertions, 22 deletions
diff --git a/pkg/tcpip/timer_test.go b/pkg/tcpip/timer_test.go
index 8c43dd627..ed1ed8ac6 100644
--- a/pkg/tcpip/timer_test.go
+++ b/pkg/tcpip/timer_test.go
@@ -129,10 +129,14 @@ func TestJobReschedule(t *testing.T) {
wg.Wait()
}
+func stdClockWithAfter() (tcpip.Clock, func(time.Duration) <-chan time.Time) {
+ return tcpip.NewStdClock(), time.After
+}
+
func TestJobExecution(t *testing.T) {
t.Parallel()
- clock := tcpip.NewStdClock()
+ clock, after := stdClockWithAfter()
var lock sync.Mutex
ch := make(chan struct{})
@@ -144,7 +148,7 @@ func TestJobExecution(t *testing.T) {
// Wait for timer to fire.
select {
case <-ch:
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
t.Fatal("timed out waiting for timer to fire")
}
@@ -152,14 +156,14 @@ func TestJobExecution(t *testing.T) {
select {
case <-ch:
t.Fatal("no other timers should have fired")
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
}
}
func TestCancellableTimerResetFromLongDuration(t *testing.T) {
t.Parallel()
- clock := tcpip.NewStdClock()
+ clock, after := stdClockWithAfter()
var lock sync.Mutex
ch := make(chan struct{})
@@ -175,7 +179,7 @@ func TestCancellableTimerResetFromLongDuration(t *testing.T) {
// Wait for timer to fire.
select {
case <-ch:
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
t.Fatal("timed out waiting for timer to fire")
}
@@ -183,14 +187,14 @@ func TestCancellableTimerResetFromLongDuration(t *testing.T) {
select {
case <-ch:
t.Fatal("no other timers should have fired")
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
}
}
func TestJobRescheduleFromShortDuration(t *testing.T) {
t.Parallel()
- clock := tcpip.NewStdClock()
+ clock, after := stdClockWithAfter()
var lock sync.Mutex
ch := make(chan struct{})
@@ -204,7 +208,7 @@ func TestJobRescheduleFromShortDuration(t *testing.T) {
select {
case <-ch:
t.Fatal("timer fired after being stopped")
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
}
job.Schedule(shortDuration)
@@ -212,7 +216,7 @@ func TestJobRescheduleFromShortDuration(t *testing.T) {
// Wait for timer to fire.
select {
case <-ch:
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
t.Fatal("timed out waiting for timer to fire")
}
@@ -220,14 +224,14 @@ func TestJobRescheduleFromShortDuration(t *testing.T) {
select {
case <-ch:
t.Fatal("no other timers should have fired")
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
}
}
func TestJobImmediatelyCancel(t *testing.T) {
t.Parallel()
- clock := tcpip.NewStdClock()
+ clock, after := stdClockWithAfter()
var lock sync.Mutex
ch := make(chan struct{})
@@ -243,14 +247,19 @@ func TestJobImmediatelyCancel(t *testing.T) {
select {
case <-ch:
t.Fatal("timer fired after being stopped")
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
}
}
+func stdClockWithAfterAndSleep() (tcpip.Clock, func(time.Duration) <-chan time.Time, func(time.Duration)) {
+ clock, after := stdClockWithAfter()
+ return clock, after, time.Sleep
+}
+
func TestJobCancelledRescheduleWithoutLock(t *testing.T) {
t.Parallel()
- clock := tcpip.NewStdClock()
+ clock, after, sleep := stdClockWithAfterAndSleep()
var lock sync.Mutex
ch := make(chan struct{})
@@ -265,7 +274,7 @@ func TestJobCancelledRescheduleWithoutLock(t *testing.T) {
lock.Lock()
// Sleep until the timer fires and gets blocked trying to take the lock.
- time.Sleep(middleDuration * 2)
+ sleep(middleDuration * 2)
job.Cancel()
lock.Unlock()
}
@@ -275,14 +284,14 @@ func TestJobCancelledRescheduleWithoutLock(t *testing.T) {
select {
case <-ch:
t.Fatal("timer fired after being stopped")
- case <-time.After(middleDuration * 2):
+ case <-after(middleDuration * 2):
}
}
func TestManyCancellableTimerResetAfterBlockedOnLock(t *testing.T) {
t.Parallel()
- clock := tcpip.NewStdClock()
+ clock, after, sleep := stdClockWithAfterAndSleep()
var lock sync.Mutex
ch := make(chan struct{})
@@ -291,7 +300,7 @@ func TestManyCancellableTimerResetAfterBlockedOnLock(t *testing.T) {
job.Schedule(shortDuration)
for i := 0; i < 10; i++ {
// Sleep until the timer fires and gets blocked trying to take the lock.
- time.Sleep(middleDuration)
+ sleep(middleDuration)
job.Cancel()
job.Schedule(shortDuration)
}
@@ -300,7 +309,7 @@ func TestManyCancellableTimerResetAfterBlockedOnLock(t *testing.T) {
// Wait for double the duration for the last timer to fire.
select {
case <-ch:
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
t.Fatal("timed out waiting for timer to fire")
}
@@ -308,14 +317,14 @@ func TestManyCancellableTimerResetAfterBlockedOnLock(t *testing.T) {
select {
case <-ch:
t.Fatal("no other timers should have fired")
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
}
}
func TestManyJobReschedulesUnderLock(t *testing.T) {
t.Parallel()
- clock := tcpip.NewStdClock()
+ clock, after := stdClockWithAfter()
var lock sync.Mutex
ch := make(chan struct{})
@@ -331,7 +340,7 @@ func TestManyJobReschedulesUnderLock(t *testing.T) {
// Wait for double the duration for the last timer to fire.
select {
case <-ch:
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
t.Fatal("timed out waiting for timer to fire")
}
@@ -339,6 +348,6 @@ func TestManyJobReschedulesUnderLock(t *testing.T) {
select {
case <-ch:
t.Fatal("no other timers should have fired")
- case <-time.After(middleDuration):
+ case <-after(middleDuration):
}
}