title: How to test retries and failures in resque-retry and Rails 4?
body: <p>I am trying to write a spec that tests the retry functionality of resque-retry and I can not seem to get the tests to hit the binding.pry's correctly. Is there a way to test this functionality using rspec 3 so I can verify they are functioning as intended?</p>
  
  <p>This is a request spec and I am trying to simulate a live request via fixtures, but no matter what I try I can't seem to get the job to retry. </p>
  
  <pre><code>gem 'resque', require: 'resque/server'
  gem 'resque-web', require: 'resque_web'
  gem 'resque-scheduler'
  gem 'resque-retry'
  gem 'resque-lock-timeout'
  </code></pre>
  
  <p>I am using <a href="https://github.com/leshill/resque_spec" rel="nofollow noreferrer">resque_rspec</a>, and trying this <a href="http://unknownerror.org/opensource/resque/resque/q/stackoverflow/5141378/how-to-bridge-the-testing-using-resque-with-rspec-examples" rel="nofollow noreferrer">testing strategy</a>.</p>
  
  <p><strong>Partial Spec</strong></p>
  
  <pre><code>it 'retries it' do
    stub_request(:any, /.*api.bigcartel.*/).to_return(body: '{}', status: 200)
    @order_shipped_json['order']['originator_id'] = @provider_order
    post "/hook/shops/#{@shop.id}", @order_shipped_json.to_json, format: :json
    ResqueSpec.perform_all(queue_name)
    ???
  end
  </code></pre>
  
  <p><strong>Queue Job</strong></p>
  
  <pre><code>class QueueHook
    extend Resque::Plugins::LockTimeout
    extend Resque::Plugins::Retry
    extend QueueLock
    extend QueueLogger
  
    @queue = AppSettings.queues[:hook_queue_name].to_sym
    @lock_timeout = 600
    @retry_exceptions = [QueueError::LockFailed]
    @retry_limit = 600
    @retry_delay = 1
  
    class &lt;&lt; self
      def perform(web_hook_payload_id, _whiplash_customer_id)
        ActiveRecord::Base.clear_active_connections!
        @web_hook_payload = WebHookPayload.find(web_hook_payload_id)
        klass_constructor
        @hook.process_event
      end
  
      def identifier(_web_hook_payload_id, whiplash_customer_id)
        "lock:integration_hook:#{whiplash_customer_id}"
      end
  
      def after_perform_delete_webhook(_web_hook_payload_id, _whiplash_customer_id)
        @web_hook_payload.destroy
      end
  
      private
  
      ...
    end
  end
  </code></pre>
  
  <p><strong>Queue Job Modules</strong></p>
  
  <pre><code>module QueueLogger
    def before_perform_log_job(*args)
      Rails.logger.info "[Resque][#{self}] running with #{args.inspect}..."
    end
  
    def on_failure_log_job(*args)
      message = "[Resque][#{self}] failed with #{args.inspect}..."
      run_counters
      Rails.logger.info message_builder(message)
    end
  
    private
  
    def run_counters
      @num_attempts += retry_attempt
      @all_attempts += retry_limit
    end
  
    def message_builder(message)
      return message unless @num_attempts
      return message += " Retrying (attempt ##{@num_attempts + 1})" if @num_attempts &lt; @all_attempts
      message += ' Giving up.'
      message
    end
  end
  
  module QueueLock
    def loner_enqueue_failed(*args)
      Rails.logger.info "[Resque][#{self}] is already enqueued: #{args.inspect}..."
    end
  
    def lock_failed(*)
      raise QueueError::LockFailed
    end
  end
  </code></pre>