rescue event register and implement transition state override
[god.git] / test / test_process.rb
blob99544900f90740599e677f0bae162974a9199f79
1 require File.dirname(__FILE__) + '/helper'
3 module God
4   class Process
5     def fork
6       raise "You forgot to stub fork"
7     end
8     
9     def exec(*args)
10       raise "You forgot to stub exec"
11     end
12   end
13 end
15 class TestProcess < Test::Unit::TestCase
16   def setup
17     @p = God::Process.new(:name => 'foo', :pid_file => 'blah.pid')
18     @p.stubs(:test).returns true # so we don't try to mkdir_p
19     Process.stubs(:detach) # because we stub fork
20   end
21   
22   # These actually excercise call_action in the back at this point - Kev
23   def test_call_action_with_string_should_fork_exec 
24     @p.start = "do something"
25     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
26     @p.expects(:fork)
27     Process.expects(:waitpid)
28     @p.call_action(:start)
29   end
30   
31   def test_call_action_with_lambda_should_call
32     cmd = lambda { puts "Hi" }
33     cmd.expects(:call)
34     @p.start = cmd
35     @p.call_action(:start)
36   end
38   def test_default_pid_file
39     assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
40   end
41   
42   def test_call_action_without_pid_should_write_pid
43     # Only for start, restart
44     [:start, :restart].each do |action|
45       @p = God::Process.new(:name => 'foo')
46       @p.stubs(:test).returns true
47       IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
48       @p.expects(:fork)
49       Process.expects(:waitpid)
50       File.expects(:open).with(@p.default_pid_file, 'w')
51       @p.send("#{action}=", "run")
52       @p.call_action(action)
53     end
54   end
55   
56   def test_call_action_should_not_write_pid_for_stop
57     @p.pid_file = nil
58     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
59     @p.expects(:fork)
60     Process.expects(:waitpid)
61     File.expects(:open).times(0)
62     @p.stop = "stopping"
63     @p.call_action(:stop)
64   end
65   
66   def test_call_action_should_mkdir_p_if_pid_file_dir_existence_test_fails
67     @p.pid_file = nil
68     IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
69     @p.expects(:fork)
70     Process.expects(:waitpid)
71     @p.expects(:test).returns(false, true)
72     FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
73     File.expects(:open)
74     @p.start = "starting"
75     @p.call_action(:start)
76   end
77   
78   def test_start_stop_restart_bang
79     [:start, :stop, :restart].each do |x|
80       @p.expects(:call_action).with(x)
81       @p.send("#{x}!")
82     end
83   end
84 end