rspec-sidekiq 0.5.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +9 -9
  2. data/.gitattributes +22 -22
  3. data/.gitignore +1 -1
  4. data/.rspec +2 -2
  5. data/.simplecov +5 -0
  6. data/CHANGES.md +52 -35
  7. data/Gemfile +8 -2
  8. data/LICENSE +8 -8
  9. data/README.md +181 -135
  10. data/lib/rspec/sidekiq/batch.rb +46 -43
  11. data/lib/rspec/sidekiq/configuration.rb +12 -10
  12. data/lib/rspec/sidekiq/helpers/within_sidekiq_retries_exhausted_block.rb +11 -0
  13. data/lib/rspec/sidekiq/helpers.rb +2 -0
  14. data/lib/rspec/sidekiq/matchers/be_delayed.rb +67 -0
  15. data/lib/rspec/sidekiq/matchers/be_processed_in.rb +32 -32
  16. data/lib/rspec/sidekiq/matchers/be_retryable.rb +38 -38
  17. data/lib/rspec/sidekiq/matchers/be_unique.rb +28 -28
  18. data/lib/rspec/sidekiq/matchers/have_enqueued_job.rb +33 -33
  19. data/lib/rspec/sidekiq/matchers/have_enqueued_jobs.rb +11 -36
  20. data/lib/rspec/sidekiq/matchers.rb +10 -9
  21. data/lib/rspec/sidekiq/sidekiq.rb +24 -20
  22. data/lib/rspec/sidekiq/version.rb +4 -4
  23. data/lib/rspec-sidekiq.rb +7 -6
  24. data/rspec-sidekiq.gemspec +35 -34
  25. data/spec/rspec/sidekiq/batch_spec.rb +27 -0
  26. data/spec/rspec/sidekiq/helpers/retries_exhausted_spec.rb +18 -0
  27. data/spec/rspec/sidekiq/matchers/be_delayed_spec.rb +238 -0
  28. data/spec/rspec/sidekiq/matchers/be_processed_in_spec.rb +107 -28
  29. data/spec/rspec/sidekiq/matchers/be_retryable_spec.rb +128 -40
  30. data/spec/rspec/sidekiq/matchers/be_unique_spec.rb +49 -14
  31. data/spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb +73 -0
  32. data/spec/rspec/sidekiq/matchers/have_enqueued_jobs_spec.rb +9 -0
  33. data/spec/rspec/sidekiq/sidekiq_spec.rb +15 -0
  34. data/spec/rspec/sidekiq/version_spec.rb +4 -6
  35. data/spec/spec_helper.rb +27 -25
  36. data/spec/support/factories.rb +22 -0
  37. data/spec/support/init.rb +3 -3
  38. data/spec/support/test_worker.rb +7 -7
  39. data/spec/support/test_worker_alternative.rb +7 -7
  40. metadata +29 -21
  41. data/spec/support/test_worker_defaults.rb +0 -6
