resque_unit 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,22 +6,50 @@ module ResqueUnit::Assertions
6
6
  # least once. If +args+ is nil, it only asserts that the klass has
7
7
  # been queued. Otherwise, it asserts that the klass has been queued
8
8
  # with the correct arguments. Pass an empty array for +args+ if you
9
- # want to assert that klass has been queued without arguments.
10
- def assert_queued(klass, args = nil, message = nil)
11
- queue = Resque.queue_for(klass)
12
- assert_block (message || "#{klass}#{args ? " with #{args.inspect}" : ""} should have been queued in #{queue}: #{Resque.queue(queue).inspect}.") do
9
+ # want to assert that klass has been queued without arguments. Pass a block
10
+ # if you want to assert something was queued within its execution.
11
+ def assert_queued(klass, args = nil, message = nil, &block)
12
+ queue_name = Resque.queue_for(klass)
13
+
14
+ queue = if block_given?
15
+ snapshot = Resque.size(queue_name)
16
+ yield
17
+ Resque.queue(queue_name)[snapshot..-1]
18
+ else
19
+ Resque.queue(queue_name)
20
+ end
21
+
22
+ assert_block (message || "#{klass}#{args ? " with #{args.inspect}" : ""} should have been queued in #{queue_name}: #{queue.inspect}.") do
13
23
  in_queue?(queue, klass, args)
14
24
  end
15
25
  end
26
+ alias assert_queues assert_queued
16
27
 
17
28
  # The opposite of +assert_queued+.
18
- def assert_not_queued(klass, args = nil, message = nil)
19
- queue = Resque.queue_for(klass)
20
- assert_block (message || "#{klass}#{args ? " with #{args.inspect}" : ""} should not have been queued in #{queue}.") do
29
+ def assert_not_queued(klass = nil, args = nil, message = nil, &block)
30
+ queue_name = Resque.queue_for(klass)
31
+
32
+ queue = if block_given?
33
+ snapshot = Resque.size(queue_name)
34
+ yield
35
+ Resque.queue(queue_name)[snapshot..-1]
36
+ else
37
+ Resque.queue(queue_name)
38
+ end
39
+
40
+ assert_block (message || "#{klass}#{args ? " with #{args.inspect}" : ""} should not have been queued in #{queue_name}.") do
21
41
  !in_queue?(queue, klass, args)
22
42
  end
23
43
  end
24
44
 
45
+ # Asserts no jobs were queued within the block passed.
46
+ def assert_nothing_queued(message = nil, &block)
47
+ snapshot = Resque.size
48
+ yield
49
+ present = Resque.size
50
+ assert_equal snapshot, present, message || "No jobs should have been queued"
51
+ end
52
+
25
53
  private
26
54
 
27
55
  def in_queue?(queue, klass, args = nil)
@@ -29,10 +57,11 @@ module ResqueUnit::Assertions
29
57
  end
30
58
 
31
59
  def matching_jobs(queue, klass, args = nil)
60
+ queue = Resque.queue(queue) if queue.is_a? Symbol
32
61
  if args # retrieve the elements that match klass and args in the queue
33
- Resque.queue(queue).select {|e| e[:klass] == klass && e[:args] == args}
62
+ queue.select {|e| e[:klass] == klass && e[:args] == args}
34
63
  else # if no args were passed, retrieve all queued jobs that match klass
35
- Resque.queue(queue).select {|e| e[:klass] == klass}
64
+ queue.select {|e| e[:klass] == klass}
36
65
  end
37
66
  end
38
67
 
@@ -17,13 +17,18 @@ module Resque
17
17
  end
18
18
  end
19
19
 
20
+ # Returns a hash of all the queue names and jobs that have been queued. The
21
+ # format is <tt>{queue_name => [job, ..]}</tt>.
22
+ def self.queues
23
+ @queue || reset!
24
+ end
25
+
20
26
  # Returns an array of all the jobs that have been queued. Each
21
27
  # element is of the form +{:klass => klass, :args => args}+ where
22
28
  # +klass+ is the job's class and +args+ is an array of the arguments
23
29
  # passed to the job.
24
30
  def queue(queue_name)
25
- self.reset! unless @queue
26
- @queue[queue_name]
31
+ queues[queue_name]
27
32
  end
28
33
 
29
34
  # Executes all jobs in all queues in an undefined order.
@@ -53,7 +58,7 @@ module Resque
53
58
  # 3. Repeat 3
54
59
  def full_run!
55
60
  until empty_queues?
56
- @queue.each do |k, v|
61
+ queues.each do |k, v|
57
62
  while job = v.shift
58
63
  job[:klass].perform(*job[:args])
59
64
  end
@@ -62,9 +67,12 @@ module Resque
62
67
  end
63
68
 
64
69
  # Returns the size of the given queue
65
- def size(queue_name)
66
- self.reset! unless @queue
67
- @queue[queue_name].length
70
+ def size(queue_name = nil)
71
+ if queue_name
72
+ queues[queue_name].length
73
+ else
74
+ queues.values.flatten.length
75
+ end
68
76
  end
69
77
 
70
78
  # :nodoc:
@@ -82,7 +90,7 @@ module Resque
82
90
 
83
91
  # :nodoc:
84
92
  def empty_queues?
85
- @queue.all? do |k, v|
93
+ queues.all? do |k, v|
86
94
  v.empty?
87
95
  end
88
96
  end
@@ -57,6 +57,69 @@ class ResqueUnitTest < Test::Unit::TestCase
57
57
  # assert number of jobs?
58
58
  end
59
59
 
60
+ context "Block assertions" do
61
+ should "pass the assert_queued(job) assertion when queued in block" do
62
+ assert_queues(HighPriorityJob) do
63
+ Resque.enqueue(HighPriorityJob)
64
+ end
65
+ end
66
+
67
+ should "pass the assert_queued(job) assertion when queued and not in block" do
68
+ Resque.enqueue(HighPriorityJob)
69
+ assert_queues(HighPriorityJob) do
70
+ Resque.enqueue(HighPriorityJob)
71
+ end
72
+ end
73
+
74
+ should "fail the assert_queued(job) assertion when not queued in block" do
75
+ Resque.enqueue(LowPriorityJob)
76
+ assert_raise Test::Unit::AssertionFailedError do
77
+ assert_queues(LowPriorityJob) do
78
+ # Nothing.
79
+ end
80
+ end
81
+ end
82
+
83
+ should "pass the assert_not_queued(job) assertion when not queued in block" do
84
+ Resque.enqueue(LowPriorityJob)
85
+ assert_not_queued(LowPriorityJob) do
86
+ # Nothing.
87
+ end
88
+ end
89
+
90
+ should "fail the assert_not_queued(job) assertion when not queued in block" do
91
+ assert_raise Test::Unit::AssertionFailedError do
92
+ assert_not_queued(LowPriorityJob) do
93
+ Resque.enqueue(LowPriorityJob)
94
+ end
95
+ end
96
+ end
97
+
98
+ should "fail the assert_not_queued(job) assertion when queued and not in block" do
99
+ assert_raise Test::Unit::AssertionFailedError do
100
+ Resque.enqueue(LowPriorityJob)
101
+ assert_not_queued(LowPriorityJob) do
102
+ Resque.enqueue(LowPriorityJob)
103
+ end
104
+ end
105
+ end
106
+
107
+ should "pass the assert_nothing_queued assertion when nothing queued in block" do
108
+ Resque.enqueue(LowPriorityJob)
109
+ assert_nothing_queued do
110
+ # Nothing.
111
+ end
112
+ end
113
+
114
+ should "fail the assert_nothing_queued assertion when queued in block" do
115
+ assert_raise Test::Unit::AssertionFailedError do
116
+ assert_nothing_queued do
117
+ Resque.enqueue(LowPriorityJob)
118
+ end
119
+ end
120
+ end
121
+ end
122
+
60
123
  context "An empty queue" do
61
124
  should "pass the assert_not_queued(job) assertion" do
62
125
  assert_not_queued(LowPriorityJob)
@@ -199,9 +262,7 @@ class ResqueUnitTest < Test::Unit::TestCase
199
262
  end
200
263
  end
201
264
  end
202
- end
203
265
 
204
- context "An assertion message" do
205
266
  context "of assert_not_queued" do
206
267
  should "include job class and queue content" do
207
268
  begin
@@ -221,6 +282,19 @@ class ResqueUnitTest < Test::Unit::TestCase
221
282
  end
222
283
  end
223
284
  end
285
+
286
+ context "of assert_nothing_queued" do
287
+ should "include diff" do
288
+ begin
289
+ Resque.reset!
290
+ assert_nothing_queued do
291
+ Resque.enqueue(LowPriorityJob)
292
+ end
293
+ rescue Test::Unit::AssertionFailedError => error
294
+ assert_equal "No jobs should have been queued.\n<0> expected but was\n<1>.", error.message
295
+ end
296
+ end
297
+ end
224
298
  end
225
299
 
226
300
  context "A job that does not specify a queue" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque_unit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 8
10
- version: 0.2.8
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Justin Weiss
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-10 00:00:00 -08:00
18
+ date: 2010-11-17 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency