2015-04-11 11:40:39 +00:00
|
|
|
package limitedstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2015-11-26 11:43:49 +00:00
|
|
|
"strconv"
|
2015-04-11 11:40:39 +00:00
|
|
|
"testing"
|
2016-01-20 15:40:13 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/tus/tusd"
|
2015-04-11 11:40:39 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 14:18:37 +00:00
|
|
|
var _ tusd.DataStore = &LimitedStore{}
|
|
|
|
var _ tusd.TerminaterDataStore = &LimitedStore{}
|
|
|
|
|
2015-04-11 11:40:39 +00:00
|
|
|
type dataStore struct {
|
2016-01-20 15:40:13 +00:00
|
|
|
t *assert.Assertions
|
2015-11-26 11:43:49 +00:00
|
|
|
numCreatedUploads int
|
|
|
|
numTerminatedUploads int
|
2015-04-11 11:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *dataStore) NewUpload(info tusd.FileInfo) (string, error) {
|
2015-11-26 11:43:49 +00:00
|
|
|
uploadId := store.numCreatedUploads
|
2015-04-11 11:40:39 +00:00
|
|
|
|
2015-11-26 11:43:49 +00:00
|
|
|
// We expect the uploads to be created in a specific order.
|
|
|
|
// These sizes correlate to this order.
|
|
|
|
expectedSize := []int64{30, 60, 80}[uploadId]
|
2015-04-11 11:40:39 +00:00
|
|
|
|
2016-01-20 15:40:13 +00:00
|
|
|
store.t.Equal(expectedSize, info.Size)
|
2015-11-26 11:43:49 +00:00
|
|
|
|
|
|
|
store.numCreatedUploads += 1
|
|
|
|
|
|
|
|
return strconv.Itoa(uploadId), nil
|
2015-04-11 11:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *dataStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *dataStore) GetInfo(id string) (tusd.FileInfo, error) {
|
|
|
|
return tusd.FileInfo{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *dataStore) Terminate(id string) error {
|
2015-11-26 11:43:49 +00:00
|
|
|
// We expect the uploads to be terminated in a specific order (the bigger
|
|
|
|
// come first)
|
|
|
|
expectedUploadId := []string{"1", "0"}[store.numTerminatedUploads]
|
|
|
|
|
2016-01-20 15:40:13 +00:00
|
|
|
store.t.Equal(expectedUploadId, id)
|
2015-11-26 11:43:49 +00:00
|
|
|
|
|
|
|
store.numTerminatedUploads += 1
|
2015-04-11 11:40:39 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitedStore(t *testing.T) {
|
2016-01-20 15:40:13 +00:00
|
|
|
a := assert.New(t)
|
2015-04-11 11:40:39 +00:00
|
|
|
dataStore := &dataStore{
|
2016-01-20 15:40:13 +00:00
|
|
|
t: a,
|
2015-04-11 11:40:39 +00:00
|
|
|
}
|
2016-02-21 22:25:35 +00:00
|
|
|
store := New(100, dataStore, dataStore)
|
2015-04-11 11:40:39 +00:00
|
|
|
|
2015-11-26 11:43:49 +00:00
|
|
|
// Create new upload (30 bytes)
|
2015-04-11 11:40:39 +00:00
|
|
|
id, err := store.NewUpload(tusd.FileInfo{
|
2015-11-26 11:43:49 +00:00
|
|
|
Size: 30,
|
2015-04-11 11:40:39 +00:00
|
|
|
})
|
2016-01-20 15:40:13 +00:00
|
|
|
a.NoError(err)
|
|
|
|
a.Equal("0", id)
|
2015-04-11 11:40:39 +00:00
|
|
|
|
2015-11-26 11:43:49 +00:00
|
|
|
// Create new upload (60 bytes)
|
2015-04-11 11:40:39 +00:00
|
|
|
id, err = store.NewUpload(tusd.FileInfo{
|
2015-11-26 11:43:49 +00:00
|
|
|
Size: 60,
|
2015-04-11 11:40:39 +00:00
|
|
|
})
|
2016-01-20 15:40:13 +00:00
|
|
|
a.NoError(err)
|
|
|
|
a.Equal("1", id)
|
2015-04-11 11:40:39 +00:00
|
|
|
|
2015-11-26 11:43:49 +00:00
|
|
|
// Create new upload (80 bytes)
|
|
|
|
id, err = store.NewUpload(tusd.FileInfo{
|
|
|
|
Size: 80,
|
|
|
|
})
|
2016-01-20 15:40:13 +00:00
|
|
|
a.NoError(err)
|
|
|
|
a.Equal("2", id)
|
2015-11-26 11:43:49 +00:00
|
|
|
|
|
|
|
if dataStore.numTerminatedUploads != 2 {
|
|
|
|
t.Error("expected two uploads to be terminated")
|
2015-04-11 11:40:39 +00:00
|
|
|
}
|
|
|
|
}
|