From 5e9426414826757c862fb9aa52953e375c1b419e Mon Sep 17 00:00:00 2001 From: $MY_NAME Date: Wed, 20 Dec 2023 16:32:52 +0300 Subject: [PATCH] d --- sync/sync_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/sync/sync_test.go b/sync/sync_test.go index eaa560ee..1289b084 100644 --- a/sync/sync_test.go +++ b/sync/sync_test.go @@ -4,19 +4,19 @@ import ( "context" "testing" + "golang.org/x/sync/errgroup" + "go.akshayshah.org/attest" ) func TestSync(t *testing.T) { t.Parallel() - wgLimited, _ := New(context.Background(), 1) - wgUnlimited, _ := New(context.Background(), -1) - t.Run("zero funcs", func(t *testing.T) { t.Parallel() { + wgLimited, _ := New(context.Background(), 1) count := 0 err := wgLimited.Go() attest.Ok(t, err) @@ -24,6 +24,7 @@ func TestSync(t *testing.T) { } { + wgUnlimited, _ := New(context.Background(), -1) count := 0 err := wgUnlimited.Go() attest.Ok(t, err) @@ -35,6 +36,7 @@ func TestSync(t *testing.T) { t.Parallel() { + wgLimited, _ := New(context.Background(), 1) count := 0 err := wgLimited.Go(func() error { count = count + 1 @@ -45,6 +47,7 @@ func TestSync(t *testing.T) { } { + wgUnlimited, _ := New(context.Background(), -1) count := 0 err := wgUnlimited.Go(func() error { count = count + 1 @@ -58,10 +61,9 @@ func TestSync(t *testing.T) { t.Run("concurrency", func(t *testing.T) { t.Parallel() - wgLimited, _ := New(context.Background(), 1) - wgUnlimited, _ := New(context.Background(), -1) - { + wgLimited, _ := New(context.Background(), 1) + funcs := []func() error{} for i := 0; i <= 4; i++ { funcs = append(funcs, @@ -80,6 +82,8 @@ func TestSync(t *testing.T) { } { + wgUnlimited, _ := New(context.Background(), -1) + funcs := []func() error{} for i := 0; i <= 4; i++ { funcs = append(funcs, @@ -98,3 +102,61 @@ func TestSync(t *testing.T) { } }) } + +func TestErrGroup(t *testing.T) { + t.Parallel() + + t.Run("concurrency", func(t *testing.T) { + t.Parallel() + + // wgLimited, _ := errgroup.WithContext(context.Background()) + // wgLimited.SetLimit(1) + wgUnlimited, _ := errgroup.WithContext(context.Background()) + + // { + // funcs := []func() error{} + // for i := 0; i <= 4; i++ { + // funcs = append(funcs, + // func() error { + // return nil + // }, + // ) + // } + + // go func() { + // wgLimited.Go(func() error { + // return nil + // }) + // }() + // wgLimited.Go(func() error { + // return nil + // }) + + // err := wgLimited.Wait() + // attest.Ok(t, err) + // } + + { + funcs := []func() error{} + for i := 0; i <= 4; i++ { + funcs = append(funcs, + func() error { + return nil + }, + ) + } + + go func() { + wgUnlimited.Go(func() error { + return nil + }) + }() + wgUnlimited.Go(func() error { + return nil + }) + + err := wgUnlimited.Wait() + attest.Ok(t, err) + } + }) +}