<commit_before>require 'test_helper'

class RenoirClientRedisTest < Minitest::Test
  def setup
    @client = Renoir::Client.new(
      CONFIG.merge(connection_adapter: :redis)
    )
  end

  def test_call_single_key_command
    assert @client.set('hoge', 123) == 'OK'
    assert @client.set('fuga', 'piyo') == 'OK'
    assert @client.set('hogera', 'hogehoge') == 'OK'
    assert @client.get('hoge') == '123'
    assert @client.get('fuga') == 'piyo'
    assert @client.get('hogera') == 'hogehoge'
  end

  def test_call_multi_key_command
    assert_raises RuntimeError do
      @client.mget('hoge', 'fuga', 'hogera')
    end

    assert @client.mget('hoge{1}', 'fuga{1}', 'hogera{1}').size == 3
  end

  def test_call_unsupported_command
    assert_raises RuntimeError do
      @client.info
    end
  end

  def test_close
    # ensure that connection is established
    connections = []
    @client.send(:refresh_slots)
    @client.each_node do |conn|
      conn.ping
      connections << conn
    end

    @client.close

    refute connections.any?(&:connected?)
  end

  def test_each_node
    @client.send(:refresh_slots)
    assert @client.each_node.all? { |conn| conn.is_a?(::Redis) }
  end

  def test_too_many_redirections
    @client.send(:refresh_slots)
    @client.each_node do |conn|
      def conn.set(*args)
        if Thread.current[:error_count] < Thread.current[:limit]
          Thread.current[:error_count] += 1
          raise ::Redis::CommandError, "MOVED 123 #{client.host}:#{client.port}"
        else
          super(*args)
        end
      end
    end

    Thread.current[:error_count] = 0
    Thread.current[:limit] = Renoir::Client::DEFAULT_OPTIONS[:max_redirection] + 1
    assert_raises Renoir::RedirectionError do
      @client.set('hoge', 123)
    end

    Thread.current[:error_count] = 0
    Thread.current[:limit] = Renoir::Client::DEFAULT_OPTIONS[:max_redirection]
    assert @client.set('hoge', 123) == 'OK'
  end

  def test_too_many_connection_errors
    @client.send(:refresh_slots)
    @client.each_node do |conn|
      def conn.set(*args)
        if Thread.current[:error_count] < Thread.current[:limit]
          Thread.current[:error_count] += 1
          raise ::Redis::CannotConnectError
        else
          super(*args)
        end
      end
    end

    Thread.current[:error_count] = 0
    Thread.current[:limit] = Renoir::Client::DEFAULT_OPTIONS[:max_connection_error] + 1
    assert_raises ::Redis::CannotConnectError do
      @client.set('hoge', 123)
    end
<commit_msg>Fix a Renoir::Client test case<commit_after>require 'test_helper'

class RenoirClientRedisTest < Minitest::Test
  def setup
    @client = Renoir::Client.new(
      CONFIG.merge(connection_adapter: :redis)
    )
  end

  def test_call_single_key_command
    assert @client.set('hoge', 123) == 'OK'
    assert @client.set('fuga', 'piyo') == 'OK'
    assert @client.set('hogera', 'hogehoge') == 'OK'
    assert @client.get('hoge') == '123'
    assert @client.get('fuga') == 'piyo'
    assert @client.get('hogera') == 'hogehoge'
  end

  def test_call_multi_key_command
    assert_raises RuntimeError do
      @client.mget('hoge', 'fuga', 'hogera')
    end

    assert @client.mget('hoge{1}', 'fuga{1}', 'hogera{1}').size == 3
  end

  def test_call_unsupported_command
    assert_raises RuntimeError do
      @client.info
    end
  end

  def test_close
    # ensure that connection is established
    connections = []
    @client.send(:refresh_slots)
    @client.each_node do |conn|
      conn.ping
      connections << conn
    end

    @client.close

    refute connections.any?(&:connected?)
  end

  def test_each_node
    @client.send(:refresh_slots)
    assert @client.each_node.all? { |conn| conn.is_a?(::Redis) }
  end

  def test_too_many_redirections
    @client.send(:refresh_slots)
    @client.each_node do |conn|
      def conn.set(*args)
        if Thread.current[:error_count] < Thread.current[:limit]
          Thread.current[:error_count] += 1
          raise ::Redis::CommandError, "MOVED 123 #{client.host}:#{client.port}"
        else
          super(*args)
        end
      end
    end

    Thread.current[:error_count] = 0
    Thread.current[:limit] = Renoir::Client::DEFAULT_OPTIONS[:max_redirection] + 1
    assert_raises Renoir::RedirectionError do
      @client.set('hoge', 123)
    end

    Thread.current[:error_count] = 0
    Thread.current[:limit] = Renoir::Client::DEFAULT_OPTIONS[:max_redirection] - 1
    assert @client.set('hoge', 123) == 'OK'
  end

  def test_too_many_connection_errors
    @client.send(:refresh_slots)
    @client.each_node do |conn|
      def conn.set(*args)
        if Thread.current[:error_count] < Thread.current[:limit]
          Thread.current[:error_count] += 1
          raise ::Redis::CannotConnectError
        else
          super(*args)
        end
      end
    end

    Thread.current[:error_count] = 0
    Thread.current[:limit] = Renoir::Client::DEFAULT_OPTIONS[:max_connection_error] + 1
    assert_raises ::Redis::CannotConnectError do
      @client.set('hoge', 123)
    end

{'fraction_non_alphanumeric': 0.1315322421613094, 'fraction_numerical': 0.011634786038256755, 'mean_word_length': 3.1437908496732025, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '2209784', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1675, 'n_words': 327}