Add test case for order of termination

This commit is contained in:
Marius 2015-11-26 12:43:49 +01:00
parent 7b1cf1f639
commit 3812aa7ca2
1 changed files with 41 additions and 24 deletions

View File

@ -3,29 +3,30 @@ package limitedstore
import ( import (
"github.com/tus/tusd" "github.com/tus/tusd"
"io" "io"
"strconv"
"testing" "testing"
) )
type dataStore struct { type dataStore struct {
t *testing.T t *testing.T
firstUploadCreated bool numCreatedUploads int
uploadTerminated bool numTerminatedUploads int
} }
func (store *dataStore) NewUpload(info tusd.FileInfo) (string, error) { func (store *dataStore) NewUpload(info tusd.FileInfo) (string, error) {
if !store.firstUploadCreated { uploadId := store.numCreatedUploads
if info.Size != 80 {
store.t.Errorf("expect size to be 80, got %v", info.Size)
}
store.firstUploadCreated = true
return "1", nil // We expect the uploads to be created in a specific order.
// These sizes correlate to this order.
expectedSize := []int64{30, 60, 80}[uploadId]
if info.Size != expectedSize {
store.t.Errorf("expect size to be %v, got %v", expectedSize, info.Size)
} }
if info.Size != 50 { store.numCreatedUploads += 1
store.t.Errorf("expect size to be 50, got %v", info.Size)
} return strconv.Itoa(uploadId), nil
return "2", nil
} }
func (store *dataStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) { func (store *dataStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
@ -41,10 +42,15 @@ func (store *dataStore) GetReader(id string) (io.Reader, error) {
} }
func (store *dataStore) Terminate(id string) error { func (store *dataStore) Terminate(id string) error {
if id != "1" { // We expect the uploads to be terminated in a specific order (the bigger
store.t.Errorf("expect first upload to be terminated, got %v", id) // come first)
expectedUploadId := []string{"1", "0"}[store.numTerminatedUploads]
if id != expectedUploadId {
store.t.Errorf("exptect upload %v to be terminated, got %v", expectedUploadId, id)
} }
store.uploadTerminated = true
store.numTerminatedUploads += 1
return nil return nil
} }
@ -55,29 +61,40 @@ func TestLimitedStore(t *testing.T) {
} }
store := New(100, dataStore) store := New(100, dataStore)
// Create new upload (80 bytes) // Create new upload (30 bytes)
id, err := store.NewUpload(tusd.FileInfo{ id, err := store.NewUpload(tusd.FileInfo{
Size: 80, Size: 30,
})
if err != nil {
t.Fatal(err)
}
if id != "0" {
t.Errorf("expected first upload to be created, got %v", id)
}
// Create new upload (60 bytes)
id, err = store.NewUpload(tusd.FileInfo{
Size: 60,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if id != "1" { if id != "1" {
t.Errorf("expected first upload to be created, got %v", id) t.Errorf("expected second upload to be created, got %v", id)
} }
// Create new upload (50 bytes) // Create new upload (80 bytes)
id, err = store.NewUpload(tusd.FileInfo{ id, err = store.NewUpload(tusd.FileInfo{
Size: 50, Size: 80,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if id != "2" { if id != "2" {
t.Errorf("expected second upload to be created, got %v", id) t.Errorf("expected thrid upload to be created, got %v", id)
} }
if !dataStore.uploadTerminated { if dataStore.numTerminatedUploads != 2 {
t.Error("expected first upload to be terminated") t.Error("expected two uploads to be terminated")
} }
} }