@@ -21,6 +21,7 @@ import (
21
21
"io"
22
22
"strings"
23
23
"testing"
24
+ "time"
24
25
25
26
"github.com/coreos/go-iptables/iptables"
26
27
"gotest.tools/v3/assert"
@@ -159,3 +160,39 @@ func TestStopCreated(t *testing.T) {
159
160
160
161
base .Cmd ("stop" , tID ).AssertOK ()
161
162
}
163
+
164
+ func TestStopWithLongTimeoutAndSIGKILL (t * testing.T ) {
165
+ t .Parallel ()
166
+ base := testutil .NewBase (t )
167
+ testContainerName := testutil .Identifier (t )
168
+ defer base .Cmd ("rm" , "-f" , testContainerName ).Run ()
169
+
170
+ // Start a container that sleeps forever
171
+ base .Cmd ("run" , "-d" , "--name" , testContainerName , testutil .CommonImage , "sleep" , "Inf" ).AssertOK ()
172
+
173
+ // Stop the container with a 5-second timeout and SIGKILL
174
+ start := time .Now ()
175
+ base .Cmd ("stop" , "--time=5" , "--signal" , "SIGKILL" , testContainerName ).AssertOK ()
176
+ elapsed := time .Since (start )
177
+
178
+ // The container should be stopped almost immediately, well before the 5-second timeout
179
+ assert .Assert (t , elapsed < 5 * time .Second , "Container wasn't stopped immediately with SIGKILL" )
180
+ }
181
+
182
+ func TestStopWithTimeout (t * testing.T ) {
183
+ t .Parallel ()
184
+ base := testutil .NewBase (t )
185
+ testContainerName := testutil .Identifier (t )
186
+ defer base .Cmd ("rm" , "-f" , testContainerName ).Run ()
187
+
188
+ // Start a container that sleeps forever
189
+ base .Cmd ("run" , "-d" , "--name" , testContainerName , testutil .CommonImage , "sleep" , "Inf" ).AssertOK ()
190
+
191
+ // Stop the container with a 3-second timeout
192
+ start := time .Now ()
193
+ base .Cmd ("stop" , "--time=3" , testContainerName ).AssertOK ()
194
+ elapsed := time .Since (start )
195
+
196
+ // The container should get the SIGKILL before the 10s default timeout
197
+ assert .Assert (t , elapsed < 10 * time .Second , "Container did not respect --timeout flag" )
198
+ }
0 commit comments