test/unit/daemon_tests.rb in qs-0.4.0 vs test/unit/daemon_tests.rb in qs-0.5.0
- old
+ new
@@ -18,10 +18,12 @@
subject{ @daemon_class }
should have_imeths :configuration
should have_imeths :name, :pid_file
should have_imeths :min_workers, :max_workers, :workers
+ should have_imeths :on_worker_start, :on_worker_shutdown
+ should have_imeths :on_worker_sleep, :on_worker_wakeup
should have_imeths :verbose_logging, :logger
should have_imeths :shutdown_timeout
should have_imeths :init, :error, :queue
should "know its configuration" do
@@ -66,10 +68,26 @@
assert_equal new_workers, subject.configuration.max_workers
assert_equal new_workers, subject.min_workers
assert_equal new_workers, subject.max_workers
end
+ should "allow reading/writing its configuration worker procs" do
+ p = proc{}
+
+ subject.on_worker_start(&p)
+ assert_equal [p], subject.configuration.worker_start_procs
+
+ subject.on_worker_shutdown(&p)
+ assert_equal [p], subject.configuration.worker_shutdown_procs
+
+ subject.on_worker_sleep(&p)
+ assert_equal [p], subject.configuration.worker_sleep_procs
+
+ subject.on_worker_wakeup(&p)
+ assert_equal [p], subject.configuration.worker_wakeup_procs
+ end
+
should "allow reading/writing its configuration verbose logging" do
new_verbose = Factory.boolean
subject.verbose_logging(new_verbose)
assert_equal new_verbose, subject.configuration.verbose_logging
assert_equal new_verbose, subject.verbose_logging
@@ -112,20 +130,30 @@
class InitSetupTests < UnitTests
setup do
@qs_init_called = false
Assert.stub(Qs, :init){ @qs_init_called = true }
- @queue = Qs::Queue.new do
- name(Factory.string)
- job 'test', TestHandler.to_s
- end
@daemon_class.name Factory.string
@daemon_class.pid_file Factory.file_path
@daemon_class.workers Factory.integer
@daemon_class.verbose_logging Factory.boolean
@daemon_class.shutdown_timeout Factory.integer
@daemon_class.error{ Factory.string }
+
+ @start_procs = Factory.integer(3).times.map{ proc{} }
+ @shutdown_procs = Factory.integer(3).times.map{ proc{} }
+ @sleep_procs = Factory.integer(3).times.map{ proc{} }
+ @wakeup_procs = Factory.integer(3).times.map{ proc{} }
+ @start_procs.each { |p| @daemon_class.on_worker_start(&p) }
+ @shutdown_procs.each { |p| @daemon_class.on_worker_shutdown(&p) }
+ @sleep_procs.each { |p| @daemon_class.on_worker_sleep(&p) }
+ @wakeup_procs.each { |p| @daemon_class.on_worker_wakeup(&p) }
+
+ @queue = Qs::Queue.new do
+ name(Factory.string)
+ job 'test', TestHandler.to_s
+ end
@daemon_class.queue @queue
@client_spy = nil
Assert.stub(Qs::QsClient, :new) do |*args|
@client_spy = ClientSpy.new(*args)
@@ -146,17 +174,23 @@
end
class InitTests < InitSetupTests
desc "when init"
setup do
+ @current_env_process_label = ENV['QS_PROCESS_LABEL']
+ ENV['QS_PROCESS_LABEL'] = Factory.string
+
@daemon = @daemon_class.new
end
+ teardown do
+ ENV['QS_PROCESS_LABEL'] = @current_env_process_label
+ end
subject{ @daemon }
should have_readers :daemon_data, :logger
should have_readers :signals_redis_key, :queue_redis_keys
- should have_imeths :name, :pid_file
+ should have_imeths :name, :process_label, :pid_file
should have_imeths :running?
should have_imeths :start, :stop, :halt
should "validate its configuration" do
assert_true @daemon_class.configuration.valid?
@@ -169,33 +203,43 @@
should "know its daemon data" do
configuration = @daemon_class.configuration
data = subject.daemon_data
assert_instance_of Qs::DaemonData, data
- assert_equal configuration.name, data.name
- assert_equal configuration.pid_file, data.pid_file
- assert_equal configuration.min_workers, data.min_workers
- assert_equal configuration.max_workers, data.max_workers
+ assert_equal configuration.name, data.name
+ assert_equal configuration.process_label, data.process_label
+ assert_equal configuration.pid_file, data.pid_file
+ assert_equal configuration.min_workers, data.min_workers
+ assert_equal configuration.max_workers, data.max_workers
+
+ assert_equal configuration.worker_start_procs, data.worker_start_procs
+ assert_equal configuration.worker_shutdown_procs, data.worker_shutdown_procs
+ assert_equal configuration.worker_sleep_procs, data.worker_sleep_procs
+ assert_equal configuration.worker_wakeup_procs, data.worker_wakeup_procs
+
assert_equal configuration.verbose_logging, data.verbose_logging
assert_equal configuration.shutdown_timeout, data.shutdown_timeout
assert_equal configuration.error_procs, data.error_procs
- assert_equal [@queue.redis_key], data.queue_redis_keys
+
+ assert_equal [@queue.redis_key], data.queue_redis_keys
assert_equal configuration.routes, data.routes.values
+
assert_instance_of configuration.logger.class, data.logger
end
should "know its signal and queues redis keys" do
data = subject.daemon_data
expected = "signals:#{data.name}-#{Socket.gethostname}-#{::Process.pid}"
assert_equal expected, subject.signals_redis_key
assert_equal data.queue_redis_keys, subject.queue_redis_keys
end
- should "know its name and pid file" do
+ should "know its name, process label and pid file" do
data = subject.daemon_data
- assert_equal data.name, subject.name
- assert_equal data.pid_file, subject.pid_file
+ assert_equal data.name, subject.name
+ assert_equal data.process_label, subject.process_label
+ assert_equal data.pid_file, subject.pid_file
end
should "build a client" do
assert_not_nil @client_spy
exp = Qs.redis_config.merge({
@@ -240,12 +284,26 @@
should "build and start a worker pool" do
assert_not_nil @worker_pool_spy
assert_equal @daemon_class.min_workers, @worker_pool_spy.min_workers
assert_equal @daemon_class.max_workers, @worker_pool_spy.max_workers
- assert_equal 1, @worker_pool_spy.on_worker_error_callbacks.size
- assert_equal 1, @worker_pool_spy.on_worker_sleep_callbacks.size
+
+ exp = 1
+ assert_equal exp, @worker_pool_spy.on_worker_error_callbacks.size
+
+ exp = @start_procs.size
+ assert_equal exp, @worker_pool_spy.on_worker_start_callbacks.size
+
+ exp = @shutdown_procs.size
+ assert_equal exp, @worker_pool_spy.on_worker_shutdown_callbacks.size
+
+ exp = @sleep_procs.size + 1 # configured plus 1 internal
+ assert_equal exp, @worker_pool_spy.on_worker_sleep_callbacks.size
+
+ exp = @wakeup_procs.size
+ assert_equal exp, @worker_pool_spy.on_worker_wakeup_callbacks.size
+
assert_true @worker_pool_spy.start_called
end
end
@@ -547,48 +605,83 @@
should have_options :name, :pid_file
should have_options :min_workers, :max_workers
should have_options :verbose_logging, :logger
should have_options :shutdown_timeout
+ should have_accessors :process_label
should have_accessors :init_procs, :error_procs
should have_accessors :queues
+ should have_readers :worker_start_procs, :worker_shutdown_procs
+ should have_readers :worker_sleep_procs, :worker_wakeup_procs
should have_imeths :routes
should have_imeths :to_hash
should have_imeths :valid?, :validate!
should "be an ns-options proxy" do
assert_includes NsOptions::Proxy, subject.class
end
- should "default its options" do
+ should "default its options and attrs" do
config = Configuration.new
assert_nil config.name
assert_nil config.pid_file
assert_equal 1, config.min_workers
assert_equal 4, config.max_workers
assert_true config.verbose_logging
assert_instance_of Qs::NullLogger, config.logger
assert_nil subject.shutdown_timeout
+
+ assert_nil config.process_label
assert_equal [], config.init_procs
assert_equal [], config.error_procs
+ assert_equal [], subject.worker_start_procs
+ assert_equal [], subject.worker_shutdown_procs
+ assert_equal [], subject.worker_sleep_procs
+ assert_equal [], subject.worker_wakeup_procs
assert_equal [], config.queues
assert_equal [], config.routes
end
+ should "prefer an env var for the label but fall back to the name option" do
+ current_env_process_label = ENV['QS_PROCESS_LABEL']
+
+ ENV['QS_PROCESS_LABEL'] = Factory.string
+ config = Configuration.new(:name => Factory.string)
+ assert_equal ENV['QS_PROCESS_LABEL'], config.process_label
+
+ ENV['QS_PROCESS_LABEL'] = ''
+ config = Configuration.new(:name => Factory.string)
+ assert_equal config.name, config.process_label
+
+ ENV.delete('QS_PROCESS_LABEL')
+ config = Configuration.new(:name => Factory.string)
+ assert_equal config.name, config.process_label
+
+ ENV['QS_PROCESS_LABEL'] = current_env_process_label
+ end
+
should "not be valid by default" do
assert_false subject.valid?
end
should "know its routes" do
assert_equal subject.queues.map(&:routes).flatten, subject.routes
end
- should "include its error procs, queue redis keys and routes in its hash" do
+ should "include some attrs (not just the options) in its hash" do
config_hash = subject.to_hash
- assert_equal subject.error_procs, config_hash[:error_procs]
- expected = subject.queues.map(&:redis_key)
- assert_equal expected, config_hash[:queue_redis_keys]
- assert_equal subject.routes, config_hash[:routes]
+
+ assert_equal subject.process_label, config_hash[:process_label]
+ assert_equal subject.error_procs, config_hash[:error_procs]
+ assert_equal subject.routes, config_hash[:routes]
+
+ exp = subject.queues.map(&:redis_key)
+ assert_equal exp, config_hash[:queue_redis_keys]
+
+ assert_equal subject.worker_start_procs, config_hash[:worker_start_procs]
+ assert_equal subject.worker_shutdown_procs, config_hash[:worker_shutdown_procs]
+ assert_equal subject.worker_sleep_procs, config_hash[:worker_sleep_procs]
+ assert_equal subject.worker_wakeup_procs, config_hash[:worker_wakeup_procs]
end
should "call its init procs when validated" do
called = false
subject.init_procs << proc{ called = true }