@@ -1,44 +1,47 @@
1
- if defined?(Sidekiq::Batch)
2
- module RSpec
3
- module Sidekiq
4
- class NullObject
5
- def initialize(*)
6
- end
7
-
8
- def method_missing(*args, &block)
9
- self
10
- end
11
- end
12
-
13
- class NullBatch < NullObject
14
- def jobs(*)
15
- yield
16
- end
17
- end
18
-
19
- class NullStatus < NullObject
20
- def join
21
- ::Sidekiq::Worker.drain_all
22
- end
23
- end
24
- end
25
- end
26
-
27
- RSpec.configure do |config|
28
- config.before(:each) do
29
- if mocked_with_mocha?
30
- Sidekiq::Batch.stubs(:new) { RSpec::Sidekiq::NullBatch.new }
31
- Sidekiq::Batch::Status.stubs(:new) { RSpec::Sidekiq::NullStatus.new }
32
- else
33
- Sidekiq::Batch.stub(:new) { RSpec::Sidekiq::NullBatch.new }
34
- Sidekiq::Batch::Status.stub(:new) { RSpec::Sidekiq::NullStatus.new }
35
- end
36
- end
37
- end
38
-
39
- ## Helpers ----------------------------------------------
40
-
41
- def mocked_with_mocha?
42
- Sidekiq::Batch.respond_to? :stubs
43
- end
1
+ if defined? Sidekiq::Batch
2
+ module RSpec
3
+ module Sidekiq
4
+ class NullObject
5
+ def initialize(*)
6
+ end
7
+
8
+ def method_missing(*args, &block)
9
+ self
10
+ end
11
+ end
12
+
13
+ class NullBatch < NullObject
14
+ def jobs(*)
15
+ yield
16
+ end
17
+ end
18
+
19
+ class NullStatus < NullObject
20
+ def join
21
+ ::Sidekiq::Worker.drain_all
22
+ end
23
+
24
+ def total
25
+ ::Sidekiq::Worker.jobs.size
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ RSpec.configure do |config|
32
+ config.before(:each) do
33
+ if mocked_with_mocha?
34
+ Sidekiq::Batch.stubs(:new) { RSpec::Sidekiq::NullBatch.new }
35
+ Sidekiq::Batch::Status.stubs(:new) { RSpec::Sidekiq::NullStatus.new }
36
+ else
37
+ Sidekiq::Batch.stub(:new) { RSpec::Sidekiq::NullBatch.new }
38
+ Sidekiq::Batch::Status.stub(:new) { RSpec::Sidekiq::NullStatus.new }
39
+ end
40
+ end
41
+ end
42
+
43
+ ## Helpers ----------------------------------------------
44
+ def mocked_with_mocha?
45
+ Sidekiq::Batch.respond_to? :stubs
46
+ end
44
47
  end
@@ -1,11 +1,13 @@
1
- module RSpec
2
- module Sidekiq
3
- class Configuration
4
- attr_accessor :clear_all_enqueued_jobs
5
-
6
- def initialize
7
- @clear_all_enqueued_jobs = true
8
- end
9
- end
10
- end
1
+ module RSpec
2
+ module Sidekiq
3
+ class Configuration
4
+ attr_accessor :clear_all_enqueued_jobs, :enable_terminal_colours, :warn_when_jobs_not_processed_by_sidekiq
5
+
6
+ def initialize
7
+ @clear_all_enqueued_jobs = true
8
+ @enable_terminal_colours = true
9
+ @warn_when_jobs_not_processed_by_sidekiq = true
10
+ end
11
+ end
12
+ end
11
13
  end
