go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/sync/waitgroup_test.go (about)

     1  // Copyright (c) 2022 Matt Way
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to
     5  // deal in the Software without restriction, including without limitation the
     6  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     7  // sell copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    18  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    19  // IN THE THE SOFTWARE.
    20  
    21  package sync_test
    22  
    23  import (
    24  	"math"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/require"
    29  	"go.mway.dev/x/sync"
    30  )
    31  
    32  func TestWaitGroupAddInc(t *testing.T) {
    33  	var (
    34  		target = math.MaxInt16
    35  		add    sync.WaitGroup
    36  		inc    sync.WaitGroup
    37  	)
    38  
    39  	add.Add(target)
    40  
    41  	for i := 0; i < target; i++ {
    42  		inc.Inc()
    43  	}
    44  
    45  	require.Equal(t, target, add.Len())
    46  	require.Equal(t, add.Len(), inc.Len())
    47  }
    48  
    49  func TestWaitGroupDone(t *testing.T) {
    50  	var (
    51  		target = math.MaxInt16
    52  		wg     sync.WaitGroup
    53  	)
    54  
    55  	require.NotPanics(t, func() {
    56  		wg.Add(target)
    57  		for i := 0; i < target; i++ {
    58  			wg.Done()
    59  			require.Equal(t, target-i-1, wg.Len())
    60  		}
    61  		require.Equal(t, 0, wg.Len())
    62  	})
    63  }
    64  
    65  func TestWaitGroupWait(t *testing.T) {
    66  	var wg sync.WaitGroup
    67  	wg.Add(5)
    68  
    69  	go func() {
    70  		ticker := time.NewTicker(50 * time.Millisecond)
    71  		defer ticker.Stop()
    72  
    73  		for wg.Len() > 0 {
    74  			<-ticker.C
    75  			wg.Done()
    76  		}
    77  	}()
    78  
    79  	done := make(chan struct{})
    80  	go func() {
    81  		defer close(done)
    82  		wg.Wait()
    83  	}()
    84  
    85  	select {
    86  	case <-done:
    87  		require.Equal(t, 0, wg.Len())
    88  	case <-time.After(time.Second):
    89  		require.FailNow(t, "WaitGroup did not unblock")
    90  	}
    91  }
    92  
    93  func TestWaitGroupLen(t *testing.T) {
    94  	var wg sync.WaitGroup
    95  	require.Equal(t, 0, wg.Len())
    96  
    97  	for i := 0; i < 1000; i++ {
    98  		wg.Add(1)
    99  		require.Equal(t, i+1, wg.Len())
   100  	}
   101  
   102  	for wg.Len() > 0 {
   103  		prev := wg.Len()
   104  		wg.Done()
   105  		require.Equal(t, prev-1, wg.Len())
   106  	}
   107  
   108  	require.NotPanics(t, func() {
   109  		wg.Add(math.MaxInt32)
   110  	})
   111  
   112  	require.Panics(t, func() {
   113  		wg.Add(1)
   114  	})
   115  }