-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocker_test.go
140 lines (133 loc) · 3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package setddblock_test
import (
"bytes"
"log"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/fatih/color"
"github.com/fujiwara/logutils"
"github.com/mashiike/setddblock"
"github.com/stretchr/testify/require"
)
func TestDDBLock(t *testing.T) {
endpoint := checkDDBLocalEndpoint(t)
defer func() {
err := setddblock.Recover(recover())
require.NoError(t, err)
}()
var buf bytes.Buffer
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"debug", "warn", "error"},
MinLevel: "warn",
ModifierFuncs: []logutils.ModifierFunc{
logutils.Color(color.FgHiBlack),
logutils.Color(color.FgYellow),
logutils.Color(color.FgRed, color.Bold),
},
Writer: &buf,
}
logger := log.New(filter, "", log.LstdFlags|log.Lmsgprefix)
var wgStart, wgEnd sync.WaitGroup
wgStart.Add(1)
var total1, total2 int
var lastTime1, lastTime2 time.Time
workerNum := 10
countMax := 10
f1 := func(workerID int, l sync.Locker) {
defer func() {
err := setddblock.Recover(recover())
require.NoError(t, err)
}()
l.Lock()
defer l.Unlock()
t.Logf("f1 wroker_id = %d start", workerID)
for i := 0; i < countMax; i++ {
total1 += 1
time.Sleep(10 * time.Millisecond)
}
lastTime1 = time.Now()
t.Logf("f1 wroker_id = %d finish", workerID)
}
f2 := func(workerID int, l sync.Locker) {
defer func() {
err := setddblock.Recover(recover())
require.NoError(t, err)
}()
l.Lock()
defer l.Unlock()
t.Logf("f2 wroker_id = %d start", workerID)
for i := 0; i < countMax; i++ {
total2 += 1
time.Sleep(20 * time.Millisecond)
}
lastTime2 = time.Now()
t.Logf("f2 wroker_id = %d finish", workerID)
}
for i := 0; i < workerNum; i++ {
wgEnd.Add(2)
go func(workerID int) {
defer wgEnd.Done()
locker, err := setddblock.New(
"ddb://test/item1",
setddblock.WithDelay(true),
setddblock.WithEndpoint(endpoint),
setddblock.WithLeaseDuration(500*time.Millisecond),
setddblock.WithLogger(logger),
)
require.NoError(t, err)
wgStart.Wait()
f1(workerID, locker)
}(i + 1)
go func(workerID int) {
defer wgEnd.Done()
locker, err := setddblock.New(
"ddb://test/item2",
setddblock.WithDelay(true),
setddblock.WithEndpoint(endpoint),
setddblock.WithLeaseDuration(100*time.Millisecond),
setddblock.WithLogger(logger),
)
require.NoError(t, err)
wgStart.Wait()
f2(workerID, locker)
}(i + 1)
}
wgStart.Done()
wgEnd.Wait()
t.Log(buf.String())
require.EqualValues(t, workerNum*countMax, total1)
require.EqualValues(t, workerNum*countMax, total2)
t.Logf("f1 last = %s", lastTime1)
t.Logf("f2 last = %s", lastTime2)
require.True(t, lastTime1.After(lastTime2))
require.False(t, strings.Contains(buf.String(), "[error]"))
}
func checkDDBLocalEndpoint(t *testing.T) string {
t.Helper()
if endpoint := os.Getenv("DYNAMODB_LOCAL_ENDPOINT"); endpoint != "" {
return endpoint
}
t.Log("ddb local endpoint not set. this test skip")
t.SkipNow()
return ""
}
func TestNoPanic(t *testing.T) {
defer func() {
err := setddblock.Recover(recover())
require.NoError(t, err, "check no panic")
}()
locker, err := setddblock.New(
"ddb://test/item3",
setddblock.WithEndpoint("http://localhost:12345"), //invalid remote endpoint
setddblock.WithNoPanic(),
)
require.NoError(t, err)
locker.Lock()
require.Error(t, locker.LastErr())
locker.ClearLastErr()
locker.Unlock()
require.Error(t, locker.LastErr())
}