resque_unit 0.2.7 → 0.2.8

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.
@@ -1,11 +1,17 @@
1
1
  module ResqueUnit
2
2
  end
3
3
 
4
+ begin
5
+ require 'yajl'
6
+ rescue LoadError
7
+ require 'json'
8
+ end
9
+
4
10
  require 'test/unit'
11
+ require 'resque_unit/helpers'
5
12
  require 'resque_unit/resque'
6
13
  require 'resque_unit/errors'
7
14
  require 'resque_unit/assertions'
8
15
 
9
-
10
16
  Test::Unit::TestCase.send(:include, ResqueUnit::Assertions)
11
17
 
@@ -0,0 +1,30 @@
1
+ module Resque
2
+ module Helpers
3
+ # Given a Ruby object, returns a string suitable for storage in a
4
+ # queue.
5
+ def encode(object)
6
+ if defined? Yajl
7
+ Yajl::Encoder.encode(object)
8
+ else
9
+ object.to_json
10
+ end
11
+ end
12
+
13
+ # Given a string, returns a Ruby object.
14
+ def decode(object)
15
+ return unless object
16
+
17
+ if defined? Yajl
18
+ begin
19
+ Yajl::Parser.parse(object, :check_utf8 => false)
20
+ rescue Yajl::ParseError
21
+ end
22
+ else
23
+ begin
24
+ JSON.parse(object)
25
+ rescue JSON::ParserError
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,13 +1,15 @@
1
1
  # The fake Resque class. This needs to be loaded after the real Resque
2
2
  # for the assertions in +ResqueUnit::Assertions+ to work.
3
3
  module Resque
4
+ include Helpers
5
+ extend self
4
6
 
5
7
  # Resets all the queues to the empty state. This should be called in
6
8
  # your test's +setup+ method until I can figure out a way for it to
7
9
  # automatically be called.
8
10
  #
9
11
  # If <tt>queue_name</tt> is given, then resets only that queue.
10
- def self.reset!(queue_name = nil)
12
+ def reset!(queue_name = nil)
11
13
  if @queue && queue_name
12
14
  @queue[queue_name] = []
13
15
  else
@@ -19,13 +21,13 @@ module Resque
19
21
  # element is of the form +{:klass => klass, :args => args}+ where
20
22
  # +klass+ is the job's class and +args+ is an array of the arguments
21
23
  # passed to the job.
22
- def self.queue(queue_name)
24
+ def queue(queue_name)
23
25
  self.reset! unless @queue
24
26
  @queue[queue_name]
25
27
  end
26
28
 
27
29
  # Executes all jobs in all queues in an undefined order.
28
- def self.run!
30
+ def run!
29
31
  old_queue = @queue.dup
30
32
  self.reset!
31
33
 
@@ -37,7 +39,7 @@ module Resque
37
39
  end
38
40
 
39
41
  # Executes all jobs in the given queue in an undefined order.
40
- def self.run_for!(queue_name)
42
+ def run_for!(queue_name)
41
43
  jobs = self.queue(queue_name)
42
44
  self.reset!(queue_name)
43
45
 
@@ -49,7 +51,7 @@ module Resque
49
51
  # 1. Execute all jobs in all queues in an undefined order,
50
52
  # 2. Check if new jobs were announced, and execute them.
51
53
  # 3. Repeat 3
52
- def self.full_run!
54
+ def full_run!
53
55
  until empty_queues?
54
56
  @queue.each do |k, v|
55
57
  while job = v.shift
@@ -60,26 +62,26 @@ module Resque
60
62
  end
61
63
 
62
64
  # Returns the size of the given queue
63
- def self.size(queue_name)
65
+ def size(queue_name)
64
66
  self.reset! unless @queue
65
67
  @queue[queue_name].length
66
68
  end
67
69
 
68
70
  # :nodoc:
69
- def self.enqueue(klass, *args)
71
+ def enqueue(klass, *args)
70
72
  queue_name = queue_for(klass)
