zookeeper 0.9.4-java → 1.0.0.beta.1-java
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.
- data/.dotfiles/rvmrc +1 -0
- data/.gitignore +3 -0
- data/.gitmodules +3 -0
- data/.travis.yml +22 -0
- data/CHANGELOG +38 -5
- data/Gemfile +18 -1
- data/README.markdown +2 -0
- data/Rakefile +47 -109
- data/ext/c_zookeeper.rb +10 -6
- data/ext/zookeeper_base.rb +23 -11
- data/ext/zookeeper_c.c +14 -10
- data/java/{zookeeper_base.rb → java_base.rb} +13 -12
- data/lib/zookeeper/acls.rb +17 -13
- data/lib/zookeeper/callbacks.rb +28 -11
- data/lib/zookeeper/client.rb +30 -0
- data/lib/zookeeper/client_methods.rb +241 -0
- data/lib/zookeeper/common/queue_with_pipe.rb +3 -7
- data/lib/zookeeper/common.rb +13 -12
- data/lib/zookeeper/compatibility.rb +135 -0
- data/lib/zookeeper/constants.rb +35 -1
- data/lib/zookeeper/em_client.rb +1 -1
- data/lib/zookeeper/exceptions.rb +117 -93
- data/lib/zookeeper/rake_tasks.rb +165 -0
- data/lib/zookeeper/stat.rb +16 -16
- data/lib/zookeeper/version.rb +2 -4
- data/lib/zookeeper.rb +32 -244
- data/scripts/upgrade-1.0-sed-alike.rb +46 -0
- data/spec/c_zookeeper_spec.rb +10 -9
- data/spec/chrooted_connection_spec.rb +2 -2
- data/spec/default_watcher_spec.rb +4 -4
- data/spec/em_spec.rb +1 -1
- data/spec/shared/connection_examples.rb +52 -37
- data/spec/spec_helper.rb +22 -84
- data/spec/support/00_spawn_zookeeper.rb +20 -0
- data/spec/support/zookeeper_spec_helpers.rb +84 -0
- data/spec/zookeeper_spec.rb +1 -1
- data/zookeeper.gemspec +9 -12
- metadata +50 -36
- data/examples/cloud_config.rb +0 -125
- data/test/test_basic.rb +0 -37
- data/test/test_callback1.rb +0 -36
- data/test/test_close.rb +0 -16
- data/test/test_esoteric.rb +0 -7
- data/test/test_watcher1.rb +0 -56
- data/test/test_watcher2.rb +0 -52
metadata
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zookeeper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 1547489243
|
5
|
+
prerelease: 6
|
6
6
|
segments:
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
|
9
|
+
- 0
|
10
|
+
- beta
|
11
|
+
- 1
|
12
|
+
version: 1.0.0.beta.1
|
11
13
|
platform: java
|
12
14
|
authors:
|
13
15
|
- Phillip Pearson
|
@@ -23,9 +25,25 @@ cert_chain: []
|
|
23
25
|
date: 2012-05-07 00:00:00 Z
|
24
26
|
dependencies:
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
28
|
+
name: backports
|
27
29
|
prerelease: false
|
28
30
|
requirement: &id001 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
hash: 25
|
36
|
+
segments:
|
37
|
+
- 2
|
38
|
+
- 5
|
39
|
+
- 1
|
40
|
+
version: 2.5.1
|
41
|
+
type: :runtime
|
42
|
+
version_requirements: *id001
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: slyphon-log4j
|
45
|
+
prerelease: false
|
46
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
47
|
none: false
|
30
48
|
requirements:
|
31
49
|
- - "="
|
@@ -37,11 +55,11 @@ dependencies:
|
|
37
55
|
- 15
|
38
56
|
version: 1.2.15
|
39
57
|
type: :runtime
|
40
|
-
version_requirements: *
|
58
|
+
version_requirements: *id002
|
41
59
|
- !ruby/object:Gem::Dependency
|
42
60
|
name: slyphon-zookeeper_jar
|
43
61
|
prerelease: false
|
44
|
-
requirement: &
|
62
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
45
63
|
none: false
|
46
64
|
requirements:
|
47
65
|
- - "="
|
@@ -53,16 +71,11 @@ dependencies:
|
|
53
71
|
- 5
|
54
72
|
version: 3.3.5
|
55
73
|
type: :runtime
|
56
|
-
version_requirements: *
|
74
|
+
version_requirements: *id003
|
57
75
|
description: |+
|
58
|
-
A low-level multi-Ruby wrapper around the ZooKeeper API bindings.
|
59
|
-
|
60
|
-
|
61
|
-
Currently supported:
|
62
|
-
|
63
|
-
MRI: 1.8.7, 1.9.2, 1.9.3
|
64
|
-
JRuby: ~> 1.6.7
|
65
|
-
Rubinius: 2.0.testing
|
76
|
+
A low-level multi-Ruby wrapper around the ZooKeeper API bindings. For a
|
77
|
+
friendlier interface, see http://github.com/slyphon/zk. Currently supported:
|
78
|
+
MRI: {1.8.7, 1.9.2, 1.9.3}, JRuby: ~> 1.6.7, Rubinius: 2.0.testing, REE 1.8.7.
|
66
79
|
|
67
80
|
This library uses version 3.3.5 of zookeeper bindings.
|
68
81
|
|
@@ -75,14 +88,16 @@ extensions: []
|
|
75
88
|
extra_rdoc_files: []
|
76
89
|
|
77
90
|
files:
|
91
|
+
- .dotfiles/rvmrc
|
78
92
|
- .gitignore
|
93
|
+
- .gitmodules
|
94
|
+
- .travis.yml
|
79
95
|
- CHANGELOG
|
80
96
|
- Gemfile
|
81
97
|
- LICENSE
|
82
98
|
- Manifest
|
83
99
|
- README.markdown
|
84
100
|
- Rakefile
|
85
|
-
- examples/cloud_config.rb
|
86
101
|
- ext/.gitignore
|
87
102
|
- ext/Rakefile
|
88
103
|
- ext/c_zookeeper.rb
|
@@ -99,18 +114,23 @@ files:
|
|
99
114
|
- ext/zookeeper_c.c
|
100
115
|
- ext/zookeeper_lib.c
|
101
116
|
- ext/zookeeper_lib.h
|
102
|
-
- java/
|
117
|
+
- java/java_base.rb
|
103
118
|
- lib/zookeeper.rb
|
104
119
|
- lib/zookeeper/acls.rb
|
105
120
|
- lib/zookeeper/callbacks.rb
|
121
|
+
- lib/zookeeper/client.rb
|
122
|
+
- lib/zookeeper/client_methods.rb
|
106
123
|
- lib/zookeeper/common.rb
|
107
124
|
- lib/zookeeper/common/queue_with_pipe.rb
|
125
|
+
- lib/zookeeper/compatibility.rb
|
108
126
|
- lib/zookeeper/constants.rb
|
109
127
|
- lib/zookeeper/em_client.rb
|
110
128
|
- lib/zookeeper/exceptions.rb
|
129
|
+
- lib/zookeeper/rake_tasks.rb
|
111
130
|
- lib/zookeeper/stat.rb
|
112
131
|
- lib/zookeeper/version.rb
|
113
132
|
- notes.txt
|
133
|
+
- scripts/upgrade-1.0-sed-alike.rb
|
114
134
|
- spec/c_zookeeper_spec.rb
|
115
135
|
- spec/chrooted_connection_spec.rb
|
116
136
|
- spec/default_watcher_spec.rb
|
@@ -119,14 +139,10 @@ files:
|
|
119
139
|
- spec/shared/all_success_return_values.rb
|
120
140
|
- spec/shared/connection_examples.rb
|
121
141
|
- spec/spec_helper.rb
|
142
|
+
- spec/support/00_spawn_zookeeper.rb
|
122
143
|
- spec/support/progress_formatter.rb
|
144
|
+
- spec/support/zookeeper_spec_helpers.rb
|
123
145
|
- spec/zookeeper_spec.rb
|
124
|
-
- test/test_basic.rb
|
125
|
-
- test/test_callback1.rb
|
126
|
-
- test/test_close.rb
|
127
|
-
- test/test_esoteric.rb
|
128
|
-
- test/test_watcher1.rb
|
129
|
-
- test/test_watcher2.rb
|
130
146
|
- zookeeper.gemspec
|
131
147
|
homepage: https://github.com/slyphon/zookeeper
|
132
148
|
licenses: []
|
@@ -149,19 +165,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
149
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
166
|
none: false
|
151
167
|
requirements:
|
152
|
-
- - "
|
168
|
+
- - ">"
|
153
169
|
- !ruby/object:Gem::Version
|
154
|
-
hash:
|
170
|
+
hash: 25
|
155
171
|
segments:
|
156
|
-
-
|
157
|
-
|
172
|
+
- 1
|
173
|
+
- 3
|
174
|
+
- 1
|
175
|
+
version: 1.3.1
|
158
176
|
requirements: []
|
159
177
|
|
160
178
|
rubyforge_project:
|
161
179
|
rubygems_version: 1.8.15
|
162
180
|
signing_key:
|
163
181
|
specification_version: 3
|
164
|
-
summary:
|
182
|
+
summary: Apache ZooKeeper driver for Rubies
|
165
183
|
test_files:
|
166
184
|
- spec/c_zookeeper_spec.rb
|
167
185
|
- spec/chrooted_connection_spec.rb
|
@@ -171,11 +189,7 @@ test_files:
|
|
171
189
|
- spec/shared/all_success_return_values.rb
|
172
190
|
- spec/shared/connection_examples.rb
|
173
191
|
- spec/spec_helper.rb
|
192
|
+
- spec/support/00_spawn_zookeeper.rb
|
174
193
|
- spec/support/progress_formatter.rb
|
194
|
+
- spec/support/zookeeper_spec_helpers.rb
|
175
195
|
- spec/zookeeper_spec.rb
|
176
|
-
- test/test_basic.rb
|
177
|
-
- test/test_callback1.rb
|
178
|
-
- test/test_close.rb
|
179
|
-
- test/test_esoteric.rb
|
180
|
-
- test/test_watcher1.rb
|
181
|
-
- test/test_watcher2.rb
|
data/examples/cloud_config.rb
DELETED
@@ -1,125 +0,0 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "zookeeper"
|
3
|
-
|
4
|
-
# A basic cloud-based YAML config library. Ruby Zookeeper client example.
|
5
|
-
#
|
6
|
-
# If you pass in a file as 'zk:/foo.yml/blah' it will go out to zookeeper.
|
7
|
-
# Otherwise the file is assumed to be local. The yml file will get parsed
|
8
|
-
# and cached locally, and keys after the .yml get interpreted as keys into
|
9
|
-
# the YAML.
|
10
|
-
#
|
11
|
-
# e.g. get(zk:/config/service.yml/key1/key2/key3..) =>
|
12
|
-
# zk.get(:path => /config/service.yml)
|
13
|
-
# yaml <= YAML.parse(data)
|
14
|
-
# yaml[key1][key2][key3]...
|
15
|
-
#
|
16
|
-
# If keys are unspecified, it returns the parsed YAML as one big object
|
17
|
-
#
|
18
|
-
# TODO if staleness is set to 0, read in YAML immediately before next
|
19
|
-
# get(...)
|
20
|
-
|
21
|
-
class CloudConfig
|
22
|
-
class NodeNotFound < StandardError; end
|
23
|
-
class BadPathError < StandardError; end
|
24
|
-
|
25
|
-
DEFAULT_SERVERS = "localhost:2181"
|
26
|
-
|
27
|
-
def initialize(zkservers = DEFAULT_SERVERS, staleness = 15) # maximum allowed staleness in seconds
|
28
|
-
@staleness = staleness
|
29
|
-
@lock = Mutex.new
|
30
|
-
@zkservers = DEFAULT_SERVERS
|
31
|
-
|
32
|
-
# cache
|
33
|
-
@data = {}
|
34
|
-
@zkcb = Zookeeper::WatcherCallback.new { dirty_callback(@zkcb.context) }
|
35
|
-
@zk = nil
|
36
|
-
end
|
37
|
-
|
38
|
-
def get(node)
|
39
|
-
filename, keys = extract_filename(node)
|
40
|
-
|
41
|
-
# read(filename) is potentially a zk call, so do not hold the lock during the read
|
42
|
-
if @lock.synchronize { !@data.has_key?(filename) }
|
43
|
-
d = YAML.load(read(filename))
|
44
|
-
@lock.synchronize { @data[filename] = d }
|
45
|
-
end
|
46
|
-
|
47
|
-
# synchronized b/c we potentially have a background thread updating data nodes from zk
|
48
|
-
# if keys is empty, return the whole file, otherwise roll up the keys
|
49
|
-
@lock.synchronize {
|
50
|
-
keys.empty? ? @data[filename] : keys.inject(@data[filename]) { |hash, key| hash[key] }
|
51
|
-
}
|
52
|
-
end
|
53
|
-
|
54
|
-
# todo:
|
55
|
-
# factor get-and-watch into a different subsystem (so you can have
|
56
|
-
# polling stat() ops on local filesystem.)
|
57
|
-
def read(yaml)
|
58
|
-
# read yaml file and register watcher. if watcher fires, set up
|
59
|
-
# background thread to do read and update data.
|
60
|
-
if yaml.match(/^zk:/)
|
61
|
-
@zk ||= init_zk
|
62
|
-
yaml = yaml['zk:'.length..-1] # strip off zk: from zk:/config/path.yml
|
63
|
-
resp = get_and_register(yaml)
|
64
|
-
|
65
|
-
if resp[:rc] != Zookeeper::ZOK
|
66
|
-
@zk.unregister_watcher(resp[:req_id])
|
67
|
-
raise NodeNotFound
|
68
|
-
end
|
69
|
-
|
70
|
-
resp[:data]
|
71
|
-
else
|
72
|
-
raise NodeNotFound unless File.exists?(yaml)
|
73
|
-
File.read(yaml)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def extract_filename(node)
|
78
|
-
path_elements = node.split("/")
|
79
|
-
|
80
|
-
yamlindex = path_elements.map{ |x| x.match("\.yml$") != nil }.index(true)
|
81
|
-
raise BadPathError unless yamlindex
|
82
|
-
|
83
|
-
yamlname = path_elements[0..yamlindex].join '/'
|
84
|
-
yamlkeys = path_elements[(yamlindex+1)..-1]
|
85
|
-
|
86
|
-
return yamlname, yamlkeys
|
87
|
-
end
|
88
|
-
|
89
|
-
private
|
90
|
-
def init_zk
|
91
|
-
Zookeeper.new(@zkservers)
|
92
|
-
end
|
93
|
-
|
94
|
-
def get_and_register(znode)
|
95
|
-
@zk.get(:path => znode, :watcher => @zkcb,
|
96
|
-
:watcher_context => { :path => znode,
|
97
|
-
:wait => rand(@staleness) })
|
98
|
-
end
|
99
|
-
|
100
|
-
def dirty_callback(context)
|
101
|
-
path = context[:path]
|
102
|
-
wait = context[:wait]
|
103
|
-
|
104
|
-
# Fire off a background update that waits a randomized period of time up
|
105
|
-
# to @staleness seconds.
|
106
|
-
Thread.new do
|
107
|
-
sleep wait
|
108
|
-
background_update(path)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
def background_update(zkpath)
|
113
|
-
# do a synchronous get/register a new watcher
|
114
|
-
resp = get_and_register(zkpath)
|
115
|
-
if resp[:rc] != Zookeeper::ZOK
|
116
|
-
# puts "Unable to read #{zkpath} from Zookeeper!" @logger.error
|
117
|
-
zk.unregister_watcher(resp[:req_id])
|
118
|
-
else
|
119
|
-
# puts "Updating data."
|
120
|
-
d = YAML.load(resp[:data])
|
121
|
-
@lock.synchronize { @data["zk:#{zkpath}"] = d }
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
data/test/test_basic.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'zookeeper'
|
3
|
-
|
4
|
-
z = Zookeeper.new("localhost:2181")
|
5
|
-
|
6
|
-
puts "root: #{z.get_children(:path => "/").inspect}"
|
7
|
-
|
8
|
-
path = "/testing_node"
|
9
|
-
|
10
|
-
puts "working with path #{path}"
|
11
|
-
|
12
|
-
h = z.stat(:path => path)
|
13
|
-
stat = h[:stat]
|
14
|
-
puts "exists? #{stat.inspect}"
|
15
|
-
|
16
|
-
if stat.exists
|
17
|
-
z.get_children(:path => path)[:children].each do |o|
|
18
|
-
puts " child object: #{o}"
|
19
|
-
end
|
20
|
-
puts "delete: #{z.delete(:path => path, :version => stat.version).inspect}"
|
21
|
-
end
|
22
|
-
|
23
|
-
puts "create: #{z.create(:path => path, :data => 'initial value').inspect}"
|
24
|
-
|
25
|
-
v = z.get(:path => path)
|
26
|
-
value, stat = v[:data], v[:stat]
|
27
|
-
puts "current value #{value}, stat #{stat.inspect}"
|
28
|
-
|
29
|
-
puts "set: #{z.set(:path => path, :data => 'this is a test', :version => stat.version).inspect}"
|
30
|
-
|
31
|
-
v = z.get(:path => path)
|
32
|
-
value, stat = v[:data], v[:stat]
|
33
|
-
puts "new value: #{value.inspect} #{stat.inspect}"
|
34
|
-
|
35
|
-
puts "delete: #{z.delete(:path => path, :version => stat.version).inspect}"
|
36
|
-
|
37
|
-
puts "exists? #{z.stat(:path => path)[:stat].exists}"
|
data/test/test_callback1.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'zookeeper'
|
3
|
-
|
4
|
-
def wait_until(timeout=10, &block)
|
5
|
-
time_to_stop = Time.now + timeout
|
6
|
-
until yield do
|
7
|
-
break if Time.now > time_to_stop
|
8
|
-
sleep 0.1
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
puts 'Initializing Zookeeper'
|
13
|
-
|
14
|
-
zk = Zookeeper.new('localhost:2181')
|
15
|
-
|
16
|
-
if zk.state != Zookeeper::ZOO_CONNECTED_STATE
|
17
|
-
puts 'Unable to connect to Zookeeper!'
|
18
|
-
Kernel.exit
|
19
|
-
end
|
20
|
-
|
21
|
-
def callback(args)
|
22
|
-
puts "CALLBACK EXECUTED, args = #{args.inspect}"
|
23
|
-
puts args.return_code == Zookeeper::ZOK ? "TEST PASSED IN CALLBACK" : "TEST FAILED IN CALLBACK"
|
24
|
-
end
|
25
|
-
|
26
|
-
ccb = Zookeeper::VoidCallback.new do
|
27
|
-
callback(ccb)
|
28
|
-
end
|
29
|
-
|
30
|
-
resp = zk.create(:path => '/test', :data => "new data", :sequence => true, :callback => ccb)
|
31
|
-
puts "#{resp.inspect}"
|
32
|
-
puts "TEST FAILED [create]" unless resp[:rc] == Zookeeper::ZOK
|
33
|
-
|
34
|
-
wait_until { ccb.completed? }
|
35
|
-
|
36
|
-
puts ccb.completed? ? "TEST PASSED" : "TEST FAILED"
|
data/test/test_close.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'zookeeper'
|
3
|
-
z = Zookeeper.new("localhost:2181")
|
4
|
-
path = "/testing_node"
|
5
|
-
z.get(:path => path)
|
6
|
-
z.create(:path => path, :data => "initial value", :ephemeral => true)
|
7
|
-
z.get(:path => path)
|
8
|
-
z.close()
|
9
|
-
sleep 5
|
10
|
-
begin
|
11
|
-
z.get(:path => path)
|
12
|
-
rescue Exception => e
|
13
|
-
puts "Rescued exception #{e.inspect}"
|
14
|
-
end
|
15
|
-
z.reopen
|
16
|
-
z.get(:path => path)
|
data/test/test_esoteric.rb
DELETED
data/test/test_watcher1.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'zookeeper'
|
3
|
-
|
4
|
-
def wait_until(timeout=10, &block)
|
5
|
-
time_to_stop = Time.now + timeout
|
6
|
-
until yield do
|
7
|
-
break if Time.now > time_to_stop
|
8
|
-
sleep 0.1
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
puts 'Initializing Zookeeper'
|
13
|
-
|
14
|
-
zk = Zookeeper.new('localhost:2181')
|
15
|
-
|
16
|
-
if zk.state != Zookeeper::ZOO_CONNECTED_STATE
|
17
|
-
puts 'Unable to connect to Zookeeper!'
|
18
|
-
Kernel.exit
|
19
|
-
end
|
20
|
-
|
21
|
-
def watcher(args)
|
22
|
-
puts "#{args.inspect}"
|
23
|
-
puts "In watcher: path=#{args.path}, context=#{args.context}"
|
24
|
-
if args.path == args.context
|
25
|
-
puts "TEST PASSED IN WATCHER"
|
26
|
-
else
|
27
|
-
puts "TEST FAILED IN WATCHER"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
wcb = Zookeeper::WatcherCallback.new do
|
32
|
-
watcher(wcb)
|
33
|
-
end
|
34
|
-
|
35
|
-
resp = zk.create(:path => '/test', :sequence => true)
|
36
|
-
puts "#{resp.inspect}"
|
37
|
-
puts "TEST FAILED [create]" unless resp[:rc] == Zookeeper::ZOK
|
38
|
-
|
39
|
-
base_path = resp[:path]
|
40
|
-
watched_file = "#{base_path}/file.does.not.exist"
|
41
|
-
|
42
|
-
resp = zk.stat(:path => watched_file, :watcher => wcb, :watcher_context => watched_file)
|
43
|
-
puts "#{resp.inspect}"
|
44
|
-
puts "TEST FAILED [stat]" unless resp[:rc] == Zookeeper::ZNONODE
|
45
|
-
|
46
|
-
resp = zk.create(:path => watched_file, :data => 'test data', :ephemeral => true)
|
47
|
-
puts "#{resp.inspect}"
|
48
|
-
puts "TEST FAILED [create]" unless resp[:rc] == Zookeeper::ZOK
|
49
|
-
|
50
|
-
wait_until { wcb.completed? }
|
51
|
-
|
52
|
-
puts "TEST FAILED" unless wcb.completed?
|
53
|
-
puts "TEST PASSED"
|
54
|
-
|
55
|
-
zk.delete(:path => watched_file)
|
56
|
-
zk.delete(:path => base_path)
|
data/test/test_watcher2.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'zookeeper'
|
3
|
-
|
4
|
-
def wait_until(timeout=10, &block)
|
5
|
-
time_to_stop = Time.now + timeout
|
6
|
-
until yield do
|
7
|
-
break if Time.now > time_to_stop
|
8
|
-
sleep 0.1
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
puts 'Initializing Zookeeper'
|
13
|
-
|
14
|
-
zk = Zookeeper.new('localhost:2181')
|
15
|
-
|
16
|
-
if zk.state != Zookeeper::ZOO_CONNECTED_STATE
|
17
|
-
puts 'Unable to connect to Zookeeper!'
|
18
|
-
Kernel.exit
|
19
|
-
end
|
20
|
-
|
21
|
-
def watcher(args)
|
22
|
-
if args.path == args.context
|
23
|
-
puts "TEST PASSED IN WATCHER"
|
24
|
-
else
|
25
|
-
puts "TEST FAILED IN WATCHER"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
wcb = Zookeeper::WatcherCallback.new do
|
30
|
-
watcher(wcb)
|
31
|
-
end
|
32
|
-
|
33
|
-
resp = zk.create(:path => '/test', :sequence => true)
|
34
|
-
puts "#{resp.inspect}"
|
35
|
-
puts "TEST FAILED [create]" unless resp[:rc] == Zookeeper::ZOK
|
36
|
-
|
37
|
-
base_path = resp[:path]
|
38
|
-
triggering_file = "#{base_path}/file.does.not.exist"
|
39
|
-
|
40
|
-
resp = zk.get_children(:path => base_path, :watcher => wcb, :watcher_context => base_path)
|
41
|
-
puts "TEST FAILED [get_children]" unless resp[:rc] == Zookeeper::ZOK
|
42
|
-
|
43
|
-
resp = zk.create(:path => triggering_file, :data => 'test data', :ephemeral => true)
|
44
|
-
puts "TEST FAILED [create]" unless resp[:rc] == Zookeeper::ZOK
|
45
|
-
|
46
|
-
wait_until { wcb.completed? }
|
47
|
-
|
48
|
-
puts "TEST FAILED" unless wcb.completed?
|
49
|
-
puts "TEST PASSED"
|
50
|
-
|
51
|
-
zk.delete(:path => triggering_file)
|
52
|
-
zk.delete(:path => base_path)
|