require 'job_uniqueness/spec_helper' RSpec.describe CanvasSync::JobUniqueness::Compat::Sidekiq do let(:config) { defined?(Sidekiq::Config) ? double(Sidekiq::Config) : class_double(Sidekiq) } let(:client_middleware) { double(Sidekiq::Middleware::Chain) } context 'client' do it 'adds client middleware' do allow(Sidekiq).to receive(:configure_client).and_yield(config) expect(config).to receive(:client_middleware).and_yield(client_middleware) expect(client_middleware).to receive(:insert_before).with(anything, CanvasSync::JobUniqueness::Compat::Sidekiq::ClientMiddleware) CanvasSync::JobUniqueness::Compat::Sidekiq.configure end end context 'server' do let(:server_middleware) { double(Sidekiq::Middleware::Chain) } it 'adds client and server middleware' do allow(Sidekiq).to receive(:configure_server).and_yield(config) expect(config).to receive(:client_middleware).and_yield(client_middleware) expect(config).to receive(:server_middleware).and_yield(server_middleware) expect(client_middleware).to receive(:insert_before).with(anything, CanvasSync::JobUniqueness::Compat::Sidekiq::ClientMiddleware) expect(server_middleware).to receive(:insert_after).with(anything, CanvasSync::JobUniqueness::Compat::Sidekiq::ServerMiddleware) CanvasSync::JobUniqueness::Compat::Sidekiq.configure end end context 'Job Extension' do let!(:dummy_worker) do Class.new do include Sidekiq::Worker end end it 'includes UniqueJobExtension' do expect(dummy_worker < CanvasSync::JobUniqueness::Compat::Sidekiq::WorkerExtension).to be true end it 'has the ensure_uniqueness method' do expect(dummy_worker.method(:ensure_uniqueness)).to be_present end end context 'Job' do include_context 'with TestWorker' it 'runs as expected' do strategy = CanvasSync::JobUniqueness::Strategy::UntilExecuted.new(nil) allow_any_instance_of(CanvasSync::JobUniqueness::LockContext).to receive(:lock_strategy) do |lock_context| strategy.instance_variable_set(:@lock_context, lock_context) strategy end allow(CanvasSync::JobUniqueness::Strategy::UntilExecuted).to receive(:new).and_return(strategy) expect(strategy).to receive(:on_enqueue).and_call_original expect(strategy).to receive(:on_perform).and_call_original expect(strategy).to receive(:batch_callback).with(:complete, anything).and_call_original expect(strategy).to receive(:batch_callback).with(:success, anything).and_call_original expect_any_instance_of(TestWorker).to receive(:perform) Sidekiq::Testing.inline! do worker.perform_async end end end end