<commit_before># encoding: UTF-8

require "helper"

class TestBlockingCommands < Test::Unit::TestCase

  include Helper

  def setup
    super

    r.rpush("foo", "s1")
    r.rpush("foo", "s2")
    r.rpush("bar", "s1")
    r.rpush("bar", "s2")
  end

  def to_protocol(obj)
    case obj
    when String
      "$#{obj.length}\r\n#{obj}\r\n"
    when Array
      "*#{obj.length}\r\n" + obj.map { |e| to_protocol(e) }.join
    else
      fail
    end
  end

  def mock(&blk)
    commands = {
      :blpop => lambda do |*args|
        to_protocol([args.first, args.last])
      end,
      :brpop => lambda do |*args|
        to_protocol([args.first, args.last])
      end,
      :brpoplpush => lambda do |*args|
        to_protocol(args.last)
      end,
    }

    redis_mock(commands, &blk)
  end

  def test_blpop
    assert_equal ["foo", "s1"], r.blpop("foo")
    assert_equal ["foo", "s2"], r.blpop(["foo"])
    assert_equal ["bar", "s1"], r.blpop(["bar", "foo"])
    assert_equal ["bar", "s2"], r.blpop(["foo", "bar"])
  end

  def test_blpop_timeout
    mock do |r|
      assert_equal ["foo", "0"], r.blpop("foo")
      assert_equal ["foo", "1"], r.blpop("foo", :timeout => 1)
    end
  end

  def test_blpop_with_old_prototype
    assert_equal ["foo", "s1"], r.blpop("foo", 0)
    assert_equal ["foo", "s2"], r.blpop("foo", 0)
    assert_equal ["bar", "s1"], r.blpop("bar", "foo", 0)
    assert_equal ["bar", "s2"], r.blpop("foo", "bar", 0)
  end

  def test_blpop_timeout_with_old_prototype
    mock do |r|
      assert_equal ["foo", "0"], r.blpop("foo", 0)
      assert_equal ["foo", "1"], r.blpop("foo", 1)
    end
  end

  def test_brpop
    assert_equal ["foo", "s2"], r.brpop("foo")
    assert_equal ["foo", "s1"], r.brpop(["foo"])
    assert_equal ["bar", "s2"], r.brpop(["bar", "foo"])
    assert_equal ["bar", "s1"], r.brpop(["foo", "bar"])
  end

  def test_brpop_timeout
    mock do |r|
      assert_equal ["foo", "0"], r.brpop("foo")
      assert_equal ["foo", "1"], r.brpop("foo", :timeout => 1)
    end
  end

  def test_brpop_with_old_prototype
    assert_equal ["foo", "s2"], r.brpop("foo", 0)
    assert_equal ["foo", "s1"], r.brpop("foo", 0)
    assert_equal ["bar", "s2"], r.brpop("bar", "foo", 0)
    assert_equal ["bar", "s1"], r.brpop("foo", "bar", 0)
  end

  def test_brpop_timeout_with_old_prototype
    mock do |r|
      assert_equal ["foo", "0"], r.brpop("foo", 0)
      assert_equal ["foo", "1"], r.brpop("foo", 1)
    end
  end

  def test_brpoplpush
    assert_equal "s2", r.brpoplpush("foo", "zap")
    assert_equal ["s2"], r.lrange("zap", 0, -1)
  end

  def test_brpoplpush_timeout
    mock do |r|
      assert_equal "0", r.brpoplpush("foo", "bar")
      assert_equal "1", r.brpoplpush("foo", "bar", :timeout => 1)
    end
  end

  def test_brpoplpush_with_old_prototype
    assert_equal "s2", r.brpoplpush("foo", "zap", 0)
    assert_equal ["s2"], r.lrange("zap", 0, -1)
  end

  def test_brpoplpush_timeout_with_old_prototype
    mock do |r|
      assert_equal "0", r.brpoplpush("foo", "bar", 0)
      assert_equal "1", r.brpoplpush("foo", "bar", 1)
    end
  end
end<commit_msg>Test that blocking pop ignores client timeout<commit_after># encoding: UTF-8

require "helper"

