1 require File.dirname(__FILE__) + '/helper'
3 class TestGod < Test::Unit::TestCase
5 Server.stubs(:new).returns(true)
15 def test_init_should_initialize_watches_to_empty_array
17 assert_equal Hash.new, God.watches
20 def test_init_should_kick_off_a_server_instance
21 Server.expects(:new).returns(true)
27 def test_pid_file_directory_should_return_default_if_not_set_explicitly
28 assert_equal '/var/run/god', God.pid_file_directory
31 def test_pid_file_directory_equals_should_set
32 God.pid_file_directory = '/foo'
33 assert_equal '/foo', God.pid_file_directory
38 def test_watch_should_get_stored
40 God.watch { |w| watch = w }
42 assert_equal 1, God.watches.size
43 assert_equal watch, God.watches.values.first
45 assert_equal 0, God.groups.size
48 def test_watch_should_register_processes
49 assert_nil God.registry['foo']
50 God.watch { |w| w.name = 'foo' }
51 assert_kind_of God::Process, God.registry['foo']
54 def test_watch_should_get_stored_by_group
63 assert_equal({'test' => [a]}, God.groups)
66 def test_watches_should_get_stored_by_group
82 assert_equal({'test' => [a, b]}, God.groups)
85 def test_watch_should_allow_multiple_watches
86 God.watch { |w| w.name = 'foo' }
88 assert_nothing_raised do
89 God.watch { |w| w.name = 'bar' }
93 def test_watch_should_disallow_duplicate_watch_names
94 God.watch { |w| w.name = 'foo' }
97 God.watch { |w| w.name = 'foo' }
101 def test_watch_should_disallow_identical_watch_and_group_names
102 God.watch { |w| w.name = 'foo'; w.group = 'bar' }
105 God.watch { |w| w.name = 'bar' }
109 def test_watch_should_disallow_identical_watch_and_group_names_other_way
110 God.watch { |w| w.name = 'bar' }
113 God.watch { |w| w.name = 'foo'; w.group = 'bar' }
119 def test_control_should_monitor_on_start
120 God.watch { |w| w.name = 'foo' }
122 w = God.watches['foo']
124 God.control('foo', 'start')
127 def test_control_should_move_to_restart_on_restart
128 God.watch { |w| w.name = 'foo' }
130 w = God.watches['foo']
131 w.expects(:move).with(:restart)
132 God.control('foo', 'restart')
135 def test_control_should_unmonitor_and_stop_on_stop
136 God.watch { |w| w.name = 'foo' }
138 w = God.watches['foo']
139 w.expects(:unmonitor).returns(w)
140 w.expects(:action).with(:stop)
141 God.control('foo', 'stop')
144 def test_control_should_unmonitor_on_unmonitor
145 God.watch { |w| w.name = 'foo' }
147 w = God.watches['foo']
148 w.expects(:unmonitor).returns(w)
149 God.control('foo', 'unmonitor')
152 def test_control_should_raise_on_invalid_command
153 God.watch { |w| w.name = 'foo' }
155 assert_raise InvalidCommandError do
156 God.control('foo', 'invalid')
160 def test_control_should_operate_on_each_watch_in_group
171 God.watches['foo1'].expects(:monitor)
172 God.watches['foo2'].expects(:monitor)
174 God.control('bar', 'start')
179 def test_start_should_check_for_at_least_one_watch
185 def test_start_should_start_event_handler
186 God.watch { |w| w.name = 'foo' }
187 Timer.any_instance.expects(:join)
188 EventHandler.expects(:start).once
194 def test_start_should_begin_monitoring_autostart_watches
199 Timer.any_instance.expects(:join)
200 Watch.any_instance.expects(:monitor).once
204 def test_start_should_not_begin_monitoring_non_autostart_watches
210 Timer.any_instance.expects(:join)
211 Watch.any_instance.expects(:monitor).never
215 def test_start_should_get_and_join_timer
216 God.watch { |w| w.name = 'foo' }
217 Timer.any_instance.expects(:join)
225 def test_at_exit_should_call_start
226 God.expects(:start).once
232 def test_load_should_collect_and_load_globbed_path
233 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
234 Kernel.expects(:load).with('a').once
235 Kernel.expects(:load).with('b').once
236 God.load('/path/to/*.thing')