4ff88b407c2a442910064b6144a6fe988bd15677
[god.git] / test / test_timer.rb
blob4ff88b407c2a442910064b6144a6fe988bd15677
1 require File.dirname(__FILE__) + '/helper'
3 class TestTimer < Test::Unit::TestCase
4   def setup
5     Timer.reset
6     @t = Timer.get
7   end
8   
9   def test_new_timer_should_have_no_events
10     assert_equal 0, @t.events.size
11   end
12   
13   def test_schedule_should_queue_event
14     w = Watch.new
15     @t.schedule(stub(:interval => 20, :watch => w))
16     
17     assert_equal 1, @t.events.size
18   end
19   
20   def test_timer_should_remove_expired_events
21     @t.schedule(stub(:interval => -1, :watch => Watch.new))
22     sleep(0.3)
23     assert_equal 0, @t.events.size
24   end
25   
26   def test_timer_should_remove_only_expired_events
27     w = Watch.new
28     @t.schedule(stub(:interval => -1, :watch => w))
29     @t.schedule(stub(:interval => 1000, :watch => w))
30     sleep(0.3)
31     assert_equal 1, @t.events.size
32   end
33   
34   def test_timer_should_sort_timer_events
35     w = Watch.new
36     @t.schedule(stub(:interval => 1000, :watch => w))
37     @t.schedule(stub(:interval => 800, :watch => w))
38     @t.schedule(stub(:interval => 900, :watch => w))
39     @t.schedule(stub(:interval => 100, :watch => w))
40     sleep(0.3)
41     assert_equal [100, 800, 900, 1000], @t.events.map { |x| x.condition.interval }
42   end
43   
44   def test_unschedule_should_remove_conditions
45     w = Watch.new
46     a = stub(:watch => w)
47     b = stub(:watch => w)
48     @t.schedule(a, 100)
49     @t.schedule(b, 200)
50     assert_equal 2, @t.conditions.size
51     @t.unschedule(a)
52     assert_equal 1, @t.conditions.size
53   end
54   
55   def test_time_should_recover_from_exceptions
56     w = Watch.new
57     @t.expects(:trigger).raises(Exception.new)
58     no_stdout do
59       @t.schedule(stub(:interval => -1, :watch => w))
60       sleep(0.3)
61       @t.schedule(stub(:interval => 0, :watch => w))
62     end
63   end
64   
65   # join
66   
67   def test_join_should_join
68     Thread.any_instance.expects(:join)
69     @t.join
70   end
71 end