File: test_event_handler.rb

package info (click to toggle)
ruby-god 0.12.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 752 kB
  • sloc: ruby: 5,913; ansic: 217; makefile: 3
file content (80 lines) | stat: -rw-r--r-- 1,851 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require File.dirname(__FILE__) + '/helper'

module God
  class EventHandler

    def self.actions=(value)
      @@actions = value
    end

    def self.actions
      @@actions
    end

    def self.handler=(value)
      @@handler = value
    end
  end
end

class TestEventHandler < Test::Unit::TestCase
  def setup
    @h = God::EventHandler
  end

  def test_register_one_event
    pid = 4445
    event = :proc_exit
    block = lambda {
      puts "Hi"
    }

    mock_handler = mock()
    mock_handler.expects(:register_process).with(pid, [event])
    @h.handler = mock_handler

    @h.register(pid, event, &block)
    assert_equal @h.actions, {pid => {event => block}}
  end

  def test_register_multiple_events_per_process
    pid = 4445
    exit_block = lambda { puts "Hi" }
    @h.actions = {pid => {:proc_exit => exit_block}}

    mock_handler = mock()
    mock_handler.expects(:register_process).with do |a, b|
      a == pid &&
      b.to_set == [:proc_exit, :proc_fork].to_set
    end
    @h.handler = mock_handler

    fork_block = lambda { puts "Forking" }
    @h.register(pid, :proc_fork, &fork_block)
    assert_equal @h.actions, {pid => {:proc_exit => exit_block,
                                     :proc_fork => fork_block }}
  end

  # JIRA PLATFORM-75
  def test_call_should_check_for_pid_and_action_before_executing
    exit_block = mock()
    exit_block.expects(:call).times 1
    @h.actions = {4445 => {:proc_exit => exit_block}}
    @h.call(4446, :proc_exit) # shouldn't call, bad pid
    @h.call(4445, :proc_fork) # shouldn't call, bad event
    @h.call(4445, :proc_exit) # should call
  end

  def teardown
    # Reset handler
    @h.actions = {}
    @h.load
  end
end

class TestEventHandlerOperational < Test::Unit::TestCase
  def test_operational
    God::EventHandler.start
    assert God::EventHandler.loaded?
  end
end