diff --git a/pkg/compose/executor_wait_slot_test.go b/pkg/compose/executor_wait_slot_test.go index 678dd83db..1f0a80d26 100644 --- a/pkg/compose/executor_wait_slot_test.go +++ b/pkg/compose/executor_wait_slot_test.go @@ -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))