71
73
  # Behaves like Resque, raise if no queue was specifed
72
74
  raise NoQueueError.new("Jobs must be placed onto a queue.") unless queue_name
73
- queue(queue_name) << {:klass => klass, :args => args}
75
+ queue(queue_name) << {:klass => klass, :args => decode(encode(args))}
74
76
  end
75
77
 
76
78
  # :nodoc:
77
- def self.queue_for(klass)
79
+ def queue_for(klass)
78
80
  klass.instance_variable_get(:@queue) || (klass.respond_to?(:queue) && klass.queue)
79
81
  end
80
82
 
81
83
  # :nodoc:
82
- def self.empty_queues?
84
+ def empty_queues?
83
85
  @queue.all? do |k, v|
84
86
  v.empty?
85
87
  end
@@ -19,7 +19,7 @@ module ResqueUnit
19
19
  end
20
20
 
21
21
  def enqueue_with_timestamp(timestamp, klass, *args)
22
- queue(queue_for(klass)) << {:klass => klass, :args => args, :timestamp => timestamp}
22
+ queue(queue_for(klass)) << {:klass => klass, :args => decode(encode(args)), :timestamp => timestamp}
23
23
  end
24
24
 
25
25
  end
@@ -75,11 +75,11 @@ class ResqueUnitTest < Test::Unit::TestCase
75
75
 
76
76
  context "A task that schedules a resque job with arguments" do
77
77
  setup do
78
- Resque.enqueue(JobWithArguments, 1, "test")
78
+ Resque.enqueue(JobWithArguments, 1, :test, {:symbol => :symbol})
79
79
  end
80
80
 
81
- should "pass the assert_queued(job, *args) assertion if the args match" do
82
- assert_queued(JobWithArguments, [1, "test"])
81
+ should "pass the assert_queued(job, *args) assertion if the args match and sees enqueued symbols as strings" do
82
+ assert_queued(JobWithArguments, [1, "test", {"symbol"=>"symbol"}])
83
83
  end
84
84
 
85
85
  should "pass the assert_queued(job) assertion with no args passed" do
@@ -98,7 +98,7 @@ class ResqueUnitTest < Test::Unit::TestCase
98
98
 
99
99
  should "fail the assert_not_queued(job) assertion if the args match" do
100
100
  assert_raise Test::Unit::AssertionFailedError do
101
- assert_not_queued(JobWithArguments, [1, "test"])
101
+ assert_not_queued(JobWithArguments, [1, "test", {"symbol"=>"symbol"}])
102
102
  end
103
103
  end
104
104
  end
@@ -30,7 +30,7 @@ end
30
30
  class JobWithArguments
31
31
  @queue = :medium
32
32
 
33
- def self.perform(num, text)
33
+ def self.perform(num, text, hash)
34
34
 
35
35
  end
36
36
  end
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: 25
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 7
10
- version: 0.2.7
9
+ - 8
10
+ version: 0.2.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Justin Weiss
@@ -15,13 +15,29 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-29 00:00:00 -07:00
18
+ date: 2010-11-10 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: shoulda
22
+ name: json
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 11
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 6
34
+ version: 1.4.6
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: shoulda
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
25
41
  none: false
26
42
  requirements:
27
43
  - - ">="
@@ -31,7 +47,7 @@ dependencies:
31
47
  - 0
32
48
  version: "0"
33
49
  type: :development
34
- version_requirements: *id001
50
+ version_requirements: *id002
35
51
  description:
36
52
  email: justin@uberweiss.org
37
53
  executables: []
@@ -43,6 +59,7 @@ extra_rdoc_files:
43
59
  files:
44
60
  - lib/resque_unit/assertions.rb
45
61
  - lib/resque_unit/errors.rb
62
+ - lib/resque_unit/helpers.rb
46
63
  - lib/resque_unit/resque.rb
47
64
  - lib/resque_unit/scheduler.rb
48
65
  - lib/resque_unit/scheduler_assertions.rb