-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlock_test.go
67 lines (65 loc) · 1.3 KB
/
lock_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
package red
import (
"context"
"github.com/stretchr/testify/assert"
"log"
"sync"
"testing"
"time"
)
func MutexAction(i int, client Connecter, mutexCount *int,debug bool) {
key := "test_mutex"
debugLog := func(v ...interface{}) {
if debug {
log.Print(v...)
}
}
mutex := Mutex{
Key: key,
Expire: time.Millisecond*100,
Retry: Retry{
Times: 3,
Interval: time.Millisecond*100,
},
}
lockSuccess, unlock, err := mutex.Lock(context.TODO(), client) ; if err != nil {
debugLog(i, "锁失败")
return
}
if lockSuccess == false {
debugLog(i, "锁被占用")
return
} else {
*mutexCount++
debugLog(i, "锁成功")
}
debugLog(i, "业务操作")
err = unlock(context.TODO()) ; if err != nil {
*mutexCount--
debugLog(i, "解锁失败:", err)
return
}
debugLog(i, "解锁成功")
}
// 完整的锁测试非常复杂,暂时用简单的测试代替
func TestMutex_Lock(t *testing.T) {
debug := false
for _, connecter := range Connecters {
wg := sync.WaitGroup{}
var mutexCount = 0
max := 5
for i:=0;i<max;i++{
wg.Add(1)
go func(i int) {
MutexAction(i, connecter, &mutexCount, debug)
wg.Done()
}(i)
}
wg.Wait()
if debug {
log.Print("mutexCount", mutexCount)
}
assert.Equal(t, mutexCount > 0, true)
assert.Equal(t, mutexCount <= 5, true)
}
}