| 1 |
package sturdyc_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"math/rand/v2" |
| 7 |
"sort" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/google/go-cmp/cmp" |
| 13 |
) |
| 14 |
|
| 15 |
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
| 16 |
|
| 17 |
func randKey(n int) string { |
| 18 |
b := make([]rune, n) |
| 19 |
for i := range b { |
| 20 |
b[i] = letters[rand.IntN(len(letters))] |
| 21 |
} |
| 22 |
return string(b) |
| 23 |
} |
| 24 |
|
| 25 |
type TestMetricsRecorder struct { |
| 26 |
sync.Mutex |
| 27 |
cacheHits int |
| 28 |
cacheMisses int |
| 29 |
backgroundRefreshes int |
| 30 |
synchronousRefreshes int |
| 31 |
missingRecords int |
| 32 |
evictions int |
| 33 |
forcedEvictions int |
| 34 |
evictedEntries int |
| 35 |
shards map[int]int |
| 36 |
batchSizes []int |
| 37 |
} |
| 38 |
|
| 39 |
func newTestMetricsRecorder(numShards int) *TestMetricsRecorder { |
| 40 |
return &TestMetricsRecorder{ |
| 41 |
shards: make(map[int]int, numShards), |
| 42 |
batchSizes: make([]int, 0), |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func (r *TestMetricsRecorder) CacheHit() { |
| 47 |
r.Lock() |
| 48 |
defer r.Unlock() |
| 49 |
r.cacheHits++ |
| 50 |
} |
| 51 |
|
| 52 |
func (r *TestMetricsRecorder) CacheMiss() { |
| 53 |
r.Lock() |
| 54 |
defer r.Unlock() |
| 55 |
r.cacheMisses++ |
| 56 |
} |
| 57 |
|
| 58 |
func (r *TestMetricsRecorder) AsynchronousRefresh() { |
| 59 |
r.Lock() |
| 60 |
defer r.Unlock() |
| 61 |
r.backgroundRefreshes++ |
| 62 |
} |
| 63 |
|
| 64 |
func (r *TestMetricsRecorder) SynchronousRefresh() { |
| 65 |
r.Lock() |
| 66 |
defer r.Unlock() |
| 67 |
r.synchronousRefreshes++ |
| 68 |
} |
| 69 |
|
| 70 |
func (r *TestMetricsRecorder) MissingRecord() { |
| 71 |
r.Lock() |
| 72 |
defer r.Unlock() |
| 73 |
r.missingRecords++ |
| 74 |
} |
| 75 |
|
| 76 |
func (r *TestMetricsRecorder) ObserveCacheSize(_ func() int) {} |
| 77 |
|
| 78 |
func (r *TestMetricsRecorder) CacheBatchRefreshSize(n int) { |
| 79 |
r.Lock() |
| 80 |
defer r.Unlock() |
| 81 |
r.batchSizes = append(r.batchSizes, n) |
| 82 |
} |
| 83 |
|
| 84 |
func (r *TestMetricsRecorder) Eviction() { |
| 85 |
r.Lock() |
| 86 |
defer r.Unlock() |
| 87 |
r.evictions++ |
| 88 |
} |
| 89 |
|
| 90 |
func (r *TestMetricsRecorder) ForcedEviction() { |
| 91 |
r.Lock() |
| 92 |
defer r.Unlock() |
| 93 |
r.forcedEvictions++ |
| 94 |
} |
| 95 |
|
| 96 |
func (r *TestMetricsRecorder) EntriesEvicted(n int) { |
| 97 |
r.Lock() |
| 98 |
defer r.Unlock() |
| 99 |
r.evictedEntries += n |
| 100 |
} |
| 101 |
|
| 102 |
func (r *TestMetricsRecorder) ShardIndex(index int) { |
| 103 |
r.Lock() |
| 104 |
defer r.Unlock() |
| 105 |
r.shards[index]++ |
| 106 |
} |
| 107 |
|
| 108 |
func (r *TestMetricsRecorder) validateShardDistribution(t *testing.T, tolerancePercentage int) { |
| 109 |
t.Helper() |
| 110 |
|
| 111 |
var sum int |
| 112 |
for _, value := range r.shards { |
| 113 |
sum += value |
| 114 |
} |
| 115 |
mean := float64(sum) / float64(len(r.shards)) |
| 116 |
upperLimit := mean * (1 + float64(tolerancePercentage)/100) |
| 117 |
lowerLimit := mean * (1 - float64(tolerancePercentage)/100) |
| 118 |
var sb strings.Builder |
| 119 |
for shardIndex, shardSize := range r.shards { |
| 120 |
if float64(shardSize) > upperLimit || float64(shardSize) < lowerLimit { |
| 121 |
if sb.Len() < 1 { |
| 122 |
sb.WriteString("\n") |
| 123 |
sb.WriteString("shard distribution is outside of acceptable tolerance.\n") |
| 124 |
sb.WriteString(fmt.Sprintf("mean: %.1f; tolerance: ±%d percent; limits: [%.1f, %.1f]\n", |
| 125 |
mean, tolerancePercentage, lowerLimit, upperLimit, |
| 126 |
)) |
| 127 |
} |
| 128 |
sb.WriteString(fmt.Sprintf("shard%d size: %d\n", shardIndex, shardSize)) |
| 129 |
} |
| 130 |
} |
| 131 |
if sb.Len() > 0 { |
| 132 |
t.Error(sb.String()) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
type FetchObserver struct { |
| 137 |
sync.Mutex |
| 138 |
fetchCount int |
| 139 |
requestedRecords []string |
| 140 |
response string |
| 141 |
batchResponse map[string]string |
| 142 |
err error |
| 143 |
FetchCompleted chan struct{} |
| 144 |
} |
| 145 |
|
| 146 |
func NewFetchObserver(bufferSize int) *FetchObserver { |
| 147 |
return &FetchObserver{ |
| 148 |
requestedRecords: make([]string, 0), |
| 149 |
FetchCompleted: make(chan struct{}, bufferSize), |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func (f *FetchObserver) Response(id string) { |
| 154 |
f.Lock() |
| 155 |
defer f.Unlock() |
| 156 |
f.response = "value" + id |
| 157 |
} |
| 158 |
|
| 159 |
// BatchResponse adds a response to the response cache for each id in ids. |
| 160 |
func (f *FetchObserver) BatchResponse(ids []string) { |
| 161 |
f.Lock() |
| 162 |
defer f.Unlock() |
| 163 |
|
| 164 |
responseMap := make(map[string]string, len(ids)) |
| 165 |
for _, id := range ids { |
| 166 |
responseMap[id] = "value" + id |
| 167 |
} |
| 168 |
f.batchResponse = responseMap |
| 169 |
} |
| 170 |
|
| 171 |
func (f *FetchObserver) Err(err error) { |
| 172 |
f.err = err |
| 173 |
} |
| 174 |
|
| 175 |
// Clear resets the responses, requested records, and error state. |
| 176 |
func (f *FetchObserver) Clear() { |
| 177 |
f.Lock() |
| 178 |
defer f.Unlock() |
| 179 |
|
| 180 |
f.response = "" |
| 181 |
f.batchResponse = make(map[string]string) |
| 182 |
f.requestedRecords = make([]string, 0) |
| 183 |
f.err = nil |
| 184 |
} |
| 185 |
|
| 186 |
func (f *FetchObserver) Fetch(_ context.Context) (string, error) { |
| 187 |
f.Lock() |
| 188 |
defer func() { |
| 189 |
f.FetchCompleted <- struct{}{} |
| 190 |
f.Unlock() |
| 191 |
}() |
| 192 |
|
| 193 |
f.fetchCount++ |
| 194 |
return f.response, f.err |
| 195 |
} |
| 196 |
|
| 197 |
func (f *FetchObserver) FetchBatch(_ context.Context, ids []string) (map[string]string, error) { |
| 198 |
f.Lock() |
| 199 |
defer func() { |
| 200 |
f.FetchCompleted <- struct{}{} |
| 201 |
f.Unlock() |
| 202 |
}() |
| 203 |
|
| 204 |
copiedIDs := make([]string, len(ids)) |
| 205 |
copy(copiedIDs, ids) |
| 206 |
f.requestedRecords = copiedIDs |
| 207 |
f.fetchCount++ |
| 208 |
|
| 209 |
if f.err != nil { |
| 210 |
return nil, f.err |
| 211 |
} |
| 212 |
|
| 213 |
response := make(map[string]string) |
| 214 |
for _, id := range ids { |
| 215 |
if val, ok := f.batchResponse[id]; ok { |
| 216 |
response[id] = val |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return response, f.err |
| 221 |
} |
| 222 |
|
| 223 |
func (f *FetchObserver) AssertRequestedRecords(t *testing.T, ids []string) { |
| 224 |
t.Helper() |
| 225 |
f.Lock() |
| 226 |
defer f.Unlock() |
| 227 |
|
| 228 |
sort.Strings(ids) |
| 229 |
sort.Strings(f.requestedRecords) |
| 230 |
if !cmp.Equal(ids, f.requestedRecords) { |
| 231 |
t.Error(cmp.Diff(ids, f.requestedRecords)) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func (f *FetchObserver) AssertFetchCount(t *testing.T, count int) { |
| 236 |
t.Helper() |
| 237 |
f.Lock() |
| 238 |
defer f.Unlock() |
| 239 |
|
| 240 |
if count != f.fetchCount { |
| 241 |
t.Errorf("expected fetch count %d, got %d", count, f.fetchCount) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func (f *FetchObserver) AssertMaxFetchCount(t *testing.T, count int) { |
| 246 |
t.Helper() |
| 247 |
f.Lock() |
| 248 |
defer f.Unlock() |
| 249 |
|
| 250 |
if f.fetchCount > count { |
| 251 |
t.Errorf("expected fetch count to be at most %d, got %d", count, f.fetchCount) |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
func (f *FetchObserver) AssertMinFetchCount(t *testing.T, count int) { |
| 256 |
t.Helper() |
| 257 |
f.Lock() |
| 258 |
defer f.Unlock() |
| 259 |
|
| 260 |
if f.fetchCount < count { |
| 261 |
t.Errorf("expected fetch count to be at minimum %d, got %d", count, f.fetchCount) |
| 262 |
} |
| 263 |
} |