func (s *PresenceSuite) TestWatchPeriod(c *gc.C) { presence.FakePeriod(1) presence.RealTimeSlot() w := presence.NewWatcher(s.presence) p := presence.NewPinger(s.presence, "a") defer w.Stop() defer p.Stop() ch := make(chan presence.Change) w.Watch("a", ch) assertChange(c, ch, presence.Change{"a", false}) // A single ping. c.Assert(p.Start(), gc.IsNil) c.Assert(p.Stop(), gc.IsNil) // Wait for next periodic refresh. time.Sleep(1 * time.Second) assertChange(c, ch, presence.Change{"a", true}) }
func (s *PresenceSuite) TestPingerPeriodAndResilience(c *gc.C) { // This test verifies both the periodic pinging, // and also a great property of the design: deaths // also expire, which means erroneous scenarios are // automatically recovered from. const period = 1 presence.FakePeriod(period) presence.RealTimeSlot() w := presence.NewWatcher(s.presence) p1 := presence.NewPinger(s.presence, "a") p2 := presence.NewPinger(s.presence, "a") defer w.Stop() defer p1.Stop() defer p2.Stop() // Start p1 and let it go on. c.Assert(p1.Start(), gc.IsNil) w.Sync() assertAlive(c, w, "a", true) // Start and kill p2, which will temporarily // invalidate p1 and set the key as dead. c.Assert(p2.Start(), gc.IsNil) c.Assert(p2.Kill(), gc.IsNil) w.Sync() assertAlive(c, w, "a", false) // Wait for two periods, and check again. Since // p1 is still alive, p2's death will expire and // the key will come back. time.Sleep(period * 2 * time.Second) w.Sync() assertAlive(c, w, "a", true) }