-
Notifications
You must be signed in to change notification settings - Fork 94
/
session_storage_test.go
183 lines (165 loc) · 3.67 KB
/
session_storage_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
package gws
import (
"testing"
"github.com/dolthub/maphash"
"github.com/lxzan/gws/internal"
"github.com/stretchr/testify/assert"
)
func TestMap(t *testing.T) {
var as = assert.New(t)
var m1 = make(map[string]any)
var m2 = newSmap()
var count = internal.AlphabetNumeric.Intn(1000)
for i := 0; i < count; i++ {
var key = string(internal.AlphabetNumeric.Generate(10))
var val = internal.AlphabetNumeric.Uint32()
m1[key] = val
m2.Store(key, val)
}
var keys = make([]string, 0)
for k, _ := range m1 {
keys = append(keys, k)
}
for i := 0; i < len(keys)/2; i++ {
delete(m1, keys[i])
m2.Delete(keys[i])
}
for k, v := range m1 {
v1, ok := m2.Load(k)
as.Equal(true, ok)
as.Equal(v, v1)
}
as.Equal(len(m1), m2.Len())
}
func TestSliceMap(t *testing.T) {
var as = assert.New(t)
var m = newSmap()
m.Store("hong", 1)
m.Store("mei", 2)
m.Store("ming", 3)
{
v, _ := m.Load("hong")
as.Equal(1, v)
}
{
m.Delete("hong")
v, ok := m.Load("hong")
as.Equal(false, ok)
as.Nil(v)
m.Store("hong", 4)
v, _ = m.Load("hong")
as.Equal(4, v)
}
}
func TestMap_Range(t *testing.T) {
var as = assert.New(t)
var m1 = make(map[any]any)
var m2 = newSmap()
var count = 1000
for i := 0; i < count; i++ {
var key = string(internal.AlphabetNumeric.Generate(10))
var val = internal.AlphabetNumeric.Uint32()
m1[key] = val
m2.Store(key, val)
}
{
var keys []any
m2.Range(func(key string, value any) bool {
v, ok := m1[key]
as.Equal(true, ok)
as.Equal(v, value)
keys = append(keys, key)
return len(keys) < 100
})
as.Equal(100, len(keys))
}
{
var keys []any
m2.Range(func(key string, value any) bool {
v, ok := m1[key]
as.Equal(true, ok)
as.Equal(v, value)
keys = append(keys, key)
return true
})
as.Equal(1000, len(keys))
}
}
func TestConcurrentMap(t *testing.T) {
var as = assert.New(t)
var m1 = make(map[string]any)
var m2 = NewConcurrentMap[string, uint32]()
as.Equal(m2.num, uint64(16))
var count = internal.AlphabetNumeric.Intn(1000)
for i := 0; i < count; i++ {
var key = string(internal.AlphabetNumeric.Generate(10))
var val = internal.AlphabetNumeric.Uint32()
m1[key] = val
m2.Store(key, val)
}
var keys = make([]string, 0)
for k, _ := range m1 {
keys = append(keys, k)
}
for i := 0; i < len(keys)/2; i++ {
delete(m1, keys[i])
m2.Delete(keys[i])
}
for k, v := range m1 {
v1, ok := m2.Load(k)
as.Equal(true, ok)
as.Equal(v, v1)
}
as.Equal(len(m1), m2.Len())
t.Run("", func(t *testing.T) {
var sum = 0
var cm = NewConcurrentMap[string, int](8, 8)
for _, item := range cm.shardings {
sum += len(item.m)
}
assert.Equal(t, sum, 0)
})
}
func TestConcurrentMap_Range(t *testing.T) {
var as = assert.New(t)
var m1 = make(map[any]any)
var m2 = NewConcurrentMap[string, uint32](13)
var count = 1000
for i := 0; i < count; i++ {
var key = string(internal.AlphabetNumeric.Generate(10))
var val = internal.AlphabetNumeric.Uint32()
m1[key] = val
m2.Store(key, val)
}
{
var keys []any
m2.Range(func(key string, value uint32) bool {
v, ok := m1[key]
as.Equal(true, ok)
as.Equal(v, value)
keys = append(keys, key)
return len(keys) < 100
})
as.Equal(100, len(keys))
}
{
var keys []any
m2.Range(func(key string, value uint32) bool {
v, ok := m1[key]
as.Equal(true, ok)
as.Equal(v, value)
keys = append(keys, key)
return true
})
as.Equal(1000, len(keys))
}
}
func TestHash(t *testing.T) {
var h = maphash.NewHasher[string]()
for i := 0; i < 1000; i++ {
var a = string(internal.AlphabetNumeric.Generate(16))
var b = string(internal.AlphabetNumeric.Generate(16))
assert.Equal(t, h.Hash(a), h.Hash(a))
assert.NotEqual(t, h.Hash(a), h.Hash(b))
}
}