From 7c360f72a8e89fd205fe98aa2a585be4f75a8b41 Mon Sep 17 00:00:00 2001 From: ajatprabha Date: Tue, 16 Jul 2024 12:40:03 +0530 Subject: [PATCH 1/2] deps: update vulnerable dependencies and use std error.Join --- .github/workflows/test.yaml | 2 +- Makefile | 8 ++++---- README.md | 2 ++ go.mod | 10 +++------- go.sum | 17 ++++++----------- manager.go | 14 ++++++-------- manager_test.go | 12 +++--------- 7 files changed, 25 insertions(+), 40 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f8c1de2..b5649a7 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [1.19.x, 1.20.x, 1.21.x, 1.22.x] + go-version: [1.21.x, 1.22.x] steps: - name: Install Go uses: actions/setup-go@v4 diff --git a/Makefile b/Makefile index cc6aa87..45ce310 100644 --- a/Makefile +++ b/Makefile @@ -42,16 +42,16 @@ test-xml: test-cov gocov-xml # ========= Helpers =========== golangci-lint: - $(call install-if-needed,GOLANGCI_LINT,github.com/golangci/golangci-lint/cmd/golangci-lint,v1.53.3) + $(call install-if-needed,GOLANGCI_LINT,github.com/golangci/golangci-lint/cmd/golangci-lint,v1.59.1) gci: - $(call install-if-needed,GCI_BIN,github.com/daixiang0/gci,v0.10.1) + $(call install-if-needed,GCI_BIN,github.com/daixiang0/gci,v0.13.4) gocov: - $(call install-if-needed,GOCOV,github.com/axw/gocov/gocov,v1.0.0) + $(call install-if-needed,GOCOV,github.com/axw/gocov/gocov,v1.1.0) gocov-xml: - $(call install-if-needed,GOCOVXML,github.com/AlekSi/gocov-xml,v1.0.0) + $(call install-if-needed,GOCOVXML,github.com/AlekSi/gocov-xml,v1.1.0) is-available = $(if $(wildcard $(LOCAL_GO_BIN_DIR)/$(1)),$(LOCAL_GO_BIN_DIR)/$(1),$(if $(shell command -v $(1) 2> /dev/null),yes,no)) diff --git a/README.md b/README.md index 33a81a7..8814e6d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ $ go get github.com/gojekfarm/xrun ## Usage +> Minimum Required Go Version: 1.20.x + - [API reference][api-docs] - [Blog post explaining motivation behind xrun][blog-link] - [Reddit post][reddit-link] diff --git a/go.mod b/go.mod index bf9d536..e26f873 100644 --- a/go.mod +++ b/go.mod @@ -2,14 +2,10 @@ module github.com/gojekfarm/xrun go 1.19 -require ( - github.com/hashicorp/go-multierror v1.1.1 - github.com/stretchr/testify v1.7.0 -) +require github.com/stretchr/testify v1.9.0 require ( - github.com/davecgh/go-spew v1.1.0 // indirect - github.com/hashicorp/errwrap v1.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index abce31d..60ce688 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,10 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= -github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= -github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/manager.go b/manager.go index 51936fa..72a7294 100644 --- a/manager.go +++ b/manager.go @@ -6,8 +6,6 @@ import ( "fmt" "sync" "time" - - "github.com/hashicorp/go-multierror" ) // NewManager creates a Manager and applies provided Option @@ -116,9 +114,9 @@ func (m *Manager) engageStopProcedure() error { defer m.mu.Unlock() m.stopping = true - var retErr *multierror.Error + var retErr error - retErrCh := make(chan *multierror.Error, 1) + retErrCh := make(chan error, 1) go m.aggregateErrors(retErrCh) go func() { @@ -136,7 +134,7 @@ func (m *Manager) engageStopProcedure() error { return fmt.Errorf("not all components were shutdown completely within grace period(%s): %w", m.shutdownTimeout, err) } - return retErr.ErrorOrNil() + return retErr } func (m *Manager) cancelFunc() context.CancelFunc { @@ -150,10 +148,10 @@ func (m *Manager) cancelFunc() context.CancelFunc { return shutdownCancel } -func (m *Manager) aggregateErrors(ch chan<- *multierror.Error) { - var r *multierror.Error +func (m *Manager) aggregateErrors(ch chan<- error) { + var r error for err := range m.errChan { - r = multierror.Append(r, err) + r = errors.Join(r, err) } ch <- r } diff --git a/manager_test.go b/manager_test.go index 04edd45..35a913b 100644 --- a/manager_test.go +++ b/manager_test.go @@ -120,10 +120,7 @@ func (s *ManagerSuite) TestNewManager() { { name: "ShutdownWhenOneComponentReturnsErrorOnExit", wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { - return assert.EqualError(t, err, `1 error occurred: - * shutdown error - -`, i...) + return assert.EqualError(t, err, "shutdown error", i...) }, components: []Component{ ComponentFunc(func(ctx context.Context) error { @@ -143,11 +140,8 @@ func (s *ManagerSuite) TestNewManager() { { name: "ShutdownWhenMoreThanOneComponentReturnsErrorOnExit", wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { - return assert.EqualError(t, err, `2 errors occurred: - * shutdown error 2 - * shutdown error 1 - -`, i...) + return assert.EqualError(t, err, `shutdown error 2 +shutdown error 1`, i...) }, components: []Component{ ComponentFunc(func(ctx context.Context) error { From 364a20de707d9beedef4fba61060f9b19092b6a5 Mon Sep 17 00:00:00 2001 From: ajatprabha Date: Tue, 16 Jul 2024 13:23:12 +0530 Subject: [PATCH 2/2] specs: make tests run faster --- component/http_server_test.go | 2 ++ manager_test.go | 47 +++++++++++++++++------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/component/http_server_test.go b/component/http_server_test.go index 7b51e48..943cc50 100644 --- a/component/http_server_test.go +++ b/component/http_server_test.go @@ -123,6 +123,8 @@ func (s *HTTPServerSuite) TestHTTPServer() { } else { s.NoError(<-errCh) } + + time.Sleep(50 * time.Millisecond) // for goroutine to exit }) } } diff --git a/manager_test.go b/manager_test.go index 35a913b..7af1c00 100644 --- a/manager_test.go +++ b/manager_test.go @@ -35,7 +35,7 @@ func (s *ManagerSuite) TestNewManager() { wantErr: assert.NoError, components: []Component{ ComponentFunc(func(ctx context.Context) error { - time.Sleep(3 * time.Second) + time.Sleep(300 * time.Millisecond) <-ctx.Done() return nil }), @@ -52,13 +52,13 @@ func (s *ManagerSuite) TestNewManager() { }, { name: "WithGracefulShutdownErrorOnOneComponent", - options: []Option{ShutdownTimeout(5 * time.Second)}, + options: []Option{ShutdownTimeout(time.Second)}, wantErr: assert.Error, components: []Component{ ComponentFunc(func(ctx context.Context) error { - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) <-ctx.Done() - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) return nil }), ComponentFunc(func(ctx context.Context) error { @@ -74,15 +74,15 @@ func (s *ManagerSuite) TestNewManager() { wantErr: assert.NoError, components: []Component{ ComponentFunc(func(ctx context.Context) error { - time.Sleep(5 * time.Second) + time.Sleep(500 * time.Millisecond) <-ctx.Done() - time.Sleep(5 * time.Second) + time.Sleep(500 * time.Millisecond) return nil }), ComponentFunc(func(ctx context.Context) error { - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) <-ctx.Done() - time.Sleep(10 * time.Second) + time.Sleep(time.Second) return nil }), }, @@ -92,9 +92,8 @@ func (s *ManagerSuite) TestNewManager() { wantErr: assert.NoError, components: []Component{ ComponentFunc(func(ctx context.Context) error { - time.Sleep(5 * time.Second) <-ctx.Done() - time.Sleep(5 * time.Second) + time.Sleep(2 * time.Second) return nil }), }, @@ -104,15 +103,15 @@ func (s *ManagerSuite) TestNewManager() { wantErr: assert.NoError, components: []Component{ ComponentFunc(func(ctx context.Context) error { - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) <-ctx.Done() - time.Sleep(2 * time.Second) + time.Sleep(200 * time.Millisecond) return nil }), ComponentFunc(func(ctx context.Context) error { - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) <-ctx.Done() - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) return ctx.Err() }), }, @@ -124,15 +123,15 @@ func (s *ManagerSuite) TestNewManager() { }, components: []Component{ ComponentFunc(func(ctx context.Context) error { - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) <-ctx.Done() - time.Sleep(2 * time.Second) + time.Sleep(200 * time.Millisecond) return nil }), ComponentFunc(func(ctx context.Context) error { - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) <-ctx.Done() - time.Sleep(time.Second) + time.Sleep(100 * time.Millisecond) return errors.New("shutdown error") }), }, @@ -146,17 +145,17 @@ shutdown error 1`, i...) components: []Component{ ComponentFunc(func(ctx context.Context) error { <-ctx.Done() - time.Sleep(2 * time.Second) + time.Sleep(200 * time.Millisecond) return nil }), ComponentFunc(func(ctx context.Context) error { <-ctx.Done() - time.Sleep(3 * time.Second) + time.Sleep(300 * time.Millisecond) return errors.New("shutdown error 1") }), ComponentFunc(func(ctx context.Context) error { <-ctx.Done() - time.Sleep(2 * time.Second) + time.Sleep(200 * time.Millisecond) return errors.New("shutdown error 2") }), }, @@ -178,7 +177,7 @@ shutdown error 1`, i...) errCh <- m.Run(ctx) }() - time.Sleep(1 * time.Second) + time.Sleep(300 * time.Millisecond) cancel() t.wantErr(s.T(), <-errCh) @@ -196,7 +195,7 @@ func (s *ManagerSuite) TestAddNewComponentAfterStop() { errCh <- m.Run(ctx) }() - time.Sleep(1 * time.Second) + time.Sleep(100 * time.Millisecond) cancel() s.NoError(<-errCh) @@ -216,7 +215,7 @@ func (s *ManagerSuite) TestAddNewComponentAfterStart() { errCh <- m.Run(ctx) }() - time.Sleep(1 * time.Second) + time.Sleep(100 * time.Millisecond) s.EqualError(m.Add(ComponentFunc(func(ctx context.Context) error { return nil