File: test_tick_loop.rb

package info (click to toggle)
ruby-eventmachine 1.0.3-6%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,000 kB
  • ctags: 3,178
  • sloc: ruby: 8,641; cpp: 5,217; java: 827; makefile: 5
file content (59 lines) | stat: -rw-r--r-- 1,374 bytes parent folder | download | duplicates (4)
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
require "test/unit"
require 'em_test_helper'

class TestEmTickLoop < Test::Unit::TestCase
  def test_em_tick_loop
    i = 0
    EM.tick_loop { i += 1; EM.stop if i == 10 }
    EM.run { EM.add_timer(1) { EM.stop } }
    assert_equal i, 10
  end
  
  def test_tick_loop_on_stop
    t = nil
    tick_loop = EM.tick_loop { :stop }
    tick_loop.on_stop { t = true }
    EM.run { EM.next_tick { EM.stop } }
    assert t
  end
  
  def test_start_twice
    i = 0
    s = 0
    tick_loop = EM.tick_loop { i += 1; :stop }
    tick_loop.on_stop { s += 1; EM.stop }
    EM.run { EM.next_tick { EM.stop } }
    assert_equal 1, i
    assert_equal 1, s
    tick_loop.start
    EM.run { EM.next_tick { EM.stop } }
    assert_equal 2, i
    assert_equal 1, s # stop callbacks are only called once
  end
  
  def test_stop
    i, s = 0, 0
    tick_loop = EM.tick_loop { i += 1 }
    tick_loop.on_stop { s += 1 }
    EM.run { EM.next_tick { tick_loop.stop; EM.next_tick { EM.stop } } }
    assert tick_loop.stopped?
    assert_equal 1, i
    assert_equal 1, s
  end
  
  def test_immediate_stops
    s = 0
    tick_loop = EM::TickLoop.new { }
    tick_loop.on_stop { s += 1 }
    tick_loop.on_stop { s += 1 }
    assert_equal 2, s
  end
  
  def test_stopped
    tick_loop = EM::TickLoop.new { }
    assert tick_loop.stopped?
    tick_loop.start
    assert !tick_loop.stopped?
  end
    
end