测试sidekiq worker和retryset Rails minitest

时间:2015-11-07 08:04:41

标签: ruby sidekiq

我有一个可以捕获自定义异常的sidekiq middlware

require 'celluloid'
require 'sidekiq/middleware/server/retry_jobs'

module Sidekiq
  class RetryMiddleware < Sidekiq::Middleware::Server::RetryJobs

    def call(worker, msg, queue)
      yield
    rescue Sidekiq::Shutdown
      # ignore, will be pushed back onto queue during hard_shutdown
      raise
    rescue Sidekiq::Retries::Retry => e
      # force a retry (for workers that have retries disabled)
      msg['retry'] = e.max_retries
      attempt_retry(worker, msg, queue, e.cause)
      raise e.cause
    rescue Sidekiq::Retries::Fail => e
      # seriously, don't retry this
      raise e.cause
    rescue Exception => e
      # ignore, will be pushed back onto queue during hard_shutdown
      raise Sidekiq::Shutdown if exception_caused_by_shutdown?(e)
      raise e unless msg['retry']
      attempt_retry(worker, msg, queue, e)
      raise e
    end

  end
end

我的工作人员如下所示

class SomeWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(input_data)
    begin
     logic to insert data into db
    rescue Ione::Io::ConnectionClosedError => e
     raise Sidekiq::Retries::Retry
    end
  end

end

当我尝试测试SomeWorker时,peform方法正在添加作业以重试。 在测试中我没有看到中间件被称为

提前致谢

1 个答案:

答案 0 :(得分:1)

你这样做太难了,只需要调用方法。

RetryMiddleware.new.call(MyWorker.new, { ... }, 'default') do
  MyWorker.new.perform(...)
end