From 9406b5e516760973813eafd8f975a64fc4b32a7c Mon Sep 17 00:00:00 2001 From: Tom Berger Date: Wed, 25 Jul 2018 12:51:38 -0400 Subject: [PATCH] Update tests to pass context and expect GetObjectSize in any order --- gcsstore/gcsservice_test.go | 32 ++++++----------- gcsstore/gcsstore_mock_test.go | 65 +++++++++++++++++----------------- gcsstore/gcsstore_test.go | 58 ++++++++++++++++++------------ 3 files changed, 80 insertions(+), 75 deletions(-) diff --git a/gcsstore/gcsservice_test.go b/gcsstore/gcsservice_test.go index b9093ef..ad9573f 100644 --- a/gcsstore/gcsservice_test.go +++ b/gcsstore/gcsservice_test.go @@ -2,7 +2,7 @@ package gcsstore_test import ( "bytes" - "context" + "golang.org/x/net/context" "testing" "gopkg.in/h2non/gock.v1" @@ -47,10 +47,9 @@ func TestGetObjectSize(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - size, err := service.GetObjectSize(GCSObjectParams{ + size, err := service.GetObjectSize(ctx, GCSObjectParams{ Bucket: "test-bucket", ID: "test-name", }) @@ -94,10 +93,9 @@ func TestDeleteObjectWithFilter(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - err = service.DeleteObjectsWithFilter(GCSFilterParams{ + err = service.DeleteObjectsWithFilter(ctx, GCSFilterParams{ Bucket: "test-bucket", Prefix: "test-prefix", }) @@ -180,10 +178,9 @@ func TestComposeObjects(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - err = service.ComposeObjects(GCSComposeParams{ + err = service.ComposeObjects(ctx, GCSComposeParams{ Bucket: "test-bucket", Sources: []string{"test1", "test2", "test3"}, Destination: "test_all", @@ -222,10 +219,9 @@ func TestGetObjectAttrs(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - attrs, err := service.GetObjectAttrs(GCSObjectParams{ + attrs, err := service.GetObjectAttrs(ctx, GCSObjectParams{ Bucket: "test-bucket", ID: "test-name", }) @@ -266,10 +262,9 @@ func TestReadObject(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - reader, err := service.ReadObject(GCSObjectParams{ + reader, err := service.ReadObject(ctx, GCSObjectParams{ Bucket: "test-bucket", ID: "test-name", }) @@ -304,10 +299,9 @@ func TestSetObjectMetadata(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - err = service.SetObjectMetadata(GCSObjectParams{ + err = service.SetObjectMetadata(ctx, GCSObjectParams{ Bucket: "test-bucket", ID: "test-name", }, map[string]string{"test": "metadata", "fake": "test"}) @@ -343,10 +337,9 @@ func TestDeleteObject(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - err = service.DeleteObject(GCSObjectParams{ + err = service.DeleteObject(ctx, GCSObjectParams{ Bucket: "test-bucket", ID: "test-name", }) @@ -376,12 +369,11 @@ func TestWriteObject(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } reader := bytes.NewReader([]byte{1}) - size, err := service.WriteObject(GCSObjectParams{ + size, err := service.WriteObject(ctx, GCSObjectParams{ Bucket: "test-bucket", ID: "test-name", }, reader) @@ -428,10 +420,9 @@ func TestComposeFrom(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - crc, err := service.ComposeFrom([]*storage.ObjectHandle{client.Bucket("test-bucket").Object("my-object")}, GCSObjectParams{ + crc, err := service.ComposeFrom(ctx, []*storage.ObjectHandle{client.Bucket("test-bucket").Object("my-object")}, GCSObjectParams{ Bucket: "test-bucket", ID: "my-object", }, "text") @@ -478,10 +469,9 @@ func TestFilterObject(t *testing.T) { service := GCSService{ Client: client, - Ctx: ctx, } - objects, err := service.FilterObjects(GCSFilterParams{ + objects, err := service.FilterObjects(ctx, GCSFilterParams{ Bucket: "test-bucket", Prefix: "test-prefix", }) diff --git a/gcsstore/gcsstore_mock_test.go b/gcsstore/gcsstore_mock_test.go index 74549ff..10da5b5 100644 --- a/gcsstore/gcsstore_mock_test.go +++ b/gcsstore/gcsstore_mock_test.go @@ -4,6 +4,7 @@ package gcsstore_test import ( + context "context" gomock "github.com/golang/mock/gomock" gcsstore "github.com/tus/tusd/gcsstore" io "io" @@ -102,86 +103,86 @@ func (_m *MockGCSAPI) EXPECT() *_MockGCSAPIRecorder { return _m.recorder } -func (_m *MockGCSAPI) ComposeObjects(_param0 gcsstore.GCSComposeParams) error { - ret := _m.ctrl.Call(_m, "ComposeObjects", _param0) +func (_m *MockGCSAPI) ComposeObjects(_param0 context.Context, _param1 gcsstore.GCSComposeParams) error { + ret := _m.ctrl.Call(_m, "ComposeObjects", _param0, _param1) ret0, _ := ret[0].(error) return ret0 } -func (_mr *_MockGCSAPIRecorder) ComposeObjects(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "ComposeObjects", arg0) +func (_mr *_MockGCSAPIRecorder) ComposeObjects(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ComposeObjects", arg0, arg1) } -func (_m *MockGCSAPI) DeleteObject(_param0 gcsstore.GCSObjectParams) error { - ret := _m.ctrl.Call(_m, "DeleteObject", _param0) +func (_m *MockGCSAPI) DeleteObject(_param0 context.Context, _param1 gcsstore.GCSObjectParams) error { + ret := _m.ctrl.Call(_m, "DeleteObject", _param0, _param1) ret0, _ := ret[0].(error) return ret0 } -func (_mr *_MockGCSAPIRecorder) DeleteObject(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "DeleteObject", arg0) +func (_mr *_MockGCSAPIRecorder) DeleteObject(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "DeleteObject", arg0, arg1) } -func (_m *MockGCSAPI) DeleteObjectsWithFilter(_param0 gcsstore.GCSFilterParams) error { - ret := _m.ctrl.Call(_m, "DeleteObjectsWithFilter", _param0) +func (_m *MockGCSAPI) DeleteObjectsWithFilter(_param0 context.Context, _param1 gcsstore.GCSFilterParams) error { + ret := _m.ctrl.Call(_m, "DeleteObjectsWithFilter", _param0, _param1) ret0, _ := ret[0].(error) return ret0 } -func (_mr *_MockGCSAPIRecorder) DeleteObjectsWithFilter(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "DeleteObjectsWithFilter", arg0) +func (_mr *_MockGCSAPIRecorder) DeleteObjectsWithFilter(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "DeleteObjectsWithFilter", arg0, arg1) } -func (_m *MockGCSAPI) FilterObjects(_param0 gcsstore.GCSFilterParams) ([]string, error) { - ret := _m.ctrl.Call(_m, "FilterObjects", _param0) +func (_m *MockGCSAPI) FilterObjects(_param0 context.Context, _param1 gcsstore.GCSFilterParams) ([]string, error) { + ret := _m.ctrl.Call(_m, "FilterObjects", _param0, _param1) ret0, _ := ret[0].([]string) ret1, _ := ret[1].(error) return ret0, ret1 } -func (_mr *_MockGCSAPIRecorder) FilterObjects(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "FilterObjects", arg0) +func (_mr *_MockGCSAPIRecorder) FilterObjects(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "FilterObjects", arg0, arg1) } -func (_m *MockGCSAPI) GetObjectSize(_param0 gcsstore.GCSObjectParams) (int64, error) { - ret := _m.ctrl.Call(_m, "GetObjectSize", _param0) +func (_m *MockGCSAPI) GetObjectSize(_param0 context.Context, _param1 gcsstore.GCSObjectParams) (int64, error) { + ret := _m.ctrl.Call(_m, "GetObjectSize", _param0, _param1) ret0, _ := ret[0].(int64) ret1, _ := ret[1].(error) return ret0, ret1 } -func (_mr *_MockGCSAPIRecorder) GetObjectSize(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "GetObjectSize", arg0) +func (_mr *_MockGCSAPIRecorder) GetObjectSize(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "GetObjectSize", arg0, arg1) } -func (_m *MockGCSAPI) ReadObject(_param0 gcsstore.GCSObjectParams) (gcsstore.GCSReader, error) { - ret := _m.ctrl.Call(_m, "ReadObject", _param0) +func (_m *MockGCSAPI) ReadObject(_param0 context.Context, _param1 gcsstore.GCSObjectParams) (gcsstore.GCSReader, error) { + ret := _m.ctrl.Call(_m, "ReadObject", _param0, _param1) ret0, _ := ret[0].(gcsstore.GCSReader) ret1, _ := ret[1].(error) return ret0, ret1 } -func (_mr *_MockGCSAPIRecorder) ReadObject(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadObject", arg0) +func (_mr *_MockGCSAPIRecorder) ReadObject(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadObject", arg0, arg1) } -func (_m *MockGCSAPI) SetObjectMetadata(_param0 gcsstore.GCSObjectParams, _param1 map[string]string) error { - ret := _m.ctrl.Call(_m, "SetObjectMetadata", _param0, _param1) +func (_m *MockGCSAPI) SetObjectMetadata(_param0 context.Context, _param1 gcsstore.GCSObjectParams, _param2 map[string]string) error { + ret := _m.ctrl.Call(_m, "SetObjectMetadata", _param0, _param1, _param2) ret0, _ := ret[0].(error) return ret0 } -func (_mr *_MockGCSAPIRecorder) SetObjectMetadata(arg0, arg1 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "SetObjectMetadata", arg0, arg1) +func (_mr *_MockGCSAPIRecorder) SetObjectMetadata(arg0, arg1, arg2 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "SetObjectMetadata", arg0, arg1, arg2) } -func (_m *MockGCSAPI) WriteObject(_param0 gcsstore.GCSObjectParams, _param1 io.Reader) (int64, error) { - ret := _m.ctrl.Call(_m, "WriteObject", _param0, _param1) +func (_m *MockGCSAPI) WriteObject(_param0 context.Context, _param1 gcsstore.GCSObjectParams, _param2 io.Reader) (int64, error) { + ret := _m.ctrl.Call(_m, "WriteObject", _param0, _param1, _param2) ret0, _ := ret[0].(int64) ret1, _ := ret[1].(error) return ret0, ret1 } -func (_mr *_MockGCSAPIRecorder) WriteObject(arg0, arg1 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteObject", arg0, arg1) +func (_mr *_MockGCSAPIRecorder) WriteObject(arg0, arg1, arg2 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteObject", arg0, arg1, arg2) } diff --git a/gcsstore/gcsstore_test.go b/gcsstore/gcsstore_test.go index 45c0545..fbefcc2 100644 --- a/gcsstore/gcsstore_test.go +++ b/gcsstore/gcsstore_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "golang.org/x/net/context" "testing" "cloud.google.com/go/storage" @@ -55,7 +56,8 @@ func TestNewUpload(t *testing.T) { ID: fmt.Sprintf("%s.info", mockID), } - service.EXPECT().WriteObject(params, r).Return(int64(r.Len()), nil) + ctx := context.Background() + service.EXPECT().WriteObject(ctx, params, r).Return(int64(r.Len()), nil) id, err := store.NewUpload(mockTusdInfo) assert.Nil(err) @@ -130,15 +132,19 @@ func TestGetInfo(t *testing.T) { infoR := bytes.NewReader(offsetInfoData) + ctx := context.Background() gomock.InOrder( - service.EXPECT().ReadObject(params).Return(r, nil), - service.EXPECT().FilterObjects(filterParams).Return(mockPartials, nil), - service.EXPECT().GetObjectSize(mockObjectParams0).Return(size, nil), - service.EXPECT().GetObjectSize(mockObjectParams1).Return(size, nil), - service.EXPECT().GetObjectSize(mockObjectParams2).Return(size, nil), - service.EXPECT().WriteObject(params, infoR).Return(int64(len(offsetInfoData)), nil), + service.EXPECT().ReadObject(ctx, params).Return(r, nil), + service.EXPECT().FilterObjects(ctx, filterParams).Return(mockPartials, nil), ) + ctxCancel, _ := context.WithCancel(ctx) + service.EXPECT().GetObjectSize(ctxCancel, mockObjectParams0).Return(size, nil) + service.EXPECT().GetObjectSize(ctxCancel, mockObjectParams1).Return(size, nil) + lastGetObjectSize := service.EXPECT().GetObjectSize(ctxCancel, mockObjectParams2).Return(size, nil) + + service.EXPECT().WriteObject(ctx, params, infoR).Return(int64(len(offsetInfoData)), nil).After(lastGetObjectSize) + info, err := store.GetInfo(mockID) assert.Nil(err) assert.Equal(mockTusdInfo, info) @@ -157,8 +163,9 @@ func TestGetInfoNotFound(t *testing.T) { ID: fmt.Sprintf("%s.info", mockID), } + ctx := context.Background() gomock.InOrder( - service.EXPECT().ReadObject(params).Return(nil, storage.ErrObjectNotExist), + service.EXPECT().ReadObject(ctx, params).Return(nil, storage.ErrObjectNotExist), ) _, err := store.GetInfo(mockID) @@ -205,7 +212,8 @@ func TestGetReader(t *testing.T) { r := MockGetReader{} - service.EXPECT().ReadObject(params).Return(r, nil) + ctx := context.Background() + service.EXPECT().ReadObject(ctx, params).Return(r, nil) reader, err := store.GetReader(mockID) assert.Nil(err) @@ -231,7 +239,8 @@ func TestTerminate(t *testing.T) { Prefix: mockID, } - service.EXPECT().DeleteObjectsWithFilter(filterParams).Return(nil) + ctx := context.Background() + service.EXPECT().DeleteObjectsWithFilter(ctx, filterParams).Return(nil) err := store.Terminate(mockID) assert.Nil(err) @@ -302,19 +311,23 @@ func TestFinishUpload(t *testing.T) { "foo": "bar", } + ctx := context.Background() gomock.InOrder( - service.EXPECT().FilterObjects(filterParams).Return(mockPartials, nil), - service.EXPECT().ComposeObjects(composeParams).Return(nil), - service.EXPECT().DeleteObjectsWithFilter(filterParams).Return(nil), - service.EXPECT().ReadObject(infoParams).Return(r, nil), - service.EXPECT().FilterObjects(filterParams2).Return(mockPartials, nil), - service.EXPECT().GetObjectSize(mockObjectParams0).Return(size, nil), - service.EXPECT().GetObjectSize(mockObjectParams1).Return(size, nil), - service.EXPECT().GetObjectSize(mockObjectParams2).Return(size, nil), - service.EXPECT().WriteObject(infoParams, infoR).Return(int64(len(offsetInfoData)), nil), - service.EXPECT().SetObjectMetadata(objectParams, metadata).Return(nil), + service.EXPECT().FilterObjects(ctx, filterParams).Return(mockPartials, nil), + service.EXPECT().ComposeObjects(ctx, composeParams).Return(nil), + service.EXPECT().DeleteObjectsWithFilter(ctx, filterParams).Return(nil), + service.EXPECT().ReadObject(ctx, infoParams).Return(r, nil), + service.EXPECT().FilterObjects(ctx, filterParams2).Return(mockPartials, nil), ) + ctxCancel, _ := context.WithCancel(ctx) + service.EXPECT().GetObjectSize(ctxCancel, mockObjectParams0).Return(size, nil) + service.EXPECT().GetObjectSize(ctxCancel, mockObjectParams1).Return(size, nil) + lastGetObjectSize := service.EXPECT().GetObjectSize(ctxCancel, mockObjectParams2).Return(size, nil) + + writeObject := service.EXPECT().WriteObject(ctx, infoParams, infoR).Return(int64(len(offsetInfoData)), nil).After(lastGetObjectSize) + service.EXPECT().SetObjectMetadata(ctx, objectParams, metadata).Return(nil).After(writeObject) + err = store.FinishUpload(mockID) assert.Nil(err) } @@ -378,9 +391,10 @@ func TestWriteChunk(t *testing.T) { rGet := bytes.NewReader([]byte(mockReaderData)) + ctx := context.Background() gomock.InOrder( - service.EXPECT().FilterObjects(filterParams).Return(partials, nil), - service.EXPECT().WriteObject(writeObjectParams, rGet).Return(int64(len(mockReaderData)), nil), + service.EXPECT().FilterObjects(ctx, filterParams).Return(partials, nil), + service.EXPECT().WriteObject(ctx, writeObjectParams, rGet).Return(int64(len(mockReaderData)), nil), ) reader := bytes.NewReader([]byte(mockReaderData))