-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapv2_test.go
213 lines (172 loc) · 3.47 KB
/
mapv2_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cmap
import (
"fmt"
"os"
"strconv"
"sync"
"testing"
"time"
)
func TestNewMapV2(t *testing.T) {
wg := sync.WaitGroup{}
var mv2 = NewMapV2(nil, 15, 10*5*time.Second)
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
var key = "hello" + strconv.Itoa(i)
mv2.Set(key, int(5))
v, _ := mv2.Get(key)
if v == nil {
fmt.Println(mv2.PrintDetailOf(key))
os.Exit(1)
}
if v.(int) != 5 {
panic("bad f1")
}
}(i)
wg.Add(1)
go func(i int) {
defer wg.Done()
var key = "hello2" + strconv.Itoa(i)
mv2.SetEx(key, int(5), 1)
v, _ := mv2.Get(key)
if v.(int) != 5 {
panic("bad f2 5")
}
time.Sleep(1 * time.Second)
v, _ = mv2.Get(key)
if v != nil {
fmt.Println(key)
fmt.Println(v)
fmt.Println(mv2.PrintDetailOf(key))
panic("bad f2 nil")
}
}(i)
wg.Add(1)
go func(i int) {
defer wg.Done()
key := fmt.Sprintf("%s:%d", "hello3", i)
mv2.SetNx(key, int(5))
v, _ := mv2.Get(key)
if v.(int) != 5 {
panic("bad f3 5")
}
mv2.SetNx(key, int(8))
v, _ = mv2.Get(key)
if v.(int) != 5 {
panic("bad f3 8")
}
mv2.SetExNx(key, int(10), 1)
v, _ = mv2.Get(key)
if v.(int) != 5 {
panic("bad f3 10")
}
time.Sleep(1 * time.Second)
v, _ = mv2.Get(key)
if v.(int) != 5 {
panic("bad f3 10")
}
key4 := key + "exnx" + strconv.Itoa(i)
mv2.SetExNx(key4, int(11), 2)
v, _ = mv2.Get(key4)
if v.(int) != 11 {
panic("bad f4 11")
}
mv2.SetExNx(key4, int(12), 9)
v, _ = mv2.Get(key4)
if v.(int) != 11 {
panic("bad f4 11")
}
time.Sleep(2 * time.Second)
v, _ = mv2.Get(key4)
if v != nil {
panic("bad f4 11")
}
}(i)
//
wg.Add(1)
go func(i int) {
defer wg.Done()
key := fmt.Sprintf("%s:%d", "hello5", i)
mv2.SetEx(key, 15, 7)
mv2.SetNx(key, 15)
time.Sleep(7 * time.Second)
v, _ := mv2.Get(key)
if v != nil {
panic("bad f5 15")
}
mv2.SetEx(key, 15, 1)
mv2.Set(key, 16)
time.Sleep(1 * time.Second)
v, _ = mv2.Get(key)
if v.(int) != 16 {
panic("bad f5 16")
}
mv2.Delete(key)
_, exist := mv2.Get(key)
if exist == true {
panic("bad delete")
}
}(i)
}
wg.Wait()
}
func TestNewMap1(t *testing.T) {
var mv2 = NewMap()
wg := sync.WaitGroup{}
wg.Add(100001)
for i := 0; i < 1; i++ {
func() {
defer wg.Done()
go mv2.ClearExpireKeys()
}()
}
for i := 0; i < 100000; i++ {
go func(i int) {
defer wg.Done()
// var circletimes int32 =0
// bug: 在busy时,set
// 在free时,get
mv2.Set("hello"+strconv.Itoa(i), int(5))
v, _ := mv2.Get("hello" + strconv.Itoa(i))
if v == nil {
mv2.PrintDetailOf("hello" + strconv.Itoa(i))
panic(fmt.Errorf("nil"))
os.Exit(1)
}
if v.(int) != 5 {
panic("bad f")
}
}(i)
}
wg.Wait()
// time.Sleep(10 * time.Second)
}
func BenchmarkNewMap1(b *testing.B) {
for i := 0; i < b.N; i++ {
var mv2 = NewMap()
func(i int) {
mv2.Set("hello"+strconv.Itoa(i), int(5))
v, _ := mv2.Get("hello" + strconv.Itoa(i))
if v.(int) != 5 {
panic("bad f")
}
}(i)
}
}
func TestIncrEx(t *testing.T) {
m := NewMapV2(nil, 2, 5*time.Minute)
var once = func(key string, seconds int) bool {
rs := m.IncrByEx(key, 1, seconds)
if rs == 1 {
return true
}
return false
}
fmt.Println(once("1111", 3)) // true
fmt.Println(once("1111", 3)) // false
time.Sleep(4 * time.Second)
fmt.Println(once("1111", 3)) // true
fmt.Println(once("1111", 3)) // false
}