1 require File.dirname(__FILE__) + '/helper'
6 # raise "You forgot to stub fork"
10 raise "You forgot to stub exec"
15 class TestProcessChild < Test::Unit::TestCase
20 @p.stubs(:test).returns true # so we don't try to mkdir_p
21 Process.stubs(:detach) # because we stub fork
23 ::Process::Sys.stubs(:setuid).returns(true)
24 ::Process::Sys.stubs(:setgid).returns(true)
29 def test_valid_should_return_true_if_auto_daemonized_and_log
36 def test_valid_should_return_true_if_auto_daemonized_and_no_stop
43 def test_valid_should_return_true_if_uid_exists
45 @p.log = '/tmp/foo.log'
51 def test_valid_should_return_true_if_uid_does_not_exists
53 @p.log = '/tmp/foo.log'
63 def test_valid_should_return_true_if_gid_exists
65 @p.log = '/tmp/foo.log'
71 def test_valid_should_return_true_if_gid_does_not_exists
73 @p.log = '/tmp/foo.log'
85 def test_call_action_should_write_pid
86 # Only for start, restart
87 [:start, :restart].each do |action|
88 @p.stubs(:test).returns true
89 IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
91 Process.expects(:waitpid)
92 File.expects(:open).with(@p.default_pid_file, 'w')
93 @p.send("#{action}=", "run")
94 @p.call_action(action)
99 ###############################################################################
103 ###############################################################################
105 class TestProcessDaemon < Test::Unit::TestCase
108 @p = God::Process.new
110 @p.pid_file = 'blah.pid'
111 @p.stubs(:test).returns true # so we don't try to mkdir_p
112 Process.stubs(:detach) # because we stub fork
117 def test_alive_should_call_system_process_exists
118 File.expects(:read).with('blah.pid').returns('1234')
119 System::Process.any_instance.expects(:exists?).returns(false)
123 def test_alive_should_return_false_if_no_such_file
124 File.expects(:read).with('blah.pid').raises(Errno::ENOENT)
130 def test_valid_should_return_false_if_no_start
138 def test_valid_should_return_false_if_self_daemonized_and_no_stop
149 def test_pid_should_return_integer_for_valid_pid_files
150 File.stubs(:read).returns("123")
151 assert_equal 123, @p.pid
154 def test_pid_should_return_nil_for_missing_files
156 assert_equal nil, @p.pid
159 def test_pid_should_return_nil_for_invalid_pid_files
160 File.stubs(:read).returns("four score and seven years ago")
161 assert_equal nil, @p.pid
164 def test_pid_should_retain_last_pid_value_if_pid_file_is_removed
165 File.stubs(:read).returns("123")
166 assert_equal 123, @p.pid
168 File.stubs(:read).returns("")
169 assert_equal 123, @p.pid
171 File.stubs(:read).returns("246")
172 assert_equal 246, @p.pid
177 def test_default_pid_file
178 assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
182 # These actually excercise call_action in the back at this point - Kev
184 def test_call_action_with_string_should_call_system
185 @p.start = "do something"
187 Process.expects(:waitpid)
188 @p.call_action(:start)
191 def test_call_action_with_lambda_should_call
192 cmd = lambda { puts "Hi" }
195 @p.call_action(:start)
198 def test_call_action_with_invalid_command_class_should_raise
204 assert_raise NotImplementedError do
205 @p.call_action(:start)
209 # start!/stop!/restart!
211 def test_start_stop_restart_bang
212 [:start, :stop, :restart].each do |x|
213 @p.expects(:call_action).with(x)