-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_data_test.go
More file actions
202 lines (170 loc) · 4.29 KB
/
custom_data_test.go
File metadata and controls
202 lines (170 loc) · 4.29 KB
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
package clusterkit
import (
"context"
"sync"
"testing"
"time"
)
// TestSetCustomData verifies basic custom data storage
func TestSetCustomData(t *testing.T) {
// Create minimal ClusterKit for testing
ck := &ClusterKit{
cluster: &Cluster{
CustomData: make(map[string][]byte),
},
mu: sync.RWMutex{},
}
// Test data
key := "test-key"
value := []byte("test-value")
// Manually set data (bypassing Raft for unit test)
ck.cluster.CustomData[key] = value
// Retrieve data
retrieved, err := ck.GetCustomData(key)
if err != nil {
t.Fatalf("GetCustomData failed: %v", err)
}
if string(retrieved) != string(value) {
t.Errorf("Expected %s, got %s", value, retrieved)
}
}
// TestGetCustomDataNotFound verifies error handling for missing keys
func TestGetCustomDataNotFound(t *testing.T) {
ck := &ClusterKit{
cluster: &Cluster{
CustomData: make(map[string][]byte),
},
mu: sync.RWMutex{},
}
_, err := ck.GetCustomData("nonexistent")
if err == nil {
t.Error("Expected error for nonexistent key, got nil")
}
}
// TestCustomDataSizeLimit verifies 1MB size limit
func TestCustomDataSizeLimit(t *testing.T) {
ck := &ClusterKit{
consensusManager: &ConsensusManager{},
}
// Create data larger than 1MB
largeData := make([]byte, 1024*1024+1)
err := ck.SetCustomData("large-key", largeData)
if err == nil {
t.Error("Expected error for data exceeding 1MB limit")
}
}
// TestCustomDataConcurrency verifies thread safety
func TestCustomDataConcurrency(t *testing.T) {
ck := &ClusterKit{
cluster: &Cluster{
CustomData: make(map[string][]byte),
},
mu: sync.RWMutex{},
}
// Concurrent writes and reads
var wg sync.WaitGroup
numGoroutines := 10
// Writers
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
key := "key-" + string(rune('0'+id))
ck.cluster.CustomData[key] = []byte("value")
}(i)
}
// Readers
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_ = ck.ListCustomDataKeys()
}()
}
wg.Wait()
// Test passes if no race conditions detected
}
// TestListCustomDataKeys verifies key listing
func TestListCustomDataKeys(t *testing.T) {
ck := &ClusterKit{
cluster: &Cluster{
CustomData: map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
"key3": []byte("value3"),
},
},
mu: sync.RWMutex{},
}
keys := ck.ListCustomDataKeys()
if len(keys) != 3 {
t.Errorf("Expected 3 keys, got %d", len(keys))
}
// Verify all keys present
keyMap := make(map[string]bool)
for _, key := range keys {
keyMap[key] = true
}
for _, expectedKey := range []string{"key1", "key2", "key3"} {
if !keyMap[expectedKey] {
t.Errorf("Expected key %s not found", expectedKey)
}
}
}
// TestCustomDataContext verifies context cancellation
func TestCustomDataContext(t *testing.T) {
ck := &ClusterKit{
cluster: &Cluster{
CustomData: make(map[string][]byte),
},
mu: sync.RWMutex{},
}
// Create cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
// Should return context.Canceled error
_, err := ck.GetCustomDataContext(ctx, "test-key")
if err != context.Canceled {
t.Errorf("Expected context.Canceled, got %v", err)
}
}
// TestCustomDataContextTimeout verifies timeout handling
func TestCustomDataContextTimeout(t *testing.T) {
ck := &ClusterKit{
cluster: &Cluster{
CustomData: make(map[string][]byte),
},
mu: sync.RWMutex{},
}
// Create context with very short timeout
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
time.Sleep(10 * time.Millisecond) // Ensure timeout
_, err := ck.GetCustomDataContext(ctx, "test-key")
if err == nil {
t.Error("Expected timeout error, got nil")
}
}
// TestCustomDataCopy verifies returned data is a copy
func TestCustomDataCopy(t *testing.T) {
ck := &ClusterKit{
cluster: &Cluster{
CustomData: map[string][]byte{
"test": []byte("original"),
},
},
mu: sync.RWMutex{},
}
// Get data
retrieved, err := ck.GetCustomData("test")
if err != nil {
t.Fatalf("GetCustomData failed: %v", err)
}
// Modify retrieved data
retrieved[0] = 'X'
// Verify original data unchanged
original := ck.cluster.CustomData["test"]
if original[0] != 'o' {
t.Error("Original data was modified (copy protection failed)")
}
}