@@ -0,0 +1,11 @@
1
+ module Sidekiq
2
+ module Worker
3
+ module ClassMethods
4
+ def within_sidekiq_retries_exhausted_block &block
5
+ block.call
6
+
7
+ self.sidekiq_retries_exhausted_block.call
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ require "rspec/core"
2
+ require "rspec/sidekiq/helpers/within_sidekiq_retries_exhausted_block"
@@ -0,0 +1,67 @@
1
+ module RSpec
2
+ module Sidekiq
3
+ module Matchers
4
+ def be_delayed *expected_arguments
5
+ BeDelayed.new *expected_arguments
6
+ end
7
+
8
+ class BeDelayed
9
+ def initialize *expected_arguments
10
+ @expected_arguments = expected_arguments
11
+ end
12
+
13
+ def description
14
+ description = "be delayed"
15
+ description += " for #{@expected_interval} seconds" if @expected_interval
16
+ description += " until #{@expected_time}" if @expected_time
17
+ description += " with arguments #{@expected_arguments}" unless @expected_arguments.empty?
18
+ description
19
+ end
20
+
21
+ def failure_message
22
+ "expected #{@expected_method.receiver}.#{@expected_method.name} to " + description
23
+ end
24
+
25
+ def for interval
26
+ @expected_interval = interval
27
+ self
28
+ end
29
+
30
+ def matches? expected_method
31
+ @expected_method = expected_method
32
+
33
+ find_job @expected_method, @expected_arguments do |job|
34
+ if @expected_interval
35
+ return job["at"].to_i == job["enqueued_at"].to_i + @expected_interval
36
+ elsif @expected_time
37
+ return job["at"].to_i == @expected_time.to_i
38
+ else
39
+ return true
40
+ end
41
+ end
42
+
43
+ return false
44
+ end
45
+
46
+ def negative_failure_message
47
+ "expected #{@expected_method.receiver}.#{@expected_method.name} to not " + description
48
+ end
49
+
50
+ def until time
51
+ @expected_time = time
52
+ self
53
+ end
54
+
55
+ private
56
+ def find_job method, arguments, &block
57
+ job = (::Sidekiq::Extensions::DelayedClass.jobs + ::Sidekiq::Extensions::DelayedModel.jobs + ::Sidekiq::Extensions::DelayedMailer.jobs).find do |job|
58
+ yaml = YAML.load(job["args"].first)
59
+ @expected_method.receiver == yaml[0] && @expected_method.name == yaml[1] && (@expected_arguments <=> yaml[2]) == 0
60
+ end
61
+
62
+ yield job if block && job
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,33 +1,33 @@
1
- module RSpec
2
- module Sidekiq
3
- module Matchers
4
- def be_processed_in expected
5
- BeProcessedIn.new expected
6
- end
7
-
8
- class BeProcessedIn
9
- def initialize expected
10
- @expected = expected
11
- end
12
-
13
- def description
14
- "be processed in the \"#{@expected}\" queue"
15
- end
16
-
17
- def failure_message
18
- "expected #{@klass} to be processed in the #{@expected} queue but got #{@actual}"
19
- end
20
-
21
- def matches? job
22
- @klass = job.kind_of?(Class) ? job : job.class
23
- @actual = @klass.get_sidekiq_options["queue"]
24
- @actual.to_s == @expected.to_s
25
- end
26
-
27
- def negative_failure_message
28
- "expected #{@klass} to not be processed in the #{@expected} queue"
29
- end
30
- end
31
- end
32
- end
1
+ module RSpec
2
+ module Sidekiq
3
+ module Matchers
4
+ def be_processed_in expected_queue
5
+ BeProcessedIn.new expected_queue
6
+ end
7
+
8
+ class BeProcessedIn
9
+ def initialize expected_queue
10
+ @expected_queue = expected_queue
11
+ end
12
+
13
+ def description
14
+ "be processed in the \"#{@expected_queue}\" queue"
15
+ end
16
+
17
+ def failure_message
18
+ "expected #{@klass} to be processed in the \"#{@expected_queue}\" queue but got \"#{@actual}\""
19
+ end
20
+
21
+ def matches? job
22
+ @klass = job.kind_of?(Class) ? job : job.class
23
+ @actual = @klass.get_sidekiq_options["queue"]
24
+ @actual.to_s == @expected_queue.to_s
25
+ end
26
+
27
+ def negative_failure_message
28
+ "expected #{@klass} to not be processed in the \"#{@expected_queue}\" queue"
29
+ end
30
+ end
31
+ end
32
+ end
33
33
  end
@@ -1,39 +1,39 @@
1
- module RSpec
2
- module Sidekiq
3
- module Matchers
4
- def be_retryable expected
5
- BeRetryable.new expected
6
- end
7
-
8
- class BeRetryable
9
- def initialize expected
10
- @expected = expected
11
- end
12
-
13
- def description
14
- if @expected.is_a?(Fixnum)
15
- "retry #{@expected} times" # retry: 5
16
- elsif @expected
17
- "retry the default number of times" # retry: true
18
- else
19
- "not retry" # retry: false
20
- end
21
- end
22
-
23
- def failure_message
24
- "expected #{@klass} to #{description} but got #{@actual}"
25
- end
26
-
27
- def matches? job
28
- @klass = job.kind_of?(Class) ? job : job.class
29
- @actual = @klass.get_sidekiq_options["retry"]
30
- @actual == @expected
31
- end
32
-
33
- def negative_failure_message
34
- "expected #{@klass} to not #{description}"
35
- end
36
- end
37
- end
38
- end
1
+ module RSpec
2
+ module Sidekiq
3
+ module Matchers
4
+ def be_retryable expected_retry
5
+ BeRetryable.new expected_retry
6
+ end
7
+
8
+ class BeRetryable
9
+ def initialize expected_retry
10
+ @expected_retry = expected_retry
11
+ end
12
+
13
+ def description
14
+ if @expected_retry.is_a?(Fixnum)
15
+ "retry #{@expected_retry} times" # retry: 5
16
+ elsif @expected_retry
17
+ "retry the default number of times" # retry: true
18
+ else
19
+ "not retry" # retry: false
20
+ end
21
+ end
22
+
23
+ def failure_message
24
+ "expected #{@klass} to #{description} but got #{@actual}"
25
+ end
26
+
27
+ def matches? job
28
+ @klass = job.kind_of?(Class) ? job : job.class
29
+ @actual = @klass.get_sidekiq_options["retry"]
30
+ @actual == @expected_retry
31
+ end
32
+
33
+ def negative_failure_message
34
+ "expected #{@klass} to not #{description}".gsub "not not ", ""
35
+ end
36
+ end
37
+ end
38
+ end
39
39
  end
