Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Check in test cases for cron trigger

  • Loading branch information...
commit e8d1ae268f370df14222459bc4c5da4951c94b07 1 parent 2034f67
gnufied authored
View
17 .autotest
@@ -0,0 +1,17 @@
+class Autotest
+ def find_files
+ result = {}
+ Find.find '.' do |f|
+ Find.prune if @exceptions and f =~ @exceptions and test ?d, f
+
+ next if test ?d, f
+ next if f =~ /(swp|~|rej|orig|flymake)$/ # temporary/patch files
+ next if f =~ /\/\.?#/ # Emacs autosave/cvs merge files
+
+ filename = f.sub(/^\.\//, '')
+
+ result[filename] = File.stat(filename).mtime rescue next
+ end
+ return result
+ end
+end
View
3  framework/packet/worker.rb
@@ -11,14 +11,13 @@ class Worker
# method initializes the eventloop for the worker
def self.start_worker(messengers = {})
- # @fd_reader = args.shift if args.length > 2
@msg_writer = messengers[:write_end]
@msg_reader = messengers[:read_end]
- # @fd_reader = messengers[:read_fd]
t_instance = new
t_instance.worker_options = messengers[:options]
t_instance.worker_init if t_instance.respond_to?(:worker_init)
t_instance.start_reactor
+ t_instance
end
def initialize
View
49 server/meta_worker.rb
@@ -176,7 +176,7 @@ def worker_init
@logger = PacketLogger.new(self)
@thread_pool = ThreadPool.new(pool_size || 20,@logger)
- if(@worker_options && @worker_options[:schedule] && no_auto_load)
+ if(worker_options && worker_options[:schedule] && no_auto_load)
load_schedule_from_args
elsif(@config_file[:schedules] && @config_file[:schedules][worker_name.to_sym])
@my_schedule = @config_file[:schedules][worker_name.to_sym]
@@ -184,7 +184,7 @@ def worker_init
end
if respond_to?(:create)
create_arity = method(:create).arity
- (create_arity == 0) ? create : create(@worker_options[:data])
+ (create_arity == 0) ? create : create(worker_options[:data])
end
@logger.info "#{worker_name} started"
@logger.info "Schedules for worker loaded"
@@ -193,7 +193,7 @@ def worker_init
# loads workers schedule from options supplied from rails
# a user may pass trigger arguments to dynamically define the schedule
def load_schedule_from_args
- @my_schedule = @worker_options[:schedule]
+ @my_schedule = worker_options[:schedule]
new_load_schedule if @my_schedule
end
@@ -239,19 +239,6 @@ def can_dump?(p_object)
end
end
- def load_schedule
- case @my_schedule[:trigger_args]
- when String
- @trigger_type = :cron_trigger
- cron_args = @my_schedule[:trigger_args] || "0 0 0 0 0"
- @trigger = BackgrounDRb::CronTrigger.new(cron_args)
- when Hash
- @trigger_type = :trigger
- @trigger = BackgrounDRb::Trigger.new(@my_schedule[:trigger_args])
- end
- @run_time = @trigger.fire_time_after(Time.now).to_i
- end
-
# new experimental scheduler
def new_load_schedule
@worker_method_triggers = { }
@@ -334,12 +321,15 @@ def check_for_timer_events
@worker_method_triggers.delete_if { |key,value| value[:trigger].respond_to?(:end_time) && value[:trigger].end_time <= Time.now }
@worker_method_triggers.each do |key,value|
- if value[:runtime] < Time.now.to_i
+ time_now = Time.now.to_i
+ if value[:runtime] < time_now
begin
(t_data = value[:data]) ? send(key,t_data) : send(key)
rescue
- logger.info($!.to_s)
- logger.info($!.backtrace.join("\n"))
+ # logger.info($!.to_s)
+# logger.info($!.backtrace.join("\n"))
+ p $!
+ p $!.backtrace
end
value[:runtime] = value[:trigger].fire_time_after(Time.now).to_i
end
@@ -350,26 +340,7 @@ def check_for_timer_events
def run_user_threads
@thread_pool.exclusive_run
end
-
- # we are overriding the function that checks for timers
- # def check_for_timer_events
- # super
- # return unless @my_schedule
- # if @run_time < Time.now.to_i
- # # self.send(@my_schedule[:worker_method]) if self.respond_to?(@my_schedule[:worker_method])
- # invoke_worker_method
- # @run_time = @trigger.fire_time_after(Time.now).to_i
- # end
- # end
-
- def invoke_worker_method
- if self.respond_to?(@my_schedule[:worker_method]) && @my_schedule[:data]
- self.send(@my_schedule[:worker_method],@my_schedule[:data])
- elsif self.respond_to?(@my_schedule[:worker_method])
- self.send(@my_schedule[:worker_method])
- end
- end
-
+
private
def load_rails_env
db_config_file = YAML.load(ERB.new(IO.read("#{RAILS_HOME}/config/database.yml")).result)
View
53 test/cron_trigger_test.rb
@@ -1,53 +0,0 @@
-require File.join(File.dirname(__FILE__) + "/bdrb_test_helper")
-require "meta_worker"
-
-context "Cron Trigger should" do
- setup do
- BackgrounDRb::MetaWorker.worker_name = "hello_worker"
- class BackgrounDRb::MetaWorker
- attr_accessor :outgoing_data
- attr_accessor :incoming_data
- def send_data(data)
- @outgoing_data = data
- end
- def ivar var
- instance_variable_get(:"@#{var}")
- end
- def start_reactor; end
- end
- @klass = BackgrounDRb::MetaWorker
- end
-
- specify "run task each second for no option" do
- t_arg = { :foo => { :trigger_args => "*/5 * * * * *"}}
-
- @klass.any_instance.stubs(:worker_options).returns(:schedule => t_arg)
- meta_worker = @klass.start_worker
- meta_worker.ivar(:my_schedule).should.not.be(nil)
- meta_worker.ivar(:my_schedule).should == t_arg
- end
-
- xspecify "run task each minute for minute option" do
-
- end
-
- xspecify "run at specified hour for hourly option" do
-
- end
-
- xspecify "run at specified day for day option" do
-
- end
-
- xspecify "run at specified week day for specified option" do
-
- end
-
- xspecify "run in appropriate month for speficied option" do
-
- end
-
- xspecify "run in appropriate year for specified option" do
-
- end
-end
View
74 test/meta_worker_test.rb
@@ -1,74 +0,0 @@
-require File.join(File.dirname(__FILE__) + "/bdrb_test_helper")
-require "meta_worker"
-
-context "A Meta Worker should" do
- setup do
- BackgrounDRb::MetaWorker.worker_name = "hello_worker"
- db_config = { :development =>
- { :adapter => "mysql",:database => "rails_sandbox_development" ,
- :username => "root",:password => "foobar"
- },
- :test => {
- :adapter => "mysql", :database => "rails_sandbox_test",
- :username => "root", :password => "foobar",
- },
- :production => {
- :adapter => "mysql", :database => "rails_sandbox_production",
- :username => "root", :password => "foobar"
- }
- }
-
- class BackgrounDRb::MetaWorker
- attr_accessor :outgoing_data
- attr_accessor :incoming_data
- def send_data(data)
- @outgoing_data = data
- end
-
- def start_reactor; end
- end
- meta_worker = BackgrounDRb::MetaWorker.start_worker
- end
-
- specify "load appropriate db environment from config file" do
- ActiveRecord::Base.connection.current_database.should == "rails_sandbox_production"
- end
-
- xspecify "remove a task from schedule if end time is reached" do
- end
-
- xspecify "load appropriate schedule from config file" do
- end
-
- xspecify "run a task if on schedule" do
-
- end
-
- xspecify "register status request should be send out to master" do
- end
-
- xspecify "load schedule from passed arguments to start worker" do
-
- end
-
- xspecify "should have access to logger objects" do
-
- end
-
- xspecify "logger object should support info, error and debug methods" do
-
- end
-
- xspecify "invoke particular method based on user arguments" do
-
- end
-
- xspecify "should send results back to master only when response can be dumped" do
-
- end
-
- xspecify "should check for arguments of the invoked worker method" do
-
- end
-end
-
View
1  test/backgroundrb_test.rb → test/test_backgroundrb.rb
@@ -1,4 +1,5 @@
require File.join(File.dirname(__FILE__) + "/bdrb_test_helper")
+require File.join(RAILS_HOME + "/config/environment")
require "backgroundrb"
context "Backgroundrb connection in general should" do
View
4 test/master_worker_test.rb → test/test_master_worker.rb
@@ -1,9 +1,9 @@
require File.join(File.dirname(__FILE__) + "/bdrb_test_helper")
-require "#{PACKET_APP}/server/master_worker"
+require "master_worker"
context "Master Worker in general should" do
setup do
- master_worker = MasterWorker.new
+ master_worker = BackgrounDRb::MasterWorker.new
end
xspecify "should invoke proper method for different requests" do
View
159 test/test_meta_worker.rb
@@ -0,0 +1,159 @@
+require File.join(File.dirname(__FILE__) + "/bdrb_test_helper")
+require "meta_worker"
+
+context "A Meta Worker should" do
+ setup do
+ BackgrounDRb::MetaWorker.worker_name = "hello_worker"
+ db_config = { :development =>
+ { :adapter => "mysql",:database => "rails_sandbox_development" ,
+ :username => "root",:password => "foobar"
+ },
+ :test => {
+ :adapter => "mysql", :database => "rails_sandbox_test",
+ :username => "root", :password => "foobar",
+ },
+ :production => {
+ :adapter => "mysql", :database => "rails_sandbox_production",
+ :username => "root", :password => "foobar"
+ }
+ }
+
+ class BackgrounDRb::MetaWorker
+ attr_accessor :outgoing_data
+ attr_accessor :incoming_data
+ def send_data(data)
+ @outgoing_data = data
+ end
+
+ def start_reactor; end
+ end
+ meta_worker = BackgrounDRb::MetaWorker.start_worker
+ end
+
+ specify "load appropriate db environment from config file" do
+ ActiveRecord::Base.connection.current_database.should == "rails_sandbox_production"
+ end
+
+ xspecify "remove a task from schedule if end time is reached" do
+ end
+
+ xspecify "load appropriate schedule from config file" do
+ end
+
+ xspecify "register status request should be send out to master" do
+ end
+
+ xspecify "load schedule from passed arguments to start worker" do
+
+ end
+
+ xspecify "should have access to logger objects" do
+
+ end
+
+ xspecify "logger object should support info, error and debug methods" do
+
+ end
+
+ xspecify "invoke particular method based on user arguments" do
+
+ end
+
+ xspecify "should send results back to master only when response can be dumped" do
+
+ end
+
+ xspecify "should check for arguments of the invoked worker method" do
+
+ end
+end
+
+context "Cron Trigger should" do
+ setup do
+ BackgrounDRb::MetaWorker.worker_name = "hello_worker"
+ class BackgrounDRb::MetaWorker
+ attr_accessor :outgoing_data
+ attr_accessor :incoming_data
+ set_no_auto_load(true)
+ def send_data(data)
+ @outgoing_data = data
+ end
+ def ivar var
+ instance_variable_get(:"@#{var}")
+ end
+ # method would disable starting of reactor loop
+ def start_reactor; end
+ end
+ @klass = BackgrounDRb::MetaWorker
+ end
+
+ def mock_worker(t_arg,next_time)
+ puts "hello world"
+ @klass.any_instance.stubs(:worker_options).returns(:schedule => t_arg)
+ meta_worker = @klass.start_worker
+ meta_worker.ivar(:my_schedule).should.not.be(nil)
+ meta_worker.ivar(:my_schedule).should == t_arg
+ meta_worker.ivar(:worker_method_triggers).should.not.be nil
+ firetime = meta_worker.ivar(:worker_method_triggers)[:foo][:runtime]
+ firetime.should.not.be.nil
+ time_object = mock()
+ proper_time = Time.now + next_time
+ Time.stubs(:now).returns(time_object,proper_time)
+ [meta_worker,time_object,firetime]
+ end
+
+ specify "run task each 5 second for no option" do
+ t_arg = { :foo => { :trigger_args => "*/5 * * * * *"}}
+ meta_worker,time_object,firetime = *mock_worker(t_arg,15)
+ time_object.stubs(:to_i).returns(firetime + 1)
+ meta_worker.expects(:foo).once
+ meta_worker.check_for_timer_events
+ end
+
+ specify "should not run the task if time to run has not arrived" do
+ t_arg = { :foo => { :trigger_args => "*/5 * * * * *"}}
+ meta_worker,time_object,firetime = *mock_worker(t_arg,10)
+ time_object.stubs(:to_i).returns(firetime - 2)
+ meta_worker.expects(:foo).never
+ meta_worker.check_for_timer_events
+ end
+
+ specify "run task each minute for minute option" do
+ t_arg = { :foo => { :trigger_args => "0 1 * * * *"}}
+
+ meta_worker,time_object,firetime = *mock_worker(t_arg,2*60)
+
+ time_object.stubs(:to_i).returns(firetime + 2)
+ meta_worker.expects(:foo).once
+ meta_worker.check_for_timer_events
+ end
+
+ specify "should not for minute arguments if time is not reached" do
+ t_arg = { :foo => { :trigger_args => "0 1 * * * *"}}
+ meta_worker,time_object,firetime = *mock_worker(t_arg,2*60)
+
+ time_object.stubs(:to_i).returns(firetime - 10)
+ meta_worker.expects(:foo).never
+ meta_worker.check_for_timer_events
+ end
+
+ xspecify "run at specified hour for hourly option" do
+
+ end
+
+ xspecify "run at specified day for day option" do
+
+ end
+
+ xspecify "run at specified week day for specified option" do
+
+ end
+
+ xspecify "run in appropriate month for speficied option" do
+
+ end
+
+ xspecify "run in appropriate year for specified option" do
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.