forked from resque/resque
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_helper.rb
More file actions
177 lines (145 loc) · 3.8 KB
/
test_helper.rb
File metadata and controls
177 lines (145 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'rubygems'
require 'bundler/setup'
require 'minitest/autorun'
require 'redis/namespace'
$dir = File.dirname(File.expand_path(__FILE__))
$LOAD_PATH.unshift $dir + '/../lib'
require 'resque'
$TESTING = true
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
#
MiniTest::Unit.after_tests do
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")
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
raise "Bad job!"
end
end
class GoodJob
def self.perform(name)
"Good job, #{name}"
end
end
class BadJobWithSyntaxError
def self.perform
raise SyntaxError, "Extra Bad job!"
end
end
class BadFailureBackend < Resque::Failure::Base
def save
raise 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
# From minitest/unit
def capture_io
require 'stringio'
orig_stdout, orig_stderr = $stdout, $stderr
captured_stdout, captured_stderr = StringIO.new, StringIO.new
$stdout, $stderr = captured_stdout, captured_stderr
yield
return captured_stdout.string, captured_stderr.string
ensure
$stdout = orig_stdout
$stderr = orig_stderr
end
# Log to log/test.log
def reset_logger
$test_logger ||= Logger.new(File.open(File.expand_path("../../log/test.log", __FILE__), "w"))
Resque.logger = $test_logger
end
reset_logger