test/unit/process_tests.rb in qs-0.6.1 vs test/unit/process_tests.rb in qs-0.7.0
- old
+ new
@@ -1,32 +1,42 @@
require 'assert'
require 'qs/process'
require 'qs/daemon'
+require 'qs/io_pipe'
require 'test/support/pid_file_spy'
class Qs::Process
class UnitTests < Assert::Context
desc "Qs::Process"
setup do
+ @current_env_skip_daemonize = ENV['QS_SKIP_DAEMONIZE']
+ ENV.delete('QS_SKIP_DAEMONIZE')
+
@process_class = Qs::Process
end
+ teardown do
+ ENV['QS_SKIP_DAEMONIZE'] = @current_env_skip_daemonize
+ end
subject{ @process_class }
should "know its wait for signals timeout" do
assert_equal 15, WAIT_FOR_SIGNALS_TIMEOUT
end
+ should "know its signal strings" do
+ assert_equal 'H', HALT
+ assert_equal 'S', STOP
+ assert_equal 'R', RESTART
+ end
+
end
class InitTests < UnitTests
desc "when init"
setup do
- @current_env_skip_daemonize = ENV['QS_SKIP_DAEMONIZE']
- ENV.delete('QS_SKIP_DAEMONIZE')
-
@daemon_spy = DaemonSpy.new
@pid_file_spy = PIDFileSpy.new(Factory.integer)
Assert.stub(Qs::PIDFile, :new).with(@daemon_spy.pid_file) do
@pid_file_spy
@@ -35,13 +45,10 @@
@restart_cmd_spy = RestartCmdSpy.new
Assert.stub(Qs::RestartCmd, :new){ @restart_cmd_spy }
@process = @process_class.new(@daemon_spy)
end
- teardown do
- ENV['QS_SKIP_DAEMONIZE'] = @current_env_skip_daemonize
- end
subject{ @process }
should have_readers :daemon, :name
should have_readers :pid_file, :signal_io, :restart_cmd
should have_imeths :run, :daemonize?
@@ -49,11 +56,13 @@
should "know its daemon" do
assert_equal @daemon_spy, subject.daemon
end
should "know its name, pid file, signal io and restart cmd" do
- assert_equal "qs: #{@daemon_spy.process_label}", subject.name
+ exp = "qs: #{@daemon_spy.process_label}"
+ assert_equal exp, subject.name
+
assert_equal @pid_file_spy, subject.pid_file
assert_instance_of Qs::IOPipe, subject.signal_io
assert_equal @restart_cmd_spy, subject.restart_cmd
end
@@ -107,16 +116,16 @@
desc "and run"
setup do
@wait_timeout = nil
Assert.stub(@process.signal_io, :wait) do |timeout|
@wait_timeout = timeout
- sleep 0.1
+ sleep 2*JOIN_SECONDS
false
end
@thread = Thread.new{ @process.run }
- @thread.join(0.1)
+ @thread.join(JOIN_SECONDS)
end
teardown do
# manually unstub or the process thread will hang forever
Assert.unstub(@process.signal_io, :wait)
end
@@ -175,11 +184,11 @@
class RunWithDaemonizeTests < RunSetupTests
desc "and run when it should daemonize"
setup do
Assert.stub(@process, :daemonize?){ true }
@thread = Thread.new{ @process.run }
- @thread.join(0.1)
+ @thread.join(JOIN_SECONDS)
end
should "daemonize the process" do
assert_true @daemonize_called
end
@@ -188,12 +197,13 @@
class RunAndHaltTests < RunSetupTests
desc "and run with a halt signal"
setup do
@thread = Thread.new{ @process.run }
+ @thread.join(JOIN_SECONDS)
@process.signal_io.write(HALT)
- @thread.join(0.1)
+ @thread.join(JOIN_SECONDS)
end
should "halt its daemon" do
assert_true @daemon_spy.halt_called
assert_equal [true], @daemon_spy.halt_args
@@ -215,12 +225,13 @@
class RunAndStopTests < RunSetupTests
desc "and run with a stop signal"
setup do
@thread = Thread.new{ @process.run }
+ @thread.join(JOIN_SECONDS)
@process.signal_io.write(STOP)
- @thread.join(0.1)
+ @thread.join(JOIN_SECONDS)
end
should "stop its daemon" do
assert_true @daemon_spy.stop_called
assert_equal [true], @daemon_spy.stop_args
@@ -242,41 +253,39 @@
class RunAndRestartTests < RunSetupTests
desc "and run with a restart signal"
setup do
@thread = Thread.new{ @process.run }
+ @thread.join(JOIN_SECONDS)
@process.signal_io.write(RESTART)
- @thread.join(0.1)
+ @thread.join(JOIN_SECONDS)
end
should "stop its daemon" do
assert_true @daemon_spy.stop_called
assert_equal [true], @daemon_spy.stop_args
end
- should "set the env var to skip daemonize" do
- assert_equal 'yes', ENV['QS_SKIP_DAEMONIZE']
- end
-
should "run the restart cmd" do
assert_true @restart_cmd_spy.run_called
end
end
class RunWithDaemonCrashTests < RunSetupTests
desc "and run with the daemon crashing"
setup do
- # lower the time it sleeps so it loops and restarts the daemon quicker
Assert.stub(@process.signal_io, :wait) do |timeout|
- sleep 0.1
+ sleep JOIN_SECONDS * 0.5 # ensure this has time to run before the thread
+ # joins below
false
end
@thread = Thread.new{ @process.run }
+ @thread.join(JOIN_SECONDS)
@daemon_spy.start_called = false
- @thread.join(0.1)
+ @thread.join(JOIN_SECONDS)
end
teardown do
# manually unstub or the process thread will hang forever
Assert.unstub(@process.signal_io, :wait)
end
@@ -292,11 +301,11 @@
setup do
# ruby throws an argument error if the OS doesn't support a signal
Assert.stub(::Signal, :trap){ raise ArgumentError }
@thread = Thread.new{ @process.run }
- @thread.join(0.1)
+ @thread.join(JOIN_SECONDS)
end
should "start normally" do
assert_true @daemon_spy.start_called
assert_equal 'sleep', @thread.status
@@ -341,13 +350,29 @@
should "know its argv" do
assert_equal [Gem.ruby, $0, ARGV].flatten, subject.argv
end
- should "change the dir and run a kernel exec when run" do
- subject.run
- assert_equal [subject.dir], @chdir_called_with
- assert_equal subject.argv, @exec_called_with
+ if RUBY_VERSION == '1.8.7'
+
+ should "set env vars, change the dir and kernel exec when run" do
+ subject.run
+
+ assert_equal 'yes', ENV['QS_SKIP_DAEMONIZE']
+ assert_equal [subject.dir], @chdir_called_with
+ assert_equal subject.argv, @exec_called_with
+ end
+
+ else
+
+ should "kernel exec when run" do
+ subject.run
+
+ env = { 'QS_SKIP_DAEMONIZE' => 'yes' }
+ options = { :chdir => subject.dir }
+ assert_equal ([env] + subject.argv + [options]), @exec_called_with
+ end
+
end
end
class RestartCmdWithPWDEnvNoMatchTests < RestartCmdTests