class TestBlockingCommands < Test::Unit::TestCase

  include Helper

  def setup
    super

    r.rpush("foo", "s1")
    r.rpush("foo", "s2")
    r.rpush("bar", "s1")
    r.rpush("bar", "s2")
  end

  def to_protocol(obj)
    case obj
    when String
      "$#{obj.length}\r\n#{obj}\r\n"
    when Array
      "*#{obj.length}\r\n" + obj.map { |e| to_protocol(e) }.join
    else
      fail
    end
  end

  def mock(options = {}, &blk)
    commands = {
      :blpop => lambda do |*args|
        sleep options[:delay] if options.has_key?(:delay)
        to_protocol([args.first, args.last])
      end,
      :brpop => lambda do |*args|
        sleep options[:delay] if options.has_key?(:delay)
        to_protocol([args.first, args.last])
      end,
      :brpoplpush => lambda do |*args|
        sleep options[:delay] if options.has_key?(:delay)
        to_protocol(args.last)
      end,
    }

    redis_mock(commands, &blk)
  end

  def assert_takes_longer_than_client_timeout
    timeout = OPTIONS[:timeout]
    delay = timeout * 2

    mock(:delay => delay) do |r|
      t1 = Time.now
      yield(r)
      t2 = Time.now

      assert timeout == r.client.timeout
      assert delay <= (t2 - t1)
    end
  end

  def test_blpop
    assert_equal ["foo", "s1"], r.blpop("foo")
    assert_equal ["foo", "s2"], r.blpop(["foo"])
    assert_equal ["bar", "s1"], r.blpop(["bar", "foo"])
    assert_equal ["bar", "s2"], r.blpop(["foo", "bar"])
  end

  def test_blpop_timeout
    mock do |r|
      assert_equal ["foo", "0"], r.blpop("foo")
      assert_equal ["foo", "1"], r.blpop("foo", :timeout => 1)
    end
  end

  def test_blpop_disable_client_timeout
    assert_takes_longer_than_client_timeout do |r|
      assert_equal ["foo", "0"], r.blpop("foo")
    end
  end

  def test_blpop_with_old_prototype
    assert_equal ["foo", "s1"], r.blpop("foo", 0)
    assert_equal ["foo", "s2"], r.blpop("foo", 0)
    assert_equal ["bar", "s1"], r.blpop("bar", "foo", 0)
    assert_equal ["bar", "s2"], r.blpop("foo", "bar", 0)
  end

  def test_blpop_timeout_with_old_prototype
    mock do |r|
      assert_equal ["foo", "0"], r.blpop("foo", 0)
      assert_equal ["foo", "1"], r.blpop("foo", 1)
    end
  end

  def test_brpop
    assert_equal ["foo", "s2"], r.brpop("foo")
    assert_equal ["foo", "s1"], r.brpop(["foo"])
    assert_equal ["bar", "s2"], r.brpop(["bar", "foo"])
    assert_equal ["bar", "s1"], r.brpop(["foo", "bar"])
  end

  def test_brpop_timeout
    mock do |r|
      assert_equal ["foo", "0"], r.brpop("foo")
      assert_equal ["foo", "1"], r.brpop("foo", :timeout => 1)
    end
  end

  def test_brpop_disable_client_timeout
    assert_takes_longer_than_client_timeout do |r|
      assert_equal ["foo", "0"], r.brpop("foo")
    end
  end

  def test_brpop_with_old_prototype
    assert_equal ["foo", "s2"], r.brpop("foo", 0)
    assert_equal ["foo", "s1"], r.brpop("foo", 0)
    assert_equal ["bar", "s2"], r.brpop("bar", "foo", 0)
    assert_equal ["bar", "s1"], r.brpop("foo", "bar", 0)
  end

  def test_brpop_timeout_with_old_prototype
    mock do |r|
      assert_equal ["foo", "0"], r.brpop("foo", 0)
      assert_equal ["foo", "1"], r.brpop("foo", 1)
    end
  end

  def test_brpoplpush
    assert_equal "s2", r.brpoplpush("foo", "zap")
    assert_equal ["s2"], r.lrange("zap", 0, -1)
  end

  def test_brpoplpush_timeout
    mock do |r|
      assert_equal "0", r.brpoplpush("foo", "bar")
      assert_equal "1", r.brpoplpush("foo", "bar", :timeout => 1)
    end
  end

  def test_brpoplpush_disable_client_timeout
    assert_takes_longer_than_client_timeout do |r|
      assert_equal "0", r.brpoplpush("foo", "bar")
    end
  end

  def test_brpoplpush_with_old_prototype
    assert_equal "s2", r.brpoplpush("foo", "zap", 0)
    assert_equal ["s2"], r.lrange("zap", 0, -1)
  end

  def test_brpoplpush_timeout_with_old_prototype
    mock do |r|
      assert_equal "0", r.brpoplpush("foo", "bar", 0)
      assert_equal "1", r.brpoplpush("foo", "bar", 1)
    end
  end
end
{'fraction_non_alphanumeric': 0.18198223209328152, 'fraction_numerical': 0.017490283176013324, 'mean_word_length': 3.09375, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 16, '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': '3197889', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2793, 'n_words': 648}