Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(40)

Delta Between Two Patch Sets: state/watcher_test.go

Issue 9738043: cmd/jujud: do not change password
Left Patch Set: cmd/jujud: do not change password Created 11 years, 10 months ago
Right Patch Set: cmd/jujud: do not change password Created 11 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « state/watcher/watcher_test.go ('k') | testing/checkers/bool.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details.
3
4 package state_test
5
6 import (
7 . "launchpad.net/gocheck"
8 "launchpad.net/juju-core/state"
9 "sort"
10 "time"
11 )
12
13 var (
14 longTime = 500 * time.Millisecond
15 shortTime = 50 * time.Millisecond
16 )
17
18 type Stopper interface {
19 Stop() error
20 }
21
22 func AssertStop(c *C, stopper Stopper) {
23 c.Assert(stopper.Stop(), IsNil)
24 }
25
26 type NotifyWatcher interface {
27 Changes() <-chan struct{}
28 }
29
30 // NotifyWatcherC embeds a gocheck.C and adds methods to help verify
31 // the behaviour of any watcher that uses a <-chan struct{}.
32 type NotifyWatcherC struct {
33 *C
34 state *state.State
35 watcher NotifyWatcher
36 }
37
38 func (c NotifyWatcherC) AssertNoChange() {
39 c.state.StartSync()
40 select {
41 case _, ok := <-c.watcher.Changes():
42 c.Fatalf("watcher sent unexpected change: (_, %v)", ok)
43 case <-time.After(shortTime):
44 }
45 }
46
47 func (c NotifyWatcherC) AssertOneChange() {
48 c.state.Sync()
49 select {
50 case _, ok := <-c.watcher.Changes():
51 c.Assert(ok, Equals, true)
52 case <-time.After(longTime):
53 c.Fatalf("watcher did not send change")
54 }
55 c.AssertNoChange()
56 }
57
58 func (c NotifyWatcherC) AssertClosed() {
59 select {
60 case _, ok := <-c.watcher.Changes():
61 c.Assert(ok, Equals, false)
62 default:
63 c.Fatalf("watcher not closed")
64 }
65 }
66
67 // StringsWatcherC embeds a gocheck.C and adds methods to help verify
68 // the behaviour of any watcher that uses a <-chan []string.
69 type StringsWatcherC struct {
70 *C
71 state *state.State
72 watcher StringsWatcher
73 }
74
75 type StringsWatcher interface {
76 Stop() error
77 Changes() <-chan []string
78 }
79
80 func (c StringsWatcherC) AssertNoChange() {
81 c.state.StartSync()
82 select {
83 case actual, ok := <-c.watcher.Changes():
84 c.Fatalf("watcher sent unexpected change: (%v, %v)", actual, ok)
85 case <-time.After(shortTime):
86 }
87 }
88
89 func (c StringsWatcherC) AssertOneChange(expect ...string) {
90 c.state.Sync()
91 select {
92 case actual, ok := <-c.watcher.Changes():
93 c.Assert(ok, Equals, true)
94 if len(expect) == 0 {
95 c.Assert(actual, HasLen, 0)
96 } else {
97 sort.Strings(expect)
98 sort.Strings(actual)
99 c.Assert(expect, DeepEquals, actual)
100 }
101 case <-time.After(longTime):
102 c.Fatalf("watcher did not send change")
103 }
104 c.AssertNoChange()
105 }
106
107 func (c StringsWatcherC) AssertClosed() {
108 select {
109 case _, ok := <-c.watcher.Changes():
110 c.Assert(ok, Equals, false)
111 default:
112 c.Fatalf("watcher not closed")
113 }
114 }
115
116 // IntsWatcherC embeds a gocheck.C and adds methods to help verify
117 // the behaviour of any watcher that uses a <-chan []int.
118 type IntsWatcherC struct {
119 *C
120 state *state.State
121 watcher IntsWatcher
122 }
123
124 type IntsWatcher interface {
125 Stop() error
126 Changes() <-chan []int
127 }
128
129 func (c IntsWatcherC) AssertNoChange() {
130 c.state.StartSync()
131 select {
132 case actual, ok := <-c.watcher.Changes():
133 c.Fatalf("watcher sent unexpected change: (%v, %v)", actual, ok)
134 case <-time.After(shortTime):
135 }
136 }
137
138 func (c IntsWatcherC) AssertOneChange(expect ...int) {
139 c.state.Sync()
140 select {
141 case actual, ok := <-c.watcher.Changes():
142 c.Assert(ok, Equals, true)
143 if len(expect) == 0 {
144 c.Assert(actual, HasLen, 0)
145 } else {
146 sort.Ints(expect)
147 sort.Ints(actual)
148 c.Assert(expect, DeepEquals, actual)
149 }
150 case <-time.After(longTime):
151 c.Fatalf("watcher did not send change")
152 }
153 c.AssertNoChange()
154 }
155
156 func (c IntsWatcherC) AssertClosed() {
157 select {
158 case _, ok := <-c.watcher.Changes():
159 c.Assert(ok, Equals, false)
160 default:
161 c.Fatalf("watcher not closed")
162 }
163 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b