zk-eventmachine 0.1.1

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.
@@ -0,0 +1,693 @@
1
+ require 'spec_helper'
2
+
3
+ module ZK::ZKEventMachine
4
+ describe 'Client' do
5
+ include EventedSpec::SpecHelper
6
+ default_timeout 2.0
7
+
8
+ before do
9
+ @zk = ::ZK.new
10
+ @base_path = '/zk-em-testing'
11
+ @zk.rm_rf(@base_path)
12
+ @zk.mkdir_p(@base_path)
13
+ @zkem = ZK::ZKEventMachine::Client.new('localhost:2181')
14
+ end
15
+
16
+ after do
17
+ @zk.rm_rf(@base_path)
18
+ @zk.close!
19
+ end
20
+
21
+ describe 'connect' do
22
+ it %[should return a deferred that fires when connected and then close] do
23
+ em do
24
+ @zkem.connect do
25
+ true.should be_true
26
+ @zkem.close! { done }
27
+ end
28
+ end
29
+ end
30
+
31
+ it %[should be able to be called mulitple times] do
32
+ em do
33
+ @zkem.connect do
34
+ logger.debug { "inside first callback" }
35
+ @zkem.connect do
36
+ logger.debug { "inside second callback" }
37
+ true.should be_true
38
+ @zkem.close! { done }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'get' do
46
+ describe 'success' do
47
+ before do
48
+ @path = [@base_path, 'foo'].join('/')
49
+ @data = 'this is data'
50
+ @zk.create(@path, @data)
51
+ end
52
+
53
+ it 'should get the data and call the callback' do
54
+ em do
55
+ @zkem.connect do
56
+ dfr = @zkem.get(@path)
57
+
58
+ dfr.callback do |*a|
59
+ logger.debug { "got callback with #{a.inspect}" }
60
+ a.should_not be_empty
61
+ a.first.should == @data
62
+ a.last.should be_instance_of(ZookeeperStat::Stat)
63
+ EM.reactor_thread?.should be_true
64
+ @zkem.close! { done }
65
+ end
66
+
67
+ dfr.errback do |exc|
68
+ raise exc
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ it 'should get the data and do a nodejs-style callback' do
75
+ em do
76
+ @zkem.connect do
77
+ @zkem.get(@path) do |exc,data,stat|
78
+ exc.should be_nil
79
+ data.should == @data
80
+ stat.should be_instance_of(ZookeeperStat::Stat)
81
+ EM.reactor_thread?.should be_true
82
+ @zkem.close! { done }
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end # success
88
+
89
+ describe 'failure' do
90
+ before do
91
+ @path = [@base_path, 'foo'].join('/')
92
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
93
+ end
94
+
95
+ it %[should call the errback in deferred style] do
96
+ em do
97
+ @zkem.connect do
98
+ d = @zkem.get(@path)
99
+
100
+ d.callback do
101
+ raise "Should not have been called"
102
+ end
103
+
104
+ d.errback do |exc|
105
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
106
+ logger.debug { "calling done" }
107
+ @zkem.close! { done }
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ it %[should have NoNode as the first argument to the block] do
114
+ em do
115
+ @zkem.connect do
116
+ @zkem.get(@path) do |exc,*a|
117
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
118
+ @zkem.close! { done }
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end # failure
124
+ end # get
125
+
126
+ describe 'create' do
127
+ describe 'success' do
128
+ before do
129
+ @path = [@base_path, 'foo'].join('/')
130
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
131
+
132
+ @data = 'this is data'
133
+ end
134
+
135
+ describe 'non-sequence node' do
136
+ it 'should create the node and call the callback' do
137
+ em do
138
+ @zkem.connect do
139
+ d = @zkem.create(@path, @data)
140
+
141
+ d.callback do |*a|
142
+ logger.debug { "got callback with #{a.inspect}" }
143
+ a.should_not be_empty
144
+ a.first.should == @path
145
+ EM.reactor_thread?.should be_true
146
+ @zkem.close! { done }
147
+ end
148
+
149
+ d.errback do |exc|
150
+ raise exc
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ it 'should get the data and do a nodejs-style callback' do
157
+ em do
158
+ @zkem.connect do
159
+ @zkem.create(@path, @data) do |exc,created_path|
160
+ exc.should be_nil
161
+ created_path.should == @path
162
+ EM.reactor_thread?.should be_true
163
+ @zkem.close! { done }
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end # non-sequence node
169
+
170
+ describe 'sequence node' do
171
+ it 'should create the node and call the callback' do
172
+ em do
173
+ @zkem.connect do
174
+ d = @zkem.create(@path, @data, :sequence => true)
175
+
176
+ d.callback do |*a|
177
+ logger.debug { "got callback with #{a.inspect}" }
178
+ a.should_not be_empty
179
+ a.first.should =~ /#{@path}\d+$/
180
+ EM.reactor_thread?.should be_true
181
+ @zkem.close! { done }
182
+ end
183
+
184
+ d.errback do |exc|
185
+ raise exc
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end # success
192
+
193
+ describe 'failure' do
194
+ before do
195
+ @path = [@base_path, 'foo'].join('/')
196
+ @zk.create(@path, '')
197
+ end
198
+
199
+ it %[should call the errback in deferred style] do
200
+ em do
201
+ @zkem.connect do
202
+ d = @zkem.create(@path, '')
203
+
204
+ d.callback do
205
+ raise "Should not have been called"
206
+ end
207
+
208
+ d.errback do |exc|
209
+ exc.should be_kind_of(ZK::Exceptions::NodeExists)
210
+ @zkem.close! { done }
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ it %[should have exception as the first argument to the block] do
217
+ em do
218
+ @zkem.connect do
219
+ @zkem.create(@path, '') do |exc,*a|
220
+ exc.should be_kind_of(ZK::Exceptions::NodeExists)
221
+ @zkem.close! { done }
222
+ end
223
+ end
224
+ end
225
+ end
226
+ end # failure
227
+ end # create
228
+
229
+
230
+ describe 'set' do
231
+ describe 'success' do
232
+ before do
233
+ @path = [@base_path, 'foo'].join('/')
234
+ @data = 'this is data'
235
+ @new_data = 'this is better data'
236
+ @zk.create(@path, @data)
237
+ @orig_stat = @zk.stat(@path)
238
+ end
239
+
240
+ it 'should set the data and call the callback' do
241
+ em do
242
+ @zkem.connect do
243
+ dfr = @zkem.set(@path, @new_data)
244
+
245
+ dfr.callback do |stat|
246
+ stat.should be_instance_of(ZookeeperStat::Stat)
247
+ stat.version.should > @orig_stat.version
248
+ EM.reactor_thread?.should be_true
249
+
250
+ @zkem.get(@path) do |_,data|
251
+ data.should == @new_data
252
+ @zkem.close! { done }
253
+ end
254
+ end
255
+
256
+ dfr.errback do |exc|
257
+ raise exc
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ it 'should set the data and do a nodejs-style callback' do
264
+ em do
265
+ @zkem.connect do
266
+ @zkem.set(@path, @new_data) do |exc,stat|
267
+ exc.should be_nil
268
+ stat.should be_instance_of(ZookeeperStat::Stat)
269
+ EM.reactor_thread?.should be_true
270
+
271
+ @zkem.get(@path) do |_,data|
272
+ data.should == @new_data
273
+ @zkem.close! { done }
274
+ end
275
+ end
276
+ end
277
+ end
278
+ end
279
+ end # success
280
+
281
+ describe 'failure' do
282
+ before do
283
+ @path = [@base_path, 'foo'].join('/')
284
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
285
+ end
286
+
287
+ it %[should call the errback in deferred style] do
288
+ em do
289
+ @zkem.connect do
290
+ d = @zkem.set(@path, '')
291
+
292
+ d.callback do
293
+ raise "Should not have been called"
294
+ end
295
+
296
+ d.errback do |exc|
297
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
298
+ @zkem.close! { done }
299
+ end
300
+ end
301
+ end
302
+ end
303
+
304
+ it %[should have NoNode as the first argument to the block] do
305
+ em do
306
+ @zkem.connect do
307
+ @zkem.set(@path, '') do |exc,_|
308
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
309
+ @zkem.close! { done }
310
+ end
311
+ end
312
+ end
313
+ end
314
+ end # failure
315
+ end # set
316
+
317
+ describe 'exists?' do
318
+ before do
319
+ @path = [@base_path, 'foo'].join('/')
320
+ @data = 'this is data'
321
+ end
322
+
323
+ it 'should call the block with true if the node exists' do
324
+ @zk.create(@path, @data)
325
+
326
+ em do
327
+ @zkem.connect do
328
+ dfr = @zkem.exists?(@path)
329
+
330
+ dfr.callback do |bool|
331
+ bool.should be_true
332
+ @zkem.close! { done }
333
+ end
334
+
335
+ dfr.errback do |exc|
336
+ raise exc
337
+ end
338
+ end
339
+ end
340
+ end
341
+
342
+ it 'should call the block with false if the node does not exist' do
343
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
344
+
345
+ em do
346
+ @zkem.connect do
347
+ dfr = @zkem.exists?(@path)
348
+
349
+ dfr.callback do |bool|
350
+ bool.should be_false
351
+ @zkem.close! { done }
352
+ end
353
+
354
+ dfr.errback do |exc|
355
+ raise exc
356
+ end
357
+ end
358
+ end
359
+ end
360
+ end
361
+
362
+ describe 'stat' do
363
+ describe 'success' do
364
+ before do
365
+ @path = [@base_path, 'foo'].join('/')
366
+ @data = 'this is data'
367
+ @zk.create(@path, @data)
368
+ @orig_stat = @zk.stat(@path)
369
+ end
370
+
371
+ it 'should get the stat and call the callback' do
372
+ em do
373
+ @zkem.connect do
374
+ dfr = @zkem.stat(@path)
375
+
376
+ dfr.callback do |stat|
377
+ stat.should_not be_nil
378
+ stat.should == @orig_stat
379
+ stat.should be_instance_of(ZookeeperStat::Stat)
380
+ EM.reactor_thread?.should be_true
381
+ @zkem.close! { done }
382
+ end
383
+
384
+ dfr.errback do |exc|
385
+ raise exc
386
+ end
387
+ end
388
+ end
389
+ end
390
+
391
+ it 'should get the stat and do a nodejs-style callback' do
392
+ em do
393
+ @zkem.connect do
394
+ @zkem.stat(@path) do |exc,stat|
395
+ exc.should be_nil
396
+ stat.should be_instance_of(ZookeeperStat::Stat)
397
+ EM.reactor_thread?.should be_true
398
+ @zkem.close! { done }
399
+ end
400
+ end
401
+ end
402
+ end
403
+ end # success
404
+
405
+ describe 'non-existent node' do
406
+ before do
407
+ @path = [@base_path, 'foo'].join('/')
408
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
409
+ end
410
+
411
+ it %[should not be an error to do stat on a non-existent node] do
412
+ em do
413
+ @zkem.connect do
414
+ dfr = @zkem.stat(@path)
415
+
416
+ dfr.callback do |stat|
417
+ stat.should_not be_nil
418
+ stat.exists?.should be_false
419
+ stat.should be_instance_of(ZookeeperStat::Stat)
420
+ EM.reactor_thread?.should be_true
421
+ @zkem.close! { done }
422
+ end
423
+
424
+ dfr.errback do |exc|
425
+ raise exc
426
+ end
427
+ end
428
+ end
429
+ end
430
+ end # non-existent node
431
+ end # stat
432
+
433
+ describe 'delete' do
434
+ describe 'success' do
435
+ before do
436
+ @path = [@base_path, 'foo'].join('/')
437
+ @data = 'this is data'
438
+ @zk.create(@path, @data)
439
+ end
440
+
441
+ it 'should delete the node and call the callback' do
442
+ em do
443
+ @zkem.connect do
444
+ d = @zkem.delete(@path)
445
+
446
+ d.callback do |*a|
447
+ a.should be_empty
448
+ EM.reactor_thread?.should be_true
449
+ @zkem.close! { done }
450
+ end
451
+
452
+ d.errback do |exc|
453
+ raise exc
454
+ end
455
+ end
456
+ end
457
+ end
458
+
459
+ it 'should delete the znode and do a nodejs-style callback' do
460
+ em do
461
+ @zkem.connect do
462
+ @zkem.delete(@path) do |exc|
463
+ exc.should be_nil
464
+ EM.reactor_thread?.should be_true
465
+ @zkem.close! { done }
466
+ end
467
+ end
468
+ end
469
+ end
470
+ end # success
471
+
472
+ describe 'failure' do
473
+ before do
474
+ @path = [@base_path, 'foo'].join('/')
475
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
476
+ end
477
+
478
+ it %[should call the errback in deferred style] do
479
+ em do
480
+ @zkem.connect do
481
+ d = @zkem.delete(@path)
482
+
483
+ d.callback do
484
+ raise "Should not have been called"
485
+ end
486
+
487
+ d.errback do |exc|
488
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
489
+ @zkem.close! { done }
490
+ end
491
+ end
492
+ end
493
+ end
494
+
495
+ it %[should have NoNode as the first argument to the block] do
496
+ em do
497
+ @zkem.connect do
498
+ @zkem.delete(@path) do |exc,_|
499
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
500
+ @zkem.close! { done }
501
+ end
502
+ end
503
+ end
504
+ end
505
+ end # failure
506
+ end # delete
507
+
508
+ describe 'children' do
509
+ describe 'success' do
510
+ before do
511
+ @path = [@base_path, 'foo'].join('/')
512
+ @child_1_path = [@path, 'child_1'].join('/')
513
+ @child_2_path = [@path, 'child_2'].join('/')
514
+
515
+ @data = 'this is data'
516
+ @zk.create(@path, @data)
517
+ @zk.create(@child_1_path, '')
518
+ @zk.create(@child_2_path, '')
519
+ end
520
+
521
+ it 'should get the children and call the callback' do
522
+ em do
523
+ @zkem.connect do
524
+ d = @zkem.children(@path)
525
+
526
+ d.callback do |children,stat|
527
+ children.should be_kind_of(Array)
528
+ children.length.should == 2
529
+ children.should include('child_1')
530
+ children.should include('child_2')
531
+
532
+ stat.should be_instance_of(ZookeeperStat::Stat)
533
+
534
+ EM.reactor_thread?.should be_true
535
+ @zkem.close! { done }
536
+ end
537
+
538
+ d.errback do |exc|
539
+ raise exc
540
+ end
541
+ end
542
+ end
543
+ end
544
+
545
+ it 'should get the children and do a nodejs-style callback' do
546
+ em do
547
+ @zkem.connect do
548
+ @zkem.children(@path) do |exc, children, stat|
549
+ exc.should be_nil
550
+ children.should be_kind_of(Array)
551
+ children.length.should == 2
552
+ children.should include('child_1')
553
+ children.should include('child_2')
554
+ stat.should be_instance_of(ZookeeperStat::Stat)
555
+ EM.reactor_thread?.should be_true
556
+ @zkem.close! { done }
557
+ end
558
+ end
559
+ end
560
+ end
561
+ end # success
562
+
563
+ describe 'failure' do
564
+ before do
565
+ @path = [@base_path, 'foo'].join('/')
566
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
567
+ end
568
+
569
+ it %[should call the errback in deferred style] do
570
+ em do
571
+ @zkem.connect do
572
+ d = @zkem.children(@path)
573
+
574
+ d.callback do
575
+ raise "Should not have been called"
576
+ end
577
+
578
+ d.errback do |exc|
579
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
580
+ @zkem.close! { done }
581
+ end
582
+ end
583
+ end
584
+ end
585
+
586
+ it %[should have NoNode as the first argument to the block] do
587
+ em do
588
+ @zkem.connect do
589
+ @zkem.children(@path) do |exc,_|
590
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
591
+ @zkem.close! { done }
592
+ end
593
+ end
594
+ end
595
+ end
596
+ end # failure
597
+ end # children
598
+
599
+ describe 'get_acl' do
600
+ describe 'success' do
601
+ before do
602
+ @path = [@base_path, 'foo'].join('/')
603
+ @data = 'this is data'
604
+ @zk.create(@path, @data)
605
+ end
606
+
607
+ it 'should get the data and call the callback' do
608
+ em do
609
+ @zkem.connect do
610
+ dfr = @zkem.get_acl(@path)
611
+
612
+ dfr.callback do |acls,stat|
613
+ acls.should be_kind_of(Array)
614
+ acls.first.should be_kind_of(ZookeeperACLs::ACL)
615
+ stat.should be_instance_of(ZookeeperStat::Stat)
616
+
617
+ EM.reactor_thread?.should be_true
618
+ @zkem.close! { done }
619
+ end
620
+
621
+ dfr.errback do |exc|
622
+ raise exc
623
+ end
624
+ end
625
+ end
626
+ end
627
+
628
+ it 'should get the data and do a nodejs-style callback' do
629
+ em do
630
+ @zkem.connect do
631
+ @zkem.get_acl(@path) do |exc,acls,stat|
632
+ exc.should be_nil
633
+ acls.should be_kind_of(Array)
634
+ acls.first.should be_kind_of(ZookeeperACLs::ACL)
635
+ stat.should be_instance_of(ZookeeperStat::Stat)
636
+ EM.reactor_thread?.should be_true
637
+ @zkem.close! { done }
638
+ end
639
+ end
640
+ end
641
+ end
642
+ end # success
643
+
644
+ describe 'failure' do
645
+ before do
646
+ @path = [@base_path, 'foo'].join('/')
647
+ @zk.delete(@path) rescue ZK::Exceptions::NoNode
648
+ end
649
+
650
+ it %[should call the errback in deferred style] do
651
+ em do
652
+ @zkem.connect do
653
+ d = @zkem.get_acl(@path)
654
+
655
+ d.callback do
656
+ raise "Should not have been called"
657
+ end
658
+
659
+ d.errback do |exc|
660
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
661
+ @zkem.close! { done }
662
+ end
663
+ end
664
+ end
665
+ end
666
+
667
+ it %[should have NoNode as the first argument to the block] do
668
+ em do
669
+ @zkem.connect do
670
+ @zkem.get_acl(@path) do |exc,*a|
671
+ exc.should be_kind_of(ZK::Exceptions::NoNode)
672
+ @zkem.close! { done }
673
+ end
674
+ end
675
+ end
676
+ end
677
+ end # failure
678
+ end # get_acl
679
+
680
+ describe 'set_acl' do
681
+ describe 'success' do
682
+ it 'should set the acl and call the callback'
683
+ it 'should set the acl and do a nodejs-style callback'
684
+ end # success
685
+
686
+ describe 'failure' do
687
+ it %[should call the errback in deferred style]
688
+ it %[should have NoNode as the first argument to the block]
689
+ end # failure
690
+ end # set_acl
691
+ end # Client
692
+ end # ZK::ZKEventMachine
693
+