func (s *PeekerSuite) TestStopWhilePeeking(c *gc.C) { source := hooktesting.NewFullUnbufferedSource() defer statetesting.AssertStop(c, source) peeker := hook.NewPeeker(source) defer statetesting.AssertStop(c, peeker) select { case peek, ok := <-peeker.Peeks(): c.Assert(ok, jc.IsTrue) c.Assert(peek.HookInfo(), gc.Equals, hook.Info{Kind: hooks.Install}) assertStopPeeker(c, peeker, source) case <-time.After(coretesting.LongWait): c.Fatalf("timed out") } }
func (s *PeekerSuite) TestUpdatesFullQueue(c *gc.C) { source := hooktesting.NewFullUnbufferedSource() defer statetesting.AssertStop(c, source) peeker := hook.NewPeeker(source) defer statetesting.AssertStop(c, peeker) // Check we're being sent peeks but not updates. timeout := time.After(coretesting.LongWait) assertActive := func() { select { case peek, ok := <-peeker.Peeks(): c.Assert(ok, jc.IsTrue) defer peek.Reject() c.Assert(peek.HookInfo(), gc.Equals, hook.Info{Kind: hooks.Install}) case update, ok := <-source.UpdatesC: c.Fatalf("got unexpected update: %#v %#v", update, ok) case <-timeout: c.Fatalf("timed out") } } assertActive() // Send an event on the Changes() chan. select { case source.ChangesC <- source.NewChange("sent"): case <-timeout: c.Fatalf("could not send change") } // Now that a change has been delivered, nothing should be sent on the out // chan, or read from the changes chan, until the Update method has completed. select { case source.ChangesC <- source.NewChange("notSent"): c.Fatalf("sent extra change while updating queue") case peek, ok := <-peeker.Peeks(): c.Fatalf("got unexpected peek while updating queue: %#v %#v", peek, ok) case got, ok := <-source.UpdatesC: c.Assert(ok, jc.IsTrue) c.Assert(got, gc.Equals, "sent") case <-timeout: c.Fatalf("timed out") } // Check we're still being sent hooks and not updates. assertActive() }
func (s *PeekerSuite) TestUpdatesFullQueueSpam(c *gc.C) { source := hooktesting.NewFullUnbufferedSource() defer statetesting.AssertStop(c, source) peeker := hook.NewPeeker(source) defer statetesting.AssertStop(c, peeker) // Spam all channels continuously for a bit. timeout := time.After(coretesting.LongWait) peekCount := 0 changeCount := 0 updateCount := 0 for i := 0; i < 100; i++ { select { case peek, ok := <-peeker.Peeks(): c.Assert(ok, jc.IsTrue) c.Assert(peek.HookInfo(), gc.DeepEquals, hook.Info{Kind: hooks.Install}) peek.Consume() peekCount++ case source.ChangesC <- source.NewChange("!"): changeCount++ case update, ok := <-source.UpdatesC: c.Assert(ok, jc.IsTrue) c.Assert(update, gc.Equals, "!") updateCount++ case <-timeout: c.Fatalf("not enough things happened in time") } } // Once we've finished sending, exhaust the updates... for i := updateCount; i < changeCount && updateCount < changeCount; i++ { select { case update, ok := <-source.UpdatesC: c.Assert(ok, jc.IsTrue) c.Assert(update, gc.DeepEquals, "!") updateCount++ case <-timeout: c.Fatalf("expected %d updates, got %d", changeCount, updateCount) } } // ...and check sane end state to validate the foregoing. c.Check(peekCount, gc.Not(gc.Equals), 0) c.Check(changeCount, gc.Not(gc.Equals), 0) }
func (s *HookSenderSuite) TestHandlesUpdatesFullQueue(c *gc.C) { source := hooktesting.NewFullUnbufferedSource() defer statetesting.AssertStop(c, source) out := make(chan hook.Info) sender := hook.NewSender(out, source) defer statetesting.AssertStop(c, sender) // Check we're being sent hooks but not updates. assertActive := func() { assertNext(c, out, hook.Info{Kind: hooks.Install}) select { case update, ok := <-source.UpdatesC: c.Fatalf("got unexpected update: %#v %#v", update, ok) case <-time.After(coretesting.ShortWait): } } assertActive() // Send an event on the Changes() chan. select { case source.ChangesC <- source.NewChange("sent"): case <-time.After(coretesting.LongWait): c.Fatalf("could not send change") } // Now that a change has been delivered, nothing should be sent on the out // chan, or read from the changes chan, until the Update method has completed. select { case source.ChangesC <- source.NewChange("notSent"): c.Fatalf("sent extra change while updating queue") case hi, ok := <-out: c.Fatalf("got unexpected hook while updating queue: %#v %#v", hi, ok) case got, ok := <-source.UpdatesC: c.Assert(ok, jc.IsTrue) c.Assert(got, gc.Equals, "sent") case <-time.After(coretesting.LongWait): c.Fatalf("timed out") } // Check we're still being sent hooks and not updates. assertActive() }
func (s *PeekerSuite) TestBlocksWhenUpdating(c *gc.C) { source := hooktesting.NewFullUnbufferedSource() defer statetesting.AssertStop(c, source) peeker := hook.NewPeeker(source) defer statetesting.AssertStop(c, peeker) // Deliver a change. timeout := time.After(coretesting.LongWait) select { case source.ChangesC <- source.NewChange("sent"): case <-timeout: c.Fatalf("failed to send change") } // Check peeks are not delivered... select { case peek, ok := <-peeker.Peeks(): c.Fatalf("got unexpected peek: %#v, %#v", peek, ok) default: } // ...before the update is handled... select { case update, ok := <-source.UpdatesC: c.Assert(ok, jc.IsTrue) c.Assert(update, gc.Equals, "sent") case <-timeout: c.Fatalf("failed to collect update") } // ...at which point peeks are expected again. select { case peek, ok := <-peeker.Peeks(): c.Assert(ok, jc.IsTrue) c.Assert(peek.HookInfo(), gc.Equals, hook.Info{Kind: hooks.Install}) peek.Reject() case <-timeout: c.Fatalf("failed to send peek") } }