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)
25 def test_init_should_abort_if_called_after_watch
26 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
35 def test_pid_file_directory_should_return_default_if_not_set_explicitly
36 assert_equal '/var/run/god', God.pid_file_directory
39 def test_pid_file_directory_equals_should_set
40 God.pid_file_directory = '/foo'
41 assert_equal '/foo', God.pid_file_directory
46 def test_watch_should_get_stored
54 assert_equal 1, God.watches.size
55 assert_equal watch, God.watches.values.first
57 assert_equal 0, God.groups.size
60 def test_watch_should_get_stored_in_pending_watches
68 assert_equal 1, God.pending_watches.size
69 assert_equal watch, God.pending_watches.first
72 def test_watch_should_register_processes
73 assert_nil God.registry['foo']
78 assert_kind_of God::Process, God.registry['foo']
81 def test_watch_should_get_stored_by_group
91 assert_equal({'test' => [a]}, God.groups)
94 def test_watches_should_get_stored_by_group
112 assert_equal({'test' => [a, b]}, God.groups)
115 def test_watch_should_allow_multiple_watches
116 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
118 assert_nothing_raised do
119 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
123 def test_watch_should_disallow_duplicate_watch_names
124 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
127 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
131 def test_watch_should_disallow_identical_watch_and_group_names
132 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
135 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
139 def test_watch_should_disallow_identical_watch_and_group_names_other_way
140 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
143 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
147 def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
148 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
151 assert_nothing_raised do
153 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
160 def test_unwatch_should_unmonitor_watch
161 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
162 w = God.watches['bar']
163 w.expects(:unmonitor)
167 def test_unwatch_should_unregister_watch
168 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
169 w = God.watches['bar']
170 w.expects(:unregister!)
176 def test_unwatch_should_remove_same_name_watches
177 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
178 w = God.watches['bar']
182 assert_equal 0, God.watches.size
185 def test_unwatch_should_remove_from_group
191 w = God.watches['bar']
195 assert !God.groups[w.group].include?(w)
200 def test_control_should_monitor_on_start
201 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
203 w = God.watches['foo']
205 God.control('foo', 'start')
208 def test_control_should_move_to_restart_on_restart
209 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
211 w = God.watches['foo']
212 w.expects(:move).with(:restart)
213 God.control('foo', 'restart')
216 def test_control_should_unmonitor_and_stop_on_stop
217 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
219 w = God.watches['foo']
220 w.expects(:unmonitor).returns(w)
221 w.expects(:action).with(:stop)
222 God.control('foo', 'stop')
225 def test_control_should_unmonitor_on_unmonitor
226 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
228 w = God.watches['foo']
229 w.expects(:unmonitor).returns(w)
230 God.control('foo', 'unmonitor')
233 def test_control_should_raise_on_invalid_command
234 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
236 assert_raise InvalidCommandError do
237 God.control('foo', 'invalid')
241 def test_control_should_operate_on_each_watch_in_group
254 God.watches['foo1'].expects(:monitor)
255 God.watches['foo2'].expects(:monitor)
257 God.control('bar', 'start')
262 def test_running_load_should_eval_code
271 God.running_load(code)
274 assert_equal 1, God.watches.size
277 def test_running_load_should_monitor_new_watches
285 Watch.any_instance.expects(:monitor)
287 God.running_load(code)
291 def test_running_load_should_not_monitor_new_watches_with_autostart_false
300 Watch.any_instance.expects(:monitor).never
302 God.running_load(code)
306 def test_running_load_should_return_array_of_affected_watches
316 w = God.running_load(code)
318 assert_equal 1, w.size
319 assert_equal 'foo', w.first.name
322 def test_running_load_should_clear_pending_watches
331 God.running_load(code)
333 assert_equal 0, God.pending_watches.size
338 def test_load_should_collect_and_load_globbed_path
339 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
340 Kernel.expects(:load).with('a').once
341 Kernel.expects(:load).with('b').once
342 God.load('/path/to/*.thing')
347 def test_start_should_check_for_at_least_one_watch
353 def test_start_should_start_event_handler
354 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
355 Timer.any_instance.expects(:join)
356 EventHandler.expects(:start).once
362 def test_start_should_begin_monitoring_autostart_watches
368 Timer.any_instance.expects(:join)
369 Watch.any_instance.expects(:monitor).once
373 def test_start_should_not_begin_monitoring_non_autostart_watches
380 Timer.any_instance.expects(:join)
381 Watch.any_instance.expects(:monitor).never
385 def test_start_should_get_and_join_timer
386 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
387 Timer.any_instance.expects(:join)
395 def test_at_exit_should_call_start
396 God.expects(:start).once