forked from mer-tools/sdk-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_process_spec.rb
148 lines (112 loc) · 3.41 KB
/
shell_process_spec.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'minitest/autorun'
require './shell_process.rb'
describe ShellProcess do
describe "initialized with properly terminating command" do
describe "only" do
it "must return ShellProcess object" do
ShellProcess.new("echo -n xxx").must_be_kind_of ShellProcess
end
it "correctly reports Z and non-Z status of command" do
process = ShellProcess.new("sleep 0.1")
non_zombie_cycles = 0
non_zombie_cycles += 1 while process.status[0] != "Z"
non_zombie_cycles.must_be :>, 0
process.status[0].must_equal "Z"
end
it "correctly reports exitstatus" do
ShellProcess.new("true").wait.exitstatus.must_equal 0
ShellProcess.new("false").wait.exitstatus.must_equal 1
end
it "allows nonblock-reading command's stdout" do
process = ShellProcess.new("echo -n xxx")
process.wait
process.stdout_read(timeout: 0).must_equal "xxx"
end
it "allows nonblock-reading command's stderr" do
process = ShellProcess.new("echo -n xxx 1>&2")
process.wait
process.stderr_read(timeout: 0).must_equal "xxx"
end
it "allows block-reading command's stdout" do
process = ShellProcess.new("echo -n xxx")
process.stdout_read(timeout: 4).must_equal "xxx"
end
it "allows checking roughly how much time passed since process start" do
t1 = Time.new
process = ShellProcess.new("sleep 0.1")
process.wait
d = Time.new - t1
process.runtime.must_be_close_to d, 0.1
end
it "allows terminating process" do
process = ShellProcess.new("sleep 10")
process.kill
process.status[0].must_equal "Z"
end
it "allows killing process" do
process = ShellProcess.new("sleep 10")
process.kill(9)
process.status[0].must_equal "Z"
end
it "allows closing all it's std pipes" do
process = ShellProcess.new("sleep 10")
process.close
process.stdout.closed?.must_equal true
process.stderr.closed?.must_equal true
process.stdin.closed?.must_equal true
end
it "can be reaped" do
process = ShellProcess.new("echo xxx")
sleep 0.5
start = Time.now
process.reap
stop = Time.now
(stop - start).must_be :<, 1
process.wait.exitstatus.must_equal 0
end
end
end
describe "initialized with properly terminating, long running command" do
describe "only" do
it "allows block-reading command's stdout as soon as command ends" do
process = ShellProcess.new("echo -n xxx")
start = Time.now
process.stdout_read(timeout: 4).must_equal "xxx"
stop = Time.now
(stop - start).must_be :<, 2
end
it "allows block-reading command's stdout, blocking not more for timeout" do
process = ShellProcess.new("echo -n xxx; sleep 2; echo -n yyy")
start = Time.now
process.stdout_read(timeout: 1).must_equal "xxx"
stop = Time.now
(stop - start).must_be :<, 1.5
(stop - start).must_be :>, 0.5
end
it "can be killed and reaped" do
process = ShellProcess.new("sleep 10")
start = Time.now
process.reap
stop = Time.now
(stop - start).must_be :<, 1
process.wait.termsig.must_equal 9
end
end
end
describe "initialized with command and" do
describe "non-hash argument" do
it "must raise exception" do
assert_raises ArgumentError do
ShellProcess.new("",1)
end
end
end
end
describe "initialized without command" do
it "must raise exception" do
assert_raises ArgumentError do
ShellProcess.new()
end
end
end
end