zk-eventmachine 0.2.0.beta.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,342 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ZK
4
- module ZKEventMachine
5
- describe 'SynchronyClient' do
6
- include EventedSpec::SpecHelper
7
- default_timeout 2.0
8
-
9
- def em_synchrony(&blk)
10
- em do
11
- EM.next_tick { Fiber.new { blk.call }.resume }
12
- end
13
- end
14
-
15
- def with_zksync
16
- em_synchrony do
17
- begin
18
- @zksync.connect
19
- yield @zksync
20
- ensure
21
- @zksync.close!
22
- done
23
- end
24
- end
25
- end
26
-
27
- before do
28
- @zk = ::ZK.new
29
- @base_path = '/zk-em-testing'
30
- @zk.rm_rf(@base_path)
31
- @zk.mkdir_p(@base_path)
32
- # $stderr.puts "zk obj_id: %x" % [@zk.object_id]
33
- @zksync = ZK::ZKEventMachine::SynchronyClient.new('localhost:2181', :zkc_log_level => 4)
34
- end
35
-
36
- after do
37
- @zk.rm_rf(@base_path)
38
- @zk.close!
39
- end
40
-
41
- describe 'connect' do
42
- it %[should connect to zookeeper] do
43
- logger.debug { "about to call connect" }
44
- em_synchrony do
45
- @zksync.connect
46
-
47
- lambda { @zksync.get(@base_path) }.should_not raise_error
48
-
49
- done { @zksync.close }
50
- end
51
- end
52
- end
53
-
54
- describe 'get' do
55
- before do
56
- @data = "this is data"
57
- @zk.set(@base_path, @data)
58
- end
59
-
60
- it %[should get the data and stat of a node that exists] do
61
- with_zksync do
62
- data, stat = @zksync.get(@base_path)
63
- data.should == @data
64
- stat.should be_kind_of(ZookeeperStat::Stat)
65
- end
66
- end
67
-
68
- it %[should raise an exception if the node does not exist] do
69
- with_zksync do
70
- lambda { @zksync.get('/thispathdoesnotexist') }.should raise_error(ZK::Exceptions::NoNode)
71
- end
72
- end
73
- end
74
-
75
- describe 'create' do
76
- describe 'success' do
77
- before do
78
- @path = [@base_path, 'foo'].join('/')
79
- @zk.delete(@path) rescue ZK::Exceptions::NoNode
80
-
81
- @data = 'this is data'
82
- end
83
-
84
- it 'should create a non-sequence node' do
85
- with_zksync do
86
- @zksync.create(@path, @data).should == @path
87
- end
88
- end
89
-
90
- it %[should create a sequence node] do
91
- with_zksync do
92
- @zksync.create(@path, @data, :sequence => true).should =~ /\A#{@path}\d+\Z/
93
- end
94
- end
95
- end
96
-
97
- describe 'failure' do
98
- it %[should barf if the node exists] do
99
- @path = [@base_path, 'foo'].join('/')
100
- @zk.create(@path, '')
101
-
102
- lambda { @zk.create(@path, '') }.should raise_error(ZK::Exceptions::NodeExists)
103
- end
104
- end
105
- end
106
-
107
- describe 'set' do
108
- before do
109
- @path = [@base_path, 'foo'].join('/')
110
- @data = 'this is data'
111
- @new_data = 'this is better data'
112
- @zk.create(@path, @data)
113
- @orig_stat = @zk.stat(@path)
114
- end
115
-
116
- it %[should set the data and return a stat] do
117
- with_zksync do
118
- stat = @zksync.set(@path, @new_data)
119
- stat.should be_instance_of(ZookeeperStat::Stat)
120
- stat.version.should > @orig_stat.version
121
-
122
- @zksync.get(@path).first.should == @new_data
123
- end
124
- end
125
-
126
- it %[should raise NoNode if the node doesn't exist] do
127
- with_zksync do
128
- lambda { @zksync.set('/thispathdoesnotexist', 'data') }.should raise_error(ZK::Exceptions::NoNode)
129
- end
130
- end
131
-
132
- it %[should raise BadVersion if the version is wrong] do
133
- with_zksync do
134
- @zksync.set(@path, @new_data)
135
- lambda { @zksync.set(@path, 'otherdata', :version => @orig_stat.version) }.should raise_error(ZK::Exceptions::BadVersion)
136
- end
137
- end
138
- end
139
-
140
- describe 'exists?' do
141
- before do
142
- @path = [@base_path, 'foo'].join('/')
143
- @data = 'this is data'
144
- end
145
-
146
- it %[should return true if the node exists] do
147
- @zk.create(@path, @data)
148
-
149
- with_zksync do
150
- @zksync.exists?(@path).should be_true
151
- end
152
- end
153
-
154
- it %[should return false if the node doesn't exist] do
155
- with_zksync do
156
- @zksync.exists?(@path).should be_false
157
- end
158
- end
159
- end
160
-
161
- describe 'stat' do
162
- describe 'success' do
163
- before do
164
- @path = [@base_path, 'foo'].join('/')
165
- @data = 'this is data'
166
- @zk.create(@path, @data)
167
- @orig_stat = @zk.stat(@path)
168
- end
169
-
170
- it %[should get the stat] do
171
- with_zksync do
172
- stat = @zksync.stat(@path)
173
-
174
- stat.should_not be_nil
175
- stat.should == @orig_stat
176
- stat.should be_instance_of(ZookeeperStat::Stat)
177
- end
178
- end
179
- end
180
-
181
- describe 'non-existent node' do
182
- before do
183
- @path = [@base_path, 'foo'].join('/')
184
- @zk.delete(@path) rescue ZK::Exceptions::NoNode
185
- end
186
-
187
- it %[should not be an error] do
188
- with_zksync do
189
- stat = @zksync.stat(@path)
190
- stat.should_not be_nil
191
- stat.exists?.should be_false
192
- stat.should be_instance_of(ZookeeperStat::Stat)
193
- end
194
- end
195
- end
196
- end
197
-
198
- describe 'delete' do
199
- it %[should delete the node] do
200
- @path = [@base_path, 'foo'].join('/')
201
- @data = 'this is data'
202
- @zk.create(@path, @data)
203
-
204
- with_zksync do
205
- @zksync.delete(@path)
206
- end
207
-
208
- @zk.exists?(@path).should be_false
209
- end
210
-
211
- it %[should raise NoNode exception if the node does not exist] do
212
- @path = [@base_path, 'foo'].join('/')
213
- @zk.delete(@path) rescue ZK::Exceptions::NoNode
214
-
215
- with_zksync do
216
- lambda { @zksync.delete(@path) }.should raise_error(ZK::Exceptions::NoNode)
217
- end
218
- end
219
- end
220
-
221
- describe 'children' do
222
- it %[should return the names of the children of the node] do
223
- @path = [@base_path, 'foo'].join('/')
224
- @child_1_path = [@path, 'child_1'].join('/')
225
- @child_2_path = [@path, 'child_2'].join('/')
226
-
227
- @data = 'this is data'
228
- @zk.create(@path, @data)
229
- @zk.create(@child_1_path, '')
230
- @zk.create(@child_2_path, '')
231
-
232
- with_zksync do
233
- children, stat = @zksync.children(@path)
234
- children.should be_kind_of(Array)
235
- children.length.should == 2
236
- children.should include('child_1')
237
- children.should include('child_2')
238
-
239
- stat.should be_instance_of(ZookeeperStat::Stat)
240
- end
241
- end
242
-
243
- it %[should raise NoNode if the node doesn't exist] do
244
- @path = [@base_path, 'foo'].join('/')
245
- @zk.delete(@path) rescue ZK::Exceptions::NoNode
246
-
247
- with_zksync do
248
- lambda { @zksync.children(@path) }.should raise_error(ZK::Exceptions::NoNode)
249
- end
250
- end
251
- end
252
-
253
- describe 'mkdir_p' do
254
- it %[should create the directory structure] do
255
- paths = [ "#{@base_path}/bar/baz", "#{@base_path}/foo/bar/quux" ]
256
-
257
- with_zksync do
258
- @zksync.mkdir_p(paths)
259
- end
260
-
261
- @zk.exists?("#{@base_path}/bar").should be_true
262
- @zk.exists?("#{@base_path}/bar/baz").should be_true
263
- @zk.exists?("#{@base_path}/foo").should be_true
264
- @zk.exists?("#{@base_path}/foo/bar").should be_true
265
- @zk.exists?("#{@base_path}/foo/bar/quux").should be_true
266
- end
267
- end
268
-
269
- describe 'rm_rf' do
270
- it %[should remove all paths listed] do
271
- @relpaths = ['disco/foo', 'prune/bar', 'fig/bar/one', 'apple/bar/two', 'orange/quux/c/d/e']
272
-
273
- @roots = @relpaths.map { |p| File.join(@base_path, p.split('/').first) }.uniq
274
- @paths = @relpaths.map { |n| File.join(@base_path, n) }
275
-
276
- @paths.each { |n| @zk.mkdir_p(n) }
277
-
278
- with_zksync do
279
- @zksync.rm_rf(@roots)
280
- end
281
-
282
- @roots.each { |n| @zk.exists?(n).should be_false }
283
-
284
- @zk.exists?(@base_path).should be_true
285
- end
286
- end
287
-
288
- describe 'event delivery' do
289
- default_timeout 0.5
290
-
291
- before do
292
- @path = [@base_path, 'foo'].join('/')
293
- @data = 'this is data'
294
- @zk.create(@path, @data)
295
- @orig_stat = @zk.stat(@path)
296
- end
297
-
298
- it %[should receive an event when the node changes] do
299
- em_synchrony do
300
- @orig_fiber = Fiber.current
301
-
302
- @zksync.connect
303
- @zksync.should be_connected
304
-
305
- @zksync.event_handler.register(@path) do |event|
306
- Fiber.current.should_not == @orig_fiber
307
-
308
- done { @zksync.close! }
309
- end
310
-
311
- @zksync.get(@path, :watch => true)
312
-
313
- stat = @zksync.set(@path, 'new data')
314
- logger.debug { "sksync set returned: #{stat}" }
315
- end
316
- end
317
-
318
- it %[should be able to register for events on a node that doesn't exist yet] do
319
- em_synchrony do
320
- @new_path = "#{@path}/blah"
321
-
322
- @zksync.connect
323
- @zksync.should be_connected
324
-
325
- @zksync.event_handler.register(@new_path) do |event|
326
- logger.debug { "got event #{event}" }
327
-
328
- # without close! wrapping itself in Fiber.new, this causes a
329
- # '[BUG] cfp consistency error - send' under 1.9.3
330
- done { @zksync.close! }
331
- end
332
-
333
- @zksync.stat(@new_path, :watch => true)
334
-
335
- @zksync.create(@new_path, '')
336
- end
337
- end
338
- end
339
- end
340
- end
341
- end
342
-