2015-01-10 01:35:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2015-12-01 04:16:40 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httputil"
|
|
|
|
"strings"
|
2015-01-10 01:35:17 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2018-08-06 23:46:09 +00:00
|
|
|
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2015-01-10 01:35:17 +00:00
|
|
|
)
|
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
func createTestLock(t *testing.T, c *Client, key string) (*Lock, *Session) {
|
|
|
|
t.Helper()
|
|
|
|
session := c.Session()
|
|
|
|
|
|
|
|
se := &SessionEntry{
|
|
|
|
Name: DefaultLockSessionName,
|
|
|
|
TTL: DefaultLockSessionTTL,
|
|
|
|
Behavior: SessionBehaviorDelete,
|
|
|
|
}
|
|
|
|
id, _, err := session.CreateNoChecks(se, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &LockOptions{
|
|
|
|
Key: key,
|
|
|
|
Session: id,
|
|
|
|
SessionName: se.Name,
|
|
|
|
SessionTTL: se.TTL,
|
|
|
|
}
|
|
|
|
lock, err := c.LockOpts(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lock, session
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockLockUnlock(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-10 01:35:17 +00:00
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
lock, session := createTestLock(t, c, "test/lock")
|
|
|
|
defer session.Destroy(lock.opts.Session, nil)
|
2015-01-10 01:35:17 +00:00
|
|
|
|
|
|
|
// Initial unlock should fail
|
2018-09-18 16:47:01 +00:00
|
|
|
err := lock.Unlock()
|
2015-01-10 01:35:17 +00:00
|
|
|
if err != ErrLockNotHeld {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should work
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("not leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double lock should fail
|
|
|
|
_, err = lock.Lock(nil)
|
|
|
|
if err != ErrLockHeld {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be leader
|
|
|
|
select {
|
|
|
|
case <-leaderCh:
|
|
|
|
t.Fatalf("should be leader")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial unlock should work
|
|
|
|
err = lock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double unlock should fail
|
|
|
|
err = lock.Unlock()
|
|
|
|
if err != ErrLockNotHeld {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:24:17 +00:00
|
|
|
// Should lose leadership
|
2015-01-10 01:35:17 +00:00
|
|
|
select {
|
|
|
|
case <-leaderCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("should not be leader")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockForceInvalidate(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-10 01:35:17 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2018-09-18 16:47:01 +00:00
|
|
|
lock, session := createTestLock(t, c, "test/lock")
|
|
|
|
defer session.Destroy(lock.opts.Session, nil)
|
2018-08-06 23:46:09 +00:00
|
|
|
|
|
|
|
// Should work
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("not leader")
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Nuke the session, simulator an operator invalidation
|
|
|
|
// or a health check failure
|
|
|
|
session := c.Session()
|
|
|
|
session.Destroy(lock.lockSession, nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Should loose leadership
|
|
|
|
select {
|
|
|
|
case <-leaderCh:
|
|
|
|
case <-time.After(time.Second):
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("should not be leader")
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
})
|
2015-01-10 01:35:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockDeleteKey(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-10 01:35:17 +00:00
|
|
|
|
2015-10-13 18:48:35 +00:00
|
|
|
// This uncovered some issues around special-case handling of low index
|
|
|
|
// numbers where it would work with a low number but fail for higher
|
|
|
|
// ones, so we loop this a bit to sweep the index up out of that
|
|
|
|
// territory.
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
func() {
|
2018-09-18 16:47:01 +00:00
|
|
|
lock, session := createTestLock(t, c, "test/lock")
|
|
|
|
defer session.Destroy(lock.opts.Session, nil)
|
2015-10-13 18:48:35 +00:00
|
|
|
|
|
|
|
// Should work
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("not leader")
|
|
|
|
}
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Nuke the key, simulate an operator intervention
|
|
|
|
kv := c.KV()
|
|
|
|
kv.Delete("test/lock", nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Should loose leadership
|
|
|
|
select {
|
|
|
|
case <-leaderCh:
|
2016-10-31 16:59:20 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
2015-10-13 18:48:35 +00:00
|
|
|
t.Fatalf("should not be leader")
|
|
|
|
}
|
|
|
|
}()
|
2015-01-10 01:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockContend(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-10 01:35:17 +00:00
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
acquired := make([]bool, 3)
|
|
|
|
for idx := range acquired {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(idx int) {
|
|
|
|
defer wg.Done()
|
2018-09-18 16:47:01 +00:00
|
|
|
lock, session := createTestLock(t, c, "test/lock")
|
|
|
|
defer session.Destroy(lock.opts.Session, nil)
|
2015-01-10 01:35:17 +00:00
|
|
|
|
|
|
|
// Should work eventually, will contend
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
2020-06-05 20:52:31 +00:00
|
|
|
t.Errorf("err: %v", err)
|
|
|
|
return
|
2015-01-10 01:35:17 +00:00
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
2020-06-05 20:52:31 +00:00
|
|
|
t.Errorf("not leader")
|
|
|
|
return
|
2015-01-10 01:35:17 +00:00
|
|
|
}
|
|
|
|
defer lock.Unlock()
|
|
|
|
log.Printf("Contender %d acquired", idx)
|
|
|
|
|
|
|
|
// Set acquired and then leave
|
|
|
|
acquired[idx] = true
|
|
|
|
}(idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for termination
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(doneCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for everybody to get a turn
|
|
|
|
select {
|
|
|
|
case <-doneCh:
|
2015-01-10 01:42:24 +00:00
|
|
|
case <-time.After(3 * DefaultLockRetryTime):
|
2015-01-10 01:35:17 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, did := range acquired {
|
|
|
|
if !did {
|
|
|
|
t.Fatalf("contender %d never acquired", idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-13 22:01:50 +00:00
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockDestroy(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-13 22:01:50 +00:00
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
lock, session := createTestLock(t, c, "test/lock")
|
|
|
|
defer session.Destroy(lock.opts.Session, nil)
|
2015-01-13 22:01:50 +00:00
|
|
|
|
|
|
|
// Should work
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("not leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy should fail
|
|
|
|
if err := lock.Destroy(); err != ErrLockHeld {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be able to release
|
|
|
|
err = lock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire with a different lock
|
2018-09-18 16:47:01 +00:00
|
|
|
l2, session := createTestLock(t, c, "test/lock")
|
|
|
|
defer session.Destroy(lock.opts.Session, nil)
|
2015-01-13 22:01:50 +00:00
|
|
|
|
|
|
|
// Should work
|
|
|
|
leaderCh, err = l2.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("not leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy should still fail
|
|
|
|
if err := lock.Destroy(); err != ErrLockInUse {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-15 12:22:08 +00:00
|
|
|
// Should release
|
2015-01-13 22:01:50 +00:00
|
|
|
err = l2.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy should work
|
|
|
|
err = lock.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double destroy should work
|
|
|
|
err = l2.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 01:32:19 +00:00
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockConflict(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-20 01:32:19 +00:00
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
sema, session := createTestSemaphore(t, c, "test/lock/", 2)
|
|
|
|
defer session.Destroy(sema.opts.Session, nil)
|
2015-01-20 01:32:19 +00:00
|
|
|
|
|
|
|
// Should work
|
|
|
|
lockCh, err := sema.Acquire(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if lockCh == nil {
|
|
|
|
t.Fatalf("not hold")
|
|
|
|
}
|
|
|
|
defer sema.Release()
|
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
lock, session := createTestLock(t, c, "test/lock/.lock")
|
|
|
|
defer session.Destroy(lock.opts.Session, nil)
|
2015-01-20 01:32:19 +00:00
|
|
|
|
|
|
|
// Should conflict with semaphore
|
|
|
|
_, err = lock.Lock(nil)
|
|
|
|
if err != ErrLockConflict {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should conflict with semaphore
|
|
|
|
err = lock.Destroy()
|
|
|
|
if err != ErrLockConflict {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-12 21:48:11 +00:00
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockReclaimLock(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2015-05-04 22:27:22 +00:00
|
|
|
defer s.Stop()
|
2015-04-12 21:48:11 +00:00
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2015-04-12 21:48:11 +00:00
|
|
|
session, _, err := c.Session().Create(&SessionEntry{}, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := c.LockOpts(&LockOptions{Key: "test/lock", Session: session})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should work
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("not leader")
|
|
|
|
}
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
l2, err := c.LockOpts(&LockOptions{Key: "test/lock", Session: session})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reclaimed := make(chan (<-chan struct{}), 1)
|
|
|
|
go func() {
|
|
|
|
l2Ch, err := l2.Lock(nil)
|
|
|
|
if err != nil {
|
2020-06-05 20:52:31 +00:00
|
|
|
t.Errorf("not locked: %v", err)
|
2015-04-12 21:48:11 +00:00
|
|
|
}
|
|
|
|
reclaimed <- l2Ch
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Should reclaim the lock
|
|
|
|
var leader2Ch <-chan struct{}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case leader2Ch = <-reclaimed:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("should have locked")
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock should work
|
|
|
|
err = l2.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Both locks should see the unlock
|
|
|
|
select {
|
|
|
|
case <-leader2Ch:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("should not be leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-leaderCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("should not be leader")
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 04:16:40 +00:00
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockMonitorRetry(t *testing.T) {
|
2015-12-01 04:16:40 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
raw, s := makeClientWithoutConnect(t)
|
2015-12-01 04:16:40 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2015-12-01 04:16:40 +00:00
|
|
|
// Set up a server that always responds with 500 errors.
|
|
|
|
failer := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
}
|
|
|
|
outage := httptest.NewServer(http.HandlerFunc(failer))
|
|
|
|
defer outage.Close()
|
|
|
|
|
|
|
|
// Set up a reverse proxy that will send some requests to the
|
|
|
|
// 500 server and pass everything else through to the real Consul
|
|
|
|
// server.
|
|
|
|
var mutex sync.Mutex
|
|
|
|
errors := 0
|
|
|
|
director := func(req *http.Request) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
req.URL.Scheme = "http"
|
|
|
|
if errors > 0 && req.Method == "GET" && strings.Contains(req.URL.Path, "/v1/kv/test/lock") {
|
|
|
|
req.URL.Host = outage.URL[7:] // Strip off "http://".
|
|
|
|
errors--
|
|
|
|
} else {
|
|
|
|
req.URL.Host = raw.config.Address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proxy := httptest.NewServer(&httputil.ReverseProxy{Director: director})
|
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
// Make another client that points at the proxy instead of the real
|
|
|
|
// Consul server.
|
|
|
|
config := raw.config
|
|
|
|
config.Address = proxy.URL[7:] // Strip off "http://".
|
|
|
|
c, err := NewClient(&config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a lock with retries enabled.
|
|
|
|
opts := &LockOptions{
|
|
|
|
Key: "test/lock",
|
|
|
|
SessionTTL: "60s",
|
|
|
|
MonitorRetries: 3,
|
|
|
|
}
|
|
|
|
lock, err := c.LockOpts(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the default got set.
|
|
|
|
if lock.opts.MonitorRetryTime != DefaultMonitorRetryTime {
|
|
|
|
t.Fatalf("bad: %d", lock.opts.MonitorRetryTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now set a custom time for the test.
|
|
|
|
opts.MonitorRetryTime = 250 * time.Millisecond
|
|
|
|
lock, err = c.LockOpts(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if lock.opts.MonitorRetryTime != 250*time.Millisecond {
|
|
|
|
t.Fatalf("bad: %d", lock.opts.MonitorRetryTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get the lock.
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("not leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poke the key using the raw client to force the monitor to wake up
|
|
|
|
// and check the lock again. This time we will return errors for some
|
|
|
|
// of the responses.
|
|
|
|
mutex.Lock()
|
|
|
|
errors = 2
|
|
|
|
mutex.Unlock()
|
|
|
|
pair, _, err := raw.KV().Get("test/lock", &QueryOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2019-06-18 13:06:29 +00:00
|
|
|
pair.Value = []byte{1}
|
2015-12-01 04:16:40 +00:00
|
|
|
if _, err := raw.KV().Put(pair, &WriteOptions{}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(5 * opts.MonitorRetryTime)
|
|
|
|
|
|
|
|
// Should still be the leader.
|
|
|
|
select {
|
|
|
|
case <-leaderCh:
|
|
|
|
t.Fatalf("should be leader")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now return an overwhelming number of errors.
|
|
|
|
mutex.Lock()
|
|
|
|
errors = 10
|
|
|
|
mutex.Unlock()
|
2019-06-18 13:06:29 +00:00
|
|
|
pair.Value = []byte{2}
|
2015-12-01 04:16:40 +00:00
|
|
|
if _, err := raw.KV().Put(pair, &WriteOptions{}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(5 * opts.MonitorRetryTime)
|
|
|
|
|
|
|
|
// Should lose leadership.
|
|
|
|
select {
|
|
|
|
case <-leaderCh:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("should not be leader")
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:40:35 +00:00
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_LockOneShot(t *testing.T) {
|
2016-01-06 00:40:35 +00:00
|
|
|
t.Parallel()
|
2018-09-10 15:58:53 +00:00
|
|
|
c, s := makeClientWithoutConnect(t)
|
2016-01-06 00:40:35 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
2018-09-18 16:47:01 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2016-01-06 00:40:35 +00:00
|
|
|
// Set up a lock as a one-shot.
|
|
|
|
opts := &LockOptions{
|
|
|
|
Key: "test/lock",
|
|
|
|
LockTryOnce: true,
|
|
|
|
}
|
|
|
|
lock, err := c.LockOpts(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the default got set.
|
|
|
|
if lock.opts.LockWaitTime != DefaultLockWaitTime {
|
|
|
|
t.Fatalf("bad: %d", lock.opts.LockWaitTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now set a custom time for the test.
|
|
|
|
opts.LockWaitTime = 250 * time.Millisecond
|
|
|
|
lock, err = c.LockOpts(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if lock.opts.LockWaitTime != 250*time.Millisecond {
|
|
|
|
t.Fatalf("bad: %d", lock.opts.LockWaitTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get the lock.
|
|
|
|
ch, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ch == nil {
|
|
|
|
t.Fatalf("not leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try with another session.
|
|
|
|
contender, err := c.LockOpts(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
ch, err = contender.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ch != nil {
|
|
|
|
t.Fatalf("should not be leader")
|
|
|
|
}
|
2017-10-17 18:38:24 +00:00
|
|
|
diff := time.Since(start)
|
2016-01-06 17:40:20 +00:00
|
|
|
if diff < contender.opts.LockWaitTime || diff > 2*contender.opts.LockWaitTime {
|
|
|
|
t.Fatalf("time out of bounds: %9.6f", diff.Seconds())
|
2016-01-06 00:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock and then make sure the contender can get it.
|
|
|
|
if err := lock.Unlock(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ch, err = contender.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if ch == nil {
|
|
|
|
t.Fatalf("should be leader")
|
|
|
|
}
|
|
|
|
}
|