-
Notifications
You must be signed in to change notification settings - Fork 4
/
spindle_test.go
59 lines (48 loc) · 936 Bytes
/
spindle_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
53
54
55
56
57
58
59
package spindle
import (
"context"
"testing"
"time"
"cloud.google.com/go/spanner"
gaxv2 "github.com/googleapis/gax-go/v2"
)
const (
db = "projects/test-project/instances/test-instance/databases/testdb"
)
func TestLock(t *testing.T) {
ctx := context.Background()
client, err := spanner.NewClient(ctx, db)
if err != nil {
t.Error(err)
return
}
defer client.Close()
done := make(chan error, 1)
quit, cancel := context.WithCancel(ctx)
lock := New(client, "locktable", "mylock", WithDuration(5000))
lock.Run(quit, done)
var cnt int
bo := gaxv2.Backoff{
Initial: time.Second,
Max: time.Second * 30,
}
for {
cnt++
locked, token := lock.HasLock()
switch {
case locked:
t.Logf("lock obtained, token=%v", token)
break
default:
t.Log("lock not obtained, retry")
time.Sleep(bo.Pause())
continue
}
if cnt >= 10 {
t.Fatalf("can't get lock")
}
break
}
cancel()
<-done
}