Sha256: 3549b18b83d4cd2bad5a512053f739e2a4e463bc5a9c06e3e75d7f87d1283b99

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'spec_helper'
require 'sidekiq/debounce'
require 'sidekiq'

class DebouncedWorker
  include Sidekiq::Worker

  sidekiq_options debounce: true

  def perform(_a, _b); end
end

describe Sidekiq::Debounce do
  before do
    stub_scheduled_set
  end

  after do
    Sidekiq.redis(&:flushdb)
  end

  let(:set) { Sidekiq::ScheduledSet.new }
  let(:sorted_entry) { Sidekiq::SortedEntry.new(set, 0, {jid: '54321'}.to_json) }

  it 'queues a job normally at first' do
    DebouncedWorker.perform_in(60, 'foo', 'bar')
    set.size.must_equal 1, 'set.size must be 1'
  end

  it 'ignores repeat jobs within the debounce time and reschedules' do
    sorted_entry.expects(:reschedule)

    DebouncedWorker.perform_in(60, 'foo', 'bar')
    DebouncedWorker.perform_in(60, 'foo', 'bar')
    set.size.must_equal 1, 'set.size must be 1'
  end

  it 'debounces jobs based on their arguments' do
    DebouncedWorker.perform_in(60, 'boo', 'far')
    DebouncedWorker.perform_in(60, 'foo', 'bar')
    set.size.must_equal 2, 'set.size must be 2'
  end

  it 'creates the job immediately when given an instant job' do
    DebouncedWorker.perform_async('foo', 'bar')
    set.size.must_equal 0, 'set.size must be 0'
  end

  def stub_scheduled_set
    set.stubs(:find_job).returns(sorted_entry)
    Sidekiq::Debounce.any_instance.stubs(:scheduled_set).returns(set)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-debounce-1.1.0 spec/sidekiq/debounce_spec.rb