2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
require 'rubocop'
|
|
|
|
require_relative '../../../../rubocop/cop/scalability/cron_worker_context'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Scalability::CronWorkerContext, type: :rubocop do
|
2020-03-13 15:44:24 +05:30
|
|
|
include CopHelper
|
|
|
|
|
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
|
|
|
it 'adds an offense when including CronjobQueue' do
|
2020-06-23 00:09:42 +05:30
|
|
|
inspect_source(<<~CODE)
|
2020-03-13 15:44:24 +05:30
|
|
|
class SomeWorker
|
|
|
|
include CronjobQueue
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
|
|
|
|
expect(cop.offenses.size).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add offenses for other workers' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_no_offenses(<<~CODE)
|
2020-03-13 15:44:24 +05:30
|
|
|
class SomeWorker
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense when the class defines a context' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_no_offenses(<<~CODE)
|
2020-03-13 15:44:24 +05:30
|
|
|
class SomeWorker
|
|
|
|
include CronjobQueue
|
|
|
|
|
|
|
|
with_context user: 'bla'
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense when the worker calls `with_context`' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_no_offenses(<<~CODE)
|
2020-03-13 15:44:24 +05:30
|
|
|
class SomeWorker
|
|
|
|
include CronjobQueue
|
|
|
|
|
|
|
|
def perform
|
|
|
|
with_context(user: 'bla') do
|
|
|
|
# more work
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense when the worker calls `bulk_perform_async_with_contexts`' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_no_offenses(<<~CODE)
|
2020-03-13 15:44:24 +05:30
|
|
|
class SomeWorker
|
|
|
|
include CronjobQueue
|
|
|
|
|
|
|
|
def perform
|
|
|
|
SomeOtherWorker.bulk_perform_async_with_contexts(things,
|
|
|
|
arguments_proc: -> (thing) { thing.id },
|
|
|
|
context_proc: -> (thing) { { project: thing.project } })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense when the worker calls `bulk_perform_in_with_contexts`' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_no_offenses(<<~CODE)
|
2020-03-13 15:44:24 +05:30
|
|
|
class SomeWorker
|
|
|
|
include CronjobQueue
|
|
|
|
|
|
|
|
def perform
|
|
|
|
SomeOtherWorker.bulk_perform_in_with_contexts(10.minutes, things,
|
|
|
|
arguments_proc: -> (thing) { thing.id },
|
|
|
|
context_proc: -> (thing) { { project: thing.project } })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
end
|