func (r *pingSuite) TestPingTimeout(c *gc.C) { triggered := make(chan struct{}) action := func() { close(triggered) } clock := jujutesting.NewClock(time.Now()) timeout := apiserver.NewPingTimeout(action, clock, 50*time.Millisecond) for i := 0; i < 2; i++ { waitAlarm(c, clock) clock.Advance(10 * time.Millisecond) timeout.Ping() } waitAlarm(c, clock) clock.Advance(49 * time.Millisecond) select { case <-triggered: c.Fatalf("action triggered early") case <-time.After(testing.ShortWait): } clock.Advance(time.Millisecond) select { case <-triggered: case <-time.After(testing.LongWait): c.Fatalf("action never triggered") } }
func (r *rootSuite) TestPingTimeoutStopped(c *gc.C) { closedc := make(chan time.Time, 1) action := func() { closedc <- time.Now() } timeout := apiserver.NewPingTimeout(action, 20*time.Millisecond) timeout.Ping() timeout.Stop() // The action should never trigger select { case <-closedc: c.Fatalf("action triggered after Stop()") case <-time.After(testing.ShortWait): } }
func (r *pingSuite) TestPingTimeoutStopped(c *gc.C) { triggered := make(chan struct{}) action := func() { close(triggered) } clock := jujutesting.NewClock(time.Now()) timeout := apiserver.NewPingTimeout(action, clock, 20*time.Millisecond) waitAlarm(c, clock) timeout.Stop() clock.Advance(time.Hour) // The action should never trigger select { case <-triggered: c.Fatalf("action triggered after Stop()") case <-time.After(testing.ShortWait): } }
func (r *rootSuite) TestPingTimeout(c *gc.C) { closedc := make(chan time.Time, 1) action := func() { closedc <- time.Now() } timeout := apiserver.NewPingTimeout(action, 50*time.Millisecond) for i := 0; i < 2; i++ { time.Sleep(10 * time.Millisecond) timeout.Ping() } // Expect action to be executed about 50ms after last ping. broken := time.Now() var closed time.Time select { case closed = <-closedc: case <-time.After(testing.LongWait): c.Fatalf("action never executed") } closeDiff := closed.Sub(broken) / time.Millisecond c.Assert(50 <= closeDiff && closeDiff <= 100, jc.IsTrue) }