File: test_timers.rb

package info (click to toggle)
libeventmachine-ruby 0.12.10-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,276 kB
  • ctags: 2,268
  • sloc: ruby: 8,499; cpp: 5,427; java: 1,325; makefile: 13
file content (160 lines) | stat: -rw-r--r-- 3,503 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# $Id$
#
# Author:: Francis Cianfrocca (gmail: blackhedd)
# Homepage::  http://rubyeventmachine.com
# Date:: 8 April 2006
# 
# See EventMachine and EventMachine::Connection for documentation and
# usage examples.
#
#----------------------------------------------------------------------------
#
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
# Gmail: blackhedd
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of either: 1) the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version; or 2) Ruby's License.
# 
# See the file COPYING for complete licensing information.
#
#---------------------------------------------------------------------------
#
#
#
#

$:.unshift "../lib"
require 'eventmachine'
require 'test/unit'

class TestTimers < Test::Unit::TestCase

  def test_timer_with_block
    x = false
    EventMachine.run {
      EventMachine::Timer.new(0.25) {
        x = true
        EventMachine.stop
      }
    }
    assert x
  end

  def test_timer_with_proc
    x = false
    EventMachine.run {
      EventMachine::Timer.new(0.25, proc {
        x = true
        EventMachine.stop
      })
    }
    assert x
  end

  def test_timer_cancel
    x = true
    EventMachine.run {
      timer = EventMachine::Timer.new(0.25, proc { x = false })
      timer.cancel
      EventMachine::Timer.new(0.5, proc {EventMachine.stop})
    }
    assert x
  end

  def test_periodic_timer
    x = 0
    EventMachine.run {
      EventMachine::PeriodicTimer.new(0.1) do
        x += 1
        EventMachine.stop if x == 4
      end
    }
    assert( x == 4 )
  end

  def test_add_periodic_timer
    x = 0
    EM.run {
      t = EM.add_periodic_timer(0.1) do
        x += 1
        EM.stop  if x == 4
      end
      assert t.respond_to?(:cancel)
    }
  end

  def test_periodic_timer_cancel
    x = 0
    EventMachine.run {
      pt = EventMachine::PeriodicTimer.new(0.25, proc { x += 1 })
      pt.cancel
      EventMachine::Timer.new(0.5) {EventMachine.stop}
    }
    assert( x == 0 )
  end

  def test_add_periodic_timer_cancel
    x = 0
    EventMachine.run {
      pt = EM.add_periodic_timer(0.1) { x += 1 }
      EM.cancel_timer(pt)
      EM.add_timer(0.2) { EM.stop }
    }
    assert( x == 0 )
  end

  def test_periodic_timer_self_cancel
    x = 0
    EventMachine.run {
      pt = EventMachine::PeriodicTimer.new(0.1) {
        x += 1
        if x == 4
          pt.cancel
          EventMachine.stop
        end
      }
    }
    assert( x == 4 )
  end


  # This test is only applicable to compiled versions of the reactor.
  # Pure ruby and java versions have no built-in limit on the number of outstanding timers.
  #
  def test_timer_change_max_outstanding
    defaults = EM.get_max_timers
    EM.set_max_timers(100)

    one_hundred_one_timers = proc { 101.times { EM.add_timer(5) {} } }

    EM.run {
      if EM.library_type == :pure_ruby
        one_hundred_one_timers.call
      elsif EM.library_type == :java
        one_hundred_one_timers.call
      else
        begin
          assert_raises( RuntimeError ) {
            one_hundred_one_timers.call
          }
        rescue Object
          p $!
          assert(false, $!.message)
        end
      end
      EM.stop
    }

    EM.set_max_timers( 101 )

    EM.run {
      one_hundred_one_timers.call
      EM.stop
    }
  ensure
    EM.set_max_timers(defaults)
  end

end