~sinzui/ubuntu/vivid/juju-core/vivid-1.24.6

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/common/watch_test.go

  • Committer: Curtis Hovey
  • Date: 2015-09-30 14:14:54 UTC
  • mfrom: (1.1.34)
  • Revision ID: curtis@hovey.name-20150930141454-o3ldf23dzyjio6c0
Backport of 1.24.6 from wily. (LP: #1500916)

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import (
7
7
        "fmt"
8
8
 
 
9
        "launchpad.net/tomb"
 
10
 
 
11
        "github.com/juju/names"
9
12
        jc "github.com/juju/testing/checkers"
10
13
        gc "gopkg.in/check.v1"
11
14
 
13
16
        "github.com/juju/juju/apiserver/params"
14
17
        apiservertesting "github.com/juju/juju/apiserver/testing"
15
18
        "github.com/juju/juju/state"
16
 
        "github.com/juju/names"
 
19
        statetesting "github.com/juju/juju/state/testing"
17
20
)
18
21
 
19
22
type agentEntityWatcherSuite struct{}
29
32
        changes := make(chan struct{}, 1)
30
33
        // Simulate initial event.
31
34
        changes <- struct{}{}
32
 
        return &fakeNotifyWatcher{changes}
 
35
        return &fakeNotifyWatcher{changes: changes}
33
36
}
34
37
 
35
38
type fakeNotifyWatcher struct {
 
39
        tomb    tomb.Tomb
36
40
        changes chan struct{}
37
41
}
38
42
 
39
 
func (*fakeNotifyWatcher) Stop() error {
40
 
        return nil
41
 
}
42
 
 
43
 
func (*fakeNotifyWatcher) Kill() {}
44
 
 
45
 
func (*fakeNotifyWatcher) Wait() error {
46
 
        return nil
47
 
}
48
 
 
49
 
func (*fakeNotifyWatcher) Err() error {
50
 
        return nil
 
43
func (w *fakeNotifyWatcher) Stop() error {
 
44
        w.Kill()
 
45
        return w.Wait()
 
46
}
 
47
 
 
48
func (w *fakeNotifyWatcher) Kill() {
 
49
        w.tomb.Kill(nil)
 
50
        w.tomb.Done()
 
51
}
 
52
 
 
53
func (w *fakeNotifyWatcher) Wait() error {
 
54
        return w.tomb.Wait()
 
55
}
 
56
 
 
57
func (w *fakeNotifyWatcher) Err() error {
 
58
        return w.tomb.Err()
51
59
}
52
60
 
53
61
func (w *fakeNotifyWatcher) Changes() <-chan struct{} {
114
122
        c.Assert(err, jc.ErrorIsNil)
115
123
        c.Assert(result.Results, gc.HasLen, 0)
116
124
}
 
125
 
 
126
type multiNotifyWatcherSuite struct{}
 
127
 
 
128
var _ = gc.Suite(&multiNotifyWatcherSuite{})
 
129
 
 
130
func (*multiNotifyWatcherSuite) TestMultiNotifyWatcher(c *gc.C) {
 
131
        w0 := &fakeNotifyWatcher{changes: make(chan struct{}, 1)}
 
132
        w1 := &fakeNotifyWatcher{changes: make(chan struct{}, 1)}
 
133
        w0.changes <- struct{}{}
 
134
        w1.changes <- struct{}{}
 
135
 
 
136
        mw := common.NewMultiNotifyWatcher(w0, w1)
 
137
        defer statetesting.AssertStop(c, mw)
 
138
 
 
139
        wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw)
 
140
        wc.AssertOneChange()
 
141
 
 
142
        w0.changes <- struct{}{}
 
143
        wc.AssertOneChange()
 
144
        w1.changes <- struct{}{}
 
145
        wc.AssertOneChange()
 
146
 
 
147
        w0.changes <- struct{}{}
 
148
        w1.changes <- struct{}{}
 
149
        wc.AssertOneChange()
 
150
}
 
151
 
 
152
func (*multiNotifyWatcherSuite) TestMultiNotifyWatcherStop(c *gc.C) {
 
153
        w0 := &fakeNotifyWatcher{changes: make(chan struct{}, 1)}
 
154
        w1 := &fakeNotifyWatcher{changes: make(chan struct{}, 1)}
 
155
        w0.changes <- struct{}{}
 
156
        w1.changes <- struct{}{}
 
157
 
 
158
        mw := common.NewMultiNotifyWatcher(w0, w1)
 
159
        wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw)
 
160
        wc.AssertOneChange()
 
161
        statetesting.AssertCanStopWhenSending(c, mw)
 
162
        wc.AssertClosed()
 
163
}
 
164
 
 
165
type nopSyncStarter struct{}
 
166
 
 
167
func (nopSyncStarter) StartSync() {}