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_get_stored_in_pending_watches
50 God.watch { |w| watch = w }
52 assert_equal 1, God.pending_watches.size
53 assert_equal watch, God.pending_watches.first
56 def test_watch_should_register_processes
57 assert_nil God.registry['foo']
58 God.watch { |w| w.name = 'foo' }
59 assert_kind_of God::Process, God.registry['foo']
62 def test_watch_should_get_stored_by_group
71 assert_equal({'test' => [a]}, God.groups)
74 def test_watches_should_get_stored_by_group
90 assert_equal({'test' => [a, b]}, God.groups)
93 def test_watch_should_allow_multiple_watches
94 God.watch { |w| w.name = 'foo' }
96 assert_nothing_raised do
97 God.watch { |w| w.name = 'bar' }
101 def test_watch_should_disallow_duplicate_watch_names
102 God.watch { |w| w.name = 'foo' }
105 God.watch { |w| w.name = 'foo' }
109 def test_watch_should_disallow_identical_watch_and_group_names
110 God.watch { |w| w.name = 'foo'; w.group = 'bar' }
113 God.watch { |w| w.name = 'bar' }
117 def test_watch_should_disallow_identical_watch_and_group_names_other_way
118 God.watch { |w| w.name = 'bar' }
121 God.watch { |w| w.name = 'foo'; w.group = 'bar' }
125 def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
126 God.watch { |w| w.name = 'foo' }
129 assert_nothing_raised do
131 God.watch { |w| w.name = 'foo' }
138 def test_unwatch_should_unmonitor_watch
139 God.watch { |w| w.name = 'bar' }
140 w = God.watches['bar']
141 w.expects(:unmonitor)
145 def test_unwatch_should_unregister_watch
146 God.watch { |w| w.name = 'bar' }
147 w = God.watches['bar']
148 w.expects(:unregister!)
154 def test_unwatch_should_remove_same_name_watches
155 God.watch { |w| w.name = 'bar' }
156 w = God.watches['bar']
160 assert_equal 0, God.watches.size
163 def test_unwatch_should_remove_from_group
168 w = God.watches['bar']
172 assert !God.groups[w.group].include?(w)
177 def test_control_should_monitor_on_start
178 God.watch { |w| w.name = 'foo' }
180 w = God.watches['foo']
182 God.control('foo', 'start')
185 def test_control_should_move_to_restart_on_restart
186 God.watch { |w| w.name = 'foo' }
188 w = God.watches['foo']
189 w.expects(:move).with(:restart)
190 God.control('foo', 'restart')
193 def test_control_should_unmonitor_and_stop_on_stop
194 God.watch { |w| w.name = 'foo' }
196 w = God.watches['foo']
197 w.expects(:unmonitor).returns(w)
198 w.expects(:action).with(:stop)
199 God.control('foo', 'stop')
202 def test_control_should_unmonitor_on_unmonitor
203 God.watch { |w| w.name = 'foo' }
205 w = God.watches['foo']
206 w.expects(:unmonitor).returns(w)
207 God.control('foo', 'unmonitor')
210 def test_control_should_raise_on_invalid_command
211 God.watch { |w| w.name = 'foo' }
213 assert_raise InvalidCommandError do
214 God.control('foo', 'invalid')
218 def test_control_should_operate_on_each_watch_in_group
229 God.watches['foo1'].expects(:monitor)
230 God.watches['foo2'].expects(:monitor)
232 God.control('bar', 'start')
237 def test_running_load_should_eval_code
245 God.running_load(code)
248 assert_equal 1, God.watches.size
251 def test_running_load_should_monitor_new_watches
258 Watch.any_instance.expects(:monitor)
260 God.running_load(code)
264 def test_running_load_should_not_monitor_new_watches_with_autostart_false
272 Watch.any_instance.expects(:monitor).never
274 God.running_load(code)
278 def test_running_load_should_return_array_of_affected_watches
287 w = God.running_load(code)
289 assert_equal 1, w.size
290 assert_equal 'foo', w.first.name
293 def test_running_load_should_clear_pending_watches
301 God.running_load(code)
303 assert_equal 0, God.pending_watches.size
308 def test_load_should_collect_and_load_globbed_path
309 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
310 Kernel.expects(:load).with('a').once
311 Kernel.expects(:load).with('b').once
312 God.load('/path/to/*.thing')
317 def test_start_should_check_for_at_least_one_watch
323 def test_start_should_start_event_handler
324 God.watch { |w| w.name = 'foo' }
325 Timer.any_instance.expects(:join)
326 EventHandler.expects(:start).once
332 def test_start_should_begin_monitoring_autostart_watches
337 Timer.any_instance.expects(:join)
338 Watch.any_instance.expects(:monitor).once
342 def test_start_should_not_begin_monitoring_non_autostart_watches
348 Timer.any_instance.expects(:join)
349 Watch.any_instance.expects(:monitor).never
353 def test_start_should_get_and_join_timer
354 God.watch { |w| w.name = 'foo' }
355 Timer.any_instance.expects(:join)
363 def test_at_exit_should_call_start
364 God.expects(:start).once