rspec-sidekiq 3.0.3 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES.md +43 -0
  3. data/LICENSE +12 -0
  4. data/README.md +217 -84
  5. data/lib/rspec/sidekiq/batch.rb +30 -3
  6. data/lib/rspec/sidekiq/configuration.rb +13 -2
  7. data/lib/rspec/sidekiq/matchers/base.rb +262 -0
  8. data/lib/rspec/sidekiq/matchers/be_delayed.rb +17 -3
  9. data/lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb +99 -0
  10. data/lib/rspec/sidekiq/matchers/have_enqueued_sidekiq_job.rb +25 -0
  11. data/lib/rspec/sidekiq/matchers.rb +13 -8
  12. data/lib/rspec/sidekiq/sidekiq.rb +1 -1
  13. data/lib/rspec/sidekiq/version.rb +1 -1
  14. data/lib/rspec-sidekiq.rb +2 -0
  15. metadata +130 -83
  16. data/.gitattributes +0 -22
  17. data/.gitignore +0 -2
  18. data/.rspec +0 -4
  19. data/.simplecov +0 -5
  20. data/Gemfile +0 -9
  21. data/lib/rspec/sidekiq/matchers/have_enqueued_job.rb +0 -183
  22. data/rspec-sidekiq.gemspec +0 -37
  23. data/spec/rspec/sidekiq/batch_spec.rb +0 -77
  24. data/spec/rspec/sidekiq/helpers/retries_exhausted_spec.rb +0 -40
  25. data/spec/rspec/sidekiq/matchers/be_delayed_spec.rb +0 -238
  26. data/spec/rspec/sidekiq/matchers/be_expired_in_spec.rb +0 -57
  27. data/spec/rspec/sidekiq/matchers/be_processed_in_spec.rb +0 -114
  28. data/spec/rspec/sidekiq/matchers/be_retryable_spec.rb +0 -129
  29. data/spec/rspec/sidekiq/matchers/be_unique_spec.rb +0 -115
  30. data/spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb +0 -228
  31. data/spec/rspec/sidekiq/matchers/save_backtrace_spec.rb +0 -136
  32. data/spec/rspec/sidekiq/sidekiq_spec.rb +0 -15
  33. data/spec/spec_helper.rb +0 -29
  34. data/spec/support/factories.rb +0 -33
  35. data/spec/support/init.rb +0 -6
  36. data/spec/support/test_action_mailer.rb +0 -6
  37. data/spec/support/test_job.rb +0 -6
  38. data/spec/support/test_resource.rb +0 -16
  39. data/spec/support/test_worker.rb +0 -8
  40. data/spec/support/test_worker_alternative.rb +0 -8
