@@ -474,20 +474,29 @@ func LaunchWorkspaceWithOptions(t *testing.T, ctx context.Context, opts *LaunchW
474
474
475
475
func stopWsF (t * testing.T , instanceID string , workspaceID string , api * ComponentAPI , isPrebuild bool ) StopWorkspaceFunc {
476
476
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 ) {
478
479
if already {
479
480
t .Logf ("already sent stop request: %s" , instanceID )
480
481
return nil , nil
481
482
}
483
+ already = true
484
+
485
+ tryUnlockParallelLimiter := func () {
486
+ if ! unlocked {
487
+ unlocked = true
488
+ <- parallelLimiter
489
+ }
490
+ }
482
491
483
- var err error
484
492
defer func () {
485
- if already {
493
+ if err == nil {
486
494
return
487
- } else {
488
- <- parallelLimiter
489
495
}
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 ()
491
500
}()
492
501
493
502
sctx , scancel := context .WithTimeout (context .Background (), perCallTimeout )
@@ -528,11 +537,8 @@ func stopWsF(t *testing.T, instanceID string, workspaceID string, api *Component
528
537
break
529
538
}
530
539
531
- if ! waitForStop {
532
- return nil , nil
533
- }
534
-
535
- for {
540
+ waitAndUnlock := func () (* wsmanapi.WorkspaceStatus , error ) {
541
+ defer tryUnlockParallelLimiter ()
536
542
select {
537
543
case err := <- errCh :
538
544
return nil , err
@@ -541,6 +547,19 @@ func stopWsF(t *testing.T, instanceID string, workspaceID string, api *Component
541
547
return s , nil
542
548
}
543
549
}
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 ()
544
563
}
545
564
}
546
565
@@ -698,7 +717,7 @@ func WaitForWorkspaceStart(t *testing.T, ctx context.Context, instanceID string,
698
717
return nil , false , nil
699
718
}
700
719
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 )
702
721
}
703
722
return nil , true , nil
704
723
}
0 commit comments