Skip to content
This repository was archived by the owner on Apr 1, 2023. It is now read-only.
This repository was archived by the owner on Apr 1, 2023. It is now read-only.

Redis::CannotConnectError also after enabling testing mode #152

Description

@tommasodri

I'm trying to test some controllers of my application with rspec, one of them use background jobs with sidekiq and sidekiq-status.
I folowed the guide (https://github.com/utgarda/sidekiq-status#testing) to enabling testing for inline mode but when i launch tests Redis::CannotConnectError exceptions is fired.
The worker is launched inside a method of my controller with:

job_id = ProductImportWorker.perform_async(file_path)

in spec_helper.rb i added the folowing lines:

  require 'sidekiq/testing/inline'
  require 'sidekiq-status/testing/inline'
  Sidekiq::Testing.inline!

Folowing the code of my worker class:

class ProductImportWorker

  include Sidekiq::Worker
  include Sidekiq::Status::Worker # enables job status tracking

  def perform(file_path, debug = false)

    logger.info "ProductImportWorker START import from file: #{file_path} ..."
    # read all import_data hash from file saved in temp directory


    unless File.exist?(file_path)
      raise "source file not found"
    end

    import_data = File.open(file_path, "r") {|from_file| Marshal.load(from_file)}

    moltiplicator = 100.0 / import_data&.size

    logger.info "ProductImportWorker START import #{import_data&.size} products..."
    # set first sidekiq status percentage end message
    at 0, "Started"

    ActiveRecord::Base.transaction do

      import_data.each_with_index do |product_hash, index|

        if debug
          sleep(0.05)
        else
          # for each product verify if it exists, then update it, else create a new one
          internal_code = product_hash.dig(:internal_code)
          existing_product = Product.find_by_internal_code(internal_code.to_s)
          if existing_product # Update existing product
            existing_product.update!(product_hash)
          else # Create a new product
            Product.create!(product_hash)
          end
        end

        # percentage of completion overall process
        perc = ((index + 1) * moltiplicator).to_i
        logger.info "CREATED product #{index}, completion: #{perc}%"
        # update sidekiq status percentage end message
        at perc, "Waiting ..."

      end
    end

    File.delete(file_path) if File.exist?(file_path)

    logger.info "ProductImportWorker FINISH"
    at 100, "Finish"
  end
end

if i comment include Sidekiq::Status::Worker with all at commands the worker is executed and test pass so the sidekiq/testing/inline works.
Seems that import require 'sidekiq-status/testing/inline' doesn't work properly, someone has encountered the same problem ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions