wireframe-resque_unit 0.4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +130 -0
- data/lib/resque_unit.rb +13 -0
- data/lib/resque_unit/assertions.rb +81 -0
- data/lib/resque_unit/errors.rb +17 -0
- data/lib/resque_unit/helpers.rb +57 -0
- data/lib/resque_unit/plugin.rb +70 -0
- data/lib/resque_unit/resque.rb +224 -0
- data/lib/resque_unit/scheduler.rb +41 -0
- data/lib/resque_unit/scheduler_assertions.rb +49 -0
- data/lib/resque_unit_scheduler.rb +4 -0
- data/test/resque_test.rb +61 -0
- data/test/resque_unit_scheduler_test.rb +201 -0
- data/test/resque_unit_test.rb +460 -0
- data/test/sample_jobs.rb +167 -0
- data/test/test_helper.rb +9 -0
- metadata +96 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
module ResqueUnit
|
2
|
+
|
3
|
+
# ResqueUnit::Scheduler is a group of functions mocking the behavior
|
4
|
+
# of resque-scheduler. It is included into Resque when
|
5
|
+
# 'resque_unit_scheduler' is required.
|
6
|
+
module Scheduler
|
7
|
+
|
8
|
+
# takes a timestamp which will be used to schedule the job
|
9
|
+
# for queueing. Until timestamp is in the past, the job will
|
10
|
+
# sit in the schedule list.
|
11
|
+
def enqueue_at(timestamp, klass, *args)
|
12
|
+
enqueue_with_timestamp(timestamp, klass, *args)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Identical to enqueue_at but takes number_of_seconds_from_now
|
16
|
+
# instead of a timestamp.
|
17
|
+
def enqueue_in(number_of_seconds_from_now, klass, *args)
|
18
|
+
enqueue_at(Time.now + number_of_seconds_from_now, klass, *args)
|
19
|
+
end
|
20
|
+
|
21
|
+
def enqueue_with_timestamp(timestamp, klass, *args)
|
22
|
+
enqueue_unit(queue_for(klass), {"class" => klass.name, "args" => args, "timestamp" => timestamp})
|
23
|
+
end
|
24
|
+
|
25
|
+
def remove_delayed(klass, *args)
|
26
|
+
# points to real queue
|
27
|
+
encoded_job_payloads = Resque.queue(queue_for(klass))
|
28
|
+
args ||= []
|
29
|
+
encoded_job_payloads.delete_if { |e| e = Resque.decode(e); e["class"] == klass.to_s && e["args"] == args }
|
30
|
+
end
|
31
|
+
|
32
|
+
def remove_delayed_job_from_timestamp(timestamp, klass, *args)
|
33
|
+
encoded_job_payloads = Resque.queue(queue_for(klass))
|
34
|
+
args ||= []
|
35
|
+
encoded_job_payloads.delete_if { |e| e = Resque.decode(e); e["class"] == klass.to_s && e["timestamp"] == timestamp.to_s && e["args"] == args }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Resque.send(:extend, Scheduler)
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# These are a group of assertions you can use in your unit tests to
|
2
|
+
# verify that your code is using resque-scheduler correctly.
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module ResqueUnit::SchedulerAssertions
|
6
|
+
|
7
|
+
# Asserts that +klass+ has been queued into its appropriate queue at
|
8
|
+
# least once, with a +timestamp+ less than or equal to
|
9
|
+
# +expected_timestamp+. If the job wasn't queued with a timestamp,
|
10
|
+
# the assertion fails.. If +args+ is nil, it only asserts that the
|
11
|
+
# klass has been queued. Otherwise, it asserts that the klass has
|
12
|
+
# been queued with the correct arguments. Pass an empty array for
|
13
|
+
# +args+ if you want to assert that klass has been queued without
|
14
|
+
# arguments.
|
15
|
+
def assert_queued_at(expected_timestamp, klass, args = nil, message = nil)
|
16
|
+
queue = Resque.queue_for(klass)
|
17
|
+
assert_block (message || "#{klass} should have been queued in #{queue} before #{expected_timestamp}: #{Resque.queue(queue).inspect}.") do
|
18
|
+
in_timestamped_queue?(queue, expected_timestamp, klass, args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Similar to +assert_queued_at+, except it takes an expected time
|
23
|
+
# difference (in seconds) instead of a timestamp.
|
24
|
+
def assert_queued_in(expected_time_difference, klass, args = nil, message = nil)
|
25
|
+
assert_queued_at(Time.now + expected_time_difference, klass, args, message)
|
26
|
+
end
|
27
|
+
|
28
|
+
# opposite of +assert_queued_at+
|
29
|
+
def assert_not_queued_at(expected_timestamp, klass, args = nil, message = nil)
|
30
|
+
queue = Resque.queue_for(klass)
|
31
|
+
assert_block (message || "#{klass} should not have been queued in #{queue} before #{expected_timestamp}.") do
|
32
|
+
!in_timestamped_queue?(queue, expected_timestamp, klass, args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# opposite of +assert_queued_in+
|
37
|
+
def assert_not_queued_in(expected_time_difference, klass, args = nil, message = nil)
|
38
|
+
assert_not_queued_at(Time.now + expected_time_difference, klass, args, message)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def in_timestamped_queue?(queue_name, expected_timestamp, klass, args = nil)
|
44
|
+
# check if we have any matching jobs with a timestamp less than
|
45
|
+
# expected_timestamp
|
46
|
+
!matching_jobs(Resque.all(queue_name), klass, args).select {|e| e["timestamp"] && Time.parse(e["timestamp"]) <= expected_timestamp}.empty?
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/test/resque_test.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ResqueTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
Resque.reset!
|
7
|
+
end
|
8
|
+
|
9
|
+
context "with one queued job" do
|
10
|
+
setup do
|
11
|
+
Resque.enqueue(MediumPriorityJob, "some args")
|
12
|
+
@job_payload = {"args"=>["some args"], "class"=>"MediumPriorityJob"}
|
13
|
+
end
|
14
|
+
|
15
|
+
should "return job payload when peek method called with count equal 1" do
|
16
|
+
assert_equal @job_payload, Resque.peek(MediumPriorityJob.queue, 0, 1)
|
17
|
+
end
|
18
|
+
|
19
|
+
should "return array of jobs' payloads when peek method called with count different than 1" do
|
20
|
+
assert_equal [@job_payload], Resque.peek(MediumPriorityJob.queue, 0, 5)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "with few queued jobs" do
|
25
|
+
setup do
|
26
|
+
Resque.enqueue(MediumPriorityJob, "1")
|
27
|
+
Resque.enqueue(MediumPriorityJob, "2")
|
28
|
+
Resque.enqueue(MediumPriorityJob, "3")
|
29
|
+
end
|
30
|
+
|
31
|
+
should "return jobs' payloads 2 and 3 when peek method called with start equal 1 and count equal 2" do
|
32
|
+
assert_equal [["2"], ["3"]], Resque.peek(MediumPriorityJob.queue, 1, 2).map{|h| h["args"]}
|
33
|
+
end
|
34
|
+
|
35
|
+
should "return empty array when peek method called with start higher than queue size" do
|
36
|
+
assert_equal [], Resque.peek(MediumPriorityJob.queue, 4, 2)
|
37
|
+
end
|
38
|
+
|
39
|
+
should "return empty array when peek method called with count equal 0" do
|
40
|
+
assert_equal [], Resque.peek(MediumPriorityJob.queue, 0, 0)
|
41
|
+
end
|
42
|
+
|
43
|
+
should "return all jobs' payloads when all method called" do
|
44
|
+
assert Resque.all(MediumPriorityJob.queue).length == 3, "should return all 3 elements"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "without queued jobs" do
|
49
|
+
should "return nil when peek method called with count equal 1" do
|
50
|
+
assert_equal nil, Resque.peek(MediumPriorityJob.queue, 0, 1)
|
51
|
+
end
|
52
|
+
|
53
|
+
should "return empty array when peek method called with count not equal 1" do
|
54
|
+
assert_equal [], Resque.peek(MediumPriorityJob.queue, 0, 999)
|
55
|
+
end
|
56
|
+
|
57
|
+
should "return empty array when all method called" do
|
58
|
+
assert_equal [], Resque.all(MediumPriorityJob.queue)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'resque_unit_scheduler'
|
3
|
+
|
4
|
+
class ResqueUnitSchedulerTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
Resque.reset!
|
8
|
+
end
|
9
|
+
|
10
|
+
context "A task that schedules a resque job in 5 minutes" do
|
11
|
+
setup { Resque.enqueue_in(600, MediumPriorityJob) }
|
12
|
+
should "pass the assert_queued(job) assertion" do
|
13
|
+
assert_queued(MediumPriorityJob)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "pass the assert_queued_in(600, job) assertion" do
|
17
|
+
assert_queued_in(600, MediumPriorityJob)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "fail the assert_queued_in(300, job) assertion" do
|
21
|
+
assert_raise Test::Unit::AssertionFailedError do
|
22
|
+
assert_queued_in(300, MediumPriorityJob)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
should "pass the assert_not_queued_in(300, job) assertion" do
|
27
|
+
assert_not_queued_in(300, MediumPriorityJob)
|
28
|
+
end
|
29
|
+
|
30
|
+
context "and then the job is removed with #remove_delayed" do
|
31
|
+
setup do
|
32
|
+
Resque.remove_delayed(MediumPriorityJob)
|
33
|
+
end
|
34
|
+
should "pass the assert_not_queued_at(@time, MediumPriorityJob) assertion" do
|
35
|
+
assert_not_queued_at(300, MediumPriorityJob)
|
36
|
+
end
|
37
|
+
|
38
|
+
should "fail the assert_queued_at(@time, MediumPriorityJob) assertion" do
|
39
|
+
assert_raise Test::Unit::AssertionFailedError do
|
40
|
+
assert_queued_at(300, MediumPriorityJob)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "A task that schedules a resque job in 5 minutes with arguments" do
|
47
|
+
setup { Resque.enqueue_in(600, JobWithArguments, 1, "test") }
|
48
|
+
should "pass the assert_queued_in(600, JobWithArguments) assertion" do
|
49
|
+
assert_queued_in(600, JobWithArguments)
|
50
|
+
end
|
51
|
+
|
52
|
+
should "pass the assert_queued_in(600, JobWithArguments, [1, 'test']) assertion" do
|
53
|
+
assert_queued_in(600, JobWithArguments, [1, 'test'])
|
54
|
+
end
|
55
|
+
|
56
|
+
should "fail the assert_queued_in(600, JobWithArguments, [2, 'test']) assertion" do
|
57
|
+
assert_raise Test::Unit::AssertionFailedError do
|
58
|
+
assert_queued_in(600, JobWithArguments, [2, 'test'])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "and then the job is removed with #remove_delayed" do
|
63
|
+
setup do
|
64
|
+
Resque.remove_delayed(JobWithArguments, 1, 'test')
|
65
|
+
end
|
66
|
+
should "pass the assert_not_queued_at(@time, JobWithArguments, 1, 'test') assertion" do
|
67
|
+
assert_not_queued_at(600, JobWithArguments, 1, 'test')
|
68
|
+
end
|
69
|
+
|
70
|
+
should "fail the assert_queued_at(@time, JobWithArguments, 1, 'test') assertion" do
|
71
|
+
assert_raise Test::Unit::AssertionFailedError do
|
72
|
+
assert_queued_at(600, JobWithArguments, 1, 'test')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "and a job of the same class but with different arguments is removed with #remove_delayed" do
|
78
|
+
setup do
|
79
|
+
Resque.remove_delayed(JobWithArguments, 2, 'test')
|
80
|
+
end
|
81
|
+
should "still pass the assert_queued_in(600, JobWithArguments) assertion" do
|
82
|
+
assert_queued_in(600, JobWithArguments)
|
83
|
+
end
|
84
|
+
|
85
|
+
should "still pass the assert_queued_in(600, JobWithArguments, [1, 'test']) assertion" do
|
86
|
+
assert_queued_in(600, JobWithArguments, [1, 'test'])
|
87
|
+
end
|
88
|
+
|
89
|
+
should "still fail the assert_queued_in(600, JobWithArguments, [2, 'test']) assertion" do
|
90
|
+
assert_raise Test::Unit::AssertionFailedError do
|
91
|
+
assert_queued_in(600, JobWithArguments, [2, 'test'])
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "A task that schedules a resque job on Sept. 6, 2016 at 6am" do
|
98
|
+
setup do
|
99
|
+
@time = Time.mktime(2016, 9, 6, 6)
|
100
|
+
Resque.enqueue_at(@time, MediumPriorityJob)
|
101
|
+
end
|
102
|
+
|
103
|
+
should "pass the assert_queued_at(@time, MediumPriorityJob) assertion" do
|
104
|
+
assert_queued_at(@time, MediumPriorityJob)
|
105
|
+
end
|
106
|
+
|
107
|
+
should "fail the assert_queued_at(@time - 100, MediumPriorityJob) assertion" do
|
108
|
+
assert_raise Test::Unit::AssertionFailedError do
|
109
|
+
assert_queued_at(@time - 100, MediumPriorityJob)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
should "pass the assert_not_queued_at(@time - 100, MediumPriorityJob) assertion" do
|
114
|
+
assert_not_queued_at(@time - 100, MediumPriorityJob)
|
115
|
+
end
|
116
|
+
|
117
|
+
context "and then the job is removed with #remove_delayed" do
|
118
|
+
setup do
|
119
|
+
Resque.remove_delayed(MediumPriorityJob)
|
120
|
+
end
|
121
|
+
should "pass the assert_not_queued_at(@time, MediumPriorityJob) assertion" do
|
122
|
+
assert_not_queued_at(@time, MediumPriorityJob)
|
123
|
+
end
|
124
|
+
|
125
|
+
should "fail the assert_queued_at(@time, MediumPriorityJob) assertion" do
|
126
|
+
assert_raise Test::Unit::AssertionFailedError do
|
127
|
+
assert_queued_at(@time, MediumPriorityJob)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "and then the job is removed with #remove_delayed_job_with_timestamp" do
|
133
|
+
setup do
|
134
|
+
Resque.remove_delayed_job_from_timestamp(@time, MediumPriorityJob)
|
135
|
+
end
|
136
|
+
|
137
|
+
should "pass the assert_not_queued_at(@time, MediumPriorityJob) assertion" do
|
138
|
+
assert_not_queued_at(@time, MediumPriorityJob)
|
139
|
+
end
|
140
|
+
|
141
|
+
should "fail the assert_queued_at(@time, MediumPriorityJob) assertion" do
|
142
|
+
assert_raise Test::Unit::AssertionFailedError do
|
143
|
+
assert_queued_at(@time, MediumPriorityJob)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "A task that schedules a resque job with arguments on on Sept. 6, 2016 at 6am" do
|
150
|
+
setup do
|
151
|
+
@time = Time.mktime(2016, 9, 6, 6)
|
152
|
+
Resque.enqueue_at(@time, JobWithArguments, 1, "test")
|
153
|
+
end
|
154
|
+
|
155
|
+
should "pass the assert_queued_at(@time, JobWithArguments, *args) assertion" do
|
156
|
+
assert_queued_at(@time, JobWithArguments, [1, "test"])
|
157
|
+
end
|
158
|
+
|
159
|
+
should "fail the assert_queued_at(@time - 100, JobWithArguments, *args) assertion" do
|
160
|
+
assert_raise Test::Unit::AssertionFailedError do
|
161
|
+
assert_queued_at(@time - 100, JobWithArguments, [1, "test"])
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
should "pass the assert_not_queued_at(@time - 100, JobWithArguments, *args) assertion" do
|
166
|
+
assert_not_queued_at(@time - 100, JobWithArguments, [1, "test"])
|
167
|
+
end
|
168
|
+
|
169
|
+
context "and then the job is removed with #remove_delayed" do
|
170
|
+
setup do
|
171
|
+
Resque.remove_delayed(JobWithArguments, 1, "test")
|
172
|
+
end
|
173
|
+
|
174
|
+
should "pass the assert_not_queued_at(@time, JobWithArguments, *args) assertion" do
|
175
|
+
assert_not_queued_at(@time, JobWithArguments, [1, "test"])
|
176
|
+
end
|
177
|
+
|
178
|
+
should "fail the assert_queued_at(@time, JobWithArguments, *args) assertion" do
|
179
|
+
assert_raise Test::Unit::AssertionFailedError do
|
180
|
+
assert_queued_at(@time, JobWithArguments, [1, "test"])
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "and then the job is removed with #remove_delayed_job_from_timestamp" do
|
186
|
+
setup do
|
187
|
+
Resque.remove_delayed_job_from_timestamp(@time, JobWithArguments, 1, "test")
|
188
|
+
end
|
189
|
+
|
190
|
+
should "pass the assert_not_queued_at(@time, JobWithArguments, *args) assertion" do
|
191
|
+
assert_not_queued_at(@time, JobWithArguments, [1, "test"])
|
192
|
+
end
|
193
|
+
|
194
|
+
should "fail the assert_queued_at(@time, MediumPriorityJob, *args) assertion" do
|
195
|
+
assert_raise Test::Unit::AssertionFailedError do
|
196
|
+
assert_queued_at(@time, JobWithArguments, [1, "test"])
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
@@ -0,0 +1,460 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ResqueUnitTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
# should probably happen automatically, but I haven't thought of a
|
7
|
+
# good way to hook setup() yet.
|
8
|
+
Resque.reset!
|
9
|
+
end
|
10
|
+
|
11
|
+
context "A task that schedules a resque job implementing self.queue" do
|
12
|
+
setup { Resque.enqueue(MediumPriorityJob) }
|
13
|
+
should "pass the assert_queued(job) assertion" do
|
14
|
+
assert_queued(MediumPriorityJob)
|
15
|
+
assert_job_created(MediumPriorityJob.queue, MediumPriorityJob)
|
16
|
+
assert_equal 1, Resque.queue(MediumPriorityJob.queue).length
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "A task that explicitly is queued to a different queue" do
|
21
|
+
setup { Resque.enqueue_to(:a_non_class_determined_queue, MediumPriorityJob) }
|
22
|
+
should "not queue to the class-determined queue" do
|
23
|
+
assert_equal 0, Resque.queue(MediumPriorityJob.queue).length
|
24
|
+
end
|
25
|
+
should "queue to the explicly-stated queue" do
|
26
|
+
assert_equal 1, Resque.queue(:a_non_class_determined_queue).length
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "A task that spawns multiple jobs on a single queue" do
|
31
|
+
setup do
|
32
|
+
3.times {Resque.enqueue(HighPriorityJob)}
|
33
|
+
end
|
34
|
+
|
35
|
+
should "allow partial runs with explicit limit" do
|
36
|
+
assert_equal 3, Resque.queue(:high).length, 'failed setup'
|
37
|
+
Resque.run_for!( :high, 1 )
|
38
|
+
assert_equal 2, Resque.queue(:high).length, 'failed to run just single job'
|
39
|
+
end
|
40
|
+
|
41
|
+
should "allow full run with too-large explicit limit" do
|
42
|
+
assert_equal 3, Resque.queue(:high).length, 'failed setup'
|
43
|
+
Resque.run_for!( :high, 50 )
|
44
|
+
assert_equal 0, Resque.queue(:high).length, 'failed to run all jobs'
|
45
|
+
end
|
46
|
+
|
47
|
+
should "allow full run with implicit limit" do
|
48
|
+
assert_equal 3, Resque.queue(:high).length, 'failed setup'
|
49
|
+
Resque.run_for!( :high )
|
50
|
+
assert_equal 0, Resque.queue(:high).length, 'failed to run all jobs'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "A task that schedules a resque job" do
|
55
|
+
setup do
|
56
|
+
@returned = Resque.enqueue(LowPriorityJob)
|
57
|
+
end
|
58
|
+
|
59
|
+
should 'return a value that evaluates to true' do
|
60
|
+
assert @returned
|
61
|
+
end
|
62
|
+
|
63
|
+
should "pass the assert_queued(job) assertion" do
|
64
|
+
assert_queued(LowPriorityJob)
|
65
|
+
end
|
66
|
+
|
67
|
+
should "fail the assert_not_queued(job) assertion" do
|
68
|
+
assert_raise Test::Unit::AssertionFailedError do
|
69
|
+
assert_not_queued(LowPriorityJob)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
should "should be size 1" do
|
74
|
+
assert_equal 1, Resque.size(:low)
|
75
|
+
end
|
76
|
+
|
77
|
+
context ", when Resque.run! is called," do
|
78
|
+
setup do
|
79
|
+
assert !LowPriorityJob.run?, "The job should not have been run yet"
|
80
|
+
Resque.run!
|
81
|
+
end
|
82
|
+
|
83
|
+
teardown do
|
84
|
+
LowPriorityJob.run = false
|
85
|
+
end
|
86
|
+
|
87
|
+
should "run the job" do
|
88
|
+
assert LowPriorityJob.run?, "The job should have run"
|
89
|
+
end
|
90
|
+
|
91
|
+
should "clear the job from the queue" do
|
92
|
+
assert_not_queued(LowPriorityJob)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# assert number of jobs?
|
97
|
+
end
|
98
|
+
|
99
|
+
context "A task that schedules a resque job with hooks" do
|
100
|
+
setup do
|
101
|
+
Resque.enable_hooks!
|
102
|
+
end
|
103
|
+
|
104
|
+
teardown do
|
105
|
+
Resque.disable_hooks!
|
106
|
+
end
|
107
|
+
|
108
|
+
context "before, around, after, failure, after_enqueue" do
|
109
|
+
setup do
|
110
|
+
JobWithHooks.clear_markers
|
111
|
+
Resque.enqueue(JobWithHooks)
|
112
|
+
end
|
113
|
+
|
114
|
+
should "have run the after_enqueue hook" do
|
115
|
+
assert_queued(JobWithHooks)
|
116
|
+
assert(JobWithHooks.markers[:after_enqueue], 'no after_queue marker set')
|
117
|
+
end
|
118
|
+
|
119
|
+
should "have run the before_enqueue hook" do
|
120
|
+
assert(JobWithHooks.markers[:before_enqueue], 'no before_queue marker set')
|
121
|
+
assert_queued(JobWithHooks)
|
122
|
+
end
|
123
|
+
|
124
|
+
should "run the before and after hooks during a run" do
|
125
|
+
Resque.run!
|
126
|
+
assert(JobWithHooks.markers[:before], 'no before marker set')
|
127
|
+
assert(JobWithHooks.markers[:around], 'no around marker set')
|
128
|
+
assert(JobWithHooks.markers[:after], 'no after marker set')
|
129
|
+
assert(!JobWithHooks.markers[:failed], 'failed marker set, and it should not')
|
130
|
+
end
|
131
|
+
|
132
|
+
should "run the before and failed hooks during a run" do
|
133
|
+
JobWithHooks.make_it_fail do
|
134
|
+
assert_raise(RuntimeError) do
|
135
|
+
Resque.run!
|
136
|
+
assert(JobWithHooks.markers[:before], 'no before marker set')
|
137
|
+
assert(JobWithHooks.markers[:around], 'no around marker set')
|
138
|
+
assert(!JobWithHooks.markers[:after], 'after marker set, and it should not')
|
139
|
+
assert(JobWithHooks.markers[:failed], 'no failed marker set')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
should "not call perform if the around hook raised Resque::Job::DontPerform" do
|
145
|
+
JobWithHooks.make_it_dont_perform do
|
146
|
+
Resque.run!
|
147
|
+
assert(JobWithHooks.markers[:before], 'no before marker set')
|
148
|
+
assert(JobWithHooks.markers[:around], 'no around marker set')
|
149
|
+
assert(!JobWithHooks.markers[:after], 'after marker set, and it should not')
|
150
|
+
assert(!JobWithHooks.markers[:failed], 'failed marker set, and it should not')
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "but without before" do
|
156
|
+
setup do
|
157
|
+
JobWithHooksWithoutBefore.clear_markers
|
158
|
+
Resque.enqueue(JobWithHooksWithoutBefore)
|
159
|
+
end
|
160
|
+
|
161
|
+
should "not run before hooks during a run" do
|
162
|
+
Resque.run!
|
163
|
+
assert(!JobWithHooksWithoutBefore.markers[:before], 'before marker set, and it should not')
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "when before_enqueue returns false" do
|
168
|
+
setup do
|
169
|
+
JobWithHooksBeforeBlocks.clear_markers
|
170
|
+
end
|
171
|
+
|
172
|
+
should "not queue" do
|
173
|
+
Resque.enqueue JobWithHooksBeforeBlocks
|
174
|
+
assert_not_queued JobWithHooksBeforeBlocks
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
context "but without around" do
|
180
|
+
setup do
|
181
|
+
JobWithHooksWithoutAround.clear_markers
|
182
|
+
Resque.enqueue(JobWithHooksWithoutAround)
|
183
|
+
end
|
184
|
+
|
185
|
+
should "not run around hooks during a run" do
|
186
|
+
Resque.run!
|
187
|
+
assert(!JobWithHooksWithoutAround.markers[:around], 'around marker set, and it should not')
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
context "Block assertions" do
|
193
|
+
should "pass the assert_queued(job) assertion when queued in block" do
|
194
|
+
assert_queues(HighPriorityJob) do
|
195
|
+
Resque.enqueue(HighPriorityJob)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
should "pass the assert_queued(job) assertion when queued in block and outside" do
|
200
|
+
Resque.enqueue(HighPriorityJob)
|
201
|
+
assert_queues(HighPriorityJob) do
|
202
|
+
Resque.enqueue(HighPriorityJob)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
should "fail the assert_queued(job) assertion when not queued in block but outside" do
|
207
|
+
Resque.enqueue(LowPriorityJob)
|
208
|
+
assert_raise Test::Unit::AssertionFailedError do
|
209
|
+
assert_queues(LowPriorityJob) do
|
210
|
+
# Nothing.
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
should "pass the assert_not_queued(job) assertion when not queued in block" do
|
216
|
+
Resque.enqueue(LowPriorityJob)
|
217
|
+
assert_not_queued(LowPriorityJob) do
|
218
|
+
# Nothing.
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
should "fail the assert_not_queued(job) assertion when not queued in block" do
|
223
|
+
assert_raise Test::Unit::AssertionFailedError do
|
224
|
+
assert_not_queued(LowPriorityJob) do
|
225
|
+
Resque.enqueue(LowPriorityJob)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
should "fail the assert_not_queued(job) assertion when queued and not in block" do
|
231
|
+
assert_raise Test::Unit::AssertionFailedError do
|
232
|
+
Resque.enqueue(LowPriorityJob)
|
233
|
+
assert_not_queued(LowPriorityJob) do
|
234
|
+
Resque.enqueue(LowPriorityJob)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
should "pass the assert_nothing_queued assertion when nothing queued in block" do
|
240
|
+
Resque.enqueue(LowPriorityJob)
|
241
|
+
assert_nothing_queued do
|
242
|
+
# Nothing.
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
should "fail the assert_nothing_queued assertion when queued in block" do
|
247
|
+
assert_raise Test::Unit::AssertionFailedError do
|
248
|
+
assert_nothing_queued do
|
249
|
+
Resque.enqueue(LowPriorityJob)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
context "An empty queue" do
|
256
|
+
should "pass the assert_not_queued(job) assertion" do
|
257
|
+
assert_not_queued(LowPriorityJob)
|
258
|
+
end
|
259
|
+
|
260
|
+
should "fail the assert_queued(job) assertion" do
|
261
|
+
assert_raise Test::Unit::AssertionFailedError do
|
262
|
+
assert_queued(LowPriorityJob)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
should "be size 0 when empty" do
|
267
|
+
assert_equal 0, Resque.size(:low)
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
context "A task that schedules a resque job with arguments" do
|
272
|
+
setup do
|
273
|
+
Resque.enqueue(JobWithArguments, 1, :test, {:symbol => :symbol})
|
274
|
+
end
|
275
|
+
|
276
|
+
should "pass the assert_queued(job, *args) assertion if the args match and sees enqueued symbols as strings" do
|
277
|
+
assert_queued(JobWithArguments, [1, "test", {"symbol"=>"symbol"}])
|
278
|
+
end
|
279
|
+
|
280
|
+
should "pass the assert_queued(job, *args) assertion if the args match using symbols" do
|
281
|
+
assert_queued(JobWithArguments, [1, :test, {:symbol => :symbol}])
|
282
|
+
end
|
283
|
+
|
284
|
+
should "pass the assert_queued(job) assertion with no args passed" do
|
285
|
+
assert_queued(JobWithArguments)
|
286
|
+
end
|
287
|
+
|
288
|
+
should "fail the assert_queued(job) assertion if the args don't match" do
|
289
|
+
assert_raise Test::Unit::AssertionFailedError do
|
290
|
+
assert_queued(JobWithArguments, [2, "test"])
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
should "pass the assert_not_queued(job) assertion if the args don't match" do
|
295
|
+
assert_not_queued(JobWithArguments, [2, "test"])
|
296
|
+
end
|
297
|
+
|
298
|
+
should "fail the assert_not_queued(job) assertion if the args match" do
|
299
|
+
assert_raise Test::Unit::AssertionFailedError do
|
300
|
+
assert_not_queued(JobWithArguments, [1, "test", {"symbol"=>"symbol"}])
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
context "A job that schedules a new resque job" do
|
306
|
+
setup do
|
307
|
+
Resque.enqueue(JobThatCreatesANewJob)
|
308
|
+
end
|
309
|
+
|
310
|
+
should "pass the assert_queued(job) assertion" do
|
311
|
+
assert_queued(JobThatCreatesANewJob)
|
312
|
+
end
|
313
|
+
|
314
|
+
should "fail the assert_not_queued(job) assertion" do
|
315
|
+
assert_raise Test::Unit::AssertionFailedError do
|
316
|
+
assert_not_queued(JobThatCreatesANewJob)
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
should "pass the assert_not_queued(LowPriorityJob) assertion" do
|
321
|
+
assert_not_queued(LowPriorityJob)
|
322
|
+
end
|
323
|
+
|
324
|
+
context ", when Resque.run! is called," do
|
325
|
+
setup do
|
326
|
+
Resque.run!
|
327
|
+
end
|
328
|
+
|
329
|
+
should "clear the job from the queue" do
|
330
|
+
assert_not_queued(JobThatCreatesANewJob)
|
331
|
+
end
|
332
|
+
|
333
|
+
should "add a LowPriorityJob" do
|
334
|
+
assert_queued(LowPriorityJob)
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
context ", when Resque.full_run!" do
|
339
|
+
setup do
|
340
|
+
assert !LowPriorityJob.run?, "The job should not have been run yet, did you call 'LowPriorityJob.run = false' in teardowns of other tests?"
|
341
|
+
Resque.full_run!
|
342
|
+
end
|
343
|
+
|
344
|
+
teardown do
|
345
|
+
LowPriorityJob.run = false
|
346
|
+
end
|
347
|
+
|
348
|
+
should "clear the jobs from the queue" do
|
349
|
+
assert_not_queued(JobThatCreatesANewJob)
|
350
|
+
assert_not_queued(LowPriorityJob)
|
351
|
+
end
|
352
|
+
|
353
|
+
should "run the new resque jobs" do
|
354
|
+
assert LowPriorityJob.run?, "LowPriorityJob should have been run"
|
355
|
+
end
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
context "A task in a different queue" do
|
360
|
+
setup do
|
361
|
+
Resque.enqueue(LowPriorityJob)
|
362
|
+
Resque.enqueue(HighPriorityJob)
|
363
|
+
end
|
364
|
+
|
365
|
+
should "add a LowPriorityJob" do
|
366
|
+
assert_queued(LowPriorityJob)
|
367
|
+
end
|
368
|
+
|
369
|
+
should "add a HighPriorityJob" do
|
370
|
+
assert_queued(HighPriorityJob)
|
371
|
+
end
|
372
|
+
|
373
|
+
context ", when Resque.run_for! is called," do
|
374
|
+
should "run only tasks in the high priority queue" do
|
375
|
+
Resque.run_for!(Resque.queue_for(HighPriorityJob))
|
376
|
+
|
377
|
+
assert_queued(LowPriorityJob)
|
378
|
+
assert_not_queued(HighPriorityJob)
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
context "An assertion message" do
|
384
|
+
context "of assert_queued" do
|
385
|
+
should "include job class and queue content" do
|
386
|
+
begin
|
387
|
+
assert_not_queued(LowPriorityJob)
|
388
|
+
rescue Test::Unit::AssertionFailedError => error
|
389
|
+
assert_equal "LowPriorityJob should have been queued in low: [].", error.message
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
should "include job arguments if provided" do
|
394
|
+
begin
|
395
|
+
assert_not_queued(JobWithArguments, [1, "test"])
|
396
|
+
rescue Test::Unit::AssertionFailedError => error
|
397
|
+
assert_equal "JobWithArguments with [1, \"test\"] should have been queued in medium: [].", error.message
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
context "of assert_not_queued" do
|
403
|
+
should "include job class and queue content" do
|
404
|
+
begin
|
405
|
+
Resque.enqueue(LowPriorityJob)
|
406
|
+
assert_not_queued(LowPriorityJob)
|
407
|
+
rescue Test::Unit::AssertionFailedError => error
|
408
|
+
assert_equal "LowPriorityJob should not have been queued in low.", error.message
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
should "include job arguments if provided" do
|
413
|
+
begin
|
414
|
+
Resque.enqueue(JobWithArguments, 1, "test")
|
415
|
+
assert_not_queued(JobWithArguments, [1, "test"])
|
416
|
+
rescue Test::Unit::AssertionFailedError => error
|
417
|
+
assert_equal "JobWithArguments with [1, \"test\"] should not have been queued in medium.", error.message
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
context "of assert_nothing_queued" do
|
423
|
+
should "include diff" do
|
424
|
+
begin
|
425
|
+
Resque.reset!
|
426
|
+
assert_nothing_queued do
|
427
|
+
Resque.enqueue(LowPriorityJob)
|
428
|
+
end
|
429
|
+
rescue Test::Unit::AssertionFailedError => error
|
430
|
+
assert_equal "No jobs should have been queued.\n<0> expected but was\n<1>.", error.message
|
431
|
+
end
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
context "A job that does not specify a queue" do
|
437
|
+
should "receive Resque::NoQueueError" do
|
438
|
+
assert_raise(Resque::NoQueueError) do
|
439
|
+
Resque.enqueue(JobThatDoesNotSpecifyAQueue)
|
440
|
+
end
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
context "A job that is created using Resque::Job.create" do
|
445
|
+
should "be queued" do
|
446
|
+
assert_nothing_raised do
|
447
|
+
Resque::Job.create(:my_custom_queue, "LowPriorityJob", "arg1", "arg2")
|
448
|
+
assert_job_created(:my_custom_queue, LowPriorityJob, ["arg1", "arg2"])
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
should "queue a job with a dasherized name" do
|
453
|
+
assert_nothing_raised do
|
454
|
+
Resque::Job.create(:my_custom_queue, "low-priority-job", "arg1", "arg2")
|
455
|
+
assert_job_created(:my_custom_queue, LowPriorityJob, ["arg1", "arg2"])
|
456
|
+
end
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
end
|