diff --git a/go.mod b/go.mod index baaa947..77f745f 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/cespare/xxhash v1.1.0 github.com/charmbracelet/glamour v0.6.0 github.com/docker/go-units v0.5.0 + github.com/fortytw2/leaktest v1.3.0 github.com/google/go-github/v30 v30.1.0 github.com/google/uuid v1.3.1 github.com/hdm/jarm-go v0.0.7 diff --git a/go.sum b/go.sum index aedd1aa..1d82ad7 100644 --- a/go.sum +++ b/go.sum @@ -55,6 +55,8 @@ github.com/ebitengine/purego v0.4.0 h1:RQVuMIxQPQ5iCGEJvjQ17YOK+1tMKjVau2FUMvXH4 github.com/ebitengine/purego v0.4.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw= +github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= diff --git a/sync/adaptivewaitgroup_test.go b/sync/adaptivewaitgroup_test.go index 11cb7f4..7463df6 100644 --- a/sync/adaptivewaitgroup_test.go +++ b/sync/adaptivewaitgroup_test.go @@ -5,6 +5,7 @@ import ( "sync/atomic" "testing" + "github.com/fortytw2/leaktest" "github.com/stretchr/testify/require" ) @@ -91,3 +92,51 @@ func TestAddWithContext(t *testing.T) { } } + +func TestMultipleResizes(t *testing.T) { + var c atomic.Int32 + swg, err := New(WithSize(2)) // Start with a size of 2 + require.Nil(t, err) + + for i := 0; i < 10000; i++ { + if i == 250 { + swg.Resize(context.TODO(), 5) // Increase size at 2500th iteration + } + if i == 500 { + swg.Resize(context.TODO(), 1) // Decrease size at 5000th iteration + } + if i == 750 { + swg.Resize(context.TODO(), 3) // Increase size again at 7500th iteration + } + + swg.Add() + go func() { + defer swg.Done() + c.Add(1) + }() + } + + swg.Wait() + if c.Load() != 10000 { + t.Fatalf("%d, not all routines have been executed.", c.Load()) + } +} + +func Test_AdaptiveWaitGroup_Leak(t *testing.T) { + defer leaktest.Check(t)() + + for j := 0; j < 1000; j++ { + wg, err := New(WithSize(10)) + if err != nil { + t.Fatal(err) + } + + for i := 0; i < 10000; i++ { + wg.Add() + go func(awg *AdaptiveWaitGroup) { + defer awg.Done() + }(wg) + } + wg.Wait() + } +}