@@ -1,29 +1,29 @@
1
- module RSpec
2
- module Sidekiq
3
- module Matchers
4
- def be_unique
5
- BeUnique.new
6
- end
7
-
8
- class BeUnique
9
- def description
10
- "be unique in the queue"
11
- end
12
-
13
- def failure_message
14
- "expected #{@klass} to be unique in the queue but it is not"
15
- end
16
-
17
- def matches? job
18
- @klass = job.kind_of?(Class) ? job : job.class
19
- @actual = @klass.get_sidekiq_options["unique"]
20
- @actual == true
21
- end
22
-
23
- def negative_failure_message
24
- "expected #{@klass} to not be unique in the queue"
25
- end
26
- end
27
- end
28
- end
1
+ module RSpec
2
+ module Sidekiq
3
+ module Matchers
4
+ def be_unique
5
+ BeUnique.new
6
+ end
7
+
8
+ class BeUnique
9
+ def description
10
+ "be unique in the queue"
11
+ end
12
+
13
+ def failure_message
14
+ "expected #{@klass} to be unique in the queue"
15
+ end
16
+
17
+ def matches? job
18
+ @klass = job.kind_of?(Class) ? job : job.class
19
+ @actual = @klass.get_sidekiq_options["unique"]
20
+ @actual == true
21
+ end
22
+
23
+ def negative_failure_message
24
+ "expected #{@klass} to not be unique in the queue"
25
+ end
26
+ end
27
+ end
28
+ end
29
29
  end
@@ -1,34 +1,34 @@
1
- module RSpec
2
- module Sidekiq
3
- module Matchers
4
- def have_enqueued_job *expected
5
- HaveEnqueuedJob.new expected
6
- end
7
-
8
- class HaveEnqueuedJob
9
- def initialize expected
10
- @expected = expected
11
- end
12
-
13
- def description
14
- "have an enqueued #{@klass} job with arguments #{@expected}"
15
- end
16
-
17
- def failure_message
18
- "expected to have an enqueued #{@klass} job with arguments #{@expected} but none found\n\n" +
19
- "found: #{@actual}"
20
- end
21
-
22
- def matches? klass
23
- @klass = klass
24
- @actual = klass.jobs.map { |job| job["args"] }
25
- @actual.include? @expected
26
- end
27
-
28
- def negative_failure_message
29
- "expected to not have an enqueued #{@klass} job with arguments #{@expected}"
30
- end
31
- end
32
- end
33
- end
1
+ module RSpec
2
+ module Sidekiq
3
+ module Matchers
4
+ def have_enqueued_job *expected_arguments
5
+ HaveEnqueuedJob.new expected_arguments
6
+ end
7
+
8
+ class HaveEnqueuedJob
9
+ def initialize expected_arguments
10
+ @expected_arguments = expected_arguments
11
+ end
12
+
13
+ def description
14
+ "have an enqueued #{@klass} job with arguments #{@expected_arguments}"
15
+ end
16
+
17
+ def failure_message
18
+ "expected to have an enqueued #{@klass} job with arguments #{@expected_arguments}\n\n" +
19
+ "found: #{@actual}"
20
+ end
21
+
22
+ def matches? klass
23
+ @klass = klass
24
+ @actual = klass.jobs.map { |job| job["args"] }
25
+ @actual.any? { |arguments| Array(@expected_arguments) == arguments }
26
+ end
27
+
28
+ def negative_failure_message
29
+ "expected to not have an enqueued #{@klass} job with arguments #{@expected_arguments}"
30
+ end
31
+ end
32
+ end
33
+ end
34
34
  end