<commit_before>require 'spec_helper'

describe Que::Worker do
  it "should work jobs when started until there are none available" do
    begin
      Que::Job.queue
      Que::Job.queue
      DB[:que_jobs].count.should be 2

      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }
      DB[:que_jobs].count.should be 0
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "#wake! should return truthy if the worker was asleep and is woken up, at which point it should work until no jobs are available" do
    begin
      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }

      Que::Job.queue
      Que::Job.queue
      DB[:que_jobs].count.should be 2

      @worker.wake!.should be true
      sleep_until { @worker.sleeping? }
      DB[:que_jobs].count.should be 0
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "#wake! should return falsy if the worker was already working" do
    begin
      BlockJob.queue
      @worker = Que::Worker.new

      $q1.pop
      DB[:que_jobs].count.should be 1
      @worker.wake!.should be nil
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should not be deterred by a job that raises an error" do
    begin
      ErrorJob.queue :priority => 1
      Que::Job.queue :priority => 5

      @worker = Que::Worker.new

      sleep_until { @worker.sleeping? }

      DB[:que_jobs].count.should be 1
      job = DB[:que_jobs].first
      job[:job_class].should == 'ErrorJob'
      job[:run_at].should be_within(3).of Time.now + 4
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should receive and respect a notification to stop down when it is working, after its current job completes" do
    begin
      BlockJob.queue :priority => 1
      Que::Job.queue :priority => 5
      DB[:que_jobs].count.should be 2

      @worker = Que::Worker.new

      $q1.pop
      @worker.stop
      $q2.push nil

      @worker.wait_until_stopped

      DB[:que_jobs].count.should be 1
      job = DB[:que_jobs].first
      job[:job_class].should == 'Que::Job'
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should receive and respect a notification to stop when it is currently asleep" do
    begin
      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }

      @worker.stop
      @worker.wait_until_stopped
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should receive and respect a notification to stop immediately when it is working, and kill the job" do
    begin
      # Worker#stop! can leave the database connection in an unpredictable
      # state, which would impact the rest of the tests, so we need a special
      # connection for it.
      pg = NEW_PG_CONNECTION.call
      Que.connection = pg

      BlockJob.queue

      @worker = Que::Worker.new
      $q1.pop
      @worker.stop!
      @worker.wait_until_stopped
    ensure
      pg.close if pg
    end
  end

  it "should receive and respect a notification to stop immediately when it is currently asleep" do
    begin
      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }

      @worker.stop!
      @worker.wait_until_stopped
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end
end<commit_msg>Spec that Worker#stop! doesn't trigger the error handling code.<commit_after>require 'spec_helper'

describe Que::Worker do
  it "should work jobs when started until there are none available" do
    begin
      Que::Job.queue
      Que::Job.queue
      DB[:que_jobs].count.should be 2

      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }
      DB[:que_jobs].count.should be 0
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "#wake! should return truthy if the worker was asleep and is woken up, at which point it should work until no jobs are available" do
    begin
      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }

      Que::Job.queue
      Que::Job.queue
      DB[:que_jobs].count.should be 2

      @worker.wake!.should be true
      sleep_until { @worker.sleeping? }
      DB[:que_jobs].count.should be 0
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "#wake! should return falsy if the worker was already working" do
    begin
      BlockJob.queue
      @worker = Que::Worker.new

      $q1.pop
      DB[:que_jobs].count.should be 1
      @worker.wake!.should be nil
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should not be deterred by a job that raises an error" do
    begin
      ErrorJob.queue :priority => 1
      Que::Job.queue :priority => 5

      @worker = Que::Worker.new

      sleep_until { @worker.sleeping? }

      DB[:que_jobs].count.should be 1
      job = DB[:que_jobs].first
      job[:job_class].should == 'ErrorJob'
      job[:run_at].should be_within(3).of Time.now + 4
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should receive and respect a notification to stop down when it is working, after its current job completes" do
    begin
      BlockJob.queue :priority => 1
      Que::Job.queue :priority => 5
      DB[:que_jobs].count.should be 2

      @worker = Que::Worker.new

      $q1.pop
      @worker.stop
      $q2.push nil

      @worker.wait_until_stopped

      DB[:que_jobs].count.should be 1
      job = DB[:que_jobs].first
      job[:job_class].should == 'Que::Job'
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should receive and respect a notification to stop when it is currently asleep" do
    begin
      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }

      @worker.stop
      @worker.wait_until_stopped
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end

  it "should receive and respect a notification to stop immediately when it is working, and kill the job" do
    begin
      # Worker#stop! can leave the database connection in an unpredictable
      # state, which would impact the rest of the tests, so we need a special
      # connection for it.
      pg = NEW_PG_CONNECTION.call
      Que.connection = pg

      error_handled = false
      Que.error_handler = proc { |error| error_handled = true }

      BlockJob.queue

      @worker = Que::Worker.new
      $q1.pop
      @worker.stop!
      @worker.wait_until_stopped

      error_handled.should be false

      job = DB[:que_jobs].first
      job[:error_count].should be 0
      job[:last_error].should be nil
    ensure
      Que.error_handler = nil
      pg.close if pg
    end
  end

  it "should receive and respect a notification to stop immediately when it is currently asleep" do
    begin
      @worker = Que::Worker.new
      sleep_until { @worker.sleeping? }

      @worker.stop!
      @worker.wait_until_stopped
    ensure
      if @worker
        @worker.thread.kill
        @worker.thread.join
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08557717941882165, 'fraction_numerical': 0.004932018128499067, 'mean_word_length': 2.123646960865945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '482333', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2231, 'n_words': 707}