<commit_before>require 'spec_helper'

# A few specs to ensure that the ideas given in the customizing_que document
# stay functional.
describe "Customizing Que" do
  it "Cron should allow for easy recurring jobs" do
    begin
      class CronJob < Que::Job
        # Default repetition interval in seconds. Can be overridden in
        # subclasses. Can use 1.minute if using Rails.
        INTERVAL = 60

        attr_reader :start_at, :end_at, :run_again_at, :time_range

        def _run
          args = attrs[:args].first
          @start_at, @end_at = Time.at(args.delete('start_at')), Time.at(args.delete('end_at'))
          @run_again_at = @end_at + self.class::INTERVAL
          @time_range = @start_at...@end_at

          super

          args['start_at'] = @end_at.to_f
          args['end_at']   = @run_again_at.to_f
          self.class.enqueue(args, run_at: @run_again_at)
        end
      end

      class MyCronJob < CronJob
        INTERVAL = 1.5

        def run(args)
          $args       = args.dup
          $time_range = time_range
        end
      end

      t = Time.now - 1000
      MyCronJob.enqueue :start_at => t.to_f, :end_at => t.to_f + 1.5, :arg => 4

      $args.should be nil
      $time_range.should be nil

      Que::Job.work

      $args.should == {'arg' => 4}
      $time_range.begin.to_f.should == t.to_f
      $time_range.end.to_f.should == t.to_f + 1.5
      $time_range.exclude_end?.should be true

      DB[:que_jobs].get(:run_at).to_f.should be_within(0.000001).of(t.to_f + 3.0)
      args = JSON.parse(DB[:que_jobs].get(:args))
      args.should == [{'arg' => 4, 'start_at' => t.to_f + 1.5, 'end_at' => t.to_f + 3.0}]
    ensure
      $args       = nil
      $time_range = nil
    end
  end

  it "Object#delay should allow for simpler job enqueueing" do
    begin
      class Delayed < Que::Job
        def run(receiver, method, args)
          Marshal.load(receiver).send method, *Marshal.load(args)
        end
      end

      class DelayedAction
        def initialize(receiver)
          @receiver = receiver
        end

        def method_missing(method, *args)
          Delayed.enqueue Marshal.dump(@receiver), method, Marshal.dump(args)
        end
      end
<commit_msg>Eliminate some intermittent spec failures.<commit_after>require 'spec_helper'

# A few specs to ensure that the ideas given in the customizing_que document
# stay functional.
describe "Customizing Que" do
  it "Cron should allow for easy recurring jobs" do
    begin
      class CronJob < Que::Job
        # Default repetition interval in seconds. Can be overridden in
        # subclasses. Can use 1.minute if using Rails.
        INTERVAL = 60

        attr_reader :start_at, :end_at, :run_again_at, :time_range

        def _run
          args = attrs[:args].first
          @start_at, @end_at = Time.at(args.delete('start_at')), Time.at(args.delete('end_at'))
          @run_again_at = @end_at + self.class::INTERVAL
          @time_range = @start_at...@end_at

          super

          args['start_at'] = @end_at.to_f
          args['end_at']   = @run_again_at.to_f
          self.class.enqueue(args, run_at: @run_again_at)
        end
      end

      class MyCronJob < CronJob
        INTERVAL = 1.5

        def run(args)
          $args       = args.dup
          $time_range = time_range
        end
      end

      t = (Time.now - 1000).to_f.round(6)
      MyCronJob.enqueue :start_at => t, :end_at => t + 1.5, :arg => 4

      $args.should be nil
      $time_range.should be nil

      Que::Job.work

      $args.should == {'arg' => 4}
      $time_range.begin.to_f.round(6).should be_within(0.000001).of t
      $time_range.end.to_f.round(6).should be_within(0.000001).of t + 1.5
      $time_range.exclude_end?.should be true

      DB[:que_jobs].get(:run_at).to_f.round(6).should be_within(0.000001).of(t + 3.0)
      args = JSON.parse(DB[:que_jobs].get(:args)).first
      args.keys.should == ['arg', 'start_at', 'end_at']
      args['arg'].should == 4
      args['start_at'].should be_within(0.000001).of(t + 1.5)
      args['end_at'].should be_within(0.000001).of(t + 3.0)
    ensure
      $args       = nil
      $time_range = nil
    end
  end

  it "Object#delay should allow for simpler job enqueueing" do
    begin
      class Delayed < Que::Job
        def run(receiver, method, args)
          Marshal.load(receiver).send method, *Marshal.load(args)
        end
      end

      class DelayedAction
        def initialize(receiver)
          @receiver = receiver
        end

        def method_missing(method, *args)
          Delayed.enqueue Marshal.dump(@receiver), method, Marshal.dump(args)
        end
      end

{'fraction_non_alphanumeric': 0.11588625187085738, 'fraction_numerical': 0.01924310455420141, 'mean_word_length': 2.190995907230559, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, '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': '3722519', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1540, 'n_words': 371}