mirror of
https://github.com/docker/compose.git
synced 2026-11-18 22:27:03 +00:00
test(executor): make the wait-slot regression test deterministic and portable
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / ci-ok (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
docs-upstream / docs-yaml (push) Waiting to run
docs-upstream / validate (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / ci-ok (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
docs-upstream / docs-yaml (push) Waiting to run
docs-upstream / validate (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
The sibling's start now only returns once the wait has polled its dependency, so without the fix the plan deadlocks whichever node takes the single slot first, instead of depending on the scheduler. It also uses the platform-neutral newTestService helper, so the file builds on Windows. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
a366252aeb
commit
507ca3aa4e
1 changed files with 73 additions and 60 deletions
|
|
@ -18,8 +18,7 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -33,71 +32,85 @@ import (
|
|||
)
|
||||
|
||||
// TestExecutePlanWaitNodesHoldNoConcurrencySlot verifies that a wait node does
|
||||
// not occupy a --parallel slot while it polls. Each pair below is a dependency
|
||||
// that is already running, so the plan has no start node for its wait to
|
||||
// depend on, and that only becomes healthy once a sibling container has been
|
||||
// started: the wait for it and that sibling's start are independent nodes. With a
|
||||
// single slot, a wait that took it first would starve the start it is waiting
|
||||
// on and only end at its timeout. Several pairs are used so the nodes racing
|
||||
// for the slot make that outcome all but certain without the fix: it takes
|
||||
// every start winning the slot before any wait does.
|
||||
// not occupy a --parallel slot while it polls. The dependency below is already
|
||||
// running, so the plan has no start node for its wait to depend on, and it only
|
||||
// becomes healthy once a sibling container has been started: the wait for it
|
||||
// and that sibling's start are independent nodes. With a single slot, a wait
|
||||
// that took it would starve the start it is waiting on and only end at its
|
||||
// timeout.
|
||||
//
|
||||
// The test does not rely on which node wins the slot: the sibling's start only
|
||||
// returns once the wait has polled the dependency. Whichever node takes the
|
||||
// only slot first without the fix, the other can never run, and the plan runs
|
||||
// into the deadline. With the fix the wait polls without a slot, which lets the
|
||||
// start return.
|
||||
func TestExecutePlanWaitNodesHoldNoConcurrencySlot(t *testing.T) {
|
||||
const pairs = 5
|
||||
|
||||
svc, apiClient, _ := newStartPhaseTestService(t)
|
||||
svc, apiClient := newTestService(t)
|
||||
svc.maxConcurrency = 1
|
||||
|
||||
plan := &Plan{}
|
||||
exec := svc.newPlanExecutor(&types.Project{Name: "test"}, emptyObservedState("test"), nil)
|
||||
const (
|
||||
dep = "dep"
|
||||
depID = "dep-id"
|
||||
siblingID = "sibling-id"
|
||||
)
|
||||
|
||||
for i := range pairs {
|
||||
dep := fmt.Sprintf("dep%d", i)
|
||||
depID := dep + "-id"
|
||||
siblingID := fmt.Sprintf("sibling%d-id", i)
|
||||
|
||||
var siblingStarted atomic.Bool
|
||||
apiClient.EXPECT().ContainerStart(gomock.Any(), siblingID, gomock.Any()).
|
||||
DoAndReturn(func(context.Context, string, client.ContainerStartOptions) (client.ContainerStartResult, error) {
|
||||
siblingStarted.Store(true)
|
||||
var (
|
||||
polled = make(chan struct{})
|
||||
polledOnce sync.Once
|
||||
siblingStarted = make(chan struct{})
|
||||
)
|
||||
apiClient.EXPECT().ContainerStart(gomock.Any(), siblingID, gomock.Any()).
|
||||
DoAndReturn(func(ctx context.Context, _ string, _ client.ContainerStartOptions) (client.ContainerStartResult, error) {
|
||||
select {
|
||||
case <-polled:
|
||||
close(siblingStarted)
|
||||
return client.ContainerStartResult{}, nil
|
||||
})
|
||||
apiClient.EXPECT().ContainerInspect(gomock.Any(), depID, gomock.Any()).
|
||||
DoAndReturn(func(context.Context, string, client.ContainerInspectOptions) (client.ContainerInspectResult, error) {
|
||||
health := container.Starting
|
||||
if siblingStarted.Load() {
|
||||
health = container.Healthy
|
||||
}
|
||||
return client.ContainerInspectResult{Container: container.InspectResponse{
|
||||
ID: depID,
|
||||
Name: "/test-" + dep + "-1",
|
||||
State: &container.State{
|
||||
Status: container.StateRunning,
|
||||
Health: &container.Health{Status: health},
|
||||
},
|
||||
Config: &container.Config{Healthcheck: &container.HealthConfig{Test: []string{"CMD", "true"}}},
|
||||
}}, nil
|
||||
}).AnyTimes()
|
||||
case <-ctx.Done():
|
||||
return client.ContainerStartResult{}, ctx.Err()
|
||||
}
|
||||
})
|
||||
apiClient.EXPECT().ContainerInspect(gomock.Any(), depID, gomock.Any()).
|
||||
DoAndReturn(func(context.Context, string, client.ContainerInspectOptions) (client.ContainerInspectResult, error) {
|
||||
polledOnce.Do(func() { close(polled) })
|
||||
health := container.Starting
|
||||
select {
|
||||
case <-siblingStarted:
|
||||
health = container.Healthy
|
||||
default:
|
||||
}
|
||||
return client.ContainerInspectResult{Container: container.InspectResponse{
|
||||
ID: depID,
|
||||
Name: "/test-" + dep + "-1",
|
||||
State: &container.State{
|
||||
Status: container.StateRunning,
|
||||
Health: &container.Health{Status: health},
|
||||
},
|
||||
Config: &container.Config{Healthcheck: &container.HealthConfig{Test: []string{"CMD", "true"}}},
|
||||
}}, nil
|
||||
}).AnyTimes()
|
||||
|
||||
exec.containersByService[dep] = Containers{{
|
||||
ID: depID,
|
||||
Names: []string{"/test-" + dep + "-1"},
|
||||
Labels: map[string]string{api.ServiceLabel: dep, api.OneoffLabel: "False"},
|
||||
}}
|
||||
plan.addNode(Operation{
|
||||
Type: OpWaitCondition,
|
||||
ResourceID: "wait:" + dep,
|
||||
Name: dep,
|
||||
Condition: types.ServiceConditionHealthy,
|
||||
}, "")
|
||||
plan.addNode(Operation{
|
||||
Type: OpStartContainer,
|
||||
ResourceID: fmt.Sprintf("service:sibling%d:1", i),
|
||||
Container: &container.Summary{ID: siblingID, Names: []string{fmt.Sprintf("/test-sibling%d-1", i)}},
|
||||
}, "")
|
||||
}
|
||||
exec := svc.newPlanExecutor(&types.Project{Name: "test"}, emptyObservedState("test"), nil)
|
||||
exec.containersByService[dep] = Containers{{
|
||||
ID: depID,
|
||||
Names: []string{"/test-" + dep + "-1"},
|
||||
Labels: map[string]string{api.ServiceLabel: dep, api.OneoffLabel: "False"},
|
||||
}}
|
||||
|
||||
// the deadline stands in for a wait timeout: without the fix the starved
|
||||
// waits only end when it expires
|
||||
plan := &Plan{}
|
||||
plan.addNode(Operation{
|
||||
Type: OpWaitCondition,
|
||||
ResourceID: "wait:" + dep,
|
||||
Name: dep,
|
||||
Condition: types.ServiceConditionHealthy,
|
||||
}, "")
|
||||
plan.addNode(Operation{
|
||||
Type: OpStartContainer,
|
||||
ResourceID: "service:sibling:1",
|
||||
Container: &container.Summary{ID: siblingID, Names: []string{"/test-sibling-1"}},
|
||||
}, "")
|
||||
|
||||
// the deadline stands in for a wait timeout: without the fix the plan only
|
||||
// ends when it expires
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
assert.NilError(t, exec.run(ctx, plan))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue