From 5ff46eb2d71db1541d077b58df99ad1e18fd3f30 Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 13 Oct 2016 18:33:33 +0200 Subject: [PATCH] Add tests for locking in various situations --- get_test.go | 21 ++++++++++++++++----- head_test.go | 31 ++++++++++++++++++++++--------- post_test.go | 14 ++++++++++++-- terminate_test.go | 26 ++++++++++++++++++++------ 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/get_test.go b/get_test.go index 8c8cc24..53c1ae3 100644 --- a/get_test.go +++ b/get_test.go @@ -25,7 +25,12 @@ func TestGet(t *testing.T) { Reader: strings.NewReader("hello"), } + ctrl := gomock.NewController(t) + defer ctrl.Finish() + locker := NewMockLocker(ctrl) + gomock.InOrder( + locker.EXPECT().LockUpload("yes"), store.EXPECT().GetInfo("yes").Return(FileInfo{ Offset: 5, Size: 20, @@ -34,21 +39,27 @@ func TestGet(t *testing.T) { }, }, nil), store.EXPECT().GetReader("yes").Return(reader, nil), + locker.EXPECT().UnlockUpload("yes"), ) + composer := NewStoreComposer() + composer.UseCore(store) + composer.UseGetReader(store) + composer.UseLocker(locker) + handler, _ := NewHandler(Config{ - DataStore: store, + StoreComposer: composer, }) (&httpTest{ - Method: "GET", - URL: "yes", - Code: http.StatusOK, - ResBody: "hello", + Method: "GET", + URL: "yes", ResHeader: map[string]string{ "Content-Length": "5", "Content-Disposition": `inline;filename="file.jpg\"evil"`, }, + Code: http.StatusOK, + ResBody: "hello", }).Run(handler, t) if !reader.closed { diff --git a/head_test.go b/head_test.go index 85e0851..15b4099 100644 --- a/head_test.go +++ b/head_test.go @@ -5,22 +5,35 @@ import ( "os" "testing" + "github.com/golang/mock/gomock" . "github.com/tus/tusd" ) func TestHead(t *testing.T) { SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore) { - store.EXPECT().GetInfo("yes").Return(FileInfo{ - Offset: 11, - Size: 44, - MetaData: map[string]string{ - "name": "lunrjs.png", - "type": "image/png", - }, - }, nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + locker := NewMockLocker(ctrl) + + gomock.InOrder( + locker.EXPECT().LockUpload("yes"), + store.EXPECT().GetInfo("yes").Return(FileInfo{ + Offset: 11, + Size: 44, + MetaData: map[string]string{ + "name": "lunrjs.png", + "type": "image/png", + }, + }, nil), + locker.EXPECT().UnlockUpload("yes"), + ) + + composer := NewStoreComposer() + composer.UseCore(store) + composer.UseLocker(locker) handler, _ := NewHandler(Config{ - DataStore: store, + StoreComposer: composer, }) res := (&httpTest{ diff --git a/post_test.go b/post_test.go index 580a42e..266a6ef 100644 --- a/post_test.go +++ b/post_test.go @@ -171,6 +171,10 @@ func TestPost(t *testing.T) { SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore) { SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + locker := NewMockLocker(ctrl) + gomock.InOrder( store.EXPECT().NewUpload(FileInfo{ Size: 300, @@ -179,12 +183,18 @@ func TestPost(t *testing.T) { "bar": "world", }, }).Return("foo", nil), + locker.EXPECT().LockUpload("foo"), store.EXPECT().WriteChunk("foo", int64(0), NewReaderMatcher("hello")).Return(int64(5), nil), + locker.EXPECT().UnlockUpload("foo"), ) + composer := NewStoreComposer() + composer.UseCore(store) + composer.UseLocker(locker) + handler, _ := NewHandler(Config{ - DataStore: store, - BasePath: "/files/", + StoreComposer: composer, + BasePath: "/files/", }) (&httpTest{ diff --git a/terminate_test.go b/terminate_test.go index 91f9294..de079eb 100644 --- a/terminate_test.go +++ b/terminate_test.go @@ -4,6 +4,7 @@ import ( "net/http" "testing" + "github.com/golang/mock/gomock" . "github.com/tus/tusd" "github.com/stretchr/testify/assert" @@ -29,14 +30,27 @@ func TestTerminate(t *testing.T) { }) SubTest(t, "Termination", func(t *testing.T, store *MockFullDataStore) { - store.EXPECT().GetInfo("foo").Return(FileInfo{ - ID: "foo", - Size: 10, - }, nil) - store.EXPECT().Terminate("foo").Return(nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + locker := NewMockLocker(ctrl) + + gomock.InOrder( + locker.EXPECT().LockUpload("foo"), + store.EXPECT().GetInfo("foo").Return(FileInfo{ + ID: "foo", + Size: 10, + }, nil), + store.EXPECT().Terminate("foo").Return(nil), + locker.EXPECT().UnlockUpload("foo"), + ) + + composer := NewStoreComposer() + composer.UseCore(store) + composer.UseTerminater(store) + composer.UseLocker(locker) handler, _ := NewHandler(Config{ - DataStore: store, + StoreComposer: composer, NotifyTerminatedUploads: true, })