Sha256: ce22cbd2013814e7129620b241f81313b99f87a85be77c3a1bd0b21172a40401

Contents?: true

Size: 1.34 KB

Versions: 63

Compression:

Stored size: 1.34 KB

Contents

require 'rubygems'
require 'zookeeper'

def wait_until(timeout=10, &block)
  time_to_stop = Time.now + timeout
  until yield do
    break if Time.now > time_to_stop
    sleep 0.1
  end
end

puts 'Initializing Zookeeper'

zk = Zookeeper.new('localhost:2181')

if zk.state != Zookeeper::ZOO_CONNECTED_STATE
  puts 'Unable to connect to Zookeeper!'
  Kernel.exit
end

def watcher(args)
  puts "#{args.inspect}"
  puts "In watcher: path=#{args.path}, context=#{args.context}"
  if args.path == args.context
    puts "TEST PASSED IN WATCHER"
  else
    puts "TEST FAILED IN WATCHER"
  end
end

wcb = Zookeeper::WatcherCallback.new do
  watcher(wcb)
end

resp = zk.create(:path => '/test', :sequence => true)
puts "#{resp.inspect}"
puts "TEST FAILED [create]" unless resp[:rc] == Zookeeper::ZOK
 
base_path = resp[:path]
watched_file = "#{base_path}/file.does.not.exist"

resp = zk.stat(:path => watched_file, :watcher => wcb, :watcher_context => watched_file)
puts "#{resp.inspect}"
puts "TEST FAILED [stat]" unless resp[:rc] == Zookeeper::ZNONODE

resp = zk.create(:path => watched_file, :data => 'test data', :ephemeral => true)
puts "#{resp.inspect}"
puts "TEST FAILED [create]" unless resp[:rc] == Zookeeper::ZOK

wait_until { wcb.completed? }

puts "TEST FAILED" unless wcb.completed?
puts "TEST PASSED"

zk.delete(:path => watched_file)
zk.delete(:path => base_path)

Version data entries

63 entries across 63 versions & 2 rubygems

Version Path
zookeeper-0.4.0 test/test_watcher1.rb
zookeeper-0.3.2 test/test_watcher1.rb
zookeeper-0.3.0 test/test_watcher1.rb