test/system/managing_test.rb in sanford-0.4.0 vs test/system/managing_test.rb in sanford-0.6.0

- old
+ new

@@ -1,104 +1,93 @@ require 'assert' -require 'sanford/manager' +require 'sanford/cli' class ManagingTest < Assert::Context - desc "Using Sanford's manager" + include Test::ManagerHelper + desc "Using Sanford's Manager" setup do - # preserve the global service hosts configuration, no matter how we - # manipulate it - Test::Environment.store_and_clear_hosts + @start_options = { :host => 'MyHost', :ip => 'localhost', :port => 12345 } end - teardown do - Test::Environment.restore_hosts - end - class CallTest < ManagingTest - include Test::ForkManagerHelper - + class RunTest < ManagingTest + desc "to run a server" setup do - Sanford.register(TestHost) + @proc = proc{ Sanford::Manager.call(:run, @start_options) } end - end - class RunTest < CallTest - desc "to run a service host" - - should "start a sanford server for the only service host that is configured" do - self.call_sanford_manager(:run) do - assert_nothing_raised{ self.open_socket(TestHost.ip, TestHost.port) } - assert File.exists?(self.expected_pid_file(TestHost, TestHost.ip, TestHost.port)) + should "run the server specified and write a PID file" do + self.fork_and_call(@proc) do + assert_nothing_raised{ self.open_socket('localhost', 12345) } + assert File.exists?('tmp/my_host.pid') end end - end - class RunWithOptionsTest < CallTest - desc "to run a service host and passing options" - setup do - # make sure that TestHost isn't the only 'host' - Sanford.register(Class.new) - end - - should "start a sanford server for the specified service host and " \ - "use the passed options to override it's configuration" do - host = Sanford.hosts.find('TestHost') - - self.call_sanford_manager(:run, { :host => 'TestHost', :port => 12345 }) do - assert_nothing_raised{ self.open_socket(host.ip, 12345) } - assert File.exists?(self.expected_pid_file(host, host.ip, 12345)) - end - end end - class RunWithEnvVarsTest < CallTest - desc "to run a service host and setting env vars" + class StartTest < ManagingTest + desc "to start a daemonized server" setup do - @current = ENV.delete('SANFORD_HOST'), ENV.delete('SANFORD_IP'), ENV.delete('SANFORD_PORT') - ENV['SANFORD_HOST'] = 'TestHost' - ENV['SANFORD_IP'], ENV['SANFORD_PORT'] = 'localhost', '54321' - # make sure that TestHost isn't the only 'host' - Sanford.register(Class.new) + @proc = proc{ Sanford::Manager.call(:start, @start_options) } end teardown do - ENV['SANFORD_HOST'], ENV['SANFORD_IP'], ENV['SANFORD_PORT'] = @current + Sanford::Manager.call(:stop, @start_options) end - should "start a sanford server for the specified service host and " \ - "use the env vars to override it's configuration" do - host = Sanford.hosts.find(ENV['SANFORD_HOST']) - port = ENV['SANFORD_PORT'].to_i - - self.call_sanford_manager(:run) do - assert_nothing_raised{ self.open_socket(ENV['SANFORD_IP'], port) } - assert File.exists?(self.expected_pid_file(host, ENV['SANFORD_IP'], port)) + should "run the server specified and write a PID file" do + self.fork_and_call(@proc) do + assert_nothing_raised{ self.open_socket('localhost', 12345) } + assert File.exists?('tmp/my_host.pid') end end + end - class BadHostTest < ManagingTest - desc "with a bad host name" + class StopTest < ManagingTest + desc "to stop a daemonized server" setup do - Sanford.hosts.clear - Sanford.register(Class.new) + @start_proc = proc{ Sanford::Manager.call(:start, @start_options) } end - should "raise an exception when a service host can't be found" do - assert_raises(Sanford::NoHostError) do - Sanford::Manager.call(:run, :host => 'not_a_real_host') + should "stop the server specified and remove the PID file" do + self.fork_and_call(@start_proc) do + Sanford::Manager.call(:stop, @start_options) + sleep 1 + + assert_raises{ self.open_socket('localhost', 12345) } + assert_not File.exists?('tmp/my_host_localhost_12345.pid') end end + end - class NoHostsTest < ManagingTest - desc "with no hosts" + class RestartTest < ManagingTest + desc "to restart a daemonized server" setup do - Sanford.hosts.clear + @start_proc = proc{ Sanford::Manager.call(:start, @start_options) } end - should "raise an exception when there aren't any service hosts" do - assert_raises(Sanford::NoHostError) do - Sanford::Manager.call(:run) + should "stop the server specified and remove the PID file" do + self.fork_and_call(@start_proc) do + exception = nil + stop = false + thread = Thread.new do + while !stop do + begin + self.open_socket('localhost', 12345) + sleep 0.1 + rescue Exception => exception + end + end + end + Sanford::Manager.call(:restart, @start_options) + thread.join(1) + + # make sure we didn't lost the ability to connect + assert_nil exception + + stop = true + thread.join end end end end