github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/sync/waitgroup_test.go (about)

     1  package sync_test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  // TestWaitGroupUncontended tests the wait group from a single goroutine.
     9  func TestWaitGroupUncontended(t *testing.T) {
    10  	// Check that a single add-and-done works.
    11  	var wg sync.WaitGroup
    12  	wg.Add(1)
    13  	wg.Done()
    14  	wg.Wait()
    15  
    16  	// Check that mixing positive and negative counts works.
    17  	wg.Add(10)
    18  	wg.Add(-8)
    19  	wg.Add(-1)
    20  	wg.Add(0)
    21  	wg.Done()
    22  	wg.Wait()
    23  }
    24  
    25  // TestWaitGroup tests the typical usage of WaitGroup.
    26  func TestWaitGroup(t *testing.T) {
    27  	const n = 5
    28  	var wg sync.WaitGroup
    29  	wg.Add(n)
    30  	for i := 0; i < n; i++ {
    31  		go wg.Done()
    32  	}
    33  
    34  	wg.Wait()
    35  }