resque_unit 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -71,4 +71,22 @@ Caveats
71
71
  * You should make sure that you call `Resque.reset!` in your test's
72
72
  setup method to clear all of the test queues.
73
73
 
74
+ Resque-Scheduler Support
75
+ ========================
76
+
77
+ By calling `require 'resque_unit_scheduler'`, ResqueUnit will provide
78
+ mocks for resque-scheduler's `enqueue_at` and `enqueue_in` methods,
79
+ along with a few extra assertions. These are used like this:
80
+
81
+ Resque.enqueue_in(600, MediumPriorityJob) # enqueues MediumPriorityJob in 600 seconds
82
+ assert_queued_in(600, MediumPriorityJob) # will pass
83
+ assert_not_queued_in(300, MediumPriorityJob) # will also pass
84
+
85
+ Resque.enqueue_at(Time.now + 10, MediumPriorityJob) # enqueues MediumPriorityJob at 10 seconds from now
86
+ assert_queued_at(Time.now + 10, MediumPriorityJob) # will pass
87
+ assert_not_queued_at(Time.now + 1, MediumPriorityJob) # will also pass
88
+
89
+ For now, `assert_queued` and `assert_not_queued` will pass for any
90
+ scheduled job. `Resque.run!` will run all scheduled jobs as well.
91
+
74
92
  Copyright (c) 2010 Justin Weiss, released under the MIT license
@@ -17,7 +17,7 @@ module ResqueUnit::Assertions
17
17
  # The opposite of +assert_queued+.
18
18
  def assert_not_queued(klass, args = nil, message = nil)
19
19
  queue = Resque.queue_for(klass)
20
- assert_block (message || "#{klass} should have been queued in #{queue}.") do
20
+ assert_block (message || "#{klass} should not have been queued in #{queue}.") do
21
21
  !in_queue?(queue, klass, args)
22
22
  end
23
23
  end
@@ -25,11 +25,15 @@ module ResqueUnit::Assertions
25
25
  private
26
26
 
27
27
  def in_queue?(queue, klass, args = nil)
28
- if args # verify the klass and args match some element in the queue
29
- !Resque.queue(queue).select {|e| e[:klass] == klass && e[:args] == args}.empty?
30
- else # if no args were passed, just verify the job is in the queue
31
- !Resque.queue(queue).select {|e| e[:klass] == klass}.empty?
28
+ !matching_jobs(queue, klass, args).empty?
29
+ end
30
+
31
+ def matching_jobs(queue, klass, args = nil)
32
+ 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}
34
+ else # if no args were passed, retrieve all queued jobs that match klass
35
+ Resque.queue(queue).select {|e| e[:klass] == klass}
32
36
  end
33
37
  end
34
-
38
+
35
39
  end
@@ -0,0 +1,29 @@
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
+ queue(queue_for(klass)) << {:klass => klass, :args => args, :timestamp => timestamp}
23
+ end
24
+
25
+ end
26
+
27
+ Resque.send(:extend, Scheduler)
28
+ end
29
+
@@ -0,0 +1,47 @@
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
+ module ResqueUnit::SchedulerAssertions
4
+
5
+ # Asserts that +klass+ has been queued into its appropriate queue at
6
+ # least once, with a +timestamp+ less than or equal to
7
+ # +expected_timestamp+. If the job wasn't queued with a timestamp,
8
+ # the assertion fails.. If +args+ is nil, it only asserts that the
9
+ # klass has been queued. Otherwise, it asserts that the klass has
10
+ # been queued with the correct arguments. Pass an empty array for
11
+ # +args+ if you want to assert that klass has been queued without
12
+ # arguments.
13
+ def assert_queued_at(expected_timestamp, klass, args = nil, message = nil)
14
+ queue = Resque.queue_for(klass)
15
+ assert_block (message || "#{klass} should have been queued in #{queue} before #{expected_timestamp}: #{Resque.queue(queue).inspect}.") do
16
+ in_timestamped_queue?(queue, expected_timestamp, klass, args)
17
+ end
18
+ end
19
+
20
+ # Similar to +assert_queued_at+, except it takes an expected time
21
+ # difference (in seconds) instead of a timestamp.
22
+ def assert_queued_in(expected_time_difference, klass, args = nil, message = nil)
23
+ assert_queued_at(Time.now + expected_time_difference, klass, args, message)
24
+ end
25
+
26
+ # opposite of +assert_queued_at+
27
+ def assert_not_queued_at(expected_timestamp, klass, args = nil, message = nil)
28
+ queue = Resque.queue_for(klass)
29
+ assert_block (message || "#{klass} should not have been queued in #{queue} before #{expected_timestamp}.") do
30
+ !in_timestamped_queue?(queue, expected_timestamp, klass, args)
31
+ end
32
+ end
33
+
34
+ # opposite of +assert_queued_in+
35
+ def assert_not_queued_in(expected_time_difference, klass, args = nil, message = nil)
36
+ assert_not_queued_at(Time.now + expected_time_difference, klass, args, message)
37
+ end
38
+
39
+ private
40
+
41
+ def in_timestamped_queue?(queue, expected_timestamp, klass, args = nil)
42
+ # check if we have any matching jobs with a timestamp less than
43
+ # expected_timestamp
44
+ !matching_jobs(queue, klass, args).select {|e| e[:timestamp] && e[:timestamp] <= expected_timestamp}.empty?
45
+ end
46
+
47
+ end
@@ -0,0 +1,4 @@
1
+ require 'resque_unit/scheduler'
2
+ require 'resque_unit/scheduler_assertions'
3
+
4
+ Test::Unit::TestCase.send(:include, ResqueUnit::SchedulerAssertions)
@@ -0,0 +1,70 @@
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
+ end
31
+
32
+ context "A task that schedules a resque job in 5 minutes with arguments" do
33
+ setup { Resque.enqueue_in(600, JobWithArguments, 1, "test") }
34
+ should "pass the assert_queued_in(600, JobWithArguments) assertion" do
35
+ assert_queued_in(600, JobWithArguments)
36
+ end
37
+
38
+ should "pass the assert_queued_in(600, JobWithArguments, [1, 'test']) assertion" do
39
+ assert_queued_in(600, JobWithArguments, [1, 'test'])
40
+ end
41
+
42
+ should "fail the assert_queued_in(600, JobWithArguments, [2, 'test']) assertion" do
43
+ assert_raise Test::Unit::AssertionFailedError do
44
+ assert_queued_in(600, JobWithArguments, [2, 'test'])
45
+ end
46
+ end
47
+ end
48
+
49
+ context "A task that schedules a resque job on Sept. 6, 2016 at 6am" do
50
+ setup do
51
+ @time = Time.mktime(2016, 9, 6, 6)
52
+ Resque.enqueue_at(@time, MediumPriorityJob)
53
+ end
54
+
55
+ should "pass the assert_queued_at(@time, MediumPriorityJob) assertion" do
56
+ assert_queued_at(@time, MediumPriorityJob)
57
+ end
58
+
59
+ should "fail the assert_queued_at(@time - 100, MediumPriorityJob) assertion" do
60
+ assert_raise Test::Unit::AssertionFailedError do
61
+ assert_queued_at(@time - 100, MediumPriorityJob)
62
+ end
63
+ end
64
+
65
+ should "pass the assert_not_queued_at(@time - 100, MediumPriorityJob) assertion" do
66
+ assert_not_queued_at(@time - 100, MediumPriorityJob)
67
+ end
68
+ end
69
+
70
+ end
@@ -1,8 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
- class ResqueUnitTest < ActiveSupport::TestCase
3
+ class ResqueUnitTest < Test::Unit::TestCase
4
4
 
5
- setup do
5
+ def setup
6
6
  # should probably happen automatically, but I haven't thought of a
7
7
  # good way to hook setup() yet.
8
8
  Resque.reset!
@@ -1,6 +1,4 @@
1
1
  require 'rubygems'
2
- require 'active_support'
3
- require 'active_support/test_case'
4
2
  require 'shoulda'
5
3
  require 'resque_unit'
6
4
  require 'sample_jobs'
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Justin Weiss
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-06 00:00:00 -07:00
17
+ date: 2010-05-25 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -40,7 +40,11 @@ extra_rdoc_files:
40
40
  files:
41
41
  - lib/resque_unit/assertions.rb
42
42
  - lib/resque_unit/resque.rb
43
+ - lib/resque_unit/scheduler.rb
44
+ - lib/resque_unit/scheduler_assertions.rb
43
45
  - lib/resque_unit.rb
46
+ - lib/resque_unit_scheduler.rb
47
+ - test/resque_unit_scheduler_test.rb
44
48
  - test/resque_unit_test.rb
45
49
  - test/sample_jobs.rb
46
50
  - test/test_helper.rb
@@ -76,4 +80,5 @@ signing_key:
76
80
  specification_version: 3
77
81
  summary: Test::Unit support for resque job queueing
78
82
  test_files:
83
+ - test/resque_unit_scheduler_test.rb
79
84
  - test/resque_unit_test.rb