Skip to content

Commit a5afbd2

Browse files
authored
[test] Fix parallel limiter unlocking before ws stopped (#18839)
1 parent ead0ba0 commit a5afbd2

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

test/pkg/integration/workspace.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,29 @@ func LaunchWorkspaceWithOptions(t *testing.T, ctx context.Context, opts *LaunchW
474474

475475
func stopWsF(t *testing.T, instanceID string, workspaceID string, api *ComponentAPI, isPrebuild bool) StopWorkspaceFunc {
476476
var already bool
477-
return func(waitForStop bool, api *ComponentAPI) (*wsmanapi.WorkspaceStatus, error) {
477+
var unlocked bool
478+
return func(waitForStop bool, api *ComponentAPI) (s *wsmanapi.WorkspaceStatus, err error) {
478479
if already {
479480
t.Logf("already sent stop request: %s", instanceID)
480481
return nil, nil
481482
}
483+
already = true
484+
485+
tryUnlockParallelLimiter := func() {
486+
if !unlocked {
487+
unlocked = true
488+
<-parallelLimiter
489+
}
490+
}
482491

483-
var err error
484492
defer func() {
485-
if already {
493+
if err == nil {
486494
return
487-
} else {
488-
<-parallelLimiter
489495
}
490-
already = true
496+
497+
// Only unlock on error here, otherwise we'll unlock below
498+
// after waiting for the workspace to stop.
499+
tryUnlockParallelLimiter()
491500
}()
492501

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

531-
if !waitForStop {
532-
return nil, nil
533-
}
534-
535-
for {
540+
waitAndUnlock := func() (*wsmanapi.WorkspaceStatus, error) {
541+
defer tryUnlockParallelLimiter()
536542
select {
537543
case err := <-errCh:
538544
return nil, err
@@ -541,6 +547,19 @@ func stopWsF(t *testing.T, instanceID string, workspaceID string, api *Component
541547
return s, nil
542548
}
543549
}
550+
551+
if !waitForStop {
552+
// Still wait for stop asynchroniously to unblock the parallelLimiter
553+
go func() {
554+
_, err = waitAndUnlock()
555+
if err != nil {
556+
t.Logf("error while waiting asynchronously for workspace to stop: %v", err)
557+
}
558+
}()
559+
return nil, nil
560+
}
561+
562+
return waitAndUnlock()
544563
}
545564
}
546565

@@ -698,7 +717,7 @@ func WaitForWorkspaceStart(t *testing.T, ctx context.Context, instanceID string,
698717
return nil, false, nil
699718
}
700719
if !cfg.CanFail {
701-
return nil, true, xerrors.New("the workspace couldn't be found")
720+
return nil, true, xerrors.Errorf("the workspace %s couldn't be found", instanceID)
702721
}
703722
return nil, true, nil
704723
}

0 commit comments

Comments
 (0)