This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
/
redissync_test.go
165 lines (150 loc) · 3.89 KB
/
redissync_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package redissync
import (
"os"
"github.com/garyburd/redigo/redis"
"testing"
"time"
)
var redisAddr = os.Getenv("REDIS_ADDR")
var password = ""
var dbIndex = 15
var pool = &redis.Pool{
MaxIdle: 3,
Wait: true,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
var server = redisAddr
if server == "" {
server = "127.0.0.1:6379"
}
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
_, err = c.Do("SELECT", dbIndex)
if err != nil {
panic("Error selecting database")
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
// Intended usage
func TestRedisSync_IntendedUsage(t *testing.T) {
_, err := pool.Get().Do("FLUSHDB")
if err != nil {
t.Fatal("Error flushing database")
}
rs := &RedisSync{Key: "special flower", Pool: pool, ErrChan: make(chan error, 1), Timeout: 1 * time.Second}
rs.Lock()
err = <-rs.ErrChan
if err != nil {
t.Fatal("failed to obtain lock", err)
}
exists, err := redis.Bool(pool.Get().Do("EXISTS", "special flower.lock"))
if err != nil {
t.Fatal("eror checking if lock key exists")
}
if exists == false {
t.Fatal("lock key doesn't exist after lock")
}
hasLock := rs.HasLock()
err = <-rs.ErrChan
if err != nil {
t.Fatal("failed to check lock", rs.Key, err)
}
if !hasLock {
t.Fatal("HasLock should return true")
}
rs.Unlock()
err = <-rs.ErrChan
if err != nil {
t.Fatal("failed to unlock key", rs.Key, err)
}
exists, err = redis.Bool(pool.Get().Do("EXISTS", "special flower.lock"))
if err != nil {
t.Fatal("eror checking if lock key exists", err)
}
if exists == true {
t.Fatal("lock key still exist after unlock")
}
hasLock = rs.HasLock()
err = <-rs.ErrChan
if err != nil {
t.Fatal("failed to check lock", rs.Key, err)
}
if hasLock {
t.Fatal("HasLock should return false")
}
}
// Error trying to unlock key owned by another locker
func TestRedisSync_ErrTryingToUnlockKeyOwnedByAnotherLocker(t *testing.T) {
_, err := pool.Get().Do("FLUSHDB")
if err != nil {
t.Fatal("Error flushing database", err)
}
_, err = pool.Get().Do("SET", "special flower", "super delicate")
if err != nil {
t.Fatal("Error setting key to test", err)
}
rs1 := &RedisSync{Key: "special flower", Pool: pool, Token: "token1", ErrChan: make(chan error, 1), Timeout: 1 * time.Second}
rs1.Lock()
err = <-rs1.ErrChan
if err != nil {
t.Fatal("failed to obtain lock", err)
}
hasLock := rs1.HasLock()
err = <-rs1.ErrChan
if err != nil {
t.Fatal("failed to check lock", rs1.Key, err)
}
if !hasLock {
t.Fatal("HasLock should return true")
}
rs2 := &RedisSync{Key: "special flower", Pool: pool, Token: "token2", ErrChan: make(chan error, 1), Timeout: 1 * time.Second}
rs2.Unlock()
err = <-rs2.ErrChan
if err == nil {
t.Fatal("should have failed to unlock key", rs1.Key, err)
}
hasLock = rs2.HasLock()
err = <-rs2.ErrChan
if err != nil {
t.Fatal("failed to check lock", rs2.Key, err)
}
if hasLock {
t.Fatal("HasLock should return false")
}
}
// Error trying to obtain lock
func TestRedisSync_ErrTryingToObtainLock(t *testing.T) {
_, err := pool.Get().Do("FLUSHDB")
if err != nil {
t.Fatal("Error flushing database", err)
}
_, err = pool.Get().Do("SET", "special flower", "super delicate")
if err != nil {
t.Fatal("Error setting key to test", err)
}
rs1 := &RedisSync{Key: "special flower", Pool: pool, Token: "token1", ErrChan: make(chan error, 1), Timeout: 1 * time.Second}
rs1.Lock()
err = <-rs1.ErrChan
if err != nil {
t.Fatal("failed to obtain lock", err)
}
rs2 := &RedisSync{Key: "special flower", Pool: pool, Token: "token2", ErrChan: make(chan error, 1), Timeout: 1 * time.Millisecond}
rs2.Lock()
err = <-rs2.ErrChan
if err == nil {
t.Fatal("should have failed to obtain lock", rs1.Key, err)
}
}