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

Delta Between Two Patch Sets: state/watcher/watcher_test.go

Issue 5901058: cmd/juju: working bootstrap and destroy commands
Left Patch Set: cmd/juju: working bootstrap and destroy commands Created 13 years ago
Right Patch Set: cmd/juju: working bootstrap and destroy commands Created 12 years, 11 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.go ('k') | store/charmd/config.yaml » ('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 package watcher_test
2
3 import (
4 . "launchpad.net/gocheck"
5 "launchpad.net/gozk/zookeeper"
6 "launchpad.net/juju/go/state/watcher"
7 "launchpad.net/juju/go/testing"
8 stdtesting "testing"
9 "time"
10 )
11
12 var zkAddr string
13
14 func TestPackage(t *stdtesting.T) {
15 srv := testing.StartZkServer()
16 defer srv.Destroy()
17 var err error
18 zkAddr, err = srv.Addr()
19 if err != nil {
20 t.Fatalf("could not get ZooKeeper server address")
21 }
22 TestingT(t)
23 }
24
25 type WatcherSuite struct {
26 zkConn *zookeeper.Conn
27 path string
28 }
29
30 var _ = Suite(&WatcherSuite{})
31
32 func (s *WatcherSuite) SetUpTest(c *C) {
33 zk, session, err := zookeeper.Dial(zkAddr, 15e9)
34 c.Assert(err, IsNil)
35 event := <-session
36 c.Assert(event.Ok(), Equals, true)
37 c.Assert(event.Type, Equals, zookeeper.EVENT_SESSION)
38 c.Assert(event.State, Equals, zookeeper.STATE_CONNECTED)
39
40 s.zkConn = zk
41 s.path = "/watcher"
42
43 c.Assert(err, IsNil)
44 }
45
46 func (s *WatcherSuite) TearDownTest(c *C) {
47 testing.ZkRemoveTree(s.zkConn, s.path)
48 s.zkConn.Close()
49 }
50
51 func (s *WatcherSuite) TestContentWatcher(c *C) {
52 contentWatcher := watcher.NewContentWatcher(s.zkConn, s.path)
53
54 go func() {
55 time.Sleep(50 * time.Millisecond)
56 s.createPath(c, "init")
57 time.Sleep(50 * time.Millisecond)
58 s.changeContent(c, "foo")
59 time.Sleep(50 * time.Millisecond)
60 s.changeContent(c, "foo")
61 time.Sleep(50 * time.Millisecond)
62 s.removePath(c)
63 time.Sleep(50 * time.Millisecond)
64 s.createPath(c, "done")
65 }()
66
67 var expectedChanges = []watcher.ContentChange{
68 {true, "init"},
69 {true, "foo"},
70 {false, ""},
71 {true, "done"},
72 }
73 for _, want := range expectedChanges {
74 select {
75 case got, ok := <-contentWatcher.Changes():
76 c.Assert(ok, Equals, true)
77 c.Assert(got, Equals, want)
78 case <-time.After(200 * time.Millisecond):
79 c.Fatalf("didn't get change: %#v", want)
80 }
81 }
82
83 select {
84 case got, _ := <-contentWatcher.Changes():
85 c.Fatalf("got unexpected change: %#v", got)
86 case <-time.After(100 * time.Millisecond):
87 }
88
89 err := contentWatcher.Stop()
90 c.Assert(err, IsNil)
91
92 select {
93 case _, ok := <-contentWatcher.Changes():
94 c.Assert(ok, Equals, false)
95 case <-time.After(200 * time.Millisecond):
96 c.Fatalf("unexpected timeout")
97 }
98 }
99
100 func (s *WatcherSuite) TestChildrenWatcher(c *C) {
101 s.createPath(c, "init")
102 childrenWatcher := watcher.NewChildrenWatcher(s.zkConn, s.path)
103
104 go func() {
105 time.Sleep(50 * time.Millisecond)
106 s.changeChildren(c, true, "foo")
107 time.Sleep(50 * time.Millisecond)
108 s.changeChildren(c, true, "bar")
109 time.Sleep(50 * time.Millisecond)
110 s.changeChildren(c, false, "foo")
111 }()
112
113 var expectedChanges = []watcher.ChildrenChange{
114 {[]string{"foo"}, nil},
115 {[]string{"bar"}, nil},
116 {nil, []string{"foo"}},
117 }
118 for _, want := range expectedChanges {
119 select {
120 case got, ok := <-childrenWatcher.Changes():
121 c.Assert(ok, Equals, true)
122 c.Assert(got, DeepEquals, want)
123 case <-time.After(200 * time.Millisecond):
124 c.Fatalf("didn't get change: %#v", want)
125 }
126 }
127
128 select {
129 case got, _ := <-childrenWatcher.Changes():
130 c.Fatalf("got unexpected change: %#v", got)
131 case <-time.After(100 * time.Millisecond):
132 }
133
134 err := childrenWatcher.Stop()
135 c.Assert(err, IsNil)
136
137 select {
138 case _, ok := <-childrenWatcher.Changes():
139 c.Assert(ok, Equals, false)
140 case <-time.After(200 * time.Millisecond):
141 c.Fatalf("unexpected timeout")
142 }
143 }
144
145 func (s *WatcherSuite) createPath(c *C, content string) {
146 _, err := s.zkConn.Create(s.path, content, 0, zookeeper.WorldACL(zookeep er.PERM_ALL))
147 c.Assert(err, IsNil)
148 }
149
150 func (s *WatcherSuite) removePath(c *C) {
151 testing.ZkRemoveTree(s.zkConn, s.path)
152 }
153
154 func (s *WatcherSuite) changeContent(c *C, content string) {
155 _, err := s.zkConn.Set(s.path, content, -1)
156 c.Assert(err, IsNil)
157 }
158
159 func (s *WatcherSuite) changeChildren(c *C, add bool, child string) {
160 var err error
161 path := s.path + "/" + child
162 if add {
163 _, err = s.zkConn.Create(path, "", 0, zookeeper.WorldACL(zookeep er.PERM_ALL))
164 } else {
165 err = s.zkConn.Delete(path, -1)
166 }
167 c.Assert(err, IsNil)
168 }
LEFTRIGHT

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