|
|
@@ -14,7 +14,7 @@ import (
|
|
|
func TestModLockAcquireAndRelease(t *testing.T) {
|
|
|
tests.RunServer(func(s *server.Server) {
|
|
|
// Acquire lock.
|
|
|
- body, err := testAcquireLock(s, "foo", 10)
|
|
|
+ body, err := testAcquireLock(s, "foo", "", 10)
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "2")
|
|
|
|
|
|
@@ -24,7 +24,7 @@ func TestModLockAcquireAndRelease(t *testing.T) {
|
|
|
assert.Equal(t, body, "2")
|
|
|
|
|
|
// Release lock.
|
|
|
- body, err = testReleaseLock(s, "foo", 2)
|
|
|
+ body, err = testReleaseLock(s, "foo", "2", "")
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "")
|
|
|
|
|
|
@@ -42,7 +42,7 @@ func TestModLockBlockUntilAcquire(t *testing.T) {
|
|
|
|
|
|
// Acquire lock #1.
|
|
|
go func() {
|
|
|
- body, err := testAcquireLock(s, "foo", 10)
|
|
|
+ body, err := testAcquireLock(s, "foo", "", 10)
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "2")
|
|
|
c <- true
|
|
|
@@ -50,11 +50,13 @@ func TestModLockBlockUntilAcquire(t *testing.T) {
|
|
|
<- c
|
|
|
|
|
|
// Acquire lock #2.
|
|
|
+ waiting := true
|
|
|
go func() {
|
|
|
c <- true
|
|
|
- body, err := testAcquireLock(s, "foo", 10)
|
|
|
+ body, err := testAcquireLock(s, "foo", "", 10)
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "4")
|
|
|
+ waiting = false
|
|
|
}()
|
|
|
<- c
|
|
|
|
|
|
@@ -65,8 +67,11 @@ func TestModLockBlockUntilAcquire(t *testing.T) {
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "2")
|
|
|
|
|
|
+ // Check that we are still waiting for lock #2.
|
|
|
+ assert.Equal(t, waiting, true)
|
|
|
+
|
|
|
// Release lock #1.
|
|
|
- body, err = testReleaseLock(s, "foo", 2)
|
|
|
+ body, err = testReleaseLock(s, "foo", "2", "")
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
// Check that we have lock #2.
|
|
|
@@ -75,7 +80,7 @@ func TestModLockBlockUntilAcquire(t *testing.T) {
|
|
|
assert.Equal(t, body, "4")
|
|
|
|
|
|
// Release lock #2.
|
|
|
- body, err = testReleaseLock(s, "foo", 4)
|
|
|
+ body, err = testReleaseLock(s, "foo", "4", "")
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
// Check that we have no lock.
|
|
|
@@ -92,7 +97,7 @@ func TestModLockExpireAndRelease(t *testing.T) {
|
|
|
|
|
|
// Acquire lock #1.
|
|
|
go func() {
|
|
|
- body, err := testAcquireLock(s, "foo", 2)
|
|
|
+ body, err := testAcquireLock(s, "foo", "", 2)
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "2")
|
|
|
c <- true
|
|
|
@@ -102,7 +107,7 @@ func TestModLockExpireAndRelease(t *testing.T) {
|
|
|
// Acquire lock #2.
|
|
|
go func() {
|
|
|
c <- true
|
|
|
- body, err := testAcquireLock(s, "foo", 10)
|
|
|
+ body, err := testAcquireLock(s, "foo", "", 10)
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "4")
|
|
|
}()
|
|
|
@@ -129,7 +134,7 @@ func TestModLockExpireAndRelease(t *testing.T) {
|
|
|
func TestModLockRenew(t *testing.T) {
|
|
|
tests.RunServer(func(s *server.Server) {
|
|
|
// Acquire lock.
|
|
|
- body, err := testAcquireLock(s, "foo", 3)
|
|
|
+ body, err := testAcquireLock(s, "foo", "", 3)
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "2")
|
|
|
|
|
|
@@ -141,7 +146,7 @@ func TestModLockRenew(t *testing.T) {
|
|
|
assert.Equal(t, body, "2")
|
|
|
|
|
|
// Renew lock.
|
|
|
- body, err = testRenewLock(s, "foo", 2, 3)
|
|
|
+ body, err = testRenewLock(s, "foo", "2", "", 3)
|
|
|
assert.NoError(t, err)
|
|
|
assert.Equal(t, body, "")
|
|
|
|
|
|
@@ -161,28 +166,59 @@ func TestModLockRenew(t *testing.T) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+// Ensure that a lock can be acquired with a value and released by value.
|
|
|
+func TestModLockAcquireAndReleaseByValue(t *testing.T) {
|
|
|
+ tests.RunServer(func(s *server.Server) {
|
|
|
+ // Acquire lock.
|
|
|
+ body, err := testAcquireLock(s, "foo", "XXX", 10)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ assert.Equal(t, body, "2")
|
|
|
+
|
|
|
+ // Check that we have the lock.
|
|
|
+ body, err = testGetLockValue(s, "foo")
|
|
|
+ assert.NoError(t, err)
|
|
|
+ assert.Equal(t, body, "XXX")
|
|
|
+
|
|
|
+ // Release lock.
|
|
|
+ body, err = testReleaseLock(s, "foo", "", "XXX")
|
|
|
+ assert.NoError(t, err)
|
|
|
+ assert.Equal(t, body, "")
|
|
|
+
|
|
|
+ // Check that we released the lock.
|
|
|
+ body, err = testGetLockValue(s, "foo")
|
|
|
+ assert.NoError(t, err)
|
|
|
+ assert.Equal(t, body, "")
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
|
|
|
-func testAcquireLock(s *server.Server, key string, ttl int) (string, error) {
|
|
|
- resp, err := tests.PostForm(fmt.Sprintf("%s/mod/v2/lock/%s?ttl=%d", s.URL(), key, ttl), nil)
|
|
|
+func testAcquireLock(s *server.Server, key string, value string, ttl int) (string, error) {
|
|
|
+ resp, err := tests.PostForm(fmt.Sprintf("%s/mod/v2/lock/%s?value=%s&ttl=%d", s.URL(), key, value, ttl), nil)
|
|
|
ret := tests.ReadBody(resp)
|
|
|
return string(ret), err
|
|
|
}
|
|
|
|
|
|
func testGetLockIndex(s *server.Server, key string) (string, error) {
|
|
|
+ resp, err := tests.Get(fmt.Sprintf("%s/mod/v2/lock/%s?field=index", s.URL(), key))
|
|
|
+ ret := tests.ReadBody(resp)
|
|
|
+ return string(ret), err
|
|
|
+}
|
|
|
+
|
|
|
+func testGetLockValue(s *server.Server, key string) (string, error) {
|
|
|
resp, err := tests.Get(fmt.Sprintf("%s/mod/v2/lock/%s", s.URL(), key))
|
|
|
ret := tests.ReadBody(resp)
|
|
|
return string(ret), err
|
|
|
}
|
|
|
|
|
|
-func testReleaseLock(s *server.Server, key string, index int) (string, error) {
|
|
|
- resp, err := tests.DeleteForm(fmt.Sprintf("%s/mod/v2/lock/%s/%d", s.URL(), key, index), nil)
|
|
|
+func testReleaseLock(s *server.Server, key string, index string, value string) (string, error) {
|
|
|
+ resp, err := tests.DeleteForm(fmt.Sprintf("%s/mod/v2/lock/%s?index=%s&value=%s", s.URL(), key, index, value), nil)
|
|
|
ret := tests.ReadBody(resp)
|
|
|
return string(ret), err
|
|
|
}
|
|
|
|
|
|
-func testRenewLock(s *server.Server, key string, index int, ttl int) (string, error) {
|
|
|
- resp, err := tests.PutForm(fmt.Sprintf("%s/mod/v2/lock/%s/%d?ttl=%d", s.URL(), key, index, ttl), nil)
|
|
|
+func testRenewLock(s *server.Server, key string, index string, value string, ttl int) (string, error) {
|
|
|
+ resp, err := tests.PutForm(fmt.Sprintf("%s/mod/v2/lock/%s?index=%s&value=%s&ttl=%d", s.URL(), key, index, value, ttl), nil)
|
|
|
ret := tests.ReadBody(resp)
|
|
|
return string(ret), err
|
|
|
}
|