@@ -1,115 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe RSpec::Sidekiq::Matchers::BeUnique do
4
- shared_context 'a unique worker' do
5
- before do
6
- stub_const(module_constant, true)
7
- end
8
- before(:each) { subject.matches? @worker }
9
-
10
- describe 'expected usage' do
11
- it 'matches' do
12
- expect(@worker).to be_unique
13
- end
14
-
15
- describe '#failure_message' do
16
- it 'returns message' do
17
- expect(subject.failure_message).to eq "expected #{@worker} to be unique in the queue"
18
- end
19
- end
20
- end
21
-
22
- describe '#matches?' do
23
- context 'when condition matches' do
24
- it 'returns true' do
25
- expect(subject.matches? @worker).to be true
26
- end
27
- end
28
-
29
- context 'when condition does not match' do
30
- it 'returns false' do
31
- expect(subject.matches? create_worker unique: false).to be false
32
- end
33
- end
34
-
35
- describe '#failure_message_when_negated' do
36
- it 'returns message' do
37
- expect(subject.failure_message_when_negated).to eq "expected #{@worker} to not be unique in the queue"
38
- end
39
- end
40
- end
41
-
42
- describe '#description' do
43
- it 'returns description' do
44
- expect(subject.description).to eq 'be unique in the queue'
45
- end
46
- end
47
- end
48
-
49
- context 'a sidekiq-enterprise scheduled worker' do
50
- let(:interval) { 3.hours }
51
- let(:module_constant) { "Sidekiq::Enterprise" }
52
- before { @worker = create_worker unique_for: interval }
53
- include_context 'a unique worker'
54
- end
55
-
56
- context 'a sidekiq-unique-jobs scheduled worker' do
57
- let(:module_constant) { "SidekiqUniqueJobs" }
58
- before { @worker = create_worker unique: :all }
59
- include_context 'a unique worker'
60
- end
61
-
62
- context 'a sidekiq-unique-jobs regular worker' do
63
- let(:module_constant) { "SidekiqUniqueJobs" }
64
- before { @worker = create_worker unique: true }
65
- include_context 'a unique worker'
66
- end
67
-
68
- describe '#be_unique' do
69
- before do
70
- stub_const("SidekiqUniqueJobs", true)
71
- end
72
-
73
- it 'returns instance' do
74
- expect(be_unique).to be_kind_of RSpec::Sidekiq::Matchers::BeUnique::Base
75
- end
76
- end
77
-
78
- describe '#failure_message_when_negated' do
79
- before do
80
- stub_const("SidekiqUniqueJobs", true)
81
- end
82
-
83
- it 'returns message' do
84
- expect(subject.failure_message_when_negated).to eq "expected #{@worker} to not be unique in the queue"
85
- end
86
- end
87
-
88
- describe '#unique_key' do
89
- context "with Sidekiq Enterprise" do
90
- before do
91
- stub_const("Sidekiq::Enterprise", true)
92
- end
93
-
94
- it "returns the correct key" do
95
- expect(subject.unique_key).to eq('unique_for')
96
- end
97
- end
98
-
99
- context "with sidekiq-unique-jobs" do
100
- before do
101
- stub_const("SidekiqUniqueJobs", true)
102
- end
103
-
104
- it "returns the correct key" do
105
- expect(subject.unique_key).to eq('unique')
106
- end
107
- end
108
-
109
- context "without a uniquing solution" do
110
- it "raises an exception" do
111
- expect{subject.unique_key}.to raise_error
112
- end
113
- end
114
- end
115
- end
@@ -1,228 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe RSpec::Sidekiq::Matchers::HaveEnqueuedJob do
4
- let(:tomorrow) { DateTime.now + 1 }
5
- let(:interval) { 3.minutes }
6
- let(:argument_subject) { RSpec::Sidekiq::Matchers::HaveEnqueuedJob.new worker_args }
7
- let(:matcher_subject) { RSpec::Sidekiq::Matchers::HaveEnqueuedJob.new [be_a(String), be_a(Fixnum), true, be_a(Hash)] }
8
- let(:worker) { create_worker }
9
- let(:worker_args) { ['string', 1, true, { key: 'value', bar: :foo, nested: [{hash: true}] }] }
10
- let(:active_job) { create_active_job :mailers }
11
- let(:resource) { TestResource.new }
12
-
13
- before(:each) do
14
- worker.perform_async *worker_args
15
- active_job.perform_later 'someResource'
16
- active_job.perform_later(resource)
17
- TestActionMailer.testmail.deliver_later
18
- TestActionMailer.testmail(resource).deliver_later
19
- end
20
-
21
- describe 'expected usage' do
22
- context 'Sidekiq' do
23
- it 'matches' do
24
- expect(worker).to have_enqueued_sidekiq_job *worker_args
25
- end
26
-
27
- it 'matches on the global Worker queue' do
28
- expect(Sidekiq::Worker).to have_enqueued_sidekiq_job *worker_args
29
- end
30
-
31
- context 'perform_in' do
32
- let(:worker_args_in) { worker_args + ['in'] }
33
-
34
- before(:each) do
35
- worker.perform_in 3.minutes, *worker_args_in
36
- end
37
-
38
- it 'matches on an scheduled job with #perform_in' do
39
- expect(worker).to have_enqueued_sidekiq_job(*worker_args_in).in(interval)
40
- end
41
- end
42
-
43
- context 'perform_at' do
44
- let(:worker_args_at) { worker_args + ['at'] }
45
-
46
- before(:each) do
47
- worker.perform_at tomorrow, *worker_args_at
48
- end
49
-
50
- it 'matches on an scheduled job with #perform_at' do
51
- expect(worker).to have_enqueued_sidekiq_job(*worker_args_at).at(tomorrow)
52
- end
53
- end
54
- end
55
-
56
- context 'ActiveJob' do
57
- it 'matches on an enqueued ActiveJob' do
58
- expect(Sidekiq::Worker).to have_enqueued_sidekiq_job 'someResource'
59
- end
60
-
61
- it 'matches on an enqueued ActiveJob by global_id' do
62
- expect(Sidekiq::Worker).to have_enqueued_sidekiq_job('_aj_globalid' => resource.to_global_id.uri.to_s)
63
- end
64
- end
65
-
66
- context 'ActionMailer' do
67
- it 'matches on ActionMailer Job' do
68
- expect(Sidekiq::Worker).to have_enqueued_sidekiq_job(
69
- 'TestActionMailer',
70
- 'testmail',
71
- 'deliver_now'
72
- )
73
- end
74
-
75
- it 'matches on ActionMailer with a resource Job' do
76
- expect(Sidekiq::Worker).to have_enqueued_sidekiq_job(
77
- 'TestActionMailer',
78
- 'testmail',
79
- 'deliver_now',
80
- { '_aj_globalid' => resource.to_global_id.uri.to_s }
81
- )
82
- end
83
- end
84
- end
85
-
86
- describe '#have_enqueued_sidekiq_job' do
87
- it 'returns instance' do
88
- expect(have_enqueued_sidekiq_job).to be_a RSpec::Sidekiq::Matchers::HaveEnqueuedJob
89
- end
90
- end
91
-
92
- describe '#have_enqueued_job' do
93
- it 'returns instance' do
94
- expect(have_enqueued_job).to be_a RSpec::Sidekiq::Matchers::HaveEnqueuedJob
95
- end
96
-
97
- it 'provides deprecation warning' do
98
- expect { have_enqueued_job }.to output(/[DEPRECATION]/).to_stderr
99
- end
100
-
101
- it 'matches the same way have_enqueued_sidekiq_job does' do
102
- expect(worker).to have_enqueued_job *worker_args
103
- end
104
- end
105
-
106
- describe '#description' do
107
- it 'returns description' do
108
- argument_subject.matches? worker
109
- expect(argument_subject.description).to eq %{have an enqueued #{worker} job with arguments [\"string\", 1, true, {\"key\"=>\"value\", \"bar\"=>\"foo\", \"nested\"=>[{\"hash\"=>true}]}]}
110
- end
111
- end
112
-
113
- describe '#failure_message' do
114
- it 'returns message' do
115
- argument_subject.matches? worker
116
- expect(argument_subject.failure_message).to eq <<-eos.gsub(/^ {6}/, '').strip
117
- expected to have an enqueued #{worker} job
118
- arguments: [\"string\", 1, true, {\"key\"=>\"value\", \"bar\"=>\"foo\", \"nested\"=>[{\"hash\"=>true}]}]
119
- found
120
- arguments: [[\"string\", 1, true, {\"key\"=>\"value\", \"bar\"=>\"foo\", \"nested\"=>[{\"hash\"=>true}]}]]
121
- eos
122
- end
123
- end
124
-
125
- describe '#failure_message_when_negated' do
126
- it 'returns message' do
127
- argument_subject.matches? worker
128
- expect(argument_subject.failure_message_when_negated).to eq <<-eos.gsub(/^ {6}/, '').strip
129
- expected not to have an enqueued #{worker} job
130
- arguments: [\"string\", 1, true, {\"key\"=>\"value\", \"bar\"=>\"foo\", \"nested\"=>[{\"hash\"=>true}]}]
131
- eos
132
- end
133
- end
134
-
135
- describe '#matches?' do
136
- context 'when condition matches' do
137
- context 'when expected are arguments' do
138
- it 'returns true' do
139
- worker.perform_async *worker_args
140
- expect(argument_subject.matches? worker).to be true
141
- end
142
- end
143
-
144
- context 'when expected are matchers' do
145
- it 'returns true' do
146
- worker.perform_async *worker_args
147
- expect(matcher_subject.matches? worker).to be true
148
- end
149
- end
150
-
151
- context 'when job is scheduled' do
152
- context 'with #perform_at' do
153
- before(:each) do
154
- worker.perform_at(tomorrow, *worker_args)
155
- end
156
-
157
- context 'and timestamp matches' do
158
- it 'returns true' do
159
- expect(matcher_subject.at(tomorrow).matches? worker).to be true
160
- end
161
- end
162
-
163
- context 'and timestamp does not match' do
164
- it 'returns false' do
165
- expect(matcher_subject.at(tomorrow + 1).matches? worker).to be false
166
- end
167
- end
168
- end
169
-
170
- context 'with #perform_in' do
171
- before(:each) do
172
- worker.perform_in(interval, *worker_args)
173
- end
174
-
175
- context 'and interval matches' do
176
- it 'returns true' do
177
- expect(matcher_subject.in(interval).matches? worker).to be true
178
- end
179
- end
180
-
181
- context 'and interval does not match' do
182
- it 'returns false' do
183
- expect(matcher_subject.in(interval + 1.minute).matches? worker).to be false
184
- end
185
- end
186
- end
187
- end
188
- end
189
-
190
- context 'when condition does not match' do
191
- before(:each) { Sidekiq::Worker.clear_all }
192
-
193
- context 'when expected are arguments' do
194
- it 'returns false' do
195
- expect(argument_subject.matches? worker).to be false
196
- end
197
- end
198
-
199
- context 'when expected are matchers' do
200
- it 'returns false' do
201
- expect(matcher_subject.matches? worker).to be false
202
- end
203
- end
204
-
205
- context 'when job is scheduled' do
206
- context 'with #perform_at' do
207
- before(:each) do
208
- allow(matcher_subject).to receive(:options).and_return(at: tomorrow + 1)
209
- end
210
-
211
- it 'returns false' do
212
- expect(matcher_subject.at(tomorrow).matches? worker).to be false
213
- end
214
- end
215
-
216
- context 'with #perform_in' do
217
- before(:each) do
218
- allow(matcher_subject).to receive(:options).and_return(in: interval + 1)
219
- end
220
-
221
- it 'returns false' do
222
- expect(matcher_subject.in(interval).matches? worker).to be false
223
- end
224
- end
225
- end
226
- end
227
- end
228
- end
@@ -1,136 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe RSpec::Sidekiq::Matchers::SaveBacktrace do
4
- let(:specific_subject) { RSpec::Sidekiq::Matchers::SaveBacktrace.new 2 }
5
- let(:specific_worker) { create_worker backtrace: 2 }
6
- let(:default_subject) { RSpec::Sidekiq::Matchers::SaveBacktrace.new true }
7
- let(:default_worker) { create_worker backtrace: true }
8
- let(:negative_subject) { RSpec::Sidekiq::Matchers::SaveBacktrace.new false }
9
- let(:negative_worker) { create_worker backtrace: false }
10
- before(:each) do
11
- specific_subject.matches? specific_worker
12
- default_subject.matches? default_worker
13
- negative_subject.matches? negative_worker
14
- end
15
-
16
- describe 'expected usage' do
17
- it 'matches' do
18
- expect(default_worker).to save_backtrace true
19
- expect(default_worker).to save_backtrace
20
- end
21
-
22
- it 'negative usage' do
23
- expect(negative_worker).to save_backtrace false
24
- expect(negative_worker).to_not save_backtrace
25
- end
26
- end
27
-
28
- describe '#save_backtrace' do
29
- it 'returns instance' do
30
- expect(save_backtrace true).to be_a RSpec::Sidekiq::Matchers::SaveBacktrace
31
- expect(save_backtrace).to be_a RSpec::Sidekiq::Matchers::SaveBacktrace
32
- end
33
- end
34
-
35
- describe '#description' do
36
- context 'when expected is a number' do
37
- it 'returns description' do
38
- expect(specific_subject.description).to eq 'save 2 lines of error backtrace'
39
- end
40
- end
41
-
42
- context 'when expected is true' do
43
- it 'returns description' do
44
- expect(default_subject.description).to eq 'save error backtrace'
45
- end
46
- end
47
-
48
- context 'when expected is false' do
49
- it 'returns description' do
50
- expect(negative_subject.description).to eq 'not save error backtrace'
51
- end
52
- end
53
- end
54
-
55
- describe '#failure_message' do
56
- context 'when expected is a number' do
57
- it 'returns message' do
58
- expect(specific_subject.failure_message).to eq "expected #{specific_worker} to save 2 lines of error backtrace but got 2"
59
- end
60
- end
61
-
62
- context 'when expected is true' do
63
- it 'returns message' do
64
- expect(default_subject.failure_message).to eq "expected #{default_worker} to save error backtrace but got true"
65
- end
66
- end
67
-
68
- context 'when expected is false' do
69
- it 'returns message' do
70
- expect(negative_subject.failure_message).to eq "expected #{negative_worker} to not save error backtrace but got false"
71
- end
72
- end
73
- end
74
-
75
- describe '#matches?' do
76
- context 'when condition matches' do
77
- context 'when expected is a number' do
78
- it 'returns true' do
79
- expect(specific_subject.matches? specific_worker).to be true
80
- end
81
- end
82
-
83
- context 'when expected is true' do
84
- it 'returns true' do
85
- expect(default_subject.matches? default_worker).to be true
86
- end
87
- end
88
-
89
- context 'when expected is false' do
90
- it 'returns true' do
91
- expect(negative_subject.matches? negative_worker).to be true
92
- end
93
- end
94
- end
95
-
96
- context 'when condition does not match' do
97
- context 'when expected is a number' do
98
- it 'returns false' do
99
- expect(specific_subject.matches? default_worker).to be false
100
- end
101
- end
102
-
103
- context 'when expected is true' do
104
- it 'returns false' do
105
- expect(default_subject.matches? negative_worker).to be false
106
- end
107
- end
108
-
109
- context 'when expected is false' do
110
- it 'returns false' do
111
- expect(negative_subject.matches? specific_worker).to be false
112
- end
113
- end
114
- end
115
- end
116
-
117
- describe '#failure_message_when_negated' do
118
- context 'when expected is a number' do
119
- it 'returns message' do
120
- expect(specific_subject.failure_message_when_negated).to eq "expected #{specific_worker} to not save 2 lines of error backtrace"
121
- end
122
- end
123
-
124
- context 'when expected is true' do
125
- it 'returns message' do
126
- expect(default_subject.failure_message_when_negated).to eq "expected #{default_worker} to not save error backtrace"
127
- end
128
- end
129
-
130
- context 'when expected is false' do
131
- it 'returns message' do
132
- expect(negative_subject.failure_message_when_negated).to eq "expected #{negative_worker} to save error backtrace"
133
- end
134
- end
135
- end
136
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe RSpec::Sidekiq do
4
- describe '#configure' do
5
- it 'yields with configuration' do
6
- expect { |block| RSpec::Sidekiq.configure(&block) }.to yield_with_args RSpec::Sidekiq.configuration
7
- end
8
- end
9
-
10
- describe '#configuration' do
11
- it 'returns instance' do
12
- expect(RSpec::Sidekiq.configuration).to be_a RSpec::Sidekiq::Configuration
13
- end
14
- end
15
- end
data/spec/spec_helper.rb DELETED
@@ -1,29 +0,0 @@
1
- require 'simplecov'
2
- require 'coveralls'
3
-
4
- require 'sidekiq'
5
- require 'rspec-sidekiq'
6
-
7
- require 'active_job'
8
- require 'action_mailer'
9
-
10
- require_relative 'support/init'
11
-
12
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(
13
- [Coveralls::SimpleCov::Formatter, SimpleCov::Formatter::HTMLFormatter]
14
- )
15
- SimpleCov.start
16
-
17
- RSpec.configure do |config|
18
- config.disable_monkey_patching!
19
-
20
- config.include RSpec::Sidekiq::Spec::Support::Factories
21
- end
22
-
23
- ActiveJob::Base.queue_adapter = :sidekiq
24
-
25
- if Gem::Dependency.new('sidekiq', '>= 5.0.0').matching_specs.any?
26
- require 'active_record'
27
- ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
28
- Sidekiq::Extensions.enable_delay!
29
- end
@@ -1,33 +0,0 @@
1
- module RSpec
2
- module Sidekiq
3
- module Spec
4
- module Support
5
- module Factories
6
- def create_worker(options = {})
7
- clazz_name = "Worker#{ rand(36**10).to_s 36 }"
8
- clazz = Class.new do
9
- include ::Sidekiq::Worker
10
-
11
- sidekiq_options options
12
-
13
- def perform
14
- end
15
- end
16
- Object.const_set clazz_name, clazz
17
- end
18
-
19
- def create_active_job(options = {})
20
- clazz_name = "ActiveJob#{ rand(36**10).to_s 36 }"
21
- clazz = Class.new(ActiveJob::Base) do
22
- queue_as options
23
-
24
- def perform
25
- end
26
- end
27
- Object.const_set clazz_name, clazz
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
data/spec/support/init.rb DELETED
@@ -1,6 +0,0 @@
1
- require_relative 'factories'
2
- require_relative 'test_worker'
3
- require_relative 'test_worker_alternative'
4
- require_relative 'test_job'
5
- require_relative 'test_resource'
6
- require_relative 'test_action_mailer'
@@ -1,6 +0,0 @@
1
- class TestActionMailer < ActionMailer::Base
2
- def testmail(resource = nil)
3
- @resource = resource
4
- mail(to: 'none@example.com', subject: 'testmail')
5
- end
6
- end
@@ -1,6 +0,0 @@
1
- class TestJob < ActiveJob::Base
2
- queue_as :mailers
3
-
4
- def perform
5
- end
6
- end
@@ -1,16 +0,0 @@
1
- class TestResource
2
- include GlobalID::Identification
3
-
4
- attr_reader :global_id
5
-
6
- def initialize
7
- @global_id = GlobalID.create(self, { app: 'rspec-sidekiq' })
8
- end
9
-
10
- def self.find(id)
11
- end
12
-
13
- def id
14
- rand(36**10).to_s 36
15
- end
16
- end
@@ -1,8 +0,0 @@
1
- class TestWorker
2
- include Sidekiq::Worker
3
-
4
- sidekiq_options queue: :data, retry: 5, unique: true
5
-
6
- def perform
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- class TestWorkerAlternative
2
- include Sidekiq::Worker
3
-
4
- sidekiq_options queue: 'data', retry: false, unique: false
5
-
6
- def perform
7
- end
8
- end