1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
// TODO(wallyworld) - move to instancepoller_test
12
gc "launchpad.net/gocheck"
14
"launchpad.net/juju-core/state"
15
coretesting "launchpad.net/juju-core/testing"
16
jc "launchpad.net/juju-core/testing/checkers"
17
"launchpad.net/juju-core/testing/testbase"
20
func TestPackage(t *stdtesting.T) {
21
coretesting.MgoTestPackage(t)
24
var _ = gc.Suite(&updaterSuite{})
26
type updaterSuite struct {
30
func (*updaterSuite) TestStopsWatcher(c *gc.C) {
31
context := &testUpdaterContext{
32
dyingc: make(chan struct{}),
34
expectErr := errors.New("some error")
35
watcher := &testMachinesWatcher{
36
changes: make(chan []string),
39
done := make(chan error)
41
done <- watchMachinesLoop(context, watcher)
46
c.Assert(err, gc.ErrorMatches, ".*"+expectErr.Error())
47
case <-time.After(coretesting.LongWait):
48
c.Fatalf("timed out waiting for watchMachinesLoop to terminate")
50
c.Assert(watcher.stopped, jc.IsTrue)
53
func (*updaterSuite) TestWatchMachinesWaitsForMachinePollers(c *gc.C) {
54
// We can't see that the machine pollers are still alive directly,
55
// but we can make the machine's Refresh method block,
56
// and test that watchMachinesLoop only terminates
58
waitRefresh := make(chan struct{})
63
refresh: func() error {
64
// Signal that we're in Refresh.
65
waitRefresh <- struct{}{}
66
// Wait to be unblocked.
71
dyingc := make(chan struct{})
72
context := &testUpdaterContext{
74
newMachineContextFunc: func() machineContext {
75
return &testMachineContext{
76
getInstanceInfo: instanceInfoGetter(c, "i1234", testAddrs, "running", nil),
80
getMachineFunc: func(id string) (machine, error) {
81
c.Check(id, gc.Equals, m.id)
85
watcher := &testMachinesWatcher{
86
changes: make(chan []string),
88
done := make(chan error)
90
done <- watchMachinesLoop(context, watcher)
92
// Send two changes; the first one should start the machineLoop;
93
// the second should call Refresh.
94
watcher.changes <- []string{"99"}
95
watcher.changes <- []string{"99"}
96
// Wait for the machineLoop to call Refresh
99
c.Logf("poller called Refresh")
100
case <-time.After(coretesting.LongWait):
101
c.Fatalf("timed out waiting for machine to be refreshed")
103
close(context.dyingc)
104
// Wait a little while to be sure that watchMachinesLoop is
105
// actually waiting for its machine poller to finish.
108
c.Fatalf("watchMachinesLoop terminated prematurely: %v", err)
109
case <-time.After(coretesting.ShortWait):
112
waitRefresh <- struct{}{}
115
c.Assert(err, gc.IsNil)
116
case <-time.After(coretesting.LongWait):
117
c.Fatalf("timed out waiting for watchMachinesLoop to terminate")
119
c.Assert(watcher.stopped, jc.IsTrue)
122
type testUpdaterContext struct {
123
newMachineContextFunc func() machineContext
124
getMachineFunc func(id string) (machine, error)
128
func (context *testUpdaterContext) newMachineContext() machineContext {
129
return context.newMachineContextFunc()
132
func (context *testUpdaterContext) getMachine(id string) (machine, error) {
133
return context.getMachineFunc(id)
136
func (context *testUpdaterContext) dying() <-chan struct{} {
137
return context.dyingc
140
type testMachinesWatcher struct {
142
changes chan []string
146
func (w *testMachinesWatcher) Changes() <-chan []string {
150
func (w *testMachinesWatcher) Stop() error {
155
func (w *testMachinesWatcher) Err() error {