prevent carry-over conditions
[god.git] / test / test_timer.rb
blobaaec522b6602acb0dff6890d8604e2e2fa27a2c2
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     Time.stubs(:now).returns(0)
15     
16     w = Watch.new
17     @t.schedule(stub(:interval => 20, :watch => w))
18     
19     assert_equal 1, @t.events.size
20   end
21   
22   def test_timer_should_remove_expired_events
23     @t.schedule(stub(:interval => -1, :watch => Watch.new))
24     sleep(0.3)
25     assert_equal 0, @t.events.size
26   end
27   
28   def test_timer_should_remove_only_expired_events
29     w = Watch.new
30     @t.schedule(stub(:interval => -1, :watch => w))
31     @t.schedule(stub(:interval => 1000, :watch => w))
32     sleep(0.3)
33     assert_equal 1, @t.events.size
34   end
35   
36   def test_timer_should_sort_timer_events
37     w = Watch.new
38     @t.schedule(stub(:interval => 1000, :watch => w))
39     @t.schedule(stub(:interval => 800, :watch => w))
40     @t.schedule(stub(:interval => 900, :watch => w))
41     @t.schedule(stub(:interval => 100, :watch => w))
42     sleep(0.3)
43     assert_equal [100, 800, 900, 1000], @t.events.map { |x| x.condition.interval }
44   end
45   
46   def test_unschedule_should_remove_conditions
47     w = Watch.new
48     a = stub(:watch => w)
49     b = stub(:watch => w)
50     @t.schedule(a, 100)
51     @t.schedule(b, 200)
52     assert_equal 2, @t.events.size
53     @t.unschedule(a)
54     assert_equal 1, @t.events.size
55   end
56   
57   def test_time_should_recover_from_exceptions
58     w = Watch.new
59     @t.expects(:trigger).raises(Exception.new)
60     no_stdout do
61       @t.schedule(stub(:interval => -1, :watch => w))
62       sleep(0.3)
63       @t.schedule(stub(:interval => 0, :watch => w))
64     end
65   end
66   
67   # join
68   
69   def test_join_should_join
70     Thread.any_instance.expects(:join)
71     @t.join
72   end
73 end