From ce0c72b30d8bce7dc6bdb537d13c13a6582b37c6 Mon Sep 17 00:00:00 2001 From: Rintaro Okamura Date: Sat, 13 Jun 2020 16:15:34 +0900 Subject: [PATCH] internal/db/storage/blob/s3: remove ctx from struct (#473) * :recycle: Remove context from writer struct Signed-off-by: Rintaro Okamura * :white_check_mark: fix tests Signed-off-by: Rintaro Okamura * :green_heart: Fix CI for agent-sidecar Signed-off-by: Rintaro Okamura --- .../workflows/dockers-agent-sidecar-image.yml | 4 +-- internal/db/storage/blob/s3/writer/writer.go | 12 +++---- .../db/storage/blob/s3/writer/writer_test.go | 31 ++++++------------- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/.github/workflows/dockers-agent-sidecar-image.yml b/.github/workflows/dockers-agent-sidecar-image.yml index c25dea2cc4..e5c7cd574d 100644 --- a/.github/workflows/dockers-agent-sidecar-image.yml +++ b/.github/workflows/dockers-agent-sidecar-image.yml @@ -11,7 +11,7 @@ on: paths: - 'internal/**' - '!internal/db/**' - - 'internal/db/storage/blob' + - 'internal/db/storage/blob/**' - '!internal/k8s/**' - 'apis/grpc/**' - 'pkg/agent/sidecar/**' @@ -23,7 +23,7 @@ on: paths: - 'internal/**' - '!internal/db/**' - - 'internal/db/storage/blob' + - 'internal/db/storage/blob/**' - '!internal/k8s/**' - 'apis/grpc/**' - 'pkg/agent/sidecar/**' diff --git a/internal/db/storage/blob/s3/writer/writer.go b/internal/db/storage/blob/s3/writer/writer.go index ef76fe1798..759cb66e60 100644 --- a/internal/db/storage/blob/s3/writer/writer.go +++ b/internal/db/storage/blob/s3/writer/writer.go @@ -40,8 +40,6 @@ type writer struct { pw io.WriteCloser wg *sync.WaitGroup - - ctx context.Context } type Writer interface { @@ -59,8 +57,6 @@ func New(opts ...Option) Writer { } func (w *writer) Open(ctx context.Context) (err error) { - w.ctx = ctx - w.wg = new(sync.WaitGroup) var pr io.ReadCloser @@ -73,7 +69,7 @@ func (w *writer) Open(ctx context.Context) (err error) { defer w.wg.Done() defer pr.Close() - return w.upload(pr) + return w.upload(ctx, pr) })) return err @@ -92,14 +88,14 @@ func (w *writer) Close() error { } func (w *writer) Write(p []byte) (n int, err error) { - if w.ctx == nil || w.pw == nil { + if w.pw == nil { return 0, errors.ErrStorageWriterNotOpened } return w.pw.Write(p) } -func (w *writer) upload(body io.Reader) (err error) { +func (w *writer) upload(ctx context.Context, body io.Reader) (err error) { uploader := s3manager.NewUploaderWithClient( w.service, func(u *s3manager.Uploader) { @@ -112,7 +108,7 @@ func (w *writer) upload(body io.Reader) (err error) { Body: body, } - res, err := uploader.UploadWithContext(w.ctx, input) + res, err := uploader.UploadWithContext(ctx, input) if err != nil { log.Error("upload failed with error: ", err) return err diff --git a/internal/db/storage/blob/s3/writer/writer_test.go b/internal/db/storage/blob/s3/writer/writer_test.go index 7ad3d6ef90..d35f97b6d9 100644 --- a/internal/db/storage/blob/s3/writer/writer_test.go +++ b/internal/db/storage/blob/s3/writer/writer_test.go @@ -80,7 +80,7 @@ func TestNew(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -112,7 +112,6 @@ func Test_writer_Open(t *testing.T) { maxPartSize int64 pw io.WriteCloser wg *sync.WaitGroup - ctx context.Context } type want struct { err error @@ -148,7 +147,6 @@ func Test_writer_Open(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -171,7 +169,6 @@ func Test_writer_Open(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -182,7 +179,7 @@ func Test_writer_Open(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -200,7 +197,6 @@ func Test_writer_Open(t *testing.T) { maxPartSize: test.fields.maxPartSize, pw: test.fields.pw, wg: test.fields.wg, - ctx: test.fields.ctx, } err := w.Open(test.args.ctx) @@ -221,7 +217,6 @@ func Test_writer_Close(t *testing.T) { maxPartSize int64 pw io.WriteCloser wg *sync.WaitGroup - ctx context.Context } type want struct { err error @@ -253,7 +248,6 @@ func Test_writer_Close(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -273,7 +267,6 @@ func Test_writer_Close(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -284,7 +277,7 @@ func Test_writer_Close(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc() } @@ -302,7 +295,6 @@ func Test_writer_Close(t *testing.T) { maxPartSize: test.fields.maxPartSize, pw: test.fields.pw, wg: test.fields.wg, - ctx: test.fields.ctx, } err := w.Close() @@ -326,7 +318,6 @@ func Test_writer_Write(t *testing.T) { maxPartSize int64 pw io.WriteCloser wg *sync.WaitGroup - ctx context.Context } type want struct { wantN int @@ -366,7 +357,6 @@ func Test_writer_Write(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -389,7 +379,6 @@ func Test_writer_Write(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -400,7 +389,7 @@ func Test_writer_Write(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -418,7 +407,6 @@ func Test_writer_Write(t *testing.T) { maxPartSize: test.fields.maxPartSize, pw: test.fields.pw, wg: test.fields.wg, - ctx: test.fields.ctx, } gotN, err := w.Write(test.args.p) @@ -432,6 +420,7 @@ func Test_writer_Write(t *testing.T) { func Test_writer_upload(t *testing.T) { type args struct { + ctx context.Context body io.Reader } type fields struct { @@ -442,7 +431,6 @@ func Test_writer_upload(t *testing.T) { maxPartSize int64 pw io.WriteCloser wg *sync.WaitGroup - ctx context.Context } type want struct { err error @@ -468,6 +456,7 @@ func Test_writer_upload(t *testing.T) { { name: "test_case_1", args: args { + ctx: nil, body: nil, }, fields: fields { @@ -478,7 +467,6 @@ func Test_writer_upload(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -491,6 +479,7 @@ func Test_writer_upload(t *testing.T) { return test { name: "test_case_2", args: args { + ctx: nil, body: nil, }, fields: fields { @@ -501,7 +490,6 @@ func Test_writer_upload(t *testing.T) { maxPartSize: 0, pw: nil, wg: sync.WaitGroup{}, - ctx: nil, }, want: want{}, checkFunc: defaultCheckFunc, @@ -512,7 +500,7 @@ func Test_writer_upload(t *testing.T) { for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -530,10 +518,9 @@ func Test_writer_upload(t *testing.T) { maxPartSize: test.fields.maxPartSize, pw: test.fields.pw, wg: test.fields.wg, - ctx: test.fields.ctx, } - err := w.upload(test.args.body) + err := w.upload(test.args.ctx, test.args.body) if err := test.checkFunc(test.want, err); err != nil { tt.Errorf("error = %v", err) }