Skip to content

[test] Fix parallel limiter unlocking before ws stopped #18839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions test/pkg/integration/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,29 @@ func LaunchWorkspaceWithOptions(t *testing.T, ctx context.Context, opts *LaunchW

func stopWsF(t *testing.T, instanceID string, workspaceID string, api *ComponentAPI, isPrebuild bool) StopWorkspaceFunc {
var already bool
return func(waitForStop bool, api *ComponentAPI) (*wsmanapi.WorkspaceStatus, error) {
var unlocked bool
return func(waitForStop bool, api *ComponentAPI) (s *wsmanapi.WorkspaceStatus, err error) {
if already {
t.Logf("already sent stop request: %s", instanceID)
return nil, nil
}
already = true

tryUnlockParallelLimiter := func() {
if !unlocked {
unlocked = true
<-parallelLimiter
}
}

var err error
defer func() {
if already {
if err == nil {
return
} else {
<-parallelLimiter
}
already = true

// Only unlock on error here, otherwise we'll unlock below
// after waiting for the workspace to stop.
tryUnlockParallelLimiter()
}()

sctx, scancel := context.WithTimeout(context.Background(), perCallTimeout)
Expand Down Expand Up @@ -528,11 +537,8 @@ func stopWsF(t *testing.T, instanceID string, workspaceID string, api *Component
break
}

if !waitForStop {
return nil, nil
}

for {
waitAndUnlock := func() (*wsmanapi.WorkspaceStatus, error) {
defer tryUnlockParallelLimiter()
select {
case err := <-errCh:
return nil, err
Expand All @@ -541,6 +547,19 @@ func stopWsF(t *testing.T, instanceID string, workspaceID string, api *Component
return s, nil
}
}

if !waitForStop {
// Still wait for stop asynchroniously to unblock the parallelLimiter
go func() {
_, err = waitAndUnlock()
if err != nil {
t.Logf("error while waiting asynchronously for workspace to stop: %v", err)
}
}()
return nil, nil
}

return waitAndUnlock()
}
}

Expand Down Expand Up @@ -698,7 +717,7 @@ func WaitForWorkspaceStart(t *testing.T, ctx context.Context, instanceID string,
return nil, false, nil
}
if !cfg.CanFail {
return nil, true, xerrors.New("the workspace couldn't be found")
return nil, true, xerrors.Errorf("the workspace %s couldn't be found", instanceID)
}
return nil, true, nil
}
Expand Down