MCPcopy
hub / github.com/moby/moby / TestStopContainerWithTimeoutCancel

Function TestStopContainerWithTimeoutCancel

integration/container/stop_linux_test.go:19–66  ·  view source on GitHub ↗

TestStopContainerWithTimeoutCancel checks that ContainerStop is not cancelled if the request is cancelled. See issue https://github.com/moby/moby/issues/45731

(t *testing.T)

Source from the content-addressed store, hash-verified

17// if the request is cancelled.
18// See issue https://github.com/moby/moby/issues/45731
19func TestStopContainerWithTimeoutCancel(t *testing.T) {
20 ctx := setupTest(t)
21 apiClient := testEnv.APIClient()
22 t.Cleanup(func() { _ = apiClient.Close() })
23
24 t.Parallel()
25
26 id := container.Run(ctx, t, apiClient,
27 container.WithCmd("sh", "-c", "trap 'echo received TERM' TERM; echo ready; while true; do usleep 10; done"),
28 )
29 poll.WaitOn(t, logsContains(ctx, apiClient, id, "ready"))
30
31 ctxCancel, cancel := context.WithCancel(ctx)
32 t.Cleanup(cancel)
33 const stopTimeout = 3
34
35 stoppedCh := make(chan error)
36 go func() {
37 sto := stopTimeout
38 _, err := apiClient.ContainerStop(ctxCancel, id, client.ContainerStopOptions{Timeout: &sto})
39 stoppedCh <- err
40 }()
41
42 poll.WaitOn(t, logsContains(ctx, apiClient, id, "received TERM"))
43
44 // Cancel the context once we verified the container was signaled, and check
45 // that the container is not killed immediately
46 cancel()
47
48 select {
49 case stoppedErr := <-stoppedCh:
50 assert.Check(t, is.ErrorType(stoppedErr, cerrdefs.IsCanceled))
51 case <-time.After(5 * time.Second):
52 t.Fatal("timeout waiting for stop request to be cancelled")
53 }
54 inspect, err := apiClient.ContainerInspect(ctx, id, client.ContainerInspectOptions{})
55 assert.Check(t, err)
56 assert.Check(t, inspect.Container.State.Running)
57
58 // container should be stopped after stopTimeout is reached. The daemon.containerStop
59 // code is rather convoluted, and waits another 2 seconds for the container to
60 // terminate after signaling it;
61 // https://github.com/moby/moby/blob/97455cc31ffa08078db6591f018256ed59c35bbc/daemon/stop.go#L101-L112
62 //
63 // Adding 3 seconds to the specified stopTimeout to take this into account,
64 // and add another second margin to try to avoid flakiness.
65 poll.WaitOn(t, container.IsStopped(ctx, apiClient, id), poll.WithTimeout((3+stopTimeout)*time.Second))
66}

Callers

nothing calls this directly

Calls 12

RunFunction · 0.92
WithCmdFunction · 0.92
IsStoppedFunction · 0.92
logsContainsFunction · 0.85
APIClientMethod · 0.80
CheckMethod · 0.80
WithTimeoutMethod · 0.80
setupTestFunction · 0.70
CleanupMethod · 0.65
CloseMethod · 0.65
ContainerStopMethod · 0.65
ContainerInspectMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…