<commit_before>require 'test_common'

unless defined? Bud::HAVE_ZOOKEEPER
  puts "Skipping Zk test: no zookeeper Gem installed"
  raise
end

# Check whether ZK is running
def zk_running?
  begin
    z = Zookeeper.new("localhost:2181")
    z.close
    return true
  rescue Exception
    return false
  end
end

unless zk_running?
  puts "Skipping Zk test: cannot connect to Zookeeper on localhost:2181"
  raise
end

class ZkMirror
  include Bud

  state do
    zktable :t1, "/foo"
  end
end

class TestZk < Test::Unit::TestCase
  def setup
    z = Zookeeper.new("localhost:2181")
    zk_rm_r(z, "/foo")
    z.close
  end

  def zk_rm_r(z, root)
    r = z.get_children(:path => root)
    return unless r[:stat].exists
    r[:children].each do |c|
      zk_rm_r(z, "#{root}/#{c}")
    end
    z.delete(:path => root)
  end

  def test_one_zk
    b = ZkMirror.new
    b.run_bg
    b.sync_do {
      assert_equal([], b.t1.to_a.sort)
    }
    b.sync_do {
      b.t1 <~ [["xyz", "zzz"]]
    }
    b.sync_do {
      assert_equal([["xyz", "zzz"]], b.t1.to_a.sort)
    }
    b.stop_bg
  end

  def test_mirror
    b1, b2 = ZkMirror.new, ZkMirror.new
    b1.run_bg
    b2.run_bg

    tuples = [["k1", "ggg"], ["k2", "ggg"]]
    q = Queue.new
    c = b2.register_callback(:t1) do |t|
      if t.length == tuples.length<commit_msg>Fix race in Zk test.<commit_after>require 'test_common'

unless defined? Bud::HAVE_ZOOKEEPER
  puts "Skipping Zk test: no zookeeper Gem installed"
  raise
end

# Check whether ZK is running
def zk_running?
  begin
    z = Zookeeper.new("localhost:2181")
    z.close
    return true
  rescue Exception
    return false
  end
end

unless zk_running?
  puts "Skipping Zk test: cannot connect to Zookeeper on localhost:2181"
  raise
end

class ZkMirror
  include Bud

  state do
    zktable :t1, "/foo"
  end
end

class TestZk < Test::Unit::TestCase
  def setup
    z = Zookeeper.new("localhost:2181")
    zk_rm_r(z, "/foo")
    z.close
  end

  def zk_rm_r(z, root)
    r = z.get_children(:path => root)
    return unless r[:stat].exists
    r[:children].each do |c|
      zk_rm_r(z, "#{root}/#{c}")
    end
    z.delete(:path => root)
  end

  def test_one_zk
    b = ZkMirror.new
    b.run_bg
    b.sync_do {
      assert_equal([], b.t1.to_a.sort)
    }
    q = Queue.new
    c = b.register_callback(:t1) do
      q.push(true)
    end
    b.sync_do {
      b.t1 <~ [["xyz", "zzz"]]
    }
    q.pop
    b.unregister_callback(c)

    b.sync_do {
      assert_equal([["xyz", "zzz"]], b.t1.to_a.sort)
    }
    b.stop_bg
  end

  def test_mirror
    b1, b2 = ZkMirror.new, ZkMirror.new
    b1.run_bg
    b2.run_bg

    tuples = [["k1", "ggg"], ["k2", "ggg"]]
    q = Queue.new
    c = b2.register_callback(:t1) do |t|
      if t.length == tuples.length
{'fraction_non_alphanumeric': 0.13123644251626898, 'fraction_numerical': 0.017715112075198842, 'mean_word_length': 2.55198973042362, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6303976', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1132, 'n_words': 270}