-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlock_by_redis_test.go
More file actions
48 lines (40 loc) · 971 Bytes
/
dlock_by_redis_test.go
File metadata and controls
48 lines (40 loc) · 971 Bytes
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
package distlock
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
fakeRedisServiceCfg = &RedisServiceConfig{
SentinelEndpoints: []string{"localhost:26379", "localhost:26380", "localhost:26381"},
SentinelMasterName: "mymaster",
SentinelPassword: "Pwd123!@",
RedisMasterPassword: "sOmE_sEcUrE_pAsS",
RedisPoolMaxIdleConns: 3,
RedisPoolMaxActiveConns: 64,
RedisConnectTimeoutMsec: 500,
RedisReadTimeoutMsec: 500,
RedisWriteTimeoutMsec: 500,
}
)
func TestDLockByRedis(t *testing.T) {
p := EstablishRedisConn(fakeRedisServiceCfg)
defer CloseRedisConn(p)
total := 0
var n sync.WaitGroup
for i := 0; i < 200; i++ {
n.Add(1)
go func(p *RedisConnPool, idx int) {
defer n.Done()
dl := NewDLockByRedis(p)
if v, ok := dl.TryLock(5); ok && v != "" {
total++
time.Sleep(time.Microsecond * 10)
dl.Unlock(v)
}
}(p, i)
}
n.Wait()
assert.Equal(t, 200, total)
}