Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (106 sloc) 3.075 kb
40f1335 @phiggins Test cleanup.
phiggins authored
1 require 'em_test_helper'
9d5b7ca migrated version_0 to trunk
blackhedd authored
2
3 class TestProcesses < Test::Unit::TestCase
4
40f1335 @phiggins Test cleanup.
phiggins authored
5 if !windows? && !jruby?
6
7 # EM::DeferrableChildProcess is a sugaring of a common use-case
8 # involving EM::popen.
9 # Call the #open method on EM::DeferrableChildProcess, passing
10 # a command-string. #open immediately returns an EM::Deferrable
11 # object. It also schedules the forking of a child process, which
12 # will execute the command passed to #open.
13 # When the forked child terminates, the Deferrable will be signalled
14 # and execute its callbacks, passing the data that the child process
15 # wrote to stdout.
16 #
17 def test_deferrable_child_process
18 ls = ""
19 EM.run {
20 d = EM::DeferrableChildProcess.open( "ls -ltr" )
21 d.callback {|data_from_child|
22 ls = data_from_child
23 EM.stop
24 }
a8f3960 @tmm1 Convert tabs to spaces in tests/test_*
tmm1 authored
25 }
40f1335 @phiggins Test cleanup.
phiggins authored
26 assert( ls.length > 0)
27 end
9d5b7ca migrated version_0 to trunk
blackhedd authored
28
40f1335 @phiggins Test cleanup.
phiggins authored
29 def setup
30 $out = nil
31 $status = nil
32 end
f264498 @tmm1 Add EM.system wrapper for EM::popen (#89)
tmm1 authored
33
40f1335 @phiggins Test cleanup.
phiggins authored
34 def test_em_system
35 EM.run{
36 EM.system('ls'){ |out,status| $out, $status = out, status; EM.stop }
37 }
f264498 @tmm1 Add EM.system wrapper for EM::popen (#89)
tmm1 authored
38
40f1335 @phiggins Test cleanup.
phiggins authored
39 assert( $out.length > 0 )
40 assert_equal($status.exitstatus, 0)
41 assert_equal($status.class, Process::Status)
42 end
f264498 @tmm1 Add EM.system wrapper for EM::popen (#89)
tmm1 authored
43
40f1335 @phiggins Test cleanup.
phiggins authored
44 def test_em_system_pid
45 $pids = []
9b7a76d EM.system returns the process pid now
Joern Barthel authored
46
40f1335 @phiggins Test cleanup.
phiggins authored
47 EM.run{
48 $pids << EM.system('echo hi', proc{ |out,status|$pids << status.pid; EM.stop })
49 }
9b7a76d EM.system returns the process pid now
Joern Barthel authored
50
40f1335 @phiggins Test cleanup.
phiggins authored
51 assert_equal $pids[0], $pids[1]
52 end
53
54 def test_em_system_with_proc
55 EM.run{
56 EM.system('ls', proc{ |out,status| $out, $status = out, status; EM.stop })
57 }
9b7a76d EM.system returns the process pid now
Joern Barthel authored
58
40f1335 @phiggins Test cleanup.
phiggins authored
59 assert( $out.length > 0 )
60 assert_equal($status.exitstatus, 0)
61 assert_equal($status.class, Process::Status)
62 end
63
64 def test_em_system_with_two_procs
65 EM.run{
66 EM.system('sh', proc{ |process|
67 process.send_data("echo hello\n")
68 process.send_data("exit\n")
69 }, proc{ |out,status|
70 $out = out
71 $status = status
72 EM.stop
73 })
74 }
f264498 @tmm1 Add EM.system wrapper for EM::popen (#89)
tmm1 authored
75
40f1335 @phiggins Test cleanup.
phiggins authored
76 assert_equal("hello\n", $out)
77 end
78
79 def test_em_system_cmd_arguments
80 EM.run{
918a901 @nahi Warning cleanups for rake test
nahi authored
81 EM.system('echo', '1', '2', 'version', proc{ |process|
40f1335 @phiggins Test cleanup.
phiggins authored
82 }, proc{ |out,status|
83 $out = out
84 $status = status
85 EM.stop
86 })
87 }
f264498 @tmm1 Add EM.system wrapper for EM::popen (#89)
tmm1 authored
88
918a901 @nahi Warning cleanups for rake test
nahi authored
89 assert_match(/1 2 version/i, $out)
40f1335 @phiggins Test cleanup.
phiggins authored
90 end
e3c02c9 @tmm1 EM.system can take multiple arguments for the command
tmm1 authored
91
40f1335 @phiggins Test cleanup.
phiggins authored
92 def test_em_system_spaced_arguments
93 EM.run{
94 EM.system('ruby', '-e', 'puts "hello"', proc{ |out,status|
95 $out = out
96 EM.stop
97 })
98 }
e3c02c9 @tmm1 EM.system can take multiple arguments for the command
tmm1 authored
99
40f1335 @phiggins Test cleanup.
phiggins authored
100 assert_equal("hello\n", $out)
101 end
eae9baa @rtomayko test EM::Connection#pause on popen connections
rtomayko authored
102
103 def test_em_popen_pause_resume
104 c_rx = 0
105
106 test_client = Module.new do
107 define_method :receive_data do |data|
108 c_rx += 1
109 pause
110 EM.add_timer(0.5) { EM.stop }
111 end
112 end
113
114 EM.run{
115 EM.popen('cat /dev/random', test_client)
116 }
117
118 assert_equal 1, c_rx
119 end
40f1335 @phiggins Test cleanup.
phiggins authored
120 else
121 warn "EM.popen not implemented, skipping tests in #{__FILE__}"
e3c02c9 @tmm1 EM.system can take multiple arguments for the command
tmm1 authored
122
40f1335 @phiggins Test cleanup.
phiggins authored
123 # Because some rubies will complain if a TestCase class has no tests
124 def test_em_popen_unsupported
125 assert true
126 end
f264498 @tmm1 Add EM.system wrapper for EM::popen (#89)
tmm1 authored
127 end
9d5b7ca migrated version_0 to trunk
blackhedd authored
128 end
Something went wrong with that request. Please try again.