Permalink
Fetching contributors…
Cannot retrieve contributors at this time
128 lines (103 sloc) 4.2 KB
require File.join(File.dirname(__FILE__), %w[spec_helper])
describe ZK do
before do
@zk = ZK.new("localhost:#{ZK_TEST_PORT}", :watcher => nil)
@base_path = "/zktests"
@zk.rm_rf(@base_path)
@zk.mkdir_p(@base_path)
end
after do
@zk.rm_rf(@base_path)
@zk.close!
end
describe ZK, "with no paths" do
it "should not exist" do
@zk.exists?("#{@base_path}/test").should be_false
end
it "should create a path" do
@zk.create("#{@base_path}/test", "test_data", :mode => :ephemeral).should == "#{@base_path}/test"
end
it "should be able to set the data" do
@zk.create("#{@base_path}/test", "something", :mode => :ephemeral)
@zk.set("#{@base_path}/test", "somethingelse")
@zk.get("#{@base_path}/test").first.should == "somethingelse"
end
it "should raise an exception for a non existent path" do
lambda { @zk.get("/non_existent_path") }.should raise_error(ZK::Exceptions::NoNode)
end
it "should create a path with sequence set" do
@zk.create("#{@base_path}/test", "test_data", :mode => :persistent_sequential).should =~ /test(\d+)/
end
it "should create an ephemeral path" do
@zk.create("#{@base_path}/test", "test_data", :mode => :ephemeral).should == "#{@base_path}/test"
end
it "should remove ephemeral path when client session ends" do
@zk.create("#{@base_path}/test", "test_data", :mode => :ephemeral).should == "#{@base_path}/test"
@zk.exists?("#{@base_path}/test").should_not be_nil
@zk.close!
wait_until(2) { !@zk.connected? }
@zk.should_not be_connected
@zk = ZK.new("localhost:#{ZK_TEST_PORT}", :watcher => nil)
wait_until{ @zk.connected? }
@zk.exists?("#{@base_path}/test").should be_false
end
it "should remove sequential ephemeral path when client session ends" do
created = @zk.create("#{@base_path}/test", "test_data", :mode => :ephemeral_sequential)
created.should =~ /test(\d+)/
@zk.exists?(created).should_not be_nil
@zk.close!
@zk = ZK.new("localhost:#{ZK_TEST_PORT}", :watcher => nil)
wait_until{ @zk.connected? }
@zk.exists?(created).should be_false
end
end
describe ZK, "with a path" do
before(:each) do
@zk.create("#{@base_path}/test", "test_data", :mode => :persistent)
end
it "should return a stat" do
@zk.stat("#{@base_path}/test").should be_instance_of(ZookeeperStat::Stat)
end
it "should return a boolean" do
@zk.exists?("#{@base_path}/test").should be_true
end
it "should get data and stat" do
data, stat = @zk.get("#{@base_path}/test")
data.should == "test_data"
stat.should be_a_kind_of(ZookeeperStat::Stat)
stat.created_time.should_not == 0
end
it "should set data with a file" do
file = File.read('spec/test_file.txt')
@zk.set("#{@base_path}/test", file)
@zk.get("#{@base_path}/test").first.should == file
end
it "should delete path" do
@zk.delete("#{@base_path}/test")
@zk.exists?("#{@base_path}/test").should be_false
end
it "should create a child path" do
@zk.create("#{@base_path}/test/child", "child", :mode => :ephemeral).should == "#{@base_path}/test/child"
end
it "should create sequential child paths" do
(child1 = @zk.create("#{@base_path}/test/child", "child1", :mode => :persistent_sequential)).should =~ /\/test\/child(\d+)/
(child2 = @zk.create("#{@base_path}/test/child", "child2", :mode => :persistent_sequential)).should =~ /\/test\/child(\d+)/
children = @zk.children("#{@base_path}/test")
children.length.should == 2
children.should be_include(child1.match(/\/test\/(child\d+)/)[1])
children.should be_include(child2.match(/\/test\/(child\d+)/)[1])
end
it "should have no children" do
@zk.children("#{@base_path}/test").should be_empty
end
end
describe ZK, "with children" do
before(:each) do
@zk.create("#{@base_path}/test", "test_data", :mode => :persistent)
@zk.create("#{@base_path}/test/child", "child", :mode => "persistent").should == "#{@base_path}/test/child"
end
it "should get children" do
@zk.children("#{@base_path}/test").should eql(["child"])
end
end
end