Add tests for locking in various situations

This commit is contained in:
Marius 2016-10-13 18:33:33 +02:00
parent 3b3e3bcef0
commit 5ff46eb2d7
4 changed files with 70 additions and 22 deletions

View File

@ -25,7 +25,12 @@ func TestGet(t *testing.T) {
Reader: strings.NewReader("hello"), Reader: strings.NewReader("hello"),
} }
ctrl := gomock.NewController(t)
defer ctrl.Finish()
locker := NewMockLocker(ctrl)
gomock.InOrder( gomock.InOrder(
locker.EXPECT().LockUpload("yes"),
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5, Offset: 5,
Size: 20, Size: 20,
@ -34,21 +39,27 @@ func TestGet(t *testing.T) {
}, },
}, nil), }, nil),
store.EXPECT().GetReader("yes").Return(reader, 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{ handler, _ := NewHandler(Config{
DataStore: store, StoreComposer: composer,
}) })
(&httpTest{ (&httpTest{
Method: "GET", Method: "GET",
URL: "yes", URL: "yes",
Code: http.StatusOK,
ResBody: "hello",
ResHeader: map[string]string{ ResHeader: map[string]string{
"Content-Length": "5", "Content-Length": "5",
"Content-Disposition": `inline;filename="file.jpg\"evil"`, "Content-Disposition": `inline;filename="file.jpg\"evil"`,
}, },
Code: http.StatusOK,
ResBody: "hello",
}).Run(handler, t) }).Run(handler, t)
if !reader.closed { if !reader.closed {

View File

@ -5,11 +5,18 @@ import (
"os" "os"
"testing" "testing"
"github.com/golang/mock/gomock"
. "github.com/tus/tusd" . "github.com/tus/tusd"
) )
func TestHead(t *testing.T) { func TestHead(t *testing.T) {
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore) { SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
locker := NewMockLocker(ctrl)
gomock.InOrder(
locker.EXPECT().LockUpload("yes"),
store.EXPECT().GetInfo("yes").Return(FileInfo{ store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 11, Offset: 11,
Size: 44, Size: 44,
@ -17,10 +24,16 @@ func TestHead(t *testing.T) {
"name": "lunrjs.png", "name": "lunrjs.png",
"type": "image/png", "type": "image/png",
}, },
}, nil) }, nil),
locker.EXPECT().UnlockUpload("yes"),
)
composer := NewStoreComposer()
composer.UseCore(store)
composer.UseLocker(locker)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
DataStore: store, StoreComposer: composer,
}) })
res := (&httpTest{ res := (&httpTest{

View File

@ -171,6 +171,10 @@ func TestPost(t *testing.T) {
SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore) { SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore) {
SubTest(t, "Create", 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( gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{ store.EXPECT().NewUpload(FileInfo{
Size: 300, Size: 300,
@ -179,11 +183,17 @@ func TestPost(t *testing.T) {
"bar": "world", "bar": "world",
}, },
}).Return("foo", nil), }).Return("foo", nil),
locker.EXPECT().LockUpload("foo"),
store.EXPECT().WriteChunk("foo", int64(0), NewReaderMatcher("hello")).Return(int64(5), nil), 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{ handler, _ := NewHandler(Config{
DataStore: store, StoreComposer: composer,
BasePath: "/files/", BasePath: "/files/",
}) })

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/golang/mock/gomock"
. "github.com/tus/tusd" . "github.com/tus/tusd"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -29,14 +30,27 @@ func TestTerminate(t *testing.T) {
}) })
SubTest(t, "Termination", func(t *testing.T, store *MockFullDataStore) { SubTest(t, "Termination", func(t *testing.T, store *MockFullDataStore) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
locker := NewMockLocker(ctrl)
gomock.InOrder(
locker.EXPECT().LockUpload("foo"),
store.EXPECT().GetInfo("foo").Return(FileInfo{ store.EXPECT().GetInfo("foo").Return(FileInfo{
ID: "foo", ID: "foo",
Size: 10, Size: 10,
}, nil) }, nil),
store.EXPECT().Terminate("foo").Return(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{ handler, _ := NewHandler(Config{
DataStore: store, StoreComposer: composer,
NotifyTerminatedUploads: true, NotifyTerminatedUploads: true,
}) })