finish lifecycle conditions handling and add flapper condition
[god.git] / test / helper.rb
blobf56f258d632ed902092b2fb8a135e8ddf6e2d182
1 require File.join(File.dirname(__FILE__), *%w[.. lib god])
3 require 'test/unit'
4 require 'set'
6 include God
8 if RUBY_PLATFORM =~ /linux/i && Process.uid != 0
9   abort <<-EOF
10 *********************************************************************
11 *                                                                   *
12 *    You need to run these tests as root (netlink requires it)      *
13 *                                                                   *
14 *********************************************************************
15 EOF
16 end
18 begin
19   require 'mocha'
20 rescue LoadError
21   unless gems ||= false
22     require 'rubygems'
23     gems = true
24     retry
25   else
26     abort "=> You need the Mocha gem to run these tests."
27   end
28 end
30 module God
31   module Conditions
32     class FakeCondition < Condition
33       def test
34         true
35       end
36     end
37   
38     class FakePollCondition < PollCondition
39       def test
40         true
41       end
42     end
43   
44     class FakeEventCondition < EventCondition
45       def register
46       end
47       def deregister
48       end
49     end
50   end
51   
52   module Behaviors
53     class FakeBehavior < Behavior
54     end
55   end
56   
57   class << self
58     alias :at_exit_orig :at_exit
59   end
60   
61   def self.at_exit
62     # disable at_exit
63   end
64   
65   def self.reset
66     self.watches = nil
67     self.groups = nil
68     self.server = nil
69     self.inited = nil
70     self.host = nil
71     self.port = nil
72     self.pid_file_directory = nil
73     self.registry.reset
74   end
75 end
77 def silence_warnings
78   old_verbose, $VERBOSE = $VERBOSE, nil
79   yield
80 ensure
81   $VERBOSE = old_verbose
82 end
84 def no_stdout
85   old_stdout = $stdout.dup
86   $stdout.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
87   yield
88   $stdout.reopen(old_stdout)
89 end
91 def no_stderr
92   old_stderr = $stderr.dup
93   $stderr.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
94   yield
95   $stderr.reopen(old_stderr)
96 end
98 module Test::Unit::Assertions
99   def assert_abort
100     assert_raise SystemExit do
101       no_stderr do
102         yield
103       end
104     end
105   end
108 # This allows you to be a good OOP citizen and honor encapsulation, but
109 # still make calls to private methods (for testing) by doing
111 #   obj.bypass.private_thingie(arg1, arg2)
113 # Which is easier on the eye than
115 #   obj.send(:private_thingie, arg1, arg2)
117 class Object
118   class Bypass
119     instance_methods.each do |m|
120       undef_method m unless m =~ /^__/
121     end
123     def initialize(ref)
124       @ref = ref
125     end
126   
127     def method_missing(sym, *args)
128       @ref.__send__(sym, *args)
129     end
130   end
132   def bypass
133     Bypass.new(self)
134   end