/
lock_test.go
52 lines (42 loc) · 1.08 KB
/
lock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package clusterlock
import (
"fmt"
"os"
"testing"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/coreos/etcd/client"
"github.com/stensonb/clusterlock/retryproxy"
)
const LockTTL = 10 * time.Second
// NOTE: this test depends on an etcd cluster available
// on 127.0.0.1:2379 (the default port)
// TODO: remove the dependence on etcd for testing
func TestLockManager(t *testing.T) {
t.Parallel()
cfg := client.Config{
Endpoints: []string{"http://127.0.0.1:2379"},
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
c, _ := client.New(cfg)
ecrpErrorChan := make(chan error)
ecrp := retryproxy.NewEtcdClientRetryProxy(c, ecrpErrorChan, 1, 60)
path := "locktest"
hn, _ := os.Hostname()
path += hn
path += uuid.New()
ttl := LockTTL
lm := NewLockManager(ecrp, path, ttl)
defer lm.Shutdown()
closeit := time.After(10 * time.Second)
for {
select {
case <-closeit:
return
case <-time.After(time.Second):
fmt.Printf("Have Lock: %t\n", lm.HaveLock())
}
}
}