Skip to content
Newer
Older
100644 105 lines (91 sloc) 2.31 KB
40f1335 @phiggins Test cleanup.
phiggins authored Dec 31, 2010
1 require 'em_test_helper'
9d5b7ca migrated version_0 to trunk
blackhedd authored Jan 4, 2008
2
3 class TestNextTick < Test::Unit::TestCase
4
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
5 def test_tick_arg
6 pr = proc {EM.stop}
7 EM.run {
8 EM.next_tick pr
9 }
10 assert true
11 end
9d5b7ca migrated version_0 to trunk
blackhedd authored Jan 4, 2008
12
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
13 def test_tick_block
14 EM.run {
15 EM.next_tick {EM.stop}
16 }
17 assert true
18 end
9d5b7ca migrated version_0 to trunk
blackhedd authored Jan 4, 2008
19
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
20 # This illustrates the solution to a long-standing problem.
21 # It's now possible to correctly nest calls to EM#run.
22 # See the source code commentary for EM#run for more info.
23 #
24 def test_run_run
25 EM.run {
26 EM.run {
27 EM.next_tick {EM.stop}
28 }
29 }
30 end
9d5b7ca migrated version_0 to trunk
blackhedd authored Jan 4, 2008
31
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
32 def test_pre_run_queue
33 x = false
34 EM.next_tick { EM.stop; x = true }
3008ae6 @raggi Tighten timeouts and so on for faster tests, 90s - 25s
raggi authored Feb 4, 2010
35 EM.run { EM.add_timer(0.01) { EM.stop } }
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
36 assert x
37 end
46e8d9c Supported nested calls to EM#run. Many people contributed ideas to this,
francis authored Jun 15, 2008
38
4e42455 @tmm1 Clean up next_tick_queue on reactor shutdown
tmm1 authored Apr 25, 2009
39 def test_cleanup_after_stop
40 x = true
41 EM.run{
42 EM.next_tick{
43 EM.stop
44 EM.next_tick{ x=false }
45 }
46 }
47 EM.run{
48 EM.next_tick{ EM.stop }
49 }
50 assert x
51 end
52
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
53 # We now support an additional parameter for EM#run.
54 # You can pass two procs to EM#run now. The first is executed as the normal
55 # run block. The second (if given) is scheduled for execution after the
56 # reactor loop completes.
57 # The reason for supporting this is subtle. There has always been an expectation
58 # that EM#run doesn't return until after the reactor loop ends. But now it's
59 # possible to nest calls to EM#run, which means that a nested call WILL
60 # RETURN. In order to write code that will run correctly either way, it's
61 # recommended to put any code which must execute after the reactor completes
62 # in the second parameter.
63 #
64 def test_run_run_2
65 a = proc {EM.stop}
66 b = proc {assert true}
67 EM.run a, b
68 end
46e8d9c Supported nested calls to EM#run. Many people contributed ideas to this,
francis authored Jun 15, 2008
69
4b49054 @raggi Allow next_tick to be used to pre-schedule reactor operations before …
raggi authored Feb 25, 2009
70
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
71 # This illustrates that EM#run returns when it's called nested.
72 # This isn't a feature, rather it's something to be wary of when writing code
73 # that must run correctly even if EM#run is called while a reactor is already
74 # running.
75 def test_run_run_3
76 a = []
77 EM.run {
78 EM.run proc {EM.stop}, proc {a << 2}
79 a << 1
80 }
81 assert_equal( [1,2], a )
82 end
46e8d9c Supported nested calls to EM#run. Many people contributed ideas to this,
francis authored Jun 15, 2008
83
84
953cf4a @raggi Added EM.schedule, EM::Channel and EM::Queue
raggi authored Mar 19, 2009
85 def test_schedule_on_reactor_thread
86 x = false
87 EM.run do
88 EM.schedule { x = true }
89 EM.stop
90 end
91 assert x
92 end
93
94 def test_schedule_from_thread
95 x = false
96 EM.run do
97 Thread.new { EM.schedule { x = true } }.join
98 assert !x
99 EM.next_tick { EM.stop }
100 end
101 assert x
102 end
46e8d9c Supported nested calls to EM#run. Many people contributed ideas to this,
francis authored Jun 15, 2008
103
9d5b7ca migrated version_0 to trunk
blackhedd authored Jan 4, 2008
104 end
Something went wrong with that request. Please try again.