-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker_test.go
66 lines (54 loc) · 1.36 KB
/
locker_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
package cron
import (
"context"
"github.com/go-redsync/redsync/v4/redis"
"testing"
"time"
)
type connMock struct {}
type testPool struct {}
func(c *connMock) Get(name string) (string, error) {return name, nil}
func(c *connMock) Set(name string, value string) (bool, error) {return true, nil}
func(c *connMock) SetNX(name string, value string, expiry time.Duration) (bool, error) {return true, nil}
func(c *connMock) Eval(script *redis.Script, keysAndArgs ...interface{}) (interface{}, error) {return keysAndArgs, nil}
func(c *connMock) PTTL(name string) (time.Duration, error) {return time.Second, nil}
func(c *connMock) Close() error {return nil}
func (p *testPool) Get(ctx context.Context) (redis.Conn, error) {
return &connMock{}, nil
}
func TestRedisLockerRace(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
l := newRedisLocker(&testPool{}, &testPool{})
job1, job2 := "test_job1", "test_job2"
err := l.Lock(ctx, job1)
if err != nil {
t.Fatal(err)
}
go func() {
for {
select {
case <-ctx.Done():
return
default:
err := l.Extend(ctx, job1)
if err != nil {
t.Fatal(err)
}
}
}
}()
time.Sleep(time.Second)
err = l.Lock(ctx, job2)
if err != nil {
t.Fatal(err)
}
cancel()
err = l.Unlock(ctx, job1)
if err != nil {
t.Fatal(err)
}
err = l.Unlock(ctx, job2)
if err != nil {
t.Fatal(err)
}
}