slyphon-zookeeper 0.3.0-java → 0.8.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1007 @@
1
+ require 'shared/all_success_return_values'
2
+
3
+ shared_examples_for "connection" do
4
+ def ensure_node(path, data)
5
+ if zk.stat(:path => path)[:stat].exists?
6
+ zk.set(:path => path, :data => data)
7
+ else
8
+ zk.create(:path => path, :data => data)
9
+ end
10
+ end
11
+
12
+ before :each do
13
+ ensure_node(path, data)
14
+ end
15
+
16
+ after :each do
17
+ ensure_node(path, data)
18
+ end
19
+
20
+ after :all do
21
+ Zookeeper.logger.warn "running shared examples after :all"
22
+ z = Zookeeper.new("localhost:2181")
23
+ z.delete(:path => path)
24
+ z.close
25
+ end
26
+
27
+ # unfortunately, we can't test w/o exercising other parts of the driver, so
28
+ # if "set" is broken, this test will fail as well (but whaddyagonnado?)
29
+ describe :get do
30
+ describe :sync do
31
+ it_should_behave_like "all success return values"
32
+
33
+ before do
34
+ @rv = zk.get(:path => path)
35
+ end
36
+
37
+ it %[should return the data] do
38
+ @rv[:data].should == data
39
+ end
40
+
41
+ it %[should return a stat] do
42
+ @rv[:stat].should_not be_nil
43
+ @rv[:stat].should be_kind_of(ZookeeperStat::Stat)
44
+ end
45
+ end
46
+
47
+ describe :sync_watch do
48
+ it_should_behave_like "all success return values"
49
+
50
+ before do
51
+ @event = nil
52
+ @watcher = Zookeeper::WatcherCallback.new
53
+
54
+ @rv = zk.get(:path => path, :watcher => @watcher, :watcher_context => path)
55
+ end
56
+
57
+ it %[should return the data] do
58
+ @rv[:data].should == data
59
+ end
60
+
61
+ it %[should set a watcher on the node] do
62
+ # test the watcher by changing node data
63
+ zk.set(:path => path, :data => 'blah')[:rc].should be_zero
64
+
65
+ wait_until(1.0) { @watcher.completed? }
66
+
67
+ @watcher.path.should == path
68
+ @watcher.context.should == path
69
+ @watcher.should be_completed
70
+ @watcher.type.should == Zookeeper::ZOO_CHANGED_EVENT
71
+ end
72
+ end
73
+
74
+ describe :async do
75
+ before do
76
+ @cb = Zookeeper::DataCallback.new
77
+
78
+ @rv = zk.get(:path => path, :callback => @cb, :callback_context => path)
79
+ wait_until(1.0) { @cb.completed? }
80
+ @cb.should be_completed
81
+ end
82
+
83
+ it_should_behave_like "all success return values"
84
+
85
+ it %[should have a return code of ZOK] do
86
+ @cb.return_code.should == Zookeeper::ZOK
87
+ end
88
+
89
+ it %[should have the stat object in the callback] do
90
+ @cb.stat.should_not be_nil
91
+ @cb.stat.should be_kind_of(ZookeeperStat::Stat)
92
+ end
93
+
94
+ it %[should have the data] do
95
+ @cb.data.should == data
96
+ end
97
+ end
98
+
99
+ describe :async_watch do
100
+ it_should_behave_like "all success return values"
101
+
102
+ before do
103
+ @cb = Zookeeper::DataCallback.new
104
+ @watcher = Zookeeper::WatcherCallback.new
105
+
106
+ @rv = zk.get(:path => path, :callback => @cb, :callback_context => path, :watcher => @watcher, :watcher_context => path)
107
+ wait_until(1.0) { @cb.completed? }
108
+ @cb.should be_completed
109
+ end
110
+
111
+ it %[should have the stat object in the callback] do
112
+ @cb.stat.should_not be_nil
113
+ @cb.stat.should be_kind_of(ZookeeperStat::Stat)
114
+ end
115
+
116
+ it %[should have the data] do
117
+ @cb.data.should == data
118
+ end
119
+
120
+ it %[should have a return code of ZOK] do
121
+ @cb.return_code.should == Zookeeper::ZOK
122
+ end
123
+
124
+ it %[should set a watcher on the node] do
125
+ zk.set(:path => path, :data => 'blah')[:rc].should be_zero
126
+
127
+ wait_until(2) { @watcher.completed? }
128
+
129
+ @watcher.should be_completed
130
+
131
+ @watcher.path.should == path
132
+ @watcher.context.should == path
133
+ end
134
+ end
135
+
136
+ describe 'bad arguments' do
137
+ it %[should barf with a BadArguments error] do
138
+ lambda { zk.get(:bad_arg => 'what!?') }.should raise_error(ZookeeperExceptions::ZookeeperException::BadArguments)
139
+ end
140
+ end
141
+ end # get
142
+
143
+ describe :set do
144
+ before do
145
+ @new_data = "Four score and \007 years ago"
146
+ @stat = zk.stat(:path => path)[:stat]
147
+ end
148
+
149
+ describe :sync do
150
+ describe 'without version' do
151
+ it_should_behave_like "all success return values"
152
+
153
+ before do
154
+ @rv = zk.set(:path => path, :data => @new_data)
155
+ end
156
+
157
+ it %[should return the new stat] do
158
+ @rv[:stat].should_not be_nil
159
+ @rv[:stat].should be_kind_of(ZookeeperStat::Stat)
160
+ @rv[:stat].version.should > @stat.version
161
+ end
162
+ end
163
+
164
+ describe 'with current version' do
165
+ it_should_behave_like "all success return values"
166
+
167
+ before do
168
+ @rv = zk.set(:path => path, :data => @new_data, :version => @stat.version)
169
+ end
170
+
171
+ it %[should return the new stat] do
172
+ @rv[:stat].should_not be_nil
173
+ @rv[:stat].should be_kind_of(ZookeeperStat::Stat)
174
+ @rv[:stat].version.should > @stat.version
175
+ end
176
+ end
177
+
178
+ describe 'with outdated version' do
179
+ before do
180
+ # need to do a couple of sets to ramp up the version
181
+ 3.times { |n| @stat = zk.set(:path => path, :data => "#{@new_data}#{n}")[:stat] }
182
+
183
+ @rv = zk.set(:path => path, :data => @new_data, :version => 0)
184
+ end
185
+
186
+ it %[should have a return code of ZBADVERSION] do
187
+ @rv[:rc].should == Zookeeper::ZBADVERSION
188
+ end
189
+
190
+ it %[should return a stat with !exists] do
191
+ @rv[:stat].exists.should be_false
192
+ end
193
+ end
194
+
195
+ describe 'error' do
196
+ it %[should barf if the data size is too large], :input_size => true do
197
+ large_data = '0' * (1024 ** 2)
198
+
199
+ lambda { zk.set(:path => path, :data => large_data) }.should raise_error(ZookeeperExceptions::ZookeeperException::DataTooLargeException)
200
+ end
201
+ end
202
+ end # sync
203
+
204
+ describe :async do
205
+ before do
206
+ @cb = Zookeeper::StatCallback.new
207
+ end
208
+
209
+ describe 'without version' do
210
+ it_should_behave_like "all success return values"
211
+
212
+ before do
213
+ @rv = zk.set(:path => path, :data => @new_data, :callback => @cb, :callback_context => path)
214
+
215
+ wait_until(2) { @cb.completed? }
216
+ @cb.should be_completed
217
+ end
218
+
219
+ it %[should have the stat in the callback] do
220
+ @cb.stat.should_not be_nil
221
+ @cb.stat.version.should > @stat.version
222
+ end
223
+
224
+ it %[should have a return code of ZOK] do
225
+ @cb.return_code.should == Zookeeper::ZOK
226
+ end
227
+ end
228
+
229
+ describe 'with current version' do
230
+ it_should_behave_like "all success return values"
231
+
232
+ before do
233
+ @rv = zk.set(:path => path, :data => @new_data, :callback => @cb, :callback_context => path, :version => @stat.version)
234
+
235
+ wait_until(2) { @cb.completed? }
236
+ @cb.should be_completed
237
+ end
238
+
239
+ it %[should have the stat in the callback] do
240
+ @cb.stat.should_not be_nil
241
+ @cb.stat.version.should > @stat.version
242
+ end
243
+
244
+ it %[should have a return code of ZOK] do
245
+ @cb.return_code.should == Zookeeper::ZOK
246
+ end
247
+ end
248
+
249
+ describe 'with outdated version' do
250
+ before do
251
+ # need to do a couple of sets to ramp up the version
252
+ 3.times { |n| @stat = zk.set(:path => path, :data => "#{@new_data}#{n}")[:stat] }
253
+
254
+ @rv = zk.set(:path => path, :data => @new_data, :callback => @cb, :callback_context => path, :version => 0)
255
+
256
+ wait_until(2) { @cb.completed? }
257
+ @cb.should be_completed
258
+ end
259
+
260
+ it %[should have a return code of ZBADVERSION] do
261
+ @cb.return_code.should == Zookeeper::ZBADVERSION
262
+ end
263
+
264
+ it %[should return a stat with !exists] do
265
+ @cb.stat.exists.should be_false
266
+ end
267
+ end
268
+
269
+ describe 'error' do
270
+ it %[should barf if the data size is too large], :input_size => true do
271
+ large_data = '0' * (1024 ** 2)
272
+
273
+ lambda { zk.set(:path => path, :data => large_data, :callback => @cb, :callback_context => path) }.should raise_error(ZookeeperExceptions::ZookeeperException::DataTooLargeException)
274
+ end
275
+ end
276
+
277
+ end # async
278
+ end # set
279
+
280
+ describe :get_children do
281
+ before do
282
+ @children = %w[child0 child1 child2]
283
+
284
+ @children.each do |name|
285
+ zk.create(:path => "#{path}/#{name}", :data => name)
286
+ end
287
+ end
288
+
289
+ after do
290
+ @children.each do |name|
291
+ zk.delete(:path => "#{path}/#{name}")
292
+ end
293
+ end
294
+
295
+ describe :sync do
296
+ it_should_behave_like "all success return values"
297
+
298
+ before do
299
+ @rv = zk.get_children(:path => path)
300
+ end
301
+
302
+ it %[should have an array of names of the children] do
303
+ @rv[:children].should be_kind_of(Array)
304
+ @rv[:children].length.should == 3
305
+ @rv[:children].sort.should == @children.sort
306
+ end
307
+
308
+ # "Three shall be the number of the counting, and the number of the counting shall be 3"
309
+
310
+ it %[should have a stat object whose num_children is 3] do
311
+ @rv[:stat].should_not be_nil
312
+ @rv[:stat].should be_kind_of(ZookeeperStat::Stat)
313
+ @rv[:stat].num_children.should == 3
314
+ end
315
+ end
316
+
317
+ describe :sync_watch do
318
+ it_should_behave_like "all success return values"
319
+
320
+ before do
321
+ @addtl_child = 'child3'
322
+
323
+ @watcher = Zookeeper::WatcherCallback.new
324
+
325
+ @rv = zk.get_children(:path => path, :watcher => @watcher, :watcher_context => path)
326
+ end
327
+
328
+ after do
329
+ zk.delete(:path => "#{path}/#{@addtl_child}")
330
+ end
331
+
332
+ it %[should have an array of names of the children] do
333
+ @rv[:children].should be_kind_of(Array)
334
+ @rv[:children].length.should == 3
335
+ @rv[:children].sort.should == @children.sort
336
+ end
337
+
338
+ it %[should have a stat object whose num_children is 3] do
339
+ @rv[:stat].should_not be_nil
340
+ @rv[:stat].should be_kind_of(ZookeeperStat::Stat)
341
+ @rv[:stat].num_children.should == 3
342
+ end
343
+
344
+ it %[should set a watcher for children on the node] do
345
+ @watcher.should_not be_completed
346
+
347
+ zk.create(:path => "#{path}/#{@addtl_child}", :data => '')[:rc].should == Zookeeper::ZOK
348
+
349
+ wait_until { @watcher.completed? }
350
+ @watcher.should be_completed
351
+
352
+ @watcher.path.should == path
353
+ @watcher.context.should == path
354
+ @watcher.type.should == Zookeeper::ZOO_CHILD_EVENT
355
+ end
356
+ end
357
+
358
+ describe :async do
359
+ it_should_behave_like "all success return values"
360
+
361
+ before do
362
+ @cb = ZookeeperCallbacks::StringsCallback.new
363
+ @rv = zk.get_children(:path => path, :callback => @cb, :callback_context => path)
364
+
365
+ wait_until { @cb.completed? }
366
+ @cb.should be_completed
367
+ end
368
+
369
+ it %[should succeed] do
370
+ @cb.return_code.should == Zookeeper::ZOK
371
+ end
372
+
373
+ it %[should return an array of children] do
374
+ @cb.children.should be_kind_of(Array)
375
+ @cb.children.length.should == 3
376
+ @cb.children.sort.should == @children.sort
377
+ end
378
+
379
+ it %[should have a stat object whose num_children is 3] do
380
+ @cb.stat.should_not be_nil
381
+ @cb.stat.should be_kind_of(ZookeeperStat::Stat)
382
+ @cb.stat.num_children.should == 3
383
+ end
384
+ end
385
+
386
+ describe :async_watch do
387
+ it_should_behave_like "all success return values"
388
+
389
+ before do
390
+ @addtl_child = 'child3'
391
+
392
+ @watcher = Zookeeper::WatcherCallback.new
393
+ @cb = ZookeeperCallbacks::StringsCallback.new
394
+
395
+ @rv = zk.get_children(:path => path, :watcher => @watcher, :watcher_context => path, :callback => @cb, :callback_context => path)
396
+ wait_until { @cb.completed? }
397
+ @cb.should be_completed
398
+ end
399
+
400
+ after do
401
+ zk.delete(:path => "#{path}/#{@addtl_child}")
402
+ end
403
+
404
+ it %[should succeed] do
405
+ @cb.return_code.should == Zookeeper::ZOK
406
+ end
407
+
408
+ it %[should return an array of children] do
409
+ @cb.children.should be_kind_of(Array)
410
+ @cb.children.length.should == 3
411
+ @cb.children.sort.should == @children.sort
412
+ end
413
+
414
+ it %[should have a stat object whose num_children is 3] do
415
+ @cb.stat.should_not be_nil
416
+ @cb.stat.should be_kind_of(ZookeeperStat::Stat)
417
+ @cb.stat.num_children.should == 3
418
+ end
419
+
420
+ it %[should set a watcher for children on the node] do
421
+ @watcher.should_not be_completed
422
+
423
+ zk.create(:path => "#{path}/#{@addtl_child}", :data => '')[:rc].should == Zookeeper::ZOK
424
+
425
+ wait_until { @watcher.completed? }
426
+ @watcher.should be_completed
427
+
428
+ @watcher.path.should == path
429
+ @watcher.context.should == path
430
+ @watcher.type.should == Zookeeper::ZOO_CHILD_EVENT
431
+ end
432
+ end
433
+ end
434
+
435
+ # NOTE: the jruby version of stat on non-existent node will have a
436
+ # return_code of 0, but the C version will have a return_code of -101
437
+ describe :stat do
438
+ describe :sync do
439
+ it_should_behave_like "all success return values"
440
+
441
+ before do
442
+ @rv = zk.stat(:path => path)
443
+ end
444
+
445
+ it %[should have a stat object] do
446
+ @rv[:stat].should be_kind_of(Zookeeper::Stat)
447
+ end
448
+ end
449
+
450
+ describe :sync_watch do
451
+ it_should_behave_like "all success return values"
452
+
453
+ before do
454
+ @watcher = Zookeeper::WatcherCallback.new
455
+
456
+ @rv = zk.stat(:path => path, :watcher => @watcher, :watcher_context => path)
457
+ end
458
+
459
+ it %[should have a stat object] do
460
+ @rv[:stat].should be_kind_of(Zookeeper::Stat)
461
+ end
462
+
463
+ it %[should set a watcher for data changes on the node] do
464
+ @watcher.should_not be_completed
465
+
466
+ zk.set(:path => path, :data => 'skunk')[:rc].should == Zookeeper::ZOK
467
+
468
+ wait_until { @watcher.completed? }
469
+ @watcher.should be_completed
470
+
471
+ @watcher.path.should == path
472
+ @watcher.context.should == path
473
+ @watcher.type.should == Zookeeper::ZOO_CHANGED_EVENT
474
+ end
475
+ end
476
+
477
+ describe :async do
478
+ it_should_behave_like "all success return values"
479
+
480
+ before do
481
+ @cb = ZookeeperCallbacks::StatCallback.new
482
+ @rv = zk.stat(:path => path, :callback => @cb, :callback_context => path)
483
+
484
+ wait_until { @cb.completed? }
485
+ @cb.should be_completed
486
+ end
487
+
488
+ it %[should succeed] do
489
+ @cb.return_code.should == Zookeeper::ZOK
490
+ end
491
+
492
+ it %[should have a stat object] do
493
+ @cb.stat.should be_kind_of(Zookeeper::Stat)
494
+ end
495
+ end
496
+
497
+ describe :async_watch do
498
+ it_should_behave_like "all success return values"
499
+
500
+ before do
501
+ @addtl_child = 'child3'
502
+
503
+ @watcher = Zookeeper::WatcherCallback.new
504
+
505
+ @cb = ZookeeperCallbacks::StatCallback.new
506
+ @rv = zk.stat(:path => path, :callback => @cb, :callback_context => path, :watcher => @watcher, :watcher_context => path)
507
+
508
+ wait_until { @cb.completed? }
509
+ @cb.should be_completed
510
+ end
511
+
512
+ after do
513
+ zk.delete(:path => "#{path}/#{@addtl_child}")
514
+ end
515
+
516
+ it %[should succeed] do
517
+ @cb.return_code.should == Zookeeper::ZOK
518
+ end
519
+
520
+ it %[should have a stat object] do
521
+ @cb.stat.should be_kind_of(Zookeeper::Stat)
522
+ end
523
+
524
+ it %[should set a watcher for data changes on the node] do
525
+ @watcher.should_not be_completed
526
+
527
+ zk.set(:path => path, :data => 'skunk')[:rc].should == Zookeeper::ZOK
528
+
529
+ wait_until { @watcher.completed? }
530
+ @watcher.should be_completed
531
+
532
+ @watcher.path.should == path
533
+ @watcher.context.should == path
534
+ @watcher.type.should == Zookeeper::ZOO_CHANGED_EVENT
535
+ end
536
+ end
537
+ end # stat
538
+
539
+ describe :create do
540
+ before do
541
+ # remove the path set up by the global 'before' block
542
+ zk.delete(:path => path)
543
+ end
544
+
545
+ describe :sync do
546
+ describe 'error' do
547
+ it %[should barf if the data size is too large], :input_size => true do
548
+ large_data = '0' * (1024 ** 2)
549
+
550
+ lambda { zk.create(:path => path, :data => large_data) }.should raise_error(ZookeeperExceptions::ZookeeperException::DataTooLargeException)
551
+ end
552
+ end
553
+
554
+ describe :default_flags do
555
+ it_should_behave_like "all success return values"
556
+
557
+ before do
558
+ @rv = zk.create(:path => path)
559
+ end
560
+
561
+ it %[should return the path that was set] do
562
+ @rv[:path].should == path
563
+ end
564
+
565
+ it %[should have created a permanent node] do
566
+ st = zk.stat(:path => path)
567
+ st[:rc].should == Zookeeper::ZOK
568
+
569
+ st[:stat].ephemeral_owner.should == 0
570
+ end
571
+ end
572
+
573
+ describe :ephemeral do
574
+ it_should_behave_like "all success return values"
575
+
576
+ before do
577
+ @rv = zk.create(:path => path, :ephemeral => true)
578
+ end
579
+
580
+ it %[should return the path that was set] do
581
+ @rv[:path].should == path
582
+ end
583
+
584
+ it %[should have created a ephemeral node] do
585
+ st = zk.stat(:path => path)
586
+ st[:rc].should == Zookeeper::ZOK
587
+
588
+ st[:stat].ephemeral_owner.should_not be_zero
589
+ end
590
+ end
591
+
592
+ describe :sequence do
593
+ it_should_behave_like "all success return values"
594
+
595
+ before do
596
+ @orig_path = path
597
+ @rv = zk.create(:path => path, :sequence => true)
598
+ @s_path = @rv[:path] # make sure this gets cleaned up
599
+ end
600
+
601
+ after do
602
+ zk.delete(:path => @s_path)
603
+ end
604
+
605
+ it %[should return the path that was set] do
606
+ @rv[:path].should_not == @orig_path
607
+ end
608
+
609
+ it %[should have created a permanent node] do
610
+ st = zk.stat(:path => @s_path)
611
+ st[:rc].should == Zookeeper::ZOK
612
+
613
+ st[:stat].ephemeral_owner.should be_zero
614
+ end
615
+ end
616
+
617
+ describe :ephemeral_sequence do
618
+ it_should_behave_like "all success return values"
619
+
620
+ before do
621
+ @orig_path = path
622
+ @rv = zk.create(:path => path, :sequence => true, :ephemeral => true)
623
+ @s_path = @rv[:path] # make sure this gets cleaned up
624
+ end
625
+
626
+ after do
627
+ zk.delete(:path => @s_path)
628
+ end
629
+
630
+ it %[should return the path that was set] do
631
+ @rv[:path].should_not == @orig_path
632
+ end
633
+
634
+ it %[should have created an ephemeral node] do
635
+ st = zk.stat(:path => @s_path)
636
+ st[:rc].should == Zookeeper::ZOK
637
+
638
+ st[:stat].ephemeral_owner.should_not be_zero
639
+ end
640
+ end
641
+
642
+ describe :acl do
643
+ it %[should work] do
644
+ pending "need to write acl tests"
645
+ end
646
+ end
647
+ end
648
+
649
+ describe :async do
650
+ before do
651
+ @cb = ZookeeperCallbacks::StringCallback.new
652
+ end
653
+
654
+ describe :default_flags do
655
+ it_should_behave_like "all success return values"
656
+
657
+ before do
658
+ @rv = zk.create(:path => path, :callback => @cb, :callback_context => path)
659
+ wait_until(2) { @cb.completed? }
660
+ @cb.should be_completed
661
+ end
662
+
663
+ it %[should have a path] do
664
+ @cb.path.should_not be_nil
665
+ end
666
+
667
+ it %[should return the path that was set] do
668
+ @cb.path.should == path
669
+ end
670
+
671
+ it %[should have created a permanent node] do
672
+ st = zk.stat(:path => path)
673
+ st[:rc].should == Zookeeper::ZOK
674
+
675
+ st[:stat].ephemeral_owner.should == 0
676
+ end
677
+ end
678
+
679
+ describe 'error' do
680
+ it %[should barf if the data size is too large], :input_size => true do
681
+ large_data = '0' * (1024 ** 2)
682
+
683
+ lambda do
684
+ zk.create(:path => path, :data => large_data, :callback => @cb, :callback_context => path)
685
+ end.should raise_error(ZookeeperExceptions::ZookeeperException::DataTooLargeException)
686
+ end
687
+ end
688
+
689
+
690
+ describe :ephemeral do
691
+ it_should_behave_like "all success return values"
692
+
693
+ before do
694
+ @rv = zk.create(:path => path, :ephemeral => true, :callback => @cb, :callback_context => path)
695
+ wait_until(2) { @cb.completed? }
696
+ @cb.should be_completed
697
+ end
698
+
699
+ it %[should have a path] do
700
+ @cb.path.should_not be_nil
701
+ end
702
+
703
+ it %[should return the path that was set] do
704
+ @cb.path.should == path
705
+ end
706
+
707
+ it %[should have created a ephemeral node] do
708
+ st = zk.stat(:path => path)
709
+ st[:rc].should == Zookeeper::ZOK
710
+
711
+ st[:stat].ephemeral_owner.should_not be_zero
712
+ end
713
+ end
714
+
715
+ describe :sequence do
716
+ it_should_behave_like "all success return values"
717
+
718
+ before do
719
+ @orig_path = path
720
+ @rv = zk.create(:path => path, :sequence => true, :callback => @cb, :callback_context => path)
721
+
722
+ wait_until(2) { @cb.completed? }
723
+ @cb.should be_completed
724
+
725
+ @s_path = @cb.path
726
+ end
727
+
728
+ after do
729
+ zk.delete(:path => @s_path)
730
+ end
731
+
732
+ it %[should have a path] do
733
+ @cb.path.should_not be_nil
734
+ end
735
+
736
+ it %[should return the path that was set] do
737
+ @cb.path.should_not == @orig_path
738
+ end
739
+
740
+ it %[should have created a permanent node] do
741
+ st = zk.stat(:path => @s_path)
742
+ st[:rc].should == Zookeeper::ZOK
743
+
744
+ st[:stat].ephemeral_owner.should be_zero
745
+ end
746
+ end
747
+
748
+ describe :ephemeral_sequence do
749
+ it_should_behave_like "all success return values"
750
+
751
+ before do
752
+ @orig_path = path
753
+ @rv = zk.create(:path => path, :sequence => true, :ephemeral => true, :callback => @cb, :callback_context => path)
754
+ path = @rv[:path] # make sure this gets cleaned up
755
+
756
+ wait_until(2) { @cb.completed? }
757
+ @cb.should be_completed
758
+ @s_path = @cb.path
759
+ end
760
+
761
+ after do
762
+ zk.delete(:path => @s_path)
763
+ end
764
+
765
+ it %[should have a path] do
766
+ @cb.path.should_not be_nil
767
+ end
768
+
769
+ it %[should return the path that was set] do
770
+ @s_path.should_not == @orig_path
771
+ end
772
+
773
+ it %[should have created an ephemeral node] do
774
+ st = zk.stat(:path => @s_path)
775
+ st[:rc].should == Zookeeper::ZOK
776
+
777
+ st[:stat].ephemeral_owner.should_not be_zero
778
+ end
779
+ end # ephemeral_sequence
780
+ end # async
781
+ end # create
782
+
783
+ describe :delete do
784
+ describe :sync do
785
+ describe 'without version' do
786
+ it_should_behave_like "all success return values"
787
+
788
+ before do
789
+ zk.create(:path => path)
790
+ @rv = zk.delete(:path => path)
791
+ end
792
+
793
+ it %[should have deleted the node] do
794
+ zk.stat(:path => path)[:stat].exists.should be_false
795
+ end
796
+ end
797
+
798
+ describe 'with current version' do
799
+ it_should_behave_like "all success return values"
800
+
801
+ before do
802
+ zk.create(:path => path)
803
+
804
+ @stat = zk.stat(:path => path)[:stat]
805
+ @stat.exists.should be_true
806
+
807
+ @rv = zk.delete(:path => path, :version => @stat.version)
808
+ end
809
+
810
+ it %[should have deleted the node] do
811
+ zk.stat(:path => path)[:stat].exists.should be_false
812
+ end
813
+ end
814
+
815
+ describe 'with old version' do
816
+ before do
817
+ 3.times { |n| @stat = zk.set(:path => path, :data => n.to_s)[:stat] }
818
+
819
+ @rv = zk.delete(:path => path, :version => 0)
820
+ end
821
+
822
+ it %[should have a return code of ZBADVERSION] do
823
+ @rv[:rc].should == Zookeeper::ZBADVERSION
824
+ end
825
+ end
826
+ end # sync
827
+
828
+ describe :async do
829
+ before do
830
+ @cb = ZookeeperCallbacks::VoidCallback.new
831
+ end
832
+
833
+ describe 'without version' do
834
+ it_should_behave_like "all success return values"
835
+
836
+ before do
837
+ @rv = zk.delete(:path => path, :callback => @cb, :callback_context => path)
838
+ wait_until { @cb.completed? }
839
+ @cb.should be_completed
840
+ end
841
+
842
+ it %[should have a success return_code] do
843
+ @cb.return_code.should == Zookeeper::ZOK
844
+ end
845
+
846
+ it %[should have deleted the node] do
847
+ zk.stat(:path => path)[:stat].exists.should be_false
848
+ end
849
+ end
850
+
851
+ describe 'with current version' do
852
+ it_should_behave_like "all success return values"
853
+
854
+ before do
855
+ @stat = zk.stat(:path => path)[:stat]
856
+ @rv = zk.delete(:path => path, :version => @stat.version, :callback => @cb, :callback_context => path)
857
+ wait_until { @cb.completed? }
858
+ @cb.should be_completed
859
+ end
860
+
861
+ it %[should have a success return_code] do
862
+ @cb.return_code.should == Zookeeper::ZOK
863
+ end
864
+
865
+ it %[should have deleted the node] do
866
+ zk.stat(:path => path)[:stat].exists.should be_false
867
+ end
868
+ end
869
+
870
+ describe 'with old version' do
871
+ before do
872
+ 3.times { |n| @stat = zk.set(:path => path, :data => n.to_s)[:stat] }
873
+
874
+ @rv = zk.delete(:path => path, :version => 0, :callback => @cb, :callback_context => path)
875
+ wait_until { @cb.completed? }
876
+ @cb.should be_completed
877
+ end
878
+
879
+ it %[should have a return code of ZBADVERSION] do
880
+ @cb.return_code.should == Zookeeper::ZBADVERSION
881
+ end
882
+ end
883
+ end # async
884
+ end # delete
885
+
886
+ describe :get_acl do
887
+ describe :sync do
888
+ it_should_behave_like "all success return values"
889
+
890
+ before do
891
+ @rv = zk.get_acl(:path => path)
892
+ end
893
+
894
+ it %[should return a stat for the path] do
895
+ @rv[:stat].should be_kind_of(ZookeeperStat::Stat)
896
+ end
897
+
898
+ it %[should return the acls] do
899
+ acls = @rv[:acl]
900
+ acls.should be_kind_of(Array)
901
+ h = acls.first
902
+
903
+ h.should be_kind_of(Hash)
904
+
905
+ h[:perms].should == Zookeeper::ZOO_PERM_ALL
906
+ h[:id][:scheme].should == 'world'
907
+ h[:id][:id].should == 'anyone'
908
+ end
909
+ end
910
+
911
+ describe :async do
912
+ it_should_behave_like "all success return values"
913
+
914
+ before do
915
+ @cb = Zookeeper::ACLCallback.new
916
+ @rv = zk.get_acl(:path => path, :callback => @cb, :callback_context => path)
917
+
918
+ wait_until(2) { @cb.completed? }
919
+ @cb.should be_completed
920
+ end
921
+
922
+ it %[should return a stat for the path] do
923
+ @cb.stat.should be_kind_of(ZookeeperStat::Stat)
924
+ end
925
+
926
+ it %[should return the acls] do
927
+ acls = @cb.acl
928
+ acls.should be_kind_of(Array)
929
+
930
+ acl = acls.first
931
+ acl.should be_kind_of(ZookeeperACLs::ACL)
932
+
933
+ acl.perms.should == Zookeeper::ZOO_PERM_ALL
934
+
935
+ acl.id.scheme.should == 'world'
936
+ acl.id.id.should == 'anyone'
937
+ end
938
+ end
939
+ end
940
+
941
+ describe :set_acl do
942
+ before do
943
+ @perms = 5
944
+ @new_acl = [ZookeeperACLs::ACL.new(:perms => @perms, :id => ZookeeperACLs::ZOO_ANYONE_ID_UNSAFE)]
945
+ pending("No idea how to set ACLs")
946
+ end
947
+
948
+ describe :sync do
949
+ it_should_behave_like "all success return values"
950
+
951
+ before do
952
+ @rv = zk.set_acl(:path => path, :acl => @new_acl)
953
+ end
954
+ end
955
+ end
956
+
957
+ describe :session_id do
958
+ it %[should return the session_id as a Fixnum] do
959
+ zk.session_id.should be_kind_of(Fixnum)
960
+ end
961
+ end
962
+
963
+ describe :session_passwd do
964
+ it %[should return the session passwd as a String] do
965
+ zk.session_passwd.should be_kind_of(String)
966
+ end
967
+ end
968
+
969
+ describe :sync do
970
+ describe :success do
971
+ it_should_behave_like "all success return values"
972
+
973
+ before do
974
+ @cb = Zookeeper::StringCallback.new
975
+ @rv = zk.sync(:path => path, :callback => @cb)
976
+
977
+ wait_until(2) { @cb.completed }
978
+ @cb.should be_completed
979
+ end
980
+ end
981
+
982
+ describe :errors do
983
+ it %[should barf with BadArguments if :callback is not given] do
984
+ lambda { zk.sync(:path => path) }.should raise_error(ZookeeperExceptions::ZookeeperException::BadArguments)
985
+ end
986
+ end
987
+ end
988
+
989
+ describe :event_dispatch_thread? do
990
+ it %[should return true when called on the event dispatching thread] do
991
+ @result = nil
992
+
993
+ cb = lambda do |hash|
994
+ @result = zk.event_dispatch_thread?
995
+ end
996
+
997
+ @rv = zk.sync(:path => path, :callback => cb)
998
+
999
+ wait_until(2) { @result == true }.should be_true
1000
+ end
1001
+
1002
+ it %[should return false when not on the event dispatching thread] do
1003
+ zk.event_dispatch_thread?.should_not be_true
1004
+ end
1005
+ end
1006
+ end
1007
+