more diagnostics
[god.git] / test / test_event_handler.rb
blob8eb3ddcef8e495c023d6ccfe80e8f93b1f187af3
1 require File.dirname(__FILE__) + '/helper'
3 module God
4   class EventHandler
5     
6     def self.actions=(value)
7       @@actions = value
8     end
9     
10     def self.actions
11       @@actions
12     end
13     
14     def self.handler=(value)
15       @@handler = value
16     end
17   end
18 end
20 class TestEventHandler < Test::Unit::TestCase
21   def setup
22     @h = God::EventHandler
23   end
24     
25   def test_register_one_event
26     pid = 4445
27     event = :proc_exit
28     block = lambda {
29       puts "Hi"
30     }
31     
32     mock_handler = mock()
33     mock_handler.expects(:register_process).with(pid, [event])
34     @h.handler = mock_handler
35     
36     @h.register(pid, event, &block)
37     assert_equal @h.actions, {pid => {event => block}}
38   end
39   
40   def test_register_multiple_events_per_process
41     pid = 4445
42     exit_block = lambda { puts "Hi" }
43     @h.actions = {pid => {:proc_exit => exit_block}}
44     
45     mock_handler = mock()
46     mock_handler.expects(:register_process).with do |a, b|
47       a == pid &&
48       b.to_set == [:proc_exit, :proc_fork].to_set
49     end
50     @h.handler = mock_handler
51     
52     fork_block = lambda { puts "Forking" }
53     @h.register(pid, :proc_fork, &fork_block)
54     assert_equal @h.actions, {pid => {:proc_exit => exit_block,
55                                      :proc_fork => fork_block }}
56   end
57   
58   # JIRA PLATFORM-75
59   def test_call_should_check_for_pid_and_action_before_executing
60     exit_block = mock()
61     exit_block.expects(:call).times 1
62     @h.actions = {4445 => {:proc_exit => exit_block}}
63     @h.call(4446, :proc_exit) # shouldn't call, bad pid
64     @h.call(4445, :proc_fork) # shouldn't call, bad event
65     @h.call(4445, :proc_exit) # should call
66   end
67   
68   def teardown
69     # Reset handler
70     @h.actions = {}
71     @h.load
72   end
73 end