Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw committed Dec 20, 2023
1 parent 5e94264 commit 35f7065
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/tmthrgd/httputils v0.0.0-20190904060602-27fdf7d93acd // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/streadway/quantile v0.0.0-20220407130108-4246515d968d h1:X4+kt6zM/OVO6gbJdAfJR60MGPsqCzbtXNnjoGqdfAs=
github.com/streadway/quantile v0.0.0-20220407130108-4246515d968d/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 4 additions & 0 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
//
// Use [New] to get a valid Waitgroup
type WaitGroup struct {
mu sync.Mutex
wg sync.WaitGroup
cancel context.CancelCauseFunc

Expand Down Expand Up @@ -121,7 +122,10 @@ func (w *WaitGroup) Go(funcs ...func() error) error {
}
}(f)
}

w.mu.Lock()
w.wg.Wait()
w.mu.Unlock()
}

if w.cancel != nil {
Expand Down
76 changes: 42 additions & 34 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/sourcegraph/conc"
"golang.org/x/sync/errgroup"

"go.akshayshah.org/attest"
Expand Down Expand Up @@ -109,42 +110,25 @@ func TestErrGroup(t *testing.T) {
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)
// }
{
wgLimited, _ := errgroup.WithContext(context.Background())
wgLimited.SetLimit(1)

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
},
)
}
wgUnlimited, _ := errgroup.WithContext(context.Background())

go func() {
wgUnlimited.Go(func() error {
Expand All @@ -160,3 +144,27 @@ func TestErrGroup(t *testing.T) {
}
})
}

func TestConc(t *testing.T) {
t.Parallel()

t.Run("concurrency", func(t *testing.T) {
t.Parallel()

{
wgLimited := conc.NewWaitGroup()

go func() {
wgLimited.Go(func() {
return

Check failure on line 159 in sync/sync_test.go

View workflow job for this annotation

GitHub Actions / run_analysis (>=1.21.5, ubuntu-22.04)

redundant return statement (S1023)
})
wgLimited.Wait()
}()
wgLimited.Go(func() {
return

Check failure on line 164 in sync/sync_test.go

View workflow job for this annotation

GitHub Actions / run_analysis (>=1.21.5, ubuntu-22.04)

redundant return statement (S1023)
})

wgLimited.Wait()
}
})
}

0 comments on commit 35f7065

Please sign in to comment.