test/unit/server_tests.rb in sanford-0.14.0 vs test/unit/server_tests.rb in sanford-0.15.0
- old
+ new
@@ -21,10 +21,12 @@
should have_imeths :configuration
should have_imeths :name, :ip, :port, :pid_file
should have_imeths :receives_keep_alive
should have_imeths :verbose_logging, :logger
should have_imeths :init, :error
+ should have_imeths :on_worker_start, :on_worker_shutdown
+ should have_imeths :on_worker_sleep, :on_worker_wakeup
should have_imeths :router, :template_source
should "know its configuration" do
config = subject.configuration
assert_instance_of Configuration, config
@@ -91,10 +93,26 @@
new_error_proc = proc{ Factory.string }
subject.error(&new_error_proc)
assert_includes new_error_proc, subject.configuration.error_procs
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 router" do
new_router = Factory.string
subject.router(new_router)
assert_equal new_router, subject.configuration.router
assert_equal new_router, subject.router
@@ -122,17 +140,30 @@
desc "when init"
setup do
@server_class.name Factory.string
@server_class.ip Factory.string
@server_class.port Factory.integer
- @server_class.error{ Factory.string }
+
+ @error_procs = Factory.integer(3).times.map{ proc{} }
+ @error_procs.each{ |p| @server_class.error(&p) }
+
+ @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| @server_class.on_worker_start(&p) }
+ @shutdown_procs.each { |p| @server_class.on_worker_shutdown(&p) }
+ @sleep_procs.each { |p| @server_class.on_worker_sleep(&p) }
+ @wakeup_procs.each { |p| @server_class.on_worker_wakeup(&p) }
+
@server_class.router do
service Factory.string, TestHandler.to_s
end
- @dat_tcp_server_spy = DatTCP::ServerSpy.new
+ @dat_tcp_server_spy = nil
Assert.stub(DatTCP::Server, :new) do |&block|
+ @dat_tcp_server_spy = DatTCP::ServerSpy.new
@dat_tcp_server_spy.serve_proc = block
@dat_tcp_server_spy
end
@server = @server_class.new
@@ -154,26 +185,38 @@
should "know its server data" do
configuration = subject.class.configuration
sd = subject.server_data
assert_instance_of Sanford::ServerData, sd
- assert_equal configuration.name, sd.name
- assert_equal configuration.ip, sd.ip
- assert_equal configuration.port, sd.port
- assert_equal configuration.verbose_logging, sd.verbose_logging
- assert_equal configuration.receives_keep_alive, sd.receives_keep_alive
- assert_equal configuration.error_procs, sd.error_procs
- assert_equal configuration.routes, sd.routes.values
+ assert_equal configuration.name, sd.name
+ assert_equal configuration.ip, sd.ip
+ assert_equal configuration.port, sd.port
+ assert_equal configuration.verbose_logging, sd.verbose_logging
+ assert_equal configuration.receives_keep_alive, sd.receives_keep_alive
+ assert_equal configuration.error_procs, sd.error_procs
+ assert_equal configuration.worker_start_procs, sd.worker_start_procs
+ assert_equal configuration.worker_shutdown_procs, sd.worker_shutdown_procs
+ assert_equal configuration.worker_sleep_procs, sd.worker_sleep_procs
+ assert_equal configuration.worker_wakeup_procs, sd.worker_wakeup_procs
+ assert_equal configuration.routes, sd.routes.values
+
assert_instance_of configuration.logger.class, sd.logger
end
should "know its dat tcp server" do
- assert_equal @dat_tcp_server_spy, subject.dat_tcp_server
+ assert_not_nil @dat_tcp_server_spy
assert_not_nil @dat_tcp_server_spy.serve_proc
+
+ assert_equal @start_procs, @dat_tcp_server_spy.worker_start_procs
+ assert_equal @shutdown_procs, @dat_tcp_server_spy.worker_shutdown_procs
+ assert_equal @sleep_procs, @dat_tcp_server_spy.worker_sleep_procs
+ assert_equal @wakeup_procs, @dat_tcp_server_spy.worker_wakeup_procs
+
+ assert_equal @dat_tcp_server_spy, subject.dat_tcp_server
end
- should "know its name, pid file" do
+ should "demeter its server data" do
assert_equal subject.server_data.name, subject.name
assert_equal subject.server_data.ip, subject.configured_ip
assert_equal subject.server_data.port, subject.configured_port
assert_equal subject.server_data.pid_file, subject.pid_file
end
@@ -193,10 +236,20 @@
subject.listen
assert_equal subject.server_data.ip, @dat_tcp_server_spy.ip
assert_equal subject.server_data.port, @dat_tcp_server_spy.port
end
+ should "write its ip and port back to its server data" do
+ ip = Factory.string
+ port = Factory.integer
+ assert_not_equal ip, subject.server_data.ip
+ assert_not_equal port, subject.server_data.port
+ subject.listen(ip, port)
+ assert_equal ip, subject.server_data.ip
+ assert_equal port, subject.server_data.port
+ end
+
should "pass any args to its dat tcp server using `listen`" do
ip, port = Factory.string, Factory.integer
subject.listen(ip, port)
assert_equal ip, @dat_tcp_server_spy.ip
assert_equal port, @dat_tcp_server_spy.port
@@ -384,24 +437,35 @@
class ConfigurationTests < UnitTests
include NsOptions::AssertMacros
desc "Configuration"
setup do
+ @orig_ip_env_var = ENV['SANFORD_IP']
+ @orig_port_env_var = ENV['SANFORD_PORT']
+ ENV.delete('SANFORD_IP')
+ ENV.delete('SANFORD_PORT')
+
@configuration = Configuration.new.tap do |c|
c.name Factory.string
- c.ip Factory.string
+ c.ip Factory.string
c.port Factory.integer
end
end
+ teardown do
+ ENV['SANFORD_IP'] = @orig_ip_env_var
+ ENV['SANFORD_PORT'] = @orig_port_env_var
+ end
subject{ @configuration }
should have_options :name, :ip, :port, :pid_file
should have_options :receives_keep_alive
should have_options :verbose_logging, :logger
should have_options :template_source
should have_accessors :init_procs, :error_procs
should have_accessors :router
+ 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
@@ -422,29 +486,47 @@
assert_instance_of Sanford::NullTemplateSource, config.template_source
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_instance_of Sanford::Router, config.router
assert_empty config.router.routes
end
+ should "use env vars for its options if they are set" do
+ ENV['SANFORD_IP'] = Factory.string
+ ENV['SANFORD_PORT'] = Factory.integer.to_s
+
+ config = Configuration.new
+ assert_equal ENV['SANFORD_IP'], config.ip
+ assert_equal ENV['SANFORD_PORT'].to_i, config.port
+ end
+
should "not be valid by default" do
assert_false subject.valid?
end
should "know its routes" do
assert_equal subject.router.routes, subject.routes
subject.router.service(Factory.string, TestHandler.to_s)
assert_equal subject.router.routes, subject.routes
end
- should "include its init/error procs and router/routes in its `to_hash`" do
+ should "include its procs and router/routes in its `to_hash`" do
config_hash = subject.to_hash
- assert_equal subject.init_procs, config_hash[:init_procs]
- assert_equal subject.error_procs, config_hash[:error_procs]
- assert_equal subject.router, config_hash[:router]
- assert_equal subject.routes, config_hash[:routes]
+ assert_equal subject.init_procs, config_hash[:init_procs]
+ assert_equal subject.error_procs, config_hash[:error_procs]
+ 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]
+ assert_equal subject.router, config_hash[:router]
+ assert_equal subject.routes, config_hash[:routes]
end
should "call its init procs when validated" do
called = false
subject.init_procs << proc{ called = true }