Skip to content

Commit aff73bf

Browse files
committed
More container tests.
1 parent 453292b commit aff73bf

File tree

2 files changed

+94
-57
lines changed

2 files changed

+94
-57
lines changed

examples/isolate.rb

Lines changed: 0 additions & 38 deletions
This file was deleted.

fixtures/async/container/a_container.rb

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,66 @@ module Container
1010
AContainer = Sus::Shared("a container") do
1111
let(:container) {subject.new}
1212

13-
it "can run concurrently" do
14-
input, output = IO.pipe
15-
16-
container.async do
17-
output.write "Hello World"
13+
with "#run" do
14+
it "can run several instances concurrently" do
15+
container.run do
16+
sleep(1)
17+
end
18+
19+
expect(container).to be(:running?)
20+
21+
container.stop(true)
22+
23+
expect(container).not.to be(:running?)
1824
end
1925

20-
container.wait
21-
22-
output.close
23-
expect(input.read).to be == "Hello World"
26+
it "can stop an uncooperative child process" do
27+
container.run do
28+
while true
29+
begin
30+
sleep(1)
31+
rescue Interrupt
32+
# Ignore.
33+
end
34+
end
35+
end
36+
37+
expect(container).to be(:running?)
38+
39+
# TODO Investigate why without this, the interrupt can occur before the process is sleeping...
40+
sleep 0.001
41+
42+
container.stop(true)
43+
44+
expect(container).not.to be(:running?)
45+
end
2446
end
2547

26-
it "can run concurrently" do
27-
container.async(name: "Sleepy Jerry") do |task, instance|
28-
3.times do |i|
29-
instance.name = "Counting Sheep #{i}"
30-
31-
sleep 0.01
48+
with "#async" do
49+
it "can run concurrently" do
50+
input, output = IO.pipe
51+
52+
container.async do
53+
output.write "Hello World"
3254
end
55+
56+
container.wait
57+
58+
output.close
59+
expect(input.read).to be == "Hello World"
3360
end
3461

35-
container.wait
62+
it "can run concurrently" do
63+
container.async(name: "Sleepy Jerry") do |task, instance|
64+
3.times do |i|
65+
instance.name = "Counting Sheep #{i}"
66+
67+
sleep 0.01
68+
end
69+
end
70+
71+
container.wait
72+
end
3673
end
3774

3875
it "should be blocking" do
@@ -66,7 +103,7 @@ module Container
66103
end
67104

68105
with "#stop" do
69-
it "can stop the child process" do
106+
it "can gracefully stop the child process" do
70107
container.spawn do
71108
sleep(1)
72109
rescue Interrupt
@@ -75,10 +112,48 @@ module Container
75112

76113
expect(container).to be(:running?)
77114

78-
# TODO Investigate why without this, the interrupt can occur before the process is sleeping...
115+
# See above.
79116
sleep 0.001
80117

81-
container.stop
118+
container.stop(true)
119+
120+
expect(container).not.to be(:running?)
121+
end
122+
123+
it "can forcefully stop the child process" do
124+
container.spawn do
125+
sleep(1)
126+
rescue Interrupt
127+
# Ignore.
128+
end
129+
130+
expect(container).to be(:running?)
131+
132+
# See above.
133+
sleep 0.001
134+
135+
container.stop(false)
136+
137+
expect(container).not.to be(:running?)
138+
end
139+
140+
it "can stop an uncooperative child process" do
141+
container.spawn do
142+
while true
143+
begin
144+
sleep(1)
145+
rescue Interrupt
146+
# Ignore.
147+
end
148+
end
149+
end
150+
151+
expect(container).to be(:running?)
152+
153+
# See above.
154+
sleep 0.001
155+
156+
container.stop(true)
82157

83158
expect(container).not.to be(:running?)
84159
end

0 commit comments

Comments
 (0)