~rogpeppe/juju-core/438-local-instance-Addresses

« back to all changes in this revision

Viewing changes to worker/addressupdater/updater_test.go

[r=rogpeppe] worker/addressupdater: wire up

It could do with some more comprehensive
testing, but it might be OK to land anyway.

https://codereview.appspot.com/14306043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
1
4
package addressupdater
2
5
 
3
6
import (
7
10
 
8
11
        gc "launchpad.net/gocheck"
9
12
 
 
13
        "launchpad.net/juju-core/state"
10
14
        coretesting "launchpad.net/juju-core/testing"
11
15
        jc "launchpad.net/juju-core/testing/checkers"
12
16
        "launchpad.net/juju-core/testing/testbase"
13
17
)
14
18
 
15
19
func TestPackage(t *stdtesting.T) {
16
 
        gc.TestingT(t)
 
20
        coretesting.MgoTestPackage(t)
17
21
}
18
22
 
19
23
var _ = gc.Suite(&updaterSuite{})
23
27
}
24
28
 
25
29
func (*updaterSuite) TestStopsWatcher(c *gc.C) {
26
 
        context := &testPublisherContext{
 
30
        context := &testUpdaterContext{
27
31
                dyingc: make(chan struct{}),
28
32
        }
29
33
        expectErr := errors.New("some error")
45
49
        c.Assert(watcher.stopped, jc.IsTrue)
46
50
}
47
51
 
48
 
type testPublisherContext struct {
 
52
func (*updaterSuite) TestWatchMachinesWaitsForMachinePollers(c *gc.C) {
 
53
        // We can't see that the machine pollers are still alive directly,
 
54
        // but we can make the machine's Refresh method block,
 
55
        // and test that watchMachinesLoop only terminates
 
56
        // when it unblocks.
 
57
        waitRefresh := make(chan struct{})
 
58
        m := &testMachine{
 
59
                id:         "99",
 
60
                instanceId: "i1234",
 
61
                life:       state.Alive,
 
62
                refresh: func() error {
 
63
                        // Signal that we're in Refresh.
 
64
                        waitRefresh <- struct{}{}
 
65
                        // Wait to be unblocked.
 
66
                        <-waitRefresh
 
67
                        return nil
 
68
                },
 
69
        }
 
70
        dyingc := make(chan struct{})
 
71
        context := &testUpdaterContext{
 
72
                dyingc: dyingc,
 
73
                newMachineContextFunc: func() machineContext {
 
74
                        return &testMachineContext{
 
75
                                getAddresses: addressesGetter(c, "i1234", testAddrs, nil),
 
76
                                dyingc:       dyingc,
 
77
                        }
 
78
                },
 
79
                getMachineFunc: func(id string) (machine, error) {
 
80
                        c.Check(id, gc.Equals, m.id)
 
81
                        return m, nil
 
82
                },
 
83
        }
 
84
        watcher := &testMachinesWatcher{
 
85
                changes: make(chan []string),
 
86
        }
 
87
        done := make(chan error)
 
88
        go func() {
 
89
                done <- watchMachinesLoop(context, watcher)
 
90
        }()
 
91
        // Send two changes; the first one should start the machineLoop;
 
92
        // the second should call Refresh.
 
93
        watcher.changes <- []string{"99"}
 
94
        watcher.changes <- []string{"99"}
 
95
        // Wait for the machineLoop to call Refresh
 
96
        select {
 
97
        case <-waitRefresh:
 
98
                c.Logf("poller called Refresh")
 
99
        case <-time.After(coretesting.LongWait):
 
100
                c.Fatalf("timed out waiting for machine to be refreshed")
 
101
        }
 
102
        close(context.dyingc)
 
103
        // Wait a little while to be sure that watchMachinesLoop is
 
104
        // actually waiting for its machine poller to finish.
 
105
        select {
 
106
        case err := <-done:
 
107
                c.Fatalf("watchMachinesLoop terminated prematurely: %v", err)
 
108
        case <-time.After(coretesting.ShortWait):
 
109
        }
 
110
 
 
111
        waitRefresh <- struct{}{}
 
112
        select {
 
113
        case err := <-done:
 
114
                c.Assert(err, gc.IsNil)
 
115
        case <-time.After(coretesting.LongWait):
 
116
                c.Fatalf("timed out waiting for watchMachinesLoop to terminate")
 
117
        }
 
118
        c.Assert(watcher.stopped, jc.IsTrue)
 
119
}
 
120
 
 
121
type testUpdaterContext struct {
49
122
        newMachineContextFunc func() machineContext
50
123
        getMachineFunc        func(id string) (machine, error)
51
124
        dyingc                chan struct{}
52
125
}
53
126
 
54
 
func (context *testPublisherContext) newMachineContext() machineContext {
 
127
func (context *testUpdaterContext) newMachineContext() machineContext {
55
128
        return context.newMachineContextFunc()
56
129
}
57
130
 
58
 
func (context *testPublisherContext) getMachine(id string) (machine, error) {
 
131
func (context *testUpdaterContext) getMachine(id string) (machine, error) {
59
132
        return context.getMachineFunc(id)
60
133
}
61
134
 
62
 
func (context *testPublisherContext) dying() <-chan struct{} {
 
135
func (context *testUpdaterContext) dying() <-chan struct{} {
63
136
        return context.dyingc
64
137
}
65
138