@@ -129,13 +129,12 @@ func waitFor(t *C, addr string) (err error) {
129129
130130func (t * GoofysTest ) deleteBlobsParallelly (cloud StorageBackend , blobs []string ) error {
131131 const concurrency = 10
132- sem := make (semaphore , concurrency )
133- sem .P (concurrency )
132+ sem := NewSemaphore (concurrency )
134133 var err error
135134 for _ , blobOuter := range blobs {
136- sem .V (1 )
135+ sem .P (1 ) // Acquire slot
137136 go func (blob string ) {
138- defer sem .P (1 )
137+ defer sem .V (1 ) // Release slot
139138 _ , localerr := cloud .DeleteBlob (& DeleteBlobInput {blob })
140139 if localerr != nil && localerr != syscall .ENOENT {
141140 err = localerr
@@ -145,7 +144,9 @@ func (t *GoofysTest) deleteBlobsParallelly(cloud StorageBackend, blobs []string)
145144 break
146145 }
147146 }
148- sem .V (concurrency )
147+ // Wait for all goroutines to complete
148+ sem .P (concurrency )
149+ sem .V (concurrency ) // Release them back
149150 return err
150151}
151152
@@ -373,12 +374,11 @@ func (s *GoofysTest) removeBlob(cloud StorageBackend, t *C, blobPath string) {
373374
374375func (s * GoofysTest ) setupBlobs (cloud StorageBackend , t * C , env map [string ]* string ) {
375376 const concurrency = 10
376- throttler := make (semaphore , concurrency )
377- throttler .P (concurrency )
377+ throttler := NewSemaphore (concurrency )
378378
379379 var globalErr atomic.Value
380380 for path , c := range env {
381- throttler .V (1 )
381+ throttler .P (1 ) // Acquire slot before spawning goroutine
382382 go func (path string , content * string ) {
383383 dir := false
384384 if content == nil {
@@ -392,7 +392,7 @@ func (s *GoofysTest) setupBlobs(cloud StorageBackend, t *C, env map[string]*stri
392392 content = & path
393393 }
394394 }
395- defer throttler .P (1 )
395+ defer throttler .V (1 ) // Release slot when goroutine completes
396396 params := & PutBlobInput {
397397 Key : path ,
398398 Body : bytes .NewReader ([]byte (* content )),
@@ -410,19 +410,20 @@ func (s *GoofysTest) setupBlobs(cloud StorageBackend, t *C, env map[string]*stri
410410 t .Assert (err , IsNil )
411411 }(path , c )
412412 }
413- throttler .V (concurrency )
414- throttler = make (semaphore , concurrency )
413+ // Wait for all goroutines to complete by acquiring all slots
415414 throttler .P (concurrency )
415+ // Release them back
416+ throttler .V (concurrency )
416417 t .Assert (globalErr .Load (), IsNil )
417418
418419 // double check, except on AWS S3, because there we sometimes
419420 // hit 404 NoSuchBucket and there's no way to distinguish that
420421 // from 404 KeyNotFound
421422 if ! hasEnv ("AWS" ) {
422423 for path , c := range env {
423- throttler .V (1 )
424+ throttler .P (1 ) // Acquire slot
424425 go func (path string , content * string ) {
425- defer throttler .P (1 )
426+ defer throttler .V (1 ) // Release slot
426427 params := & HeadBlobInput {Key : path }
427428 res , err := cloud .HeadBlob (params )
428429 if err != nil {
@@ -442,7 +443,9 @@ func (s *GoofysTest) setupBlobs(cloud StorageBackend, t *C, env map[string]*stri
442443 }
443444 }(path , c )
444445 }
445- throttler .V (concurrency )
446+ // Wait for all goroutines to complete
447+ throttler .P (concurrency )
448+ throttler .V (concurrency ) // Release them back
446449 t .Assert (globalErr .Load (), IsNil )
447450 }
448451}
0 commit comments