forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_next_tick.rb
133 lines (119 loc) · 3.15 KB
/
test_next_tick.rb
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
# $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 TestNextTick < Test::Unit::TestCase
def test_tick_arg
pr = proc {EM.stop}
EM.run {
EM.next_tick pr
}
assert true
end
def test_tick_block
EM.run {
EM.next_tick {EM.stop}
}
assert true
end
# This illustrates the solution to a long-standing problem.
# It's now possible to correctly nest calls to EM#run.
# See the source code commentary for EM#run for more info.
#
def test_run_run
EM.run {
EM.run {
EM.next_tick {EM.stop}
}
}
end
def test_pre_run_queue
x = false
EM.next_tick { EM.stop; x = true }
EM.run { EM.add_timer(0.01) { EM.stop } }
assert x
end
def test_cleanup_after_stop
x = true
EM.run{
EM.next_tick{
EM.stop
EM.next_tick{ x=false }
}
}
EM.run{
EM.next_tick{ EM.stop }
}
assert x
end
# We now support an additional parameter for EM#run.
# You can pass two procs to EM#run now. The first is executed as the normal
# run block. The second (if given) is scheduled for execution after the
# reactor loop completes.
# The reason for supporting this is subtle. There has always been an expectation
# that EM#run doesn't return until after the reactor loop ends. But now it's
# possible to nest calls to EM#run, which means that a nested call WILL
# RETURN. In order to write code that will run correctly either way, it's
# recommended to put any code which must execute after the reactor completes
# in the second parameter.
#
def test_run_run_2
a = proc {EM.stop}
b = proc {assert true}
EM.run a, b
end
# This illustrates that EM#run returns when it's called nested.
# This isn't a feature, rather it's something to be wary of when writing code
# that must run correctly even if EM#run is called while a reactor is already
# running.
def test_run_run_3
a = []
EM.run {
EM.run proc {EM.stop}, proc {a << 2}
a << 1
}
assert_equal( [1,2], a )
end
def test_schedule_on_reactor_thread
x = false
EM.run do
EM.schedule { x = true }
EM.stop
end
assert x
end
def test_schedule_from_thread
x = false
EM.run do
Thread.new { EM.schedule { x = true } }.join
assert !x
EM.next_tick { EM.stop }
end
assert x
end
end