<commit_before>require 'spec_helper'


describe Appsignal::Hooks::ShoryukenMiddleware do

  let(:current_transaction) { background_job_transaction }

  let(:worker_instance) { double }
  let(:queue) { double }
  let(:sqs_msg) {
    double(:attributes => {})
  }
  let(:body) {{}}

  before do
    Appsignal.stub(:is_ignored_exception? => false)
    Appsignal::Transaction.stub(:current => current_transaction)
    start_agent
  end

  context "with an erroring call" do
    let(:error) { VerySpecificError.new('on fire') }
    
    it "should add the exception to appsignal" do
      Appsignal::Transaction.any_instance.should_receive(:set_error).with(error)
    end

    after do
      begin
        Timecop.freeze(Time.parse('01-01-2001 10:01:00UTC')) do
          Appsignal::Hooks::ShoryukenMiddleware.new.call(worker_instance, queue, sqs_msg, body) do
            raise error
          end
        end
      rescue VerySpecificError
      end
    end<commit_msg>complete tests for any case in shoryuken hook/middleware<commit_after>require 'spec_helper'


describe Appsignal::Hooks::ShoryukenMiddleware do

  let(:current_transaction) { background_job_transaction }

  let(:worker_instance) { double }
  let(:queue) { double }
  let(:sqs_msg) {
    double(:attributes => {})
  }
  let(:body) {{}}

  before do
    Appsignal.stub(:is_ignored_exception? => false)
    Appsignal::Transaction.stub(:current => current_transaction)
    start_agent
  end

  context "with a performance call" do
    
    let(:queue) { 'some-funky-queue-name' }
    let(:sqs_msg) {
      double(:attributes => { 'SentTimestamp' => Time.parse('1976-11-18 0:00:00UTC').to_i * 1000 })
    }
    let(:body) {{
      'job_class' => 'TestClass',
      'arguments' => ['Model', "1"],
    }}

    it "should wrap in a transaction with the correct params" do
      Appsignal.should_receive(:monitor_transaction).with(
        'perform_job.shoryuken',
        :class => 'TestClass',
        :method => 'perform',
        :metadata => {
          :queue => 'some-funky-queue-name',
          'SentTimestamp' => 217123200000
        },
        :params => ['Model', "1"],
        :queue_start => Time.parse('1976-11-18 0:00:00UTC').utc
      )
    end

    after do
      Timecop.freeze(Time.parse('01-01-2001 10:01:00UTC')) do
        Appsignal::Hooks::ShoryukenMiddleware.new.call(worker_instance, queue, sqs_msg, body) do
          # nothing
        end
      end
    end
  end

  context "with an erroring call" do
    let(:error) { VerySpecificError.new('on fire') }
    
    it "should add the exception to appsignal" do
      Appsignal::Transaction.any_instance.should_receive(:set_error).with(error)
    end

    after do
      begin
        Timecop.freeze(Time.parse('01-01-2001 10:01:00UTC')) do
          Appsignal::Hooks::ShoryukenMiddleware.new.call(worker_instance, queue, sqs_msg, body) do
            raise error
          end
        end
      rescue VerySpecificError
      end
    end
{'fraction_non_alphanumeric': 0.12390572390572391, 'fraction_numerical': 0.028956228956228958, 'mean_word_length': 2.9092105263157895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '2967824', 'n_tokens_mistral': 1000, 'n_tokens_neox': 932, 'n_words': 190}