require 'rubygems' require 'bundler/setup' $dir = File.dirname(File.expand_path(__FILE__)) $LOAD_PATH.unshift $dir + '/../lib' $TESTING = true require 'test/unit' require 'redis/namespace' require 'resque' require 'resque-loner' begin require 'leftright' rescue LoadError end # # make sure we can run redis # if !system('which redis-server') puts '', "** can't find `redis-server` in your path" puts '** try running `sudo rake install`' abort '' end # # start our own redis when the tests start, # kill it when they end # at_exit do next if $ERROR_INFO if defined?(MiniTest) exit_code = MiniTest::Unit.new.run(ARGV) else exit_code = Test::Unit::AutoRunner.run end processes = `ps -A -o pid,command | grep [r]edis-test`.split("\n") pids = processes.map { |process| process.split(' ')[0] } puts 'Killing test redis server...' pids.each { |pid| Process.kill('TERM', pid.to_i) } system("rm -f #{$dir}/dump.rdb #{$dir}/dump-cluster.rdb") exit exit_code end if ENV.key? 'RESQUE_DISTRIBUTED' require 'redis/distributed' puts 'Starting redis for testing at localhost:9736 and localhost:9737...' `redis-server #{$dir}/redis-test.conf` `redis-server #{$dir}/redis-test-cluster.conf` r = Redis::Distributed.new(['redis://localhost:9736', 'redis://localhost:9737']) Resque.redis = Redis::Namespace.new :resque, redis: r else puts 'Starting redis for testing at localhost:9736...' `redis-server #{$dir}/redis-test.conf` Resque.redis = 'localhost:9736' end ## # test/spec/mini 3 # http://gist.github.com/25455 # chris@ozmm.org # def context(*args, &block) return super unless (name = args.first) && block require 'test/unit' klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do def self.test(name, &block) define_method("test_#{name.gsub(/\W/, '_')}", &block) if block end def self.xtest(*args) end def self.setup(&block) define_method(:setup, &block) end def self.teardown(&block) define_method(:teardown, &block) end end (class << klass; self end).send(:define_method, :name) { name.gsub(/\W/, '_') } klass.class_eval &block # XXX: In 1.8.x, not all tests will run unless anonymous classes are kept in scope. ($test_classes ||= []) << klass end ## # Helper to perform job classes # module PerformJob def perform_job(klass, *args) resque_job = Resque::Job.new(:testqueue, 'class' => klass, 'args' => args) resque_job.perform end end # # fixture classes # class SomeJob def self.perform(repo_id, path) end end class SomeIvarJob < SomeJob @queue = :ivar end class SomeMethodJob < SomeJob def self.queue :method end end class BadJob def self.perform fail 'Bad job!' end end class GoodJob def self.perform(name) "Good job, #{name}" end end class BadJobWithSyntaxError def self.perform fail SyntaxError, 'Extra Bad job!' end end class BadFailureBackend < Resque::Failure::Base def save fail Exception.new('Failure backend error') end end def with_failure_backend(failure_backend, &block) previous_backend = Resque::Failure.backend Resque::Failure.backend = failure_backend yield block ensure Resque::Failure.backend = previous_backend end require 'time' class Time # Thanks, Timecop class << self attr_accessor :fake_time alias_method :now_without_mock_time, :now def now fake_time || now_without_mock_time end end self.fake_time = nil end def capture_stderr # The output stream must be an IO-like object. In this case we capture it in # an in-memory IO object so we can return the string value. You can assign any # IO object here. previous_stderr, $stderr = $stderr, StringIO.new yield $stderr.string ensure # Restore the previous value of stderr (typically equal to STDERR). $stderr = previous_stderr end