-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmightymap_redis_test.go
219 lines (190 loc) · 5.04 KB
/
mightymap_redis_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
214
215
216
217
218
219
package mightymap_test
import (
"testing"
"time"
"github.com/thisisdevelopment/mightymap"
"github.com/thisisdevelopment/mightymap/storage"
)
func TestMightyMap_RedisStorage(t *testing.T) {
store := storage.NewMightyMapRedisStorage[int, string](
storage.WithRedisExpire(1*time.Hour),
storage.WithRedisTimeout(5*time.Second),
storage.WithRedisMock(t),
)
cm := mightymap.New[int, string](true, store)
// Clean up any existing data
cm.Clear(ctx)
t.Run("Store and Load", func(t *testing.T) {
cm.Store(ctx, 1, "one")
value, ok := cm.Load(ctx, 1)
if !ok || value != "one" {
t.Errorf("Expected to load 'one', got '%v'", value)
}
})
t.Run("Has", func(t *testing.T) {
if !cm.Has(ctx, 1) {
t.Errorf("Expected key 1 to exist")
}
if cm.Has(ctx, 2) {
t.Errorf("Did not expect key 2 to exist")
}
})
t.Run("Delete", func(t *testing.T) {
cm.Delete(ctx, 1)
if cm.Has(ctx, 1) {
t.Errorf("Expected key 1 to be deleted")
}
})
t.Run("Range", func(t *testing.T) {
cm.Store(ctx, 2, "two")
cm.Store(ctx, 3, "three")
keys := make(map[int]bool)
cm.Range(ctx, func(key int, value string) bool {
keys[key] = true
return true
})
if len(keys) != 2 {
t.Errorf("Expected to range over 2 keys, got %d", len(keys))
}
})
t.Run("Pop", func(t *testing.T) {
value, ok := cm.Pop(ctx, 2)
if !ok || value != "two" {
t.Errorf("Expected to pop 'two', got '%v'", value)
}
if cm.Has(ctx, 2) {
t.Errorf("Expected key 2 to be deleted after Pop")
}
})
t.Run("Pop Nonexistent Key", func(t *testing.T) {
value, ok := cm.Pop(ctx, 42)
if ok {
t.Errorf("Expected pop to return false for nonexistent key, got value '%v'", value)
}
})
t.Run("Next", func(t *testing.T) {
cm.Store(ctx, 4, "four")
value, key, ok := cm.Next(ctx)
if !ok {
t.Errorf("Expected to get next value")
}
if value != "three" && value != "four" {
t.Errorf("Unexpected value '%v'", value)
}
if cm.Has(ctx, key) {
t.Errorf("Expected key %d to be deleted after Next", key)
}
})
t.Run("Len", func(t *testing.T) {
// Clear first to have a known state
cm.Clear(ctx)
cm.Store(ctx, 1, "one")
if cm.Len(ctx) != 1 {
t.Errorf("Expected map to have 1 item, got %d", cm.Len(ctx))
}
})
t.Run("Clear", func(t *testing.T) {
cm.Store(ctx, 5, "five")
cm.Clear(ctx)
if cm.Len(ctx) != 0 {
t.Errorf("Expected map to be empty after Clear, got length %d", cm.Len(ctx))
}
})
t.Run("Close", func(t *testing.T) {
err := cm.Close(ctx)
if err != nil {
t.Errorf("Error closing map: %v", err)
}
})
}
func TestMightyMap_RedisStorage_Configuration(t *testing.T) {
t.Run("Custom Configuration", func(t *testing.T) {
store := storage.NewMightyMapRedisStorage[int, string](
storage.WithRedisAddr("localhost:6379"),
storage.WithRedisPassword(""),
storage.WithRedisDB(0),
storage.WithRedisPoolSize(10),
storage.WithRedisMaxRetries(5),
storage.WithRedisPrefix("test_"),
storage.WithRedisExpire(1*time.Hour),
storage.WithRedisTimeout(5*time.Second),
storage.WithRedisMock(t),
)
cm := mightymap.New[int, string](true, store)
cm.Store(ctx, 1, "one")
value, ok := cm.Load(ctx, 1)
if !ok || value != "one" {
t.Errorf("Expected to load 'one', got '%v'", value)
}
err := cm.Close(ctx)
if err != nil {
t.Errorf("Error closing map: %v", err)
}
})
}
func TestMightyMap_RedisStorage_Concurrency(t *testing.T) {
store := storage.NewMightyMapRedisStorage[int, string](
storage.WithRedisMock(t),
)
cm := mightymap.New[int, string](true, store)
// Reference to existing concurrency test pattern
t.Run("Concurrent Operations", func(t *testing.T) {
const numOperations = 100
done := make(chan bool)
// Concurrent stores
for i := 0; i < numOperations; i++ {
go func(val int) {
cm.Store(ctx, val, "value")
done <- true
}(i)
}
// Wait for all stores to complete
for i := 0; i < numOperations; i++ {
<-done
}
// Verify length
if cm.Len(ctx) != numOperations {
t.Errorf("Expected length %d, got %d", numOperations, cm.Len(ctx))
}
// Concurrent loads
for i := 0; i < numOperations; i++ {
go func(val int) {
_, ok := cm.Load(ctx, val)
if !ok {
t.Errorf("Failed to load value for key %d", val)
}
done <- true
}(i)
}
// Wait for all loads to complete
for i := 0; i < numOperations; i++ {
<-done
}
cm.Clear(ctx)
err := cm.Close(ctx)
if err != nil {
t.Errorf("Error closing map: %v", err)
}
})
}
type Session struct {
UserID string
}
func TestMightyMap_RedisStorage_ComplexObjects(t *testing.T) {
store := storage.NewMightyMapRedisStorage[string, *Session](
storage.WithRedisExpire(1*time.Hour),
storage.WithRedisTimeout(5*time.Second),
storage.WithRedisPrefix("session_"),
storage.WithRedisMock(t),
)
cm := mightymap.New[string, *Session](true, store)
// Clean up any existing data
cm.Clear(ctx)
t.Run("Store and Load", func(t *testing.T) {
cm.Store(ctx, "123", &Session{UserID: "123"})
value, ok := cm.Load(ctx, "123")
if !ok || value.UserID != "123" {
t.Errorf("Expected to load 'session_123', got '%v'", value)
}
})
}