require 'spec_helper' RSpec.describe CanvasSync::JobBatches::BatchAwareJob do include ActiveJob::TestHelper after do clear_enqueued_jobs clear_performed_jobs end context "When Performing" do context 'when without batch' do it 'just yields' do expect(CanvasSync::JobBatches::Batch).not_to receive(:process_successful_job) expect(CanvasSync::JobBatches::Batch).not_to receive(:process_failed_job) expect_any_instance_of(BatchTestJobBase).to receive(:perform) BatchTestJobBase.perform_now end end context 'when in batch' do let(:bid) { 'SAMPLEBID' } context 'when successful' do it 'yields' do expect_any_instance_of(BatchTestJobBase).to receive(:perform) BatchTestJobBase.perform_now end it 'calls process_successful_job' do job = BatchTestJobBase.new job.instance_variable_set(:@bid, bid) expect(CanvasSync::JobBatches::Batch).to receive(:process_successful_job).with(bid, job.job_id) job.perform_now end end context 'when failed' do it 'calls process_failed_job and reraises exception' do reraised = false job = FailingBatchTestJobBase.new job.instance_variable_set(:@bid, bid) expect(CanvasSync::JobBatches::Batch).to receive(:process_failed_job) begin job.perform_now rescue reraised = true end expect(reraised).to be_truthy end end end end context "When Enqueueing" do context 'when without batch' do it 'just yields' do expect(CanvasSync::JobBatches::Batch).not_to receive(:increment_job_queue) BatchTestJobBase.perform_later expect(BatchTestJobBase).to have_been_enqueued end end context 'when in batch' do let(:bid) { 'SAMPLEBID' } before do Thread.current[CanvasSync::JobBatches::CURRENT_BATCH_THREAD_KEY] = CanvasSync::JobBatches::Batch.new(bid) Thread.current[CanvasSync::JobBatches::CURRENT_BATCH_THREAD_KEY].instance_variable_set(:@open, true) end after { Thread.current[CanvasSync::JobBatches::CURRENT_BATCH_THREAD_KEY] = nil } it 'yields' do expect { BatchTestJobBase.perform_later }.to enqueue_job(BatchTestJobBase) end it 'assigns bid to job metadata' do job = BatchTestJobBase.perform_later expect(job.bid).to eq bid expect(job.serialize['batch_id']).to eq bid end end end context 'worker' do it 'defines method bid' do expect(ActiveJob::Base.instance_methods).to include(:bid) end it 'defines method batch' do expect(ActiveJob::Base.instance_methods).to include(:batch) end it 'defines method valid_within_batch?' do expect(ActiveJob::Base.instance_methods).to include(:valid_within_batch?) end end end