| 1 |
require File.join(File.dirname(__FILE__) + "/../bdrb_test_helper") |
|---|
| 2 |
require "meta_worker" |
|---|
| 3 |
require "chronic" |
|---|
| 4 |
|
|---|
| 5 |
context "A Meta Worker should" do |
|---|
| 6 |
def dump_object data |
|---|
| 7 |
t = Marshal.dump(data) |
|---|
| 8 |
t.length.to_s.rjust(9,'0') + t |
|---|
| 9 |
end |
|---|
| 10 |
|
|---|
| 11 |
setup do |
|---|
| 12 |
options = {:schedules => |
|---|
| 13 |
{ |
|---|
| 14 |
:proper_worker => { :barbar => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }}, |
|---|
| 15 |
:bar_worker => { :do_job => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }} |
|---|
| 16 |
}, |
|---|
| 17 |
:backgroundrb => {:log => "foreground", :debug_log => false, :environment => "production", :port => 11006, :ip => "localhost"} |
|---|
| 18 |
} |
|---|
| 19 |
BDRB_CONFIG.set(options) |
|---|
| 20 |
|
|---|
| 21 |
BackgrounDRb::MetaWorker.worker_name = "hello_worker" |
|---|
| 22 |
|
|---|
| 23 |
class ProperWorker < BackgrounDRb::MetaWorker |
|---|
| 24 |
attr_accessor :outgoing_data |
|---|
| 25 |
attr_accessor :incoming_data |
|---|
| 26 |
set_worker_name :proper_worker |
|---|
| 27 |
def send_data(data) |
|---|
| 28 |
@outgoing_data = data |
|---|
| 29 |
end |
|---|
| 30 |
|
|---|
| 31 |
def start_reactor; end |
|---|
| 32 |
|
|---|
| 33 |
def ivar(var) |
|---|
| 34 |
instance_variable_get("@#{var}") |
|---|
| 35 |
end |
|---|
| 36 |
end |
|---|
| 37 |
@meta_worker = ProperWorker.start_worker |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
specify "load appropriate db environment from config file" do |
|---|
| 41 |
ENV["RAILS_ENV"] = BDRB_CONFIG[:backgroundrb][:environment] |
|---|
| 42 |
@meta_worker.send(:load_rails_env) |
|---|
| 43 |
ActiveRecord::Base.connection.current_database.should == "rails_sandbox_production" |
|---|
| 44 |
end |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
specify "load appropriate schedule from config file" do |
|---|
| 48 |
@meta_worker.my_schedule.should.not == nil |
|---|
| 49 |
@meta_worker.my_schedule.should == {:barbar=>{:data=>"Hello World", :trigger_args=>"*/5 * * * * *"}} |
|---|
| 50 |
trigger = @meta_worker.ivar(:worker_method_triggers) |
|---|
| 51 |
trigger.should.not == nil |
|---|
| 52 |
trigger[:barbar][:data].should == "Hello World" |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
specify "should load passed data and invoke methods" do |
|---|
| 56 |
a = {:data=>{:worker_method=>"who", :arg=>"rails"}, :type=>:request, :result=>false, :client_signature=>9} |
|---|
| 57 |
b = {:data=>{:worker_method=>"baz", :arg=>"rails"}, :type=>:request, :result=>true, :client_signature=>9} |
|---|
| 58 |
c = {:data=>{:job_key=>:start_message}, :type=>:get_result, :result=>true, :client_signature=>9} |
|---|
| 59 |
t_request = "000000088\004\b{\t:\ttype:\frequest:\025client_signaturei\016:\vresultF:\tdata{\a:\022worker_method\"\bwho:\barg\"\nrails" |
|---|
| 60 |
@meta_worker.expects(:receive_data).with(a).returns(nil) |
|---|
| 61 |
@meta_worker.receive_internal_data(t_request) |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
specify "should invoke async tasks without sending results" do |
|---|
| 65 |
a = {:data=>{:worker_method=>"who", :arg=>"rails",:job_key => "lol"}, :type=>:request, :result=>false, :client_signature=>9} |
|---|
| 66 |
@meta_worker.expects(:who).with("rails").returns(nil) |
|---|
| 67 |
@meta_worker.receive_internal_data(dump_object(a)) |
|---|
| 68 |
Thread.current[:job_key].should == "lol" |
|---|
| 69 |
end |
|---|
| 70 |
|
|---|
| 71 |
specify "should invoke sync methods and return results back" do |
|---|
| 72 |
class << @meta_worker |
|---|
| 73 |
def baz args |
|---|
| 74 |
"hello : #{args}" |
|---|
| 75 |
end |
|---|
| 76 |
end |
|---|
| 77 |
b = {:data=>{:worker_method=>"baz", :arg=>"rails"}, :type=>:request, :result=>true, :client_signature=>9} |
|---|
| 78 |
@meta_worker.expects(:send_data).with({:data=>"hello : rails", :type=>:response, :result=>true, :client_signature=>9}).returns("hello : rails") |
|---|
| 79 |
@meta_worker.receive_internal_data(dump_object(b)) |
|---|
| 80 |
Thread.current[:job_key].should == nil |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
specify "should invoke methods with and without args correctly" do |
|---|
| 84 |
class << @meta_worker |
|---|
| 85 |
attr_accessor :outgoing_data |
|---|
| 86 |
def send_data data |
|---|
| 87 |
@outgoing_data = data |
|---|
| 88 |
end |
|---|
| 89 |
end |
|---|
| 90 |
b = {:data=> {:worker_method=>"baz", :arg => { :name => "bdrb",:age => 10} }, :type=>:request, :result=>true, :client_signature=>9 } |
|---|
| 91 |
# @meta_worker.expects(:send_data).with({:data=>"hello : rails", :type=>:response, :result=>true, :client_signature=>9}).returns("hello : rails") |
|---|
| 92 |
@meta_worker.expects(:baz).with({ :name => "bdrb",:age => 10}).returns("foo") |
|---|
| 93 |
@meta_worker.receive_internal_data(dump_object(b)) |
|---|
| 94 |
@meta_worker.outgoing_data[:data].should == "foo" |
|---|
| 95 |
Thread.current[:job_key].should == nil |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
specify "for result request" do |
|---|
| 99 |
class << @meta_worker |
|---|
| 100 |
attr_accessor :t_result |
|---|
| 101 |
def send_data data |
|---|
| 102 |
@t_result = data |
|---|
| 103 |
end |
|---|
| 104 |
end |
|---|
| 105 |
@meta_worker.cache[:start_message] = "helloworld" |
|---|
| 106 |
c = {:data=>{:job_key=>:start_message}, :type=>:get_result, :result=>true, :client_signature=>9} |
|---|
| 107 |
@meta_worker.receive_internal_data(dump_object(c)) |
|---|
| 108 |
@meta_worker.t_result[:data].should == "helloworld" |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
specify "for results that cant be dumped" do |
|---|
| 112 |
class << @meta_worker |
|---|
| 113 |
def baz args |
|---|
| 114 |
proc { "boy"} |
|---|
| 115 |
end |
|---|
| 116 |
def send_data input |
|---|
| 117 |
dump_object(input) |
|---|
| 118 |
end |
|---|
| 119 |
end |
|---|
| 120 |
b = {:data=>{:worker_method=>"baz", :arg=>"rails"}, :type=>:request, :result=>true, :client_signature=>9} |
|---|
| 121 |
a = @meta_worker.receive_internal_data(dump_object(b)) |
|---|
| 122 |
a.should == nil |
|---|
| 123 |
Thread.current[:job_key].should == nil |
|---|
| 124 |
end |
|---|
| 125 |
|
|---|
| 126 |
end |
|---|
| 127 |
|
|---|
| 128 |
context "For unix schedulers" do |
|---|
| 129 |
specify "remove a task from schedule if end time is reached" do |
|---|
| 130 |
options = {:schedules => |
|---|
| 131 |
{ |
|---|
| 132 |
:unix_worker => { :barbar => { :trigger_args => |
|---|
| 133 |
{ |
|---|
| 134 |
:start => (Time.now + 2.seconds).to_s, |
|---|
| 135 |
:end => (Time.now + 10.seconds).to_s, |
|---|
| 136 |
:repeat_interval => 2.seconds, |
|---|
| 137 |
:data => "unix_worker" |
|---|
| 138 |
} |
|---|
| 139 |
} |
|---|
| 140 |
}, |
|---|
| 141 |
}, |
|---|
| 142 |
:backgroundrb => |
|---|
| 143 |
{ |
|---|
| 144 |
:log => "foreground", :debug_log => false, :environment => "production", :port => 11006, :ip => "localhost" |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
BDRB_CONFIG.set(options) |
|---|
| 148 |
|
|---|
| 149 |
class UnixWorker < BackgrounDRb::MetaWorker |
|---|
| 150 |
attr_accessor :outgoing_data |
|---|
| 151 |
attr_accessor :incoming_data |
|---|
| 152 |
set_worker_name :unix_worker |
|---|
| 153 |
def send_data(data) |
|---|
| 154 |
@outgoing_data = data |
|---|
| 155 |
end |
|---|
| 156 |
|
|---|
| 157 |
def start_reactor; end |
|---|
| 158 |
|
|---|
| 159 |
def ivar(var) |
|---|
| 160 |
instance_variable_get("@#{var}") |
|---|
| 161 |
end |
|---|
| 162 |
end |
|---|
| 163 |
@meta_worker = UnixWorker.start_worker |
|---|
| 164 |
@meta_worker.my_schedule.should.not == nil |
|---|
| 165 |
@meta_worker.ivar(:worker_method_triggers).should.not == nil |
|---|
| 166 |
@meta_worker.ivar(:worker_method_triggers)[:barbar].should.not == nil |
|---|
| 167 |
end |
|---|
| 168 |
end |
|---|
| 169 |
|
|---|
| 170 |
context "Worker without names" do |
|---|
| 171 |
specify "should throw an error on initialization" do |
|---|
| 172 |
options = {:schedules => |
|---|
| 173 |
{ |
|---|
| 174 |
:foo_worker => { :barbar => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }}, |
|---|
| 175 |
:bar_worker => { :do_job => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }} |
|---|
| 176 |
}, |
|---|
| 177 |
:backgroundrb => {:log => "foreground", :debug_log => false, :environment => "production", :port => 11006, :ip => "localhost"} |
|---|
| 178 |
} |
|---|
| 179 |
BDRB_CONFIG.set(options) |
|---|
| 180 |
|
|---|
| 181 |
BackgrounDRb::MetaWorker.worker_name = "hello_worker" |
|---|
| 182 |
|
|---|
| 183 |
class BoyWorker < BackgrounDRb::MetaWorker |
|---|
| 184 |
attr_accessor :outgoing_data |
|---|
| 185 |
attr_accessor :incoming_data |
|---|
| 186 |
def send_data(data) |
|---|
| 187 |
@outgoing_data = data |
|---|
| 188 |
end |
|---|
| 189 |
|
|---|
| 190 |
def start_reactor; end |
|---|
| 191 |
end |
|---|
| 192 |
should.raise { @meta_worker = BoyWorker.start_worker } |
|---|
| 193 |
end |
|---|
| 194 |
end |
|---|
| 195 |
|
|---|
| 196 |
context "Worker with options" do |
|---|
| 197 |
specify "should load schedule from passed options" do |
|---|
| 198 |
options = { :backgroundrb => {:log => "foreground", :debug_log => false, :environment => "production", :port => 11006, :ip => "localhost"}} |
|---|
| 199 |
BDRB_CONFIG.set(options) |
|---|
| 200 |
|
|---|
| 201 |
BackgrounDRb::MetaWorker.worker_name = "hello_worker" |
|---|
| 202 |
|
|---|
| 203 |
class CrapWorker < BackgrounDRb::MetaWorker |
|---|
| 204 |
set_worker_name :crap_worker |
|---|
| 205 |
set_no_auto_load true |
|---|
| 206 |
attr_accessor :outgoing_data |
|---|
| 207 |
attr_accessor :incoming_data |
|---|
| 208 |
def send_data(data) |
|---|
| 209 |
@outgoing_data = data |
|---|
| 210 |
end |
|---|
| 211 |
|
|---|
| 212 |
def start_reactor; end |
|---|
| 213 |
def ivar(var); instance_variable_get("@#{var}"); end |
|---|
| 214 |
end |
|---|
| 215 |
write_end = mock() |
|---|
| 216 |
read_end = mock() |
|---|
| 217 |
worker_options = { :write_end => mock(),:read_end => mock(), |
|---|
| 218 |
:options => { |
|---|
| 219 |
:data => "hello", :schedule => { |
|---|
| 220 |
:hello_world => { :trigger_args => "*/5 * * * * * *", |
|---|
| 221 |
:data => "hello_world" |
|---|
| 222 |
} |
|---|
| 223 |
} |
|---|
| 224 |
} |
|---|
| 225 |
} |
|---|
| 226 |
CrapWorker.any_instance.expects(:create).with("hello").returns(true) |
|---|
| 227 |
@meta_worker = CrapWorker.start_worker(worker_options) |
|---|
| 228 |
@meta_worker.my_schedule.should == {:hello_world=>{:data=>"hello_world", :trigger_args=>"*/5 * * * * * *"}} |
|---|
| 229 |
end |
|---|
| 230 |
end |
|---|
| 231 |
|
|---|
| 232 |
context "For enqueued tasks" do |
|---|
| 233 |
setup do |
|---|
| 234 |
options = {:schedules => |
|---|
| 235 |
{ |
|---|
| 236 |
:proper_worker => { :barbar => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }}, |
|---|
| 237 |
:bar_worker => { :do_job => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }} |
|---|
| 238 |
}, |
|---|
| 239 |
:backgroundrb => {:log => "foreground", :debug_log => false, :environment => "production", :port => 11006, :ip => "localhost"} |
|---|
| 240 |
} |
|---|
| 241 |
BDRB_CONFIG.set(options) |
|---|
| 242 |
|
|---|
| 243 |
class BdrbJobQueue < ActiveRecord::Base; end |
|---|
| 244 |
class QueueWorker < BackgrounDRb::MetaWorker |
|---|
| 245 |
attr_accessor :outgoing_data |
|---|
| 246 |
attr_accessor :incoming_data |
|---|
| 247 |
set_worker_name :queue_worker |
|---|
| 248 |
def send_data(data) |
|---|
| 249 |
@outgoing_data = data |
|---|
| 250 |
end |
|---|
| 251 |
|
|---|
| 252 |
def start_reactor; end |
|---|
| 253 |
|
|---|
| 254 |
def ivar(var) |
|---|
| 255 |
instance_variable_get("@#{var}") |
|---|
| 256 |
end |
|---|
| 257 |
end |
|---|
| 258 |
end |
|---|
| 259 |
|
|---|
| 260 |
specify "should run enqueued tasks with arguments if they are there in the queue" do |
|---|
| 261 |
@meta_worker = QueueWorker.start_worker |
|---|
| 262 |
mocked_task = mock() |
|---|
| 263 |
mocked_task.expects(:worker_method).returns(:barbar).times(3) |
|---|
| 264 |
mocked_task.expects(:args).returns(Marshal.dump("hello")) |
|---|
| 265 |
mocked_task.expects(:[]).returns(1).times(2) |
|---|
| 266 |
@meta_worker.expects(:barbar).with("hello").returns(true) |
|---|
| 267 |
BdrbJobQueue.expects(:find_next).with("queue_worker").returns(mocked_task) |
|---|
| 268 |
@meta_worker.check_for_enqueued_tasks |
|---|
| 269 |
end |
|---|
| 270 |
|
|---|
| 271 |
specify "should run enqueued tasks without arguments if they are there in the queue" do |
|---|
| 272 |
@meta_worker = QueueWorker.start_worker |
|---|
| 273 |
mocked_task = mock() |
|---|
| 274 |
mocked_task.expects(:[]).returns(1).times(2) |
|---|
| 275 |
mocked_task.expects(:worker_method).returns(:barbar).times(3) |
|---|
| 276 |
mocked_task.expects(:args).returns(nil) |
|---|
| 277 |
@meta_worker.expects(:barbar) |
|---|
| 278 |
BdrbJobQueue.expects(:find_next).with("queue_worker").returns(mocked_task) |
|---|
| 279 |
@meta_worker.check_for_enqueued_tasks |
|---|
| 280 |
end |
|---|
| 281 |
end |
|---|