diff --git a/Gemfile.lock b/Gemfile.lock
index edb784d..159d147 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,17 +1,64 @@
PATH
remote: .
specs:
- resque_unit (0.4.8)
- json (>= 1.4.6)
+ resque_unit (1.0.0.beta.2)
+ fakeredis
+ resque
GEM
remote: https://rubygems.org/
specs:
+ byebug (4.0.5)
+ columnize (= 0.9.0)
+ coderay (1.1.0)
+ columnize (0.9.0)
+ fakeredis (0.5.0)
+ redis (~> 3.0)
json (1.8.3)
+ method_source (0.8.2)
minitest (5.8.0)
+ mono_logger (1.1.0)
+ multi_json (1.11.2)
+ pry (0.10.1)
+ coderay (~> 1.1.0)
+ method_source (~> 0.8.1)
+ slop (~> 3.4)
+ pry-byebug (3.1.0)
+ byebug (~> 4.0)
+ pry (~> 0.10)
+ rack (1.6.4)
+ rack-protection (1.5.3)
+ rack
rake (10.2.2)
rdoc (4.1.1)
json (~> 1.4)
+ redis (3.2.1)
+ redis-namespace (1.5.2)
+ redis (~> 3.0, >= 3.0.4)
+ resque (1.25.2)
+ mono_logger (~> 1.0)
+ multi_json (~> 1.0)
+ redis-namespace (~> 1.3)
+ sinatra (>= 0.9.2)
+ vegas (~> 0.1.2)
+ resque-scheduler (4.0.0)
+ mono_logger (~> 1.0)
+ redis (~> 3.0)
+ resque (~> 1.25)
+ rufus-scheduler (~> 3.0)
+ rufus-scheduler (3.0.9)
+ tzinfo
+ sinatra (1.4.6)
+ rack (~> 1.4)
+ rack-protection (~> 1.4)
+ tilt (>= 1.3, < 3)
+ slop (3.6.0)
+ thread_safe (0.3.5)
+ tilt (2.0.1)
+ tzinfo (1.2.2)
+ thread_safe (~> 0.1)
+ vegas (0.1.11)
+ rack (>= 1.0.0)
PLATFORMS
ruby
@@ -19,9 +66,11 @@ PLATFORMS
DEPENDENCIES
bundler
minitest (~> 5.0)
+ pry-byebug
rake
rdoc
+ resque-scheduler
resque_unit!
BUNDLED WITH
- 1.10.5
+ 1.10.6
diff --git a/README.md b/README.md
index 0eba9da..937d679 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ ResqueUnit
ResqueUnit provides some extra assertions and a mock Resque for
testing Rails code that depends on Resque. You can install it as
-either a gem or a plugin:
+either a gem:
gem install resque_unit
@@ -11,12 +11,6 @@ and in your test.rb:
config.gem 'resque_unit'
-If you'd rather install it as a plugin, you should be able to run
-
- script/plugin install git://github.com/justinweiss/resque_unit.git
-
-inside your Rails projects.
-
Examples
========
@@ -103,10 +97,6 @@ Caveats
* You should make sure that you call `Resque.reset!` in your test's
setup method to clear all of the test queues.
-* Hooks support is optional. Just because you probably don't want to call
- them during unit tests if they play with a DB. Call `Resque.enable_hooks!`
- in your tests's setup method to enable hooks. To disable hooks, call
- `Resque.disable_hooks!`.
Resque-Scheduler Support
========================
diff --git a/lib/resque_unit.rb b/lib/resque_unit.rb
index bacf30c..e07df33 100644
--- a/lib/resque_unit.rb
+++ b/lib/resque_unit.rb
@@ -6,12 +6,9 @@ module ResqueUnit
rescue LoadError
require 'json'
end
-
-require 'resque_unit/helpers'
+require 'resque'
require 'resque_unit/resque'
-require 'resque_unit/errors'
require 'resque_unit/assertions'
-require 'resque_unit/plugin'
if defined?(Test::Unit::TestCase)
Test::Unit::TestCase.send(:include, ResqueUnit::Assertions)
diff --git a/lib/resque_unit/assertions.rb b/lib/resque_unit/assertions.rb
index 3c903a8..65f0a06 100644
--- a/lib/resque_unit/assertions.rb
+++ b/lib/resque_unit/assertions.rb
@@ -8,14 +8,14 @@ module ResqueUnit::Assertions
# want to assert that klass has been queued without arguments. Pass a block
# if you want to assert something was queued within its execution.
def assert_queued(klass, args = nil, message = nil, &block)
- queue_name = Resque.queue_for(klass)
+ queue_name = Resque.queue_from_class(klass)
assert_job_created(queue_name, klass, args, message, &block)
end
alias assert_queues assert_queued
# The opposite of +assert_queued+.
def assert_not_queued(klass = nil, args = nil, message = nil, &block)
- queue_name = Resque.queue_for(klass)
+ queue_name = Resque.queue_from_class(klass)
queue = if block_given?
snapshot = Resque.size(queue_name)
@@ -31,9 +31,9 @@ def assert_not_queued(klass = nil, args = nil, message = nil, &block)
# Asserts no jobs were queued within the block passed.
def assert_nothing_queued(message = nil, &block)
- snapshot = Resque.size
+ snapshot = total_job_count
yield
- present = Resque.size
+ present = total_job_count
assert_equal snapshot, present, message || "No jobs should have been queued"
end
@@ -53,6 +53,11 @@ def assert_job_created(queue_name, klass, args = nil, message = nil, &block)
private
+ # The total count of all the jobs in Resque.
+ def total_job_count
+ Resque.queues.inject(0) { |acc, queue| acc + Resque.size(queue) }
+ end
+
# In Test::Unit, +assert_block+ displays only the message on a test
# failure and +assert+ always appends a message to the end of the
# passed-in assertion message. In MiniTest, it's the other way
diff --git a/lib/resque_unit/errors.rb b/lib/resque_unit/errors.rb
deleted file mode 100644
index 992724e..0000000
--- a/lib/resque_unit/errors.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-# Re-define errors in from Resque, in case the 'resque' gem was not loaded.
-module Resque
- # Raised whenever we need a queue but none is provided.
- unless defined?(NoQueueError)
- class NoQueueError < RuntimeError; end
- end
-
- # Raised when trying to create a job without a class
- unless defined?(NoClassError)
- class NoClassError < RuntimeError; end
- end
-
- # Raised when a worker was killed while processing a job.
- unless defined?(DirtyExit)
- class DirtyExit < RuntimeError; end
- end
-end
diff --git a/lib/resque_unit/helpers.rb b/lib/resque_unit/helpers.rb
deleted file mode 100644
index 25cbc73..0000000
--- a/lib/resque_unit/helpers.rb
+++ /dev/null
@@ -1,57 +0,0 @@
-module ResqueUnit
- module Helpers
- # Given a Ruby object, returns a string suitable for storage in a
- # queue.
- def encode(object)
- if defined? Yajl
- Yajl::Encoder.encode(object)
- else
- object.to_json
- end
- end
-
- # Given a string, returns a Ruby object.
- def decode(object)
- return unless object
-
- if defined? Yajl
- begin
- Yajl::Parser.parse(object, :check_utf8 => false)
- rescue Yajl::ParseError
- end
- else
- begin
- JSON.parse(object)
- rescue JSON::ParserError
- end
- end
- end
-
- # Given a word with dashes, returns a camel cased version of it.
- #
- # classify('job-name') # => 'JobName'
- def classify(dashed_word)
- dashed_word.split('-').each { |part| part[0] = part[0].chr.upcase }.join
- end
-
- # Given a camel cased word, returns the constant it represents
- #
- # constantize('JobName') # => JobName
- def constantize(camel_cased_word)
- camel_cased_word = camel_cased_word.to_s
-
- if camel_cased_word.include?('-')
- camel_cased_word = classify(camel_cased_word)
- end
-
- names = camel_cased_word.split('::')
- names.shift if names.empty? || names.first.empty?
-
- constant = Object
- names.each do |name|
- constant = constant.const_get(name) || constant.const_missing(name)
- end
- constant
- end
- end
-end
diff --git a/lib/resque_unit/plugin.rb b/lib/resque_unit/plugin.rb
deleted file mode 100644
index 364e40a..0000000
--- a/lib/resque_unit/plugin.rb
+++ /dev/null
@@ -1,70 +0,0 @@
-# A copy of the original Resque:Plugin class from the "resque" gem
-# No need to redefine this class if the resque gem was already loaded
-unless defined?(Resque::Plugin)
-
- module Resque
- module Plugin
- extend self
-
- LintError = Class.new(RuntimeError)
-
- # Ensure that your plugin conforms to good hook naming conventions.
- #
- # Resque::Plugin.lint(MyResquePlugin)
- def lint(plugin)
- hooks = before_hooks(plugin) + around_hooks(plugin) + after_hooks(plugin)
-
- hooks.each do |hook|
- if hook =~ /perform$/
- raise LintError, "#{plugin}.#{hook} is not namespaced"
- end
- end
-
- failure_hooks(plugin).each do |hook|
- if hook =~ /failure$/
- raise LintError, "#{plugin}.#{hook} is not namespaced"
- end
- end
- end
-
- # Given an object, returns a list `before_perform` hook names.
- def before_hooks(job)
- job.methods.grep(/^before_perform/).sort
- end
-
- # Given an object, returns a list `around_perform` hook names.
- def around_hooks(job)
- job.methods.grep(/^around_perform/).sort
- end
-
- # Given an object, returns a list `after_perform` hook names.
- def after_hooks(job)
- job.methods.grep(/^after_perform/).sort
- end
-
- # Given an object, returns a list `on_failure` hook names.
- def failure_hooks(job)
- job.methods.grep(/^on_failure/).sort
- end
-
- # Given an object, returns a list `after_enqueue` hook names.
- def after_enqueue_hooks(job)
- job.methods.grep(/^after_enqueue/).sort
- end
-
- # Given an object, returns a list `before_enqueue` hook names.
- def before_enqueue_hooks(job)
- job.methods.grep(/^before_enqueue/).sort
- end
- end
- end
-
-end
-
-unless defined?(Resque::Job::DontPerform)
- module Resque
- class Job
- DontPerform = Class.new(StandardError)
- end
- end
-end
diff --git a/lib/resque_unit/resque.rb b/lib/resque_unit/resque.rb
index 0b93a71..5940e06 100644
--- a/lib/resque_unit/resque.rb
+++ b/lib/resque_unit/resque.rb
@@ -1,224 +1,15 @@
-# The fake Resque class. This needs to be loaded after the real Resque
-# for the assertions in +ResqueUnit::Assertions+ to work.
-module Resque
- include ResqueUnit::Helpers
- extend self
-
- # 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.
- #
- # If queue_name is given, then resets only that queue.
- def reset!(queue_name = nil)
- if @queue && queue_name
- @queue[queue_name] = []
- else
- @queue = Hash.new { |h, k| h[k] = [] }
- end
- end
-
- # Returns a hash of all the queue names and jobs that have been queued. The
- # format is {queue_name => [job, ..]}.
- def self.queues
- @queue || reset!
- end
-
- # Returns an array of all the jobs that have been queued. Each
- # element is of the form +{"class" => klass, "args" => args}+ where
- # +klass+ is the job's class and +args+ is an array of the arguments
- # passed to the job.
- def queue(queue_name)
- queues[queue_name]
- end
-
- # Return an array of all jobs' payloads for queue
- # Elements are decoded
- def all(queue_name)
- result = list_range(queue_name, 0, size(queue_name))
- result.is_a?(Array) ? result : [ result ]
- end
-
- # Returns an array of jobs' payloads for queue.
- #
- # start and count should be integer and can be used for pagination.
- # start is the item to begin, count is how many items to return.
- #
- # To get the 3rd page of a 30 item, paginatied list one would use:
- # Resque.peek('my_list', 59, 30)
- def peek(queue_name, start = 0, count = 1)
- list_range(queue_name, start, count)
- end
-
- # Gets a range of jobs' payloads from queue.
- # Returns single element if count equal 1
- # Elements are decoded
- def list_range(key, start = 0, count = 1)
- data = if count == 1
- decode(queues[key][start])
- else
- (queues[key][start...start + count] || []).map { |entry| decode(entry) }
- end
- end
-
- # Yes, all Resque hooks!
- def enable_hooks!
- @hooks_enabled = true
- end
-
- def disable_hooks!
- @hooks_enabled = nil
- end
-
- # Executes all jobs in all queues in an undefined order.
- def run!
- payloads = []
- @queue.each do |queue_name, queue|
- payloads.concat queue.slice!(0, queue.size)
- end
- exec_payloads payloads.shuffle
- end
-
- def run_for!(queue_name, limit=false)
- queue = @queue[queue_name]
- exec_payloads queue.slice!(0, ( limit ? limit : queue.size) ).shuffle
- end
-
- def exec_payloads(raw_payloads)
- raw_payloads.each do |raw_payload|
- job_payload = decode(raw_payload)
- @hooks_enabled ? perform_with_hooks(job_payload) : perform_without_hooks(job_payload)
- end
- end
- private :exec_payloads
-
- # 1. Execute all jobs in all queues in an undefined order,
- # 2. Check if new jobs were announced, and execute them.
- # 3. Repeat 3
- def full_run!
- run! until empty_queues?
- end
-
- # Returns the size of the given queue
- def size(queue_name = nil)
- if queue_name
- queues[queue_name].length
- else
- queues.values.flatten.length
- end
- end
-
- # :nodoc:
- def enqueue(klass, *args)
- enqueue_to( queue_for(klass), klass, *args)
- end
-
- # :nodoc:
- def enqueue_to( queue_name, klass, *args )
- # Behaves like Resque, raise if no queue was specifed
- raise NoQueueError.new("Jobs must be placed onto a queue.") unless queue_name
- enqueue_unit(queue_name, {"class" => klass.to_s, "args" => args })
- end
-
- # :nodoc:
- def queue_for(klass)
- klass.instance_variable_get(:@queue) || (klass.respond_to?(:queue) && klass.queue)
- end
- alias :queue_from_class :queue_for
-
- # :nodoc:
- def empty_queues?
- queues.all? do |k, v|
- v.empty?
- end
- end
-
- def enqueue_unit(queue_name, hash)
- klass = constantize(hash["class"])
- if @hooks_enabled
- before_hooks = Plugin.before_enqueue_hooks(klass).map do |hook|
- klass.send(hook, *hash["args"])
- end
- return nil if before_hooks.any? { |result| result == false }
- end
- queue(queue_name) << encode(hash)
- if @hooks_enabled
- Plugin.after_enqueue_hooks(klass).each do |hook|
- klass.send(hook, *hash["args"])
- end
- end
- queue(queue_name).size
- end
-
- # Call perform on the job class
- def perform_without_hooks(job_payload)
- constantize(job_payload["class"]).perform(*job_payload["args"])
- end
-
- # Call perform on the job class, and adds support for Resque hooks.
- def perform_with_hooks(job_payload)
- job_class = constantize(job_payload["class"])
- before_hooks = Resque::Plugin.before_hooks(job_class)
- around_hooks = Resque::Plugin.around_hooks(job_class)
- after_hooks = Resque::Plugin.after_hooks(job_class)
- failure_hooks = Resque::Plugin.failure_hooks(job_class)
-
- begin
- # Execute before_perform hook. Abort the job gracefully if
- # Resque::DontPerform is raised.
- begin
- before_hooks.each do |hook|
- job_class.send(hook, *job_payload["args"])
- end
- rescue Resque::Job::DontPerform
- return false
- end
-
- # Execute the job. Do it in an around_perform hook if available.
- if around_hooks.empty?
- perform_without_hooks(job_payload)
- job_was_performed = true
- else
- # We want to nest all around_perform plugins, with the last one
- # finally calling perform
- stack = around_hooks.reverse.inject(nil) do |last_hook, hook|
- if last_hook
- lambda do
- job_class.send(hook, *job_payload["args"]) { last_hook.call }
- end
- else
- lambda do
- job_class.send(hook, *job_payload["args"]) do
- result = perform_without_hooks(job_payload)
- job_was_performed = true
- result
- end
- end
- end
- end
- stack.call
- end
-
- # Execute after_perform hook
- after_hooks.each do |hook|
- job_class.send(hook, *job_payload["args"])
- end
-
- # Return true if the job was performed
- return job_was_performed
-
- # If an exception occurs during the job execution, look for an
- # on_failure hook then re-raise.
- rescue Object => e
- failure_hooks.each { |hook| job_class.send(hook, e, *job_payload["args"]) }
- raise e
- end
- end
-
- class Job
- extend ResqueUnit::Helpers
- def self.create(queue, klass_name, *args)
- Resque.enqueue_unit(queue, {"class" => constantize(klass_name).to_s, "args" => args})
- end
- end
-
-end
+require 'resque'
+require 'resque_unit/resque/test_extensions'
+
+# This is a little weird. Fakeredis registers itself as the default
+# redis driver after you load it. This might not be what you want,
+# though -- resque_unit needs fakeredis, but you may have a reason to
+# use a different redis driver for the rest of your test code. So
+# we'll store the old default here, and restore it afer we're done
+# loading fakeredis. Then, we'll point resque at fakeredis
+# specifically.
+default_redis_driver = Redis::Connection.drivers.pop
+require 'fakeredis'
+Redis::Connection.drivers << default_redis_driver if default_redis_driver
+
+Resque.extend Resque::TestExtensions
diff --git a/lib/resque_unit/resque/test_extensions.rb b/lib/resque_unit/resque/test_extensions.rb
new file mode 100644
index 0000000..900a7df
--- /dev/null
+++ b/lib/resque_unit/resque/test_extensions.rb
@@ -0,0 +1,66 @@
+module Resque
+ module TestExtensions
+ # A redis connection that always uses fakeredis.
+ def fake_redis
+ @fake_redis ||= Redis.new(driver: :memory)
+ end
+
+ # Always return the fake redis.
+ def redis
+ fake_redis
+ 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.
+ #
+ # If queue_name is given, then resets only that queue.
+ def reset!(queue = nil)
+ if queue
+ remove_queue(queue)
+ else
+ redis.flushall
+ end
+ end
+
+ # Return an array of all jobs' payloads for queue
+ # Elements are decoded
+ def all(queue_name)
+ jobs = peek(queue_name, 0, size(queue_name))
+ jobs.kind_of?(Array) ? jobs : [jobs]
+ end
+ alias queue all
+
+ # Executes all jobs in all queues in an undefined order.
+ def run!
+ payloads = []
+ queues.each do |queue|
+ size(queue).times { payloads << pop(queue) }
+ end
+ exec_payloads payloads.shuffle
+ end
+
+ def run_for!(queue, limit = Float::INFINITY)
+ job_count = [limit, size(queue)].min
+ payloads = []
+
+ job_count.times { payloads << pop(queue) }
+ exec_payloads payloads.shuffle
+ end
+
+ def exec_payloads(raw_payloads)
+ raw_payloads.each do |raw_payload|
+ Resque::Job.new(:inline, raw_payload).perform
+ end
+ end
+
+ private :exec_payloads
+
+ # 1. Execute all jobs in all queues in an undefined order,
+ # 2. Check if new jobs were announced, and execute them.
+ # 3. Repeat 3
+ def full_run!
+ run! until queues.all? { |queue| size(queue) == 0 }
+ end
+ end
+end
diff --git a/lib/resque_unit/scheduler.rb b/lib/resque_unit/scheduler.rb
deleted file mode 100644
index 16fd159..0000000
--- a/lib/resque_unit/scheduler.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-module ResqueUnit
-
- # ResqueUnit::Scheduler is a group of functions mocking the behavior
- # of resque-scheduler. It is included into Resque when
- # 'resque_unit_scheduler' is required.
- module Scheduler
-
- # takes a timestamp which will be used to schedule the job
- # for queueing. Until timestamp is in the past, the job will
- # sit in the schedule list.
- def enqueue_at(timestamp, klass, *args)
- enqueue_with_timestamp(timestamp, klass, *args)
- end
-
- # Identical to enqueue_at but takes number_of_seconds_from_now
- # instead of a timestamp.
- def enqueue_in(number_of_seconds_from_now, klass, *args)
- enqueue_at(Time.now + number_of_seconds_from_now, klass, *args)
- end
-
- def enqueue_with_timestamp(timestamp, klass, *args)
- enqueue_unit(queue_for(klass), {"class" => klass.name, "args" => args, "timestamp" => timestamp})
- end
-
- def remove_delayed(klass, *args)
- # points to real queue
- encoded_job_payloads = Resque.queue(queue_for(klass))
- args ||= []
- encoded_job_payloads.delete_if { |e| e = Resque.decode(e); e["class"] == klass.to_s && e["args"] == args }
- end
-
- def remove_delayed_job_from_timestamp(timestamp, klass, *args)
- encoded_job_payloads = Resque.queue(queue_for(klass))
- args ||= []
- encoded_job_payloads.delete_if { |e| e = Resque.decode(e); e["class"] == klass.to_s && Time.parse(e["timestamp"]).to_i == Time.parse(timestamp.to_s).to_i && e["args"] == args }
- end
- end
-
- Resque.send(:extend, Scheduler)
-end
diff --git a/lib/resque_unit/scheduler_assertions.rb b/lib/resque_unit/scheduler_assertions.rb
index 78513d8..25929a0 100644
--- a/lib/resque_unit/scheduler_assertions.rb
+++ b/lib/resque_unit/scheduler_assertions.rb
@@ -3,7 +3,7 @@
require 'time'
module ResqueUnit::SchedulerAssertions
-
+
# Asserts that +klass+ has been queued into its appropriate queue at
# least once, with a +timestamp+ less than or equal to
# +expected_timestamp+. If the job wasn't queued with a timestamp,
@@ -13,7 +13,7 @@ module ResqueUnit::SchedulerAssertions
# +args+ if you want to assert that klass has been queued without
# arguments.
def assert_queued_at(expected_timestamp, klass, args = nil, message = nil)
- queue = Resque.queue_for(klass)
+ queue = Resque.queue_from_class(klass)
assert in_timestamped_queue?(queue, expected_timestamp, klass, args),
(message || "#{klass} should have been queued in #{queue} before #{expected_timestamp}: #{Resque.queue(queue).inspect}.")
end
@@ -23,10 +23,10 @@ def assert_queued_at(expected_timestamp, klass, args = nil, message = nil)
def assert_queued_in(expected_time_difference, klass, args = nil, message = nil)
assert_queued_at(Time.now + expected_time_difference, klass, args, message)
end
-
+
# opposite of +assert_queued_at+
def assert_not_queued_at(expected_timestamp, klass, args = nil, message = nil)
- queue = Resque.queue_for(klass)
+ queue = Resque.queue_from_class(klass)
assert !in_timestamped_queue?(queue, expected_timestamp, klass, args),
(message || "#{klass} should not have been queued in #{queue} before #{expected_timestamp}.")
end
@@ -38,10 +38,24 @@ def assert_not_queued_in(expected_time_difference, klass, args = nil, message =
private
- def in_timestamped_queue?(queue_name, expected_timestamp, klass, args = nil)
+ def in_queue?(queue, klass, args = nil)
+ super(queue, klass, args) || !matching_jobs(all_jobs_scheduled_before_or_at(:forever), klass, args).empty?
+ end
+
+ def in_timestamped_queue?(queue_name, max_timestamp, klass, args = nil)
# check if we have any matching jobs with a timestamp less than
# expected_timestamp
- !matching_jobs(Resque.all(queue_name), klass, args).select {|e| e["timestamp"] && Time.parse(e["timestamp"]) <= expected_timestamp}.empty?
+ !matching_jobs(all_jobs_scheduled_before_or_at(max_timestamp), klass, args).empty?
end
-
+
+ def all_jobs_scheduled_before_or_at(max_timestamp = :forever)
+ timestamps = Resque.delayed_queue_peek(0, Resque.delayed_queue_schedule_size).map(&:to_i)
+
+ if max_timestamp != :forever
+ timestamps.select! { |timestamp| Time.at(timestamp) <= Time.at(max_timestamp) }
+ end
+
+ timestamps.flat_map { |timestamp| Resque.delayed_timestamp_peek(timestamp, 0, Resque.delayed_timestamp_size(timestamp)) }
+ end
+
end
diff --git a/lib/resque_unit_scheduler.rb b/lib/resque_unit_scheduler.rb
index 7dcf04d..b79d246 100644
--- a/lib/resque_unit_scheduler.rb
+++ b/lib/resque_unit_scheduler.rb
@@ -1,4 +1,5 @@
-require 'resque_unit/scheduler'
+require 'resque_unit'
+require 'resque-scheduler'
require 'resque_unit/scheduler_assertions'
if defined?(Test::Unit::TestCase)
diff --git a/resque_unit.gemspec b/resque_unit.gemspec
index e70fa63..5906202 100644
--- a/resque_unit.gemspec
+++ b/resque_unit.gemspec
@@ -1,8 +1,11 @@
spec = Gem::Specification.new do |s|
s.name = 'resque_unit'
- s.version = '0.4.8'
- s.summary = 'Test::Unit support for resque job queueing'
- s.add_dependency "json", ">= 1.4.6"
+ s.version = '1.0.0.beta.2'
+ s.summary = 'Minitest and Test::Unit support for resque job queueing'
+ s.add_dependency "fakeredis"
+ s.add_dependency "resque"
+ s.add_development_dependency "resque-scheduler"
+ s.add_development_dependency "pry-byebug"
s.add_development_dependency "bundler"
s.add_development_dependency "minitest", "~> 5.0"
s.author = "Justin Weiss"
diff --git a/test/redis_test.rb b/test/redis_test.rb
new file mode 100644
index 0000000..cc24f6b
--- /dev/null
+++ b/test/redis_test.rb
@@ -0,0 +1,11 @@
+require 'test_helper'
+
+describe Redis do
+ it "uses a real Redis connection by default" do
+ redis = Redis.current
+ refute_equal Redis::Connection::Memory, redis.client.driver
+
+ redis = Redis.new
+ refute_equal Redis::Connection::Memory, redis.client.driver
+ end
+end
diff --git a/test/resque_test.rb b/test/resque_test.rb
index 6513f02..54d8000 100644
--- a/test/resque_test.rb
+++ b/test/resque_test.rb
@@ -35,8 +35,9 @@
assert_equal [], Resque.peek(MediumPriorityJob.queue, 4, 2)
end
- it "returns an empty array when peek method called with count equal 0" do
- assert_equal [], Resque.peek(MediumPriorityJob.queue, 0, 0)
+ it "returns all jobs' payloads when peek method called with count equal 0" do
+ # This is the way Resque.peek works, so it's now the way resque-unit works!
+ assert_equal Resque.all(MediumPriorityJob.queue), Resque.peek(MediumPriorityJob.queue, 0, 0)
end
it "returns all jobs' payloads when all method called" do
diff --git a/test/resque_unit_scheduler_test.rb b/test/resque_unit_scheduler_test.rb
index e6b1ab9..b62d78b 100644
--- a/test/resque_unit_scheduler_test.rb
+++ b/test/resque_unit_scheduler_test.rb
@@ -1,8 +1,9 @@
require 'test_helper'
require 'resque_unit_scheduler'
-describe ResqueUnit::Scheduler do
+describe Resque do
include ResqueUnit::Assertions
+ include ResqueUnit::SchedulerAssertions
before do
Resque.reset!
diff --git a/test/resque_unit_test.rb b/test/resque_unit_test.rb
index 7f092d9..8b57065 100644
--- a/test/resque_unit_test.rb
+++ b/test/resque_unit_test.rb
@@ -34,19 +34,19 @@
it "allows partial runs with explicit limit" do
assert_equal 3, Resque.queue(:high).length, 'failed setup'
- Resque.run_for!( :high, 1 )
+ Resque.run_for!(:high, 1)
assert_equal 2, Resque.queue(:high).length, 'failed to run just single job'
end
it "allows full run with too-large explicit limit" do
assert_equal 3, Resque.queue(:high).length, 'failed setup'
- Resque.run_for!( :high, 50 )
+ Resque.run_for!(:high, 50)
assert_equal 0, Resque.queue(:high).length, 'failed to run all jobs'
end
it "allows full run with implicit limit" do
assert_equal 3, Resque.queue(:high).length, 'failed setup'
- Resque.run_for!( :high )
+ Resque.run_for!(:high)
assert_equal 0, Resque.queue(:high).length, 'failed to run all jobs'
end
end
@@ -97,14 +97,6 @@
end
describe "A task that schedules a resque job with hooks" do
- before do
- Resque.enable_hooks!
- end
-
- after do
- Resque.disable_hooks!
- end
-
describe "before, around, after, failure, after_enqueue" do
before do
JobWithHooks.clear_markers
@@ -372,7 +364,7 @@
describe ", when Resque.run_for! is called," do
it "runs only tasks in the high priority queue" do
- Resque.run_for!(Resque.queue_for(HighPriorityJob))
+ Resque.run_for!(Resque.queue_from_class(HighPriorityJob))
assert_queued(LowPriorityJob)
assert_not_queued(HighPriorityJob)
@@ -446,11 +438,6 @@
Resque::Job.create(:my_custom_queue, "LowPriorityJob", "arg1", "arg2")
assert_job_created(:my_custom_queue, LowPriorityJob, ["arg1", "arg2"])
end
-
- it "queues a job with a dasherized name" do
- Resque::Job.create(:my_custom_queue, "low-priority-job", "arg1", "arg2")
- assert_job_created(:my_custom_queue, LowPriorityJob, ["arg1", "arg2"])
- end
end
end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 2d12096..c18b35a 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,5 +1,7 @@
require 'rubygems'
require 'minitest/spec'
require 'resque_unit'
+require 'resque_unit_scheduler'
require 'sample_jobs'
require 'minitest/autorun'
+require 'pry-byebug'