@@ -10,29 +10,66 @@ module Container
10
10
AContainer = Sus ::Shared ( "a container" ) do
11
11
let ( :container ) { subject . new }
12
12
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? )
18
24
end
19
25
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
24
46
end
25
47
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"
32
54
end
55
+
56
+ container . wait
57
+
58
+ output . close
59
+ expect ( input . read ) . to be == "Hello World"
33
60
end
34
61
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
36
73
end
37
74
38
75
it "should be blocking" do
@@ -66,7 +103,7 @@ module Container
66
103
end
67
104
68
105
with "#stop" do
69
- it "can stop the child process" do
106
+ it "can gracefully stop the child process" do
70
107
container . spawn do
71
108
sleep ( 1 )
72
109
rescue Interrupt
@@ -75,10 +112,48 @@ module Container
75
112
76
113
expect ( container ) . to be ( :running? )
77
114
78
- # TODO Investigate why without this, the interrupt can occur before the process is sleeping.. .
115
+ # See above .
79
116
sleep 0.001
80
117
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 )
82
157
83
158
expect ( container ) . not . to be ( :running? )
84
159
end
0 commit comments