Add test for lockingstore.MemoryLocker

This commit is contained in:
Marius 2015-12-09 20:48:41 +01:00
parent 49d7c2ff78
commit 45936806a9
3 changed files with 48 additions and 15 deletions

View File

@ -1,5 +1,11 @@
package lockingstore
import (
"io"
"github.com/tus/tusd"
)
type Locker interface {
LockUpload(id string) error
UnlockUpload(id string) error
@ -7,16 +13,16 @@ type Locker interface {
type LockingStore struct {
tusd.DataStore
Locker *Locker
Locker Locker
}
func (store LockingStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
if err := store.LockUpload(id); err != nil {
func (store LockingStore) WriteChunk(id string, offset int64, src io.Reader) (n int64, err error) {
if err := store.Locker.LockUpload(id); err != nil {
return 0, err
}
defer func() {
if unlockErr := store.UnlockUpload(id); unlockErr != nil {
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
err = unlockErr
}
}()
@ -24,13 +30,13 @@ func (store LockingStore) WriteChunk(id string, offset int64, src io.Reader) (in
return store.DataStore.WriteChunk(id, offset, src)
}
func (store LockingStore) GetInfo(id string) (FileInfo, error) {
if err := store.LockUpload(id); err != nil {
return nil, err
func (store LockingStore) GetInfo(id string) (info tusd.FileInfo, err error) {
if err := store.Locker.LockUpload(id); err != nil {
return info, err
}
defer func() {
if unlockErr := store.UnlockUpload(id); unlockErr != nil {
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
err = unlockErr
}
}()
@ -38,13 +44,13 @@ func (store LockingStore) GetInfo(id string) (FileInfo, error) {
return store.DataStore.GetInfo(id)
}
func (store LockingStore) GetReader(id string) (io.Reader, error) {
if err := store.LockUpload(id); err != nil {
func (store LockingStore) GetReader(id string) (src io.Reader, err error) {
if err := store.Locker.LockUpload(id); err != nil {
return nil, err
}
defer func() {
if unlockErr := store.UnlockUpload(id); unlockErr != nil {
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
err = unlockErr
}
}()
@ -52,13 +58,13 @@ func (store LockingStore) GetReader(id string) (io.Reader, error) {
return store.DataStore.GetReader(id)
}
func (store LockingStore) Terminate(id string) error {
if err := store.LockUpload(id); err != nil {
func (store LockingStore) Terminate(id string) (err error) {
if err := store.Locker.LockUpload(id); err != nil {
return err
}
defer func() {
if unlockErr := store.UnlockUpload(id); unlockErr != nil {
if unlockErr := store.Locker.UnlockUpload(id); unlockErr != nil {
err = unlockErr
}
}()

View File

@ -21,7 +21,7 @@ func (locker *MemoryLocker) LockUpload(id string) error {
return tusd.ErrFileLocked
}
handler.locks[id] = true
locker.locks[id] = true
return nil
}

View File

@ -0,0 +1,27 @@
package lockingstore
import (
"testing"
"github.com/tus/tusd"
)
func TestMemoryLocker(t *testing.T) {
locker := New()
if err := locker.LockUpload("one"); err != nil {
t.Errorf("unexpected error when locking file: %s", err)
}
if err := locker.LockUpload("one"); err != tusd.ErrFileLocked {
t.Errorf("expected error when locking locked file: %s", err)
}
if err := locker.UnlockUpload("one"); err != nil {
t.Errorf("unexpected error when unlocking file: %s", err)
}
if err := locker.UnlockUpload("one"); err != nil {
t.Errorf("unexpected error when unlocking file again: %s", err)
}
}