-
Notifications
You must be signed in to change notification settings - Fork 1
/
redislock_test.go
49 lines (40 loc) · 835 Bytes
/
redislock_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
package dlock
import (
"context"
"os"
"testing"
"time"
"github.com/gomodule/redigo/redis"
)
var (
host = os.Getenv("REDIS_HOST")
pass = os.Getenv("REDIS_PASSWORD")
)
func Test__LockUnlock(t *testing.T) {
if host == "" {
return
}
// Test with a single Redis instance with password.
pool := NewRedisPool(host, WithPassword(pass))
// Use 1 Redis pool.
l := NewRedisLock("testredislock", WithPools([]*redis.Pool{pool}))
ch := make(chan error, 1)
go func() {
time.Sleep(time.Second)
l.Lock(context.Background())
t.Log("locked from goroutine")
time.Sleep(time.Second * 3)
l.Unlock()
t.Log("unlocked from goroutine")
ch <- nil
}()
// This should lock first.
err := l.Lock(context.TODO())
if err != nil {
t.Fatal(err)
}
t.Log("locked from main")
l.Unlock()
t.Log("unlocked from main")
<-ch
}