From 95c94d1d044e6e77c8fe1218c79ae6388811aac4 Mon Sep 17 00:00:00 2001 From: Harry Pidcock Date: Mon, 8 Aug 2022 11:38:08 +1000 Subject: [PATCH] Remove panic and test advancing after timer fires but not sent. --- testclock/dilated.go | 2 +- testclock/dilated_test.go | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/testclock/dilated.go b/testclock/dilated.go index 930829a..4333b11 100644 --- a/testclock/dilated.go +++ b/testclock/dilated.go @@ -162,7 +162,7 @@ func (t *dilatedWallTimer) run() { t.offset = newOffset stopped := t.timer.Stop() if !stopped { - panic("stopped timer but still running") + continue } realNow := time.Now() dialatedNow := dilateTime(t.dc.epoch, realNow, t.dc.realSecondDuration, t.offset) diff --git a/testclock/dilated_test.go b/testclock/dilated_test.go index 399991e..3d41472 100644 --- a/testclock/dilated_test.go +++ b/testclock/dilated_test.go @@ -168,7 +168,7 @@ func (*dilatedClockSuite) TestReset(c *gc.C) { select { case <-a.Chan(): case <-time.After(shortWait): - c.Fatal("clock did not fire") + c.Fatal("timer did not fire") } for i := 0; i < 3; i++ { if runtime.NumGoroutine() == numGo { @@ -192,7 +192,27 @@ func (*dilatedClockSuite) TestStopReset(c *gc.C) { select { case <-a.Chan(): case <-time.After(shortWait): - c.Fatal("clock did not fire") + c.Fatal("timer did not fire") + } + for i := 0; i < 3; i++ { + if runtime.NumGoroutine() == numGo { + break + } + time.Sleep(shortWait) + } + c.Assert(runtime.NumGoroutine(), gc.Equals, numGo, gc.Commentf("clock goroutine still running")) +} + +func (*dilatedClockSuite) TestAdvanceAlreadyFired(c *gc.C) { + numGo := runtime.NumGoroutine() + cl := testclock.NewDilatedWallClock(time.Second) + t := cl.NewTimer(time.Millisecond) + time.Sleep(shortWait) + cl.Advance(time.Second) + select { + case <-t.Chan(): + case <-time.After(shortWait): + c.Fatal("timer did not fire") } for i := 0; i < 3; i++ { if runtime.NumGoroutine() == numGo {