2018-11-10 20:05:44 +00:00
|
|
|
package etcd3locker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-09-11 10:03:39 +00:00
|
|
|
etcd_harness "github.com/chen-anders/go-etcd-harness"
|
|
|
|
"github.com/coreos/etcd/clientv3"
|
|
|
|
|
2018-11-10 20:05:44 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-11 16:23:20 +00:00
|
|
|
"github.com/tus/tusd/pkg/handler"
|
2018-11-10 20:05:44 +00:00
|
|
|
)
|
|
|
|
|
2019-09-11 10:03:39 +00:00
|
|
|
var _ handler.Locker = &Etcd3Locker{}
|
|
|
|
|
2018-11-10 20:05:44 +00:00
|
|
|
func TestEtcd3Locker(t *testing.T) {
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
harness, err := etcd_harness.New(os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed starting etcd harness: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("will use etcd harness endpoint: %v", harness.Endpoint)
|
|
|
|
defer func() {
|
|
|
|
harness.Stop()
|
|
|
|
t.Logf("cleaned up etcd harness")
|
|
|
|
}()
|
|
|
|
|
|
|
|
client, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: []string{harness.Endpoint},
|
|
|
|
DialTimeout: 5 * time.Second,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to connect to etcd3: %v", err)
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
shortTTL := 3
|
|
|
|
testPrefix := "/test-tusd"
|
|
|
|
|
|
|
|
lockerOptions := NewLockerOptions(shortTTL, testPrefix)
|
|
|
|
locker, err := NewWithLockerOptions(client, lockerOptions)
|
|
|
|
a.NoError(err)
|
2019-09-11 10:03:39 +00:00
|
|
|
|
|
|
|
lock1, err := locker.NewLock("one")
|
|
|
|
a.NoError(err)
|
|
|
|
a.NoError(lock1.Lock())
|
|
|
|
|
|
|
|
//a.Equal(handler.ErrFileLocked, lock1.Lock())
|
2018-11-10 20:05:44 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
// test that we can't take over the upload via a different etcd3 session
|
|
|
|
// while an upload is already taking place; testing etcd3 session KeepAlive
|
2019-09-11 10:03:39 +00:00
|
|
|
lock2, err := locker.NewLock("one")
|
|
|
|
a.NoError(err)
|
|
|
|
a.Equal(handler.ErrFileLocked, lock2.Lock())
|
|
|
|
a.NoError(lock1.Unlock())
|
|
|
|
a.Equal(ErrLockNotHeld, lock1.Unlock())
|
2018-11-10 20:05:44 +00:00
|
|
|
|
|
|
|
testPrefix = "/test-tusd2"
|
|
|
|
locker2, err := NewWithPrefix(client, testPrefix)
|
|
|
|
a.NoError(err)
|
2019-09-11 10:03:39 +00:00
|
|
|
|
|
|
|
lock3, err := locker2.NewLock("one")
|
|
|
|
a.NoError(err)
|
|
|
|
|
|
|
|
a.NoError(lock3.Lock())
|
|
|
|
a.Equal(handler.ErrFileLocked, lock3.Lock())
|
|
|
|
a.Equal(handler.ErrFileLocked, lock3.Lock())
|
|
|
|
a.NoError(lock3.Unlock())
|
|
|
|
a.Equal(ErrLockNotHeld, lock3.Unlock())
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|