-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
redislock_test.go
69 lines (56 loc) · 1.82 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package redislock
import (
"context"
"strings"
"testing"
"time"
"github.com/go-co-op/gocron/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
testcontainersredis "github.com/testcontainers/testcontainers-go/modules/redis"
)
func TestEnableDistributedLocking(t *testing.T) {
ctx := context.Background()
redisContainer, err := testcontainersredis.RunContainer(ctx)
require.NoError(t, err)
t.Cleanup(func() {
if err := redisContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
uri, err := redisContainer.ConnectionString(ctx)
require.NoError(t, err)
resultChan := make(chan int, 10)
f := func(schedulerInstance int) {
resultChan <- schedulerInstance
}
redisClient := redis.NewClient(&redis.Options{Addr: strings.TrimPrefix(uri, "redis://")})
l, err := NewRedisLocker(redisClient, WithTries(1))
require.NoError(t, err)
s1, err := gocron.NewScheduler(gocron.WithDistributedLocker(l))
require.NoError(t, err)
_, err = s1.NewJob(gocron.DurationJob(500*time.Millisecond), gocron.NewTask(f, 1))
require.NoError(t, err)
s2, err := gocron.NewScheduler(gocron.WithDistributedLocker(l))
require.NoError(t, err)
_, err = s1.NewJob(gocron.DurationJob(500*time.Millisecond), gocron.NewTask(f, 2))
require.NoError(t, err)
s3, err := gocron.NewScheduler(gocron.WithDistributedLocker(l))
require.NoError(t, err)
_, err = s1.NewJob(gocron.DurationJob(500*time.Millisecond), gocron.NewTask(f, 3))
require.NoError(t, err)
s1.Start()
s2.Start()
s3.Start()
time.Sleep(1700 * time.Millisecond)
require.NoError(t, s1.Shutdown())
require.NoError(t, s2.Shutdown())
require.NoError(t, s3.Shutdown())
close(resultChan)
var results []int
for r := range resultChan {
results = append(results, r)
}
assert.Len(t, results, 4)
}