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"),
}
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 {

View File

@ -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{

View File

@ -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{

View File

@ -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,
})