-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
120 lines (107 loc) · 2.6 KB
/
integration_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package lock
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
var lockTable = "prod.locks"
func TestLockBasics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test")
}
rand.Seed(time.Now().UnixNano())
conf := &aws.Config{}
db := dynamodb.New(session.New(), conf.WithRegion("us-west-2"))
lk := &Locker{
NodeID: "testNode",
TableName: lockTable,
DB: db,
}
lockKey := fmt.Sprintf("test:key-%d", rand.Int63())
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
defer cancel()
locked, err := lk.Lock(ctx, lockKey, time.Now().Add(10*time.Minute))
if err != nil {
t.Fatal(err)
}
if !locked {
t.Error("failed to lock first lock")
}
// Lock again
locked, err = lk.Lock(ctx, lockKey, time.Now().Add(10*time.Minute))
if err != nil {
t.Fatal(err)
}
if !locked {
t.Fatal("failed to lock second lock")
}
// Attempt lock from another node
otherLk := &Locker{
NodeID: "testNode2",
TableName: lockTable,
DB: db,
}
olock, err := otherLk.Lock(ctx, lockKey, time.Now().Add(10*time.Minute))
if err != nil {
t.Fatalf("Err attempting to lock from another node - %s", err.Error())
}
if olock {
t.Fatal("Other node was able to aquire a locked key.")
}
err = lk.Unlock(ctx, lockKey)
if err != nil {
t.Error(err)
}
// Unlock again
err = lk.Unlock(ctx, lockKey)
if err != nil {
t.Error(err)
}
}
func TestLockExpiration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test")
}
rand.Seed(time.Now().UnixNano())
conf := &aws.Config{}
db := dynamodb.New(session.New(), conf.WithRegion("us-west-2"))
lk := &Locker{
NodeID: "testNode",
TableName: lockTable,
DB: db,
}
lockKey := fmt.Sprintf("test:key-%d", rand.Int63())
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
defer cancel()
// Lock with expiration in the past
locked, err := lk.Lock(ctx, lockKey, time.Now().Add(-10*time.Second))
if err != nil {
t.Fatal(err)
}
if !locked {
t.Fatal("failed to lock")
}
// Attempt lock from another node
otherLk := &Locker{
NodeID: "testNode2",
TableName: lockTable,
DB: db,
}
olock, err := otherLk.Lock(ctx, lockKey, time.Now().Add(10*time.Minute))
if err != nil {
t.Errorf("Err attempting to lock from another node - %s", err.Error())
}
if !olock {
t.Fatal("Unable to aquire lock even after it expired.")
}
// cleanup
err = otherLk.Unlock(ctx, lockKey)
if err != nil {
t.Error(err)
}
}