Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 14, 2011
@nfo nfo Merge remote-tracking branch 'justinweiss/master' 7e48b8d
@nfo nfo Mock the Redis connection with the "mock_redis" gem. It allows Resque…
… plugins not to rely on a real Redis server.
2a58973
@nfo nfo Reset the mocked Redis connection in Resque.reset!. 9f5b245
Commits on Oct 21, 2011
@nfo nfo Freeze the job arguments, to ensure that they are not updated. It is …
…problematic for resque-batched-job, as it encodes job args in JSON to know how many jobs remain in the batch. Updating the job args changes the JSON string and thus resque-batched-job doesn't remove the current job from the batch. If one wants to update an argument before queueing it to another job, then one must call the `dup` method. The new object is a clone and is unfrozen. The origin job arguments stored in Resque::Job remains the same.
6a2c850
View
2  Gemfile.lock
@@ -3,11 +3,13 @@ PATH
specs:
resque_unit (0.4.0)
json (>= 1.4.6)
+ mock_redis
GEM
remote: http://rubygems.org/
specs:
json (1.5.3)
+ mock_redis (0.2.0)
rake (0.8.7)
shoulda (2.11.3)
View
1  lib/resque_unit.rb
@@ -8,6 +8,7 @@ module ResqueUnit
end
require 'test/unit'
+require 'mock_redis'
require 'resque_unit/helpers'
require 'resque_unit/resque'
require 'resque_unit/errors'
View
28 lib/resque_unit/resque.rb
@@ -4,12 +4,30 @@ module Resque
include Helpers
extend self
+ # When Resque tries to configure Redis, it just initialize an instance
+ # of MockRedis.
+ # Not meant to mock the main Resque features, just here for plugins.
+ def redis=(*args)
+ redis
+ end
+
+ # Returns the current Redis mocked connection. If none has been created,
+ # will create a new one.
+ # Not meant to mock the main Resque features, just here for plugins.
+ def redis
+ @redis = MockRedis.new unless @redis.is_a?(MockRedis)
+ @redis ||= MockRedis.new
+ end
+
# Resets all the queues to the empty state. This should be called in
# your test's +setup+ method until I can figure out a way for it to
# automatically be called.
+ # Also reset the mocked Redis connection.
#
# If <tt>queue_name</tt> is given, then resets only that queue.
def reset!(queue_name = nil)
+ @redis = nil
+
if @queue && queue_name
@queue[queue_name] = []
else
@@ -86,6 +104,16 @@ def run_for!(queue_name, limit=false)
def exec_payloads(raw_payloads)
raw_payloads.each do |raw_payload|
job_payload = decode(raw_payload)
+
+ # Freeze the job arguments, to ensure that they are not updated.
+ # It is problematic for resque-batched-job, as it encodes job args in JSON to know how many jobs
+ # remain in the batch. Updating the job args changes the JSON string and thus resque-batched-job
+ # doesn't remove the current job from the batch.
+ # If one wants to update an argument before queueing it to another job, then one must call the `dup`
+ # method. The new object is a clone and is unfrozen. The origin job arguments stored in Resque::Job
+ # remains the same.
+ job_payload["args"].map(&:freeze)
+
@hooks_enabled ? perform_with_hooks(job_payload) : perform_without_hooks(job_payload)
end
end
View
2  lib/resque_unit/scheduler.rb
@@ -19,7 +19,7 @@ def enqueue_in(number_of_seconds_from_now, klass, *args)
end
def enqueue_with_timestamp(timestamp, klass, *args)
- enqueue_unit(queue_for(klass), {"class" => klass, "args" => args, "timestamp" => timestamp})
+ enqueue_unit(queue_for(klass), {"class" => klass.name, "args" => args, "timestamp" => timestamp})
end
def remove_delayed(klass, *args)
View
1  resque_unit.gemspec
@@ -14,4 +14,5 @@ spec = Gem::Specification.new do |s|
s.files = Dir['lib/**/*.rb'] + Dir['test/**/*.rb']
s.test_files = Dir.glob('test/*_test.rb')
+ s.add_dependency "mock_redis" # For Resque plugins
end
View
28 test/resque_unit_test.rb
@@ -457,4 +457,32 @@ def setup
end
end
+ context "A job that tries to update one of its arguments" do
+ should "raise a RuntimeError" do
+ Resque.enqueue(JobThatTriesToChangeAJobArg, {})
+ assert_raise(RuntimeError) do
+ Resque.run_for!(Resque.queue_for(JobThatTriesToChangeAJobArg))
+ end
+ end
+ should "raise a RuntimeError complaining about a frozen object" do
+ Resque.enqueue(JobThatTriesToChangeAJobArg, {})
+ begin
+ Resque.run_for!(Resque.queue_for(JobThatTriesToChangeAJobArg))
+ rescue RuntimeError => rt
+ assert_match /frozen/, rt.message
+ end
+ end
+ end
+
+ context "A plugin using Redis" do
+ should "use an instance of MockRedis" do
+ assert_instance_of MockRedis, Resque.redis
+ end
+
+ should "use an instance of MockRedis even if Resque tries to configure Redis" do
+ Resque.redis = 'lol'
+ assert_instance_of MockRedis, Resque.redis
+ end
+ end
+
end
View
7 test/sample_jobs.rb
@@ -165,3 +165,10 @@ def self.before_perform_mark(*args)
markers[:before] = true
end
end
+
+class JobThatTriesToChangeAJobArg
+ @queue = :tries_to_change_a_job_arg
+ def self.perform(hash)
+ hash['lol'] = 'cats'
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.