<commit_before>require 'helper'

class TestGirlFriday < MiniTest::Unit::TestCase

  class TestErrorHandler
    include MiniTest::Assertions
  end

  def test_should_process_messages
    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('test') do |msg|
        assert_equal 'foo', msg[:text]
        cb.call
      end
      queue.push(:text => 'foo')
    end
  end

  def test_should_handle_worker_error
    async_test do |cb|
      TestErrorHandler.send(:define_method, :handle) do |ex|
        assert_equal 'oops', ex.message
        assert_equal 'RuntimeError', ex.class.name
        cb.call
      end

      queue = GirlFriday::WorkQueue.new('test', :error_handler => TestErrorHandler) do |msg|
        raise 'oops'
      end
      queue.push(:text => 'foo')
    end
  end

  def test_should_call_callback_when_complete
    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('test', :size => 1) do |msg|
        assert_equal 'foo', msg[:text]
        'camel'
      end
      queue.push(:text => 'foo') do |result|
        assert_equal 'camel', result
        cb.call
      end
    end
  end

  def test_should_provide_status
    mutex = Mutex.new
    total = 200
    count = 0
    incr = Proc.new do
      mutex.synchronize do
        count += 1
      end
    end

    actual = nil
    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('image_crawler', :size => 3) do |msg|
        mycount = incr.call
        actual = queue.status if mycount == 100
        cb.call if mycount == total
      end
      total.times do |idx|
        queue.push(:text => 'foo')
      end
    end

    refute_nil actual
    refute_nil actual['image_crawler']
    metrics = actual['image_crawler']
    assert metrics[:total_queued] > 0
    assert metrics[:total_queued] <= total
    assert_equal 3, metrics[:pool_size]
    assert_equal 3, metrics[:busy]
    assert_equal 0, metrics[:ready]
    assert(metrics[:backlog] > 0)
    assert(metrics[:total_processed] > 0)
  end

  def test_should_persist_with_redis
    begin
      require 'redis'
      redis = Redis.new
      redis.flushdb
    rescue LoadError
      return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
    rescue Errno::ECONNREFUSED
      return puts 'Skipping redis test, not running locally'
    end

    mutex = Mutex.new
    total = 100
    count = 0
    incr = Proc.new do
      mutex.synchronize do
        count += 1
      end
    end

    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('test', :size => 2, :store => GirlFriday::Store::Redis) do |msg|
        incr.call
        cb.call if count == total
      end
      total.times do
        queue.push(:text => 'foo')
      end
    end
  end

  def test_should_persist_with_redis_instance
    begin
      require 'redis'
      redis = Redis.new(:path => '/tmp/redis.sock')
      redis.flushdb
    rescue LoadError
      return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
    rescue Errno::ECONNREFUSED<commit_msg>Remove Redis socket connection<commit_after>require 'helper'

class TestGirlFriday < MiniTest::Unit::TestCase

  class TestErrorHandler
    include MiniTest::Assertions
  end

  def test_should_process_messages
    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('test') do |msg|
        assert_equal 'foo', msg[:text]
        cb.call
      end
      queue.push(:text => 'foo')
    end
  end

  def test_should_handle_worker_error
    async_test do |cb|
      TestErrorHandler.send(:define_method, :handle) do |ex|
        assert_equal 'oops', ex.message
        assert_equal 'RuntimeError', ex.class.name
        cb.call
      end

      queue = GirlFriday::WorkQueue.new('test', :error_handler => TestErrorHandler) do |msg|
        raise 'oops'
      end
      queue.push(:text => 'foo')
    end
  end

  def test_should_call_callback_when_complete
    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('test', :size => 1) do |msg|
        assert_equal 'foo', msg[:text]
        'camel'
      end
      queue.push(:text => 'foo') do |result|
        assert_equal 'camel', result
        cb.call
      end
    end
  end

  def test_should_provide_status
    mutex = Mutex.new
    total = 200
    count = 0
    incr = Proc.new do
      mutex.synchronize do
        count += 1
      end
    end

    actual = nil
    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('image_crawler', :size => 3) do |msg|
        mycount = incr.call
        actual = queue.status if mycount == 100
        cb.call if mycount == total
      end
      total.times do |idx|
        queue.push(:text => 'foo')
      end
    end

    refute_nil actual
    refute_nil actual['image_crawler']
    metrics = actual['image_crawler']
    assert metrics[:total_queued] > 0
    assert metrics[:total_queued] <= total
    assert_equal 3, metrics[:pool_size]
    assert_equal 3, metrics[:busy]
    assert_equal 0, metrics[:ready]
    assert(metrics[:backlog] > 0)
    assert(metrics[:total_processed] > 0)
  end

  def test_should_persist_with_redis
    begin
      require 'redis'
      redis = Redis.new
      redis.flushdb
    rescue LoadError
      return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
    rescue Errno::ECONNREFUSED
      return puts 'Skipping redis test, not running locally'
    end

    mutex = Mutex.new
    total = 100
    count = 0
    incr = Proc.new do
      mutex.synchronize do
        count += 1
      end
    end

    async_test do |cb|
      queue = GirlFriday::WorkQueue.new('test', :size => 2, :store => GirlFriday::Store::Redis) do |msg|
        incr.call
        cb.call if count == total
      end
      total.times do
        queue.push(:text => 'foo')
      end
    end
  end

  def test_should_persist_with_redis_instance
    begin
      require 'redis'
      redis = Redis.new
      redis.flushdb
    rescue LoadError
      return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
    rescue Errno::ECONNREFUSED
{'fraction_non_alphanumeric': 0.10065206487209497, 'fraction_numerical': 0.007356629326199632, 'mean_word_length': 2.424155695477962, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 30, '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': '2326839', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1911, 'n_words': 503}