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

Redis::CannotConnectError also after enabling testing mode #152

Open
tommasodri opened this issue Sep 5, 2019 · 1 comment
Open

Redis::CannotConnectError also after enabling testing mode #152

tommasodri opened this issue Sep 5, 2019 · 1 comment

Comments

@tommasodri
Copy link

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 ?

@dawidof
Copy link

dawidof commented Jun 23, 2022

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants