<commit_before>require 'spec_helper'
require 'sneakers'
require 'timeout'


class DummyWorker
  include Sneakers::Worker
  from_queue 'downloads',
             :durable => false,
             :ack => false,
             :threads => 50,
             :prefetch => 40,
             :timeout_job_after => 1,
             :exchange => 'dummy',
             :heartbeat => 5

  def work(msg)
  end
end

class DefaultsWorker
  include Sneakers::Worker
  from_queue 'defaults'

  def work(msg)
  end
end

class TimeoutWorker
  include Sneakers::Worker
  from_queue 'defaults',
    :timeout_job_after => 0.5,
    :ack => true

  def work(msg)
  end
end

class AcksWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => true

  def work(msg)
    if msg == :ack
      ack!
    elsif msg == :nack
      nack!
    elsif msg == :reject
      reject!
    else
      msg
    end
  end
end

class PublishingWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => false,
             :exchange => 'foochange'

  def work(msg)
    publish msg, :to_queue => 'target'
  end
end



class LoggingWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => false

  def work(msg)
    logger.info "hello"
  end
end


class MetricsWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => true,
             :timeout_job_after => 0.5

  def work(msg)
    metrics.increment "foobar"
    msg
  end
end

class WithParamsWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => true,
             :timeout_job_after => 0.5

  def work_with_params(msg, delivery_info, metadata)
    msg
  end
end


class TestPool
  def process(*args,&block)
    block.call
  end
end

def with_test_queuefactory(ctx, ack=true, msg=nil, nowork=false)
  qf = Object.new
  q = Object.new
  s = Object.new
  hdr = Object.new
  mock(qf).build_queue(anything, anything, anything) { q }
  mock(q).subscribe(anything){ s }

  mock(s).each(anything) { |h,b| b.call(hdr, msg) unless nowork }
  mock(hdr).ack{true} if !nowork && ack
  mock(hdr).reject{true} if !nowork && !ack

  mock(ctx).queue_factory { qf } # should return our own
end

describe Sneakers::Worker do
  before do
    @queue = Object.new
    @exchange = Object.new
    stub(@queue).name { 'test-queue' }
    stub(@queue).opts { {} }
    stub(@queue).exchange { @exchange }

    Sneakers.clear!
    Sneakers.configure(:daemonize => true, :log => 'sneakers.log')
    Sneakers::Worker.configure_metrics
  end

  describe ".enqueue" do
    it "publishes a message to the class queue" do
      message = "my message"
      mock = MiniTest::Mock.new

      mock.expect(:publish, true) do |msg, opts|
        msg.must_equal(message)
        opts.must_equal(:to_queue => "defaults")
      end

      stub(Sneakers::Publisher).new { mock }
      DefaultsWorker.enqueue(message)
    end
  end

  describe "#initialize" do
    describe "builds an internal queue" do
      before do
        @dummy_q = DummyWorker.new.queue
        @defaults_q = DefaultsWorker.new.queue
      end

      it "should build a queue with correct configuration given defaults" do
        @defaults_q.name.must_equal('defaults')
        @defaults_q.opts.to_hash.must_equal(
          :runner_config_file => nil,
          :metrics => nil,
          :daemonize => true,
          :start_worker_delay => 0.2,
          :workers => 4,
          :log => "sneakers.log",
          :pid_path => "sneakers.pid",
          :timeout_job_after => 5,
          :prefetch => 10,
          :threads => 10,
          :durable => true,
          :ack => true,
          :amqp => "amqp://guest:guest@localhost:5672",
          :vhost => "/",
          :exchange => "sneakers",
          :exchange_type => :direct,
          :exchange_arguments => {},
          :hooks => {},
          :handler => Sneakers::Handlers::Oneshot,
          :heartbeat  =>  2
        )
      end

      it "should build a queue with given configuration" do
        @dummy_q.name.must_equal('downloads')
        @dummy_q.opts.to_hash.must_equal(
          :runner_config_file => nil,
          :metrics => nil,
          :daemonize => true,
          :start_worker_delay => 0.2,
          :workers => 4,
          :log => "sneakers.log",
          :pid_path => "sneakers.pid",
          :timeout_job_after => 1,
          :prefetch => 40,
          :threads => 50,
          :durable => false,
          :ack => false,
          :amqp => "amqp://guest:guest@localhost:5672",
          :vhost => "/",
          :exchange => "dummy",
          :exchange_type => :direct,
          :exchange_arguments => {},
          :hooks => {},
          :handler => Sneakers::Handlers::Oneshot,
          :heartbeat => 5
        )
      end<commit_msg>fix broken tests<commit_after>require 'spec_helper'
require 'sneakers'
require 'timeout'


class DummyWorker
  include Sneakers::Worker
  from_queue 'downloads',
             :durable => false,
             :ack => false,
             :threads => 50,
             :prefetch => 40,
             :timeout_job_after => 1,
             :exchange => 'dummy',
             :heartbeat => 5

  def work(msg)
  end
end

class DefaultsWorker
  include Sneakers::Worker
  from_queue 'defaults'

  def work(msg)
  end
end

class TimeoutWorker
  include Sneakers::Worker
  from_queue 'defaults',
    :timeout_job_after => 0.5,
    :ack => true

  def work(msg)
  end
end

class AcksWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => true

  def work(msg)
    if msg == :ack
      ack!
    elsif msg == :nack
      nack!
    elsif msg == :reject
      reject!
    else
      msg
    end
  end
end

class PublishingWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => false,
             :exchange => 'foochange'

  def work(msg)
    publish msg, :to_queue => 'target'
  end
end



class LoggingWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => false

  def work(msg)
    logger.info "hello"
  end
end


class MetricsWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => true,
             :timeout_job_after => 0.5

  def work(msg)
    metrics.increment "foobar"
    msg
  end
end

class WithParamsWorker
  include Sneakers::Worker
  from_queue 'defaults',
             :ack => true,
             :timeout_job_after => 0.5

  def work_with_params(msg, delivery_info, metadata)
    msg
  end
end


class TestPool
  def process(*args,&block)
    block.call
  end
end

def with_test_queuefactory(ctx, ack=true, msg=nil, nowork=false)
  qf = Object.new
  q = Object.new
  s = Object.new
  hdr = Object.new
  mock(qf).build_queue(anything, anything, anything) { q }
  mock(q).subscribe(anything){ s }

  mock(s).each(anything) { |h,b| b.call(hdr, msg) unless nowork }
  mock(hdr).ack{true} if !nowork && ack
  mock(hdr).reject{true} if !nowork && !ack

  mock(ctx).queue_factory { qf } # should return our own
end

describe Sneakers::Worker do
  before do
    @queue = Object.new
    @exchange = Object.new
    stub(@queue).name { 'test-queue' }
    stub(@queue).opts { {} }
    stub(@queue).exchange { @exchange }

    Sneakers.clear!
    Sneakers.configure(:daemonize => true, :log => 'sneakers.log')
    Sneakers::Worker.configure_metrics
  end

  describe ".enqueue" do
    it "publishes a message to the class queue" do
      message = "my message"
      mock = MiniTest::Mock.new

      mock.expect(:publish, true) do |msg, opts|
        msg.must_equal(message)
        opts.must_equal(:to_queue => "defaults")
      end

      stub(Sneakers::Publisher).new { mock }
      DefaultsWorker.enqueue(message)
    end
  end

  describe "#initialize" do
    describe "builds an internal queue" do
      before do
        @dummy_q = DummyWorker.new.queue
        @defaults_q = DefaultsWorker.new.queue
      end

      it "should build a queue with correct configuration given defaults" do
        @defaults_q.name.must_equal('defaults')
        @defaults_q.opts.to_hash.must_equal(
          :runner_config_file => nil,
          :metrics => nil,
          :daemonize => true,
          :start_worker_delay => 0.2,
          :workers => 4,
          :log => "sneakers.log",
          :pid_path => "sneakers.pid",
          :timeout_job_after => 5,
          :prefetch => 10,
          :threads => 10,
          :share_threads => false,
          :durable => true,
          :ack => true,
          :amqp => "amqp://guest:guest@localhost:5672",
          :vhost => "/",
          :exchange => "sneakers",
          :exchange_type => :direct,
          :exchange_arguments => {},
          :hooks => {},
          :handler => Sneakers::Handlers::Oneshot,
          :heartbeat  =>  2
        )
      end

      it "should build a queue with given configuration" do
        @dummy_q.name.must_equal('downloads')
        @dummy_q.opts.to_hash.must_equal(
          :runner_config_file => nil,
          :metrics => nil,
          :daemonize => true,
          :start_worker_delay => 0.2,
          :workers => 4,
          :log => "sneakers.log",
          :pid_path => "sneakers.pid",
          :timeout_job_after => 1,
          :prefetch => 40,
          :threads => 50,
          :share_threads => false,
          :durable => false,
          :ack => false,
          :amqp => "amqp://guest:guest@localhost:5672",
          :vhost => "/",
          :exchange => "dummy",
          :exchange_type => :direct,
          :exchange_arguments => {},
          :hooks => {},
          :handler => Sneakers::Handlers::Oneshot,
          :heartbeat => 5
        )
      end
{'fraction_non_alphanumeric': 0.12174634449860001, 'fraction_numerical': 0.007881364720522659, 'mean_word_length': 2.1383013342011066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '789242', 'n_tokens_mistral': 3181, 'n_tokens_neox': 3046, 'n_words': 729}