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_default_pid_file
150 assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
154 # These actually excercise call_action in the back at this point - Kev
156 def test_call_action_with_string_should_call_system
157 @p.start = "do something"
159 Process.expects(:waitpid)
160 @p.call_action(:start)
163 def test_call_action_with_lambda_should_call
164 cmd = lambda { puts "Hi" }
167 @p.call_action(:start)
170 def test_call_action_with_invalid_command_class_should_raise
176 assert_raise NotImplementedError do
177 @p.call_action(:start)
181 # start!/stop!/restart!
183 def test_start_stop_restart_bang
184 [:start, :stop, :restart].each do |x|
185 @p.expects(:call_action).with(x)