root/trunk/tests/test_next_tick.rb

Revision 697, 2.5 kB (checked in by francis, 1 year ago)

Supported nested calls to EM#run. Many people contributed ideas to this,
notably raggi and tmm1.

  • Property svn:keywords set to Id
Line 
1 # $Id$
2 #
3 # Author:: Francis Cianfrocca (gmail: blackhedd)
4 # Homepage::  http://rubyeventmachine.com
5 # Date:: 8 April 2006
6 #
7 # See EventMachine and EventMachine::Connection for documentation and
8 # usage examples.
9 #
10 #----------------------------------------------------------------------------
11 #
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13 # Gmail: blackhedd
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of either: 1) the GNU General Public License
17 # as published by the Free Software Foundation; either version 2 of the
18 # License, or (at your option) any later version; or 2) Ruby's License.
19 #
20 # See the file COPYING for complete licensing information.
21 #
22 #---------------------------------------------------------------------------
23 #
24 #
25 #
26 #
27
28 $:.unshift "../lib"
29 require 'eventmachine'
30 require 'test/unit'
31
32
33
34 class TestNextTick < Test::Unit::TestCase
35
36         def setup
37         end
38
39         def teardown
40         end
41
42         def test_tick_arg
43                 pr = proc {EM.stop}
44                 EM.epoll
45                 EM.run {
46                         EM.next_tick pr
47                 }
48                 assert true
49         end
50
51         def test_tick_block
52                 EM.epoll
53                 EM.run {
54                         EM.next_tick {EM.stop}
55                 }
56                 assert true
57         end
58
59         # This illustrates the solution to a long-standing problem.
60         # It's now possible to correctly nest calls to EM#run.
61         # See the source code commentary for EM#run for more info.
62         #
63         def test_run_run
64                 EM.run {
65                         EM.run {
66                                 EM.next_tick {EM.stop}
67                         }
68                 }
69         end
70
71         # We now support an additional parameter for EM#run.
72         # You can pass two procs to EM#run now. The first is executed as the normal
73         # run block. The second (if given) is scheduled for execution after the
74         # reactor loop completes.
75         # The reason for supporting this is subtle. There has always been an expectation
76         # that EM#run doesn't return until after the reactor loop ends. But now it's
77         # possible to nest calls to EM#run, which means that a nested call WILL
78         # RETURN. In order to write code that will run correctly either way, it's
79         # recommended to put any code which must execute after the reactor completes
80         # in the second parameter.
81         #
82         def test_run_run_2
83                 a = proc {EM.stop}
84                 b = proc {assert true}
85                 EM.run a, b
86         end
87
88
89         # This illustrates that EM#run returns when it's called nested.
90         # This isn't a feature, rather it's something to be wary of when writing code
91         # that must run correctly even if EM#run is called while a reactor is already
92         # running.
93         def test_run_run_3
94                 a = []
95                 EM.run {
96                         EM.run proc {EM.stop}, proc {a << 2}
97                         a << 1
98                 }
99                 assert_equal( [1,2], a )
100         end
101
102 end
Note: See TracBrowser for help on using the browser.