Пример #1
0
func (s *PeekerSuite) TestPeeks(c *gc.C) {
	expect := hooktesting.HookList(hooks.Install, hooks.ConfigChanged, hooks.Start)
	source := hook.NewListSource(expect)
	peeker := hook.NewPeeker(source)
	defer statetesting.AssertStop(c, peeker)

	timeout := time.After(coretesting.LongWait)
	for _, expectInfo := range expect {
		select {
		case peek, ok := <-peeker.Peeks():
			c.Assert(ok, jc.IsTrue)
			c.Assert(peek.HookInfo(), gc.Equals, expectInfo)
			peek.Reject()
		case <-timeout:
			c.Fatalf("ran out of time")
		}
		select {
		case peek, ok := <-peeker.Peeks():
			c.Assert(ok, jc.IsTrue)
			c.Assert(peek.HookInfo(), gc.Equals, expectInfo)
			peek.Consume()
		case <-timeout:
			c.Fatalf("ran out of time")
		}
	}
	select {
	case <-time.After(coretesting.ShortWait):
	case peek, ok := <-peeker.Peeks():
		c.Fatalf("unexpected peek from empty queue: %#v, %#v", peek, ok)
	}
}
Пример #2
0
func (s *ListSourceSuite) TestNoUpdates(c *gc.C) {
	source := hook.NewListSource(hooktesting.HookList(hooks.Start, hooks.Stop))
	c.Check(source.Changes(), gc.IsNil)

	ch := source.Changes()
	c.Check(ch, gc.IsNil)

	err := source.Stop()
	c.Check(err, jc.ErrorIsNil)
}
Пример #3
0
func (s *HookSenderSuite) TestStopsHooks(c *gc.C) {
	expect := hooktesting.HookList(hooks.Install, hooks.ConfigChanged, hooks.Start)
	source := hook.NewListSource(expect)
	out := make(chan hook.Info)
	sender := hook.NewSender(out, source)
	defer statetesting.AssertStop(c, sender)

	assertNext(c, out, expect[0])
	assertNext(c, out, expect[1])
	statetesting.AssertStop(c, sender)
	assertEmpty(c, out)
	c.Assert(source.Next(), gc.Equals, expect[2])
}
Пример #4
0
func (s *HookSenderSuite) TestSendsHooks(c *gc.C) {
	expect := hooktesting.HookList(hooks.Install, hooks.ConfigChanged, hooks.Start)
	source := hook.NewListSource(expect)
	out := make(chan hook.Info)
	sender := hook.NewSender(out, source)
	defer statetesting.AssertStop(c, sender)

	for i := range expect {
		assertNext(c, out, expect[i])
	}
	assertEmpty(c, out)
	statetesting.AssertStop(c, sender)
	c.Assert(source.Empty(), jc.IsTrue)
}
Пример #5
0
func (s *ListSourceSuite) TestQueue(c *gc.C) {
	for i, test := range [][]hook.Info{
		hooktesting.HookList(),
		hooktesting.HookList(hooks.Install, hooks.Install),
		hooktesting.HookList(hooks.Stop, hooks.Start, hooks.Stop),
	} {
		c.Logf("test %d: %v", i, test)
		source := hook.NewListSource(test)
		for _, expect := range test {
			c.Check(source.Empty(), jc.IsFalse)
			c.Check(source.Next(), gc.DeepEquals, expect)
			source.Pop()
		}
		c.Check(source.Empty(), jc.IsTrue)
		c.Check(source.Next, gc.PanicMatches, "Source is empty")
		c.Check(source.Pop, gc.PanicMatches, "Source is empty")
	}
}
Пример #6
0
// NewDyingHookSource returns a new hook.Source that generates all hooks
// necessary to clean up the supplied initial relation hook state, while
// preserving the guarantees Juju makes about hook execution order.
func NewDyingHookSource(initial *State) hook.Source {
	var list []hook.Info

	// Honour any expected relation-changed hook.
	if initial.ChangedPending != "" {
		list = append(list, hook.Info{
			Kind:          hooks.RelationChanged,
			RelationId:    initial.RelationId,
			RemoteUnit:    initial.ChangedPending,
			ChangeVersion: initial.Members[initial.ChangedPending],
		})
	}

	// Depart in consistent order, mainly for testing purposes.
	departs := []string{}
	for name := range initial.Members {
		departs = append(departs, name)
	}
	sort.Strings(departs)
	for _, name := range departs {
		list = append(list, hook.Info{
			Kind:          hooks.RelationDeparted,
			RelationId:    initial.RelationId,
			RemoteUnit:    name,
			ChangeVersion: initial.Members[name],
		})
	}

	// Finally break the relation.
	list = append(list, hook.Info{
		Kind:       hooks.RelationBroken,
		RelationId: initial.RelationId,
	})

	return hook.NewListSource(list)
}