trying to do TDD! Can't test sidekiq workers called by models

89 views
Skip to first unread message

Chew Manfoo

unread,
Mar 17, 2015, 2:35:14 PM3/17/15
to sid...@googlegroups.com
here's what I see: https://gist.github.com/chewmanfoo/9b13c52c8ba409e25bc0

I started sidekiq like this:

bundle exec sidekiq -etest

here's my test:

  test "should execute action when executed" do
    ps = PathState.create(:name => "TestName", :action => "touch /tmp/PathStateTest")
    ps.execute
    result = `[ -f /tmp/PathStateTest ] && echo 0 || echo 1`
    assert result==0, "OOPS! PathState doesn't execute action when executed!"
  end

here's my PathState model:

class PathState < ActiveRecord::Base

  validates :name, presence: true

  def execute
    PathStateRunner.perform_async(id)
  end
end

here's PathStateRunner:

class PathStateRunner
  include Sidekiq::Worker

  sidekiq_options :retry => 2, :backtrace => true # job will retry according to retry mechanism specified

  sidekiq_retry_in do |count|
    15 * (count + 1) # (i.e. 15, 30, 45)
  end

  sidekiq_retries_exhausted do |msg|
    Sidekiq.logger.warn "---> Failed #{msg['class']} with #{msg['args']}: #{msg['error_message']}"
  end

  def perform(path_state_id)
    ps = PathState.find(path_state_id)

    @result = `#{ps.action} 2>&1`
    unless $?.exitstatus.zero?
      # failed state
      Sidekiq.logger.warn "---> PathStateRunner worker failed action: '#{ps.action}'"
    end
  end
end

What gives?

Mike Perham

unread,
Mar 17, 2015, 2:38:42 PM3/17/15
to sid...@googlegroups.com
You should not be running Sidekiq in the test environment.  The test runner will silently roll back database transactions so your scenario won't work.

--
You received this message because you are subscribed to the Google Groups "Sidekiq" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sidekiq+u...@googlegroups.com.
To post to this group, send email to sid...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sidekiq/d809fa09-7840-4c66-b995-fdf1d97222fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Mike Perham – Contributed Systems
Smart, effective open source infrastructure for your apps.

Chew Manfoo

unread,
Mar 17, 2015, 2:47:51 PM3/17/15
to sid...@googlegroups.com
Mike - sorry I know you're busy.  one more question:

So, you just don't test things that interact with sidekiq?  i.e. I can't make this test run so I should just delete this test?  I'm new to TDD.

Mike Perham

unread,
Mar 17, 2015, 2:52:46 PM3/17/15
to sid...@googlegroups.com
Did you read the Testing wiki page? https://github.com/mperham/sidekiq/wiki/Testing

You test the "two" sides to Sidekiq:

1. Verify that jobs are created by your controllers/models.  This verifies that a job will push to Redis.

  assert_equal 1, MyWorker.jobs.size

2. Verify that jobs execute correctly.  This verifies that a job pulled from Redis will run.

  MyWorker.new.perform(some, args)
  assert_something



--
You received this message because you are subscribed to the Google Groups "Sidekiq" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sidekiq+u...@googlegroups.com.
To post to this group, send email to sid...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages