Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cron_worker_context_spec.rb « scalability « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28db12fd07576093d15079fb97dc5ca0d56e58a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/scalability/cron_worker_context'

RSpec.describe RuboCop::Cop::Scalability::CronWorkerContext do
  subject(:cop) { described_class.new }

  it 'adds an offense when including CronjobQueue' do
    expect_offense(<<~CODE)
      class SomeWorker
        include CronjobQueue
                ^^^^^^^^^^^^ Manually define an ApplicationContext for cronjob-workers.[...]
      end
    CODE
  end

  it 'does not add offenses for other workers' do
    expect_no_offenses(<<~CODE)
      class SomeWorker
      end
    CODE
  end

  it 'does not add an offense when the class defines a context' do
    expect_no_offenses(<<~CODE)
      class SomeWorker
        include CronjobQueue

        with_context user: 'bla'
      end
    CODE
  end

  it 'does not add an offense when the worker calls `with_context`' do
    expect_no_offenses(<<~CODE)
      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
    expect_no_offenses(<<~CODE)
      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
    expect_no_offenses(<<~CODE)
      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