| 1 |
package sturdyc_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"math/rand/v2" |
| 6 |
"strconv" |
| 7 |
"sync" |
| 8 |
"sync/atomic" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/viccon/sturdyc" |
| 13 |
) |
| 14 |
|
| 15 |
func createBatchFn(prefix string, calls *atomic.Int32, cond *sync.Cond) sturdyc.BatchFetchFn[string] { |
| 16 |
return func(_ context.Context, ids []string) (map[string]string, error) { |
| 17 |
calls.Add(1) |
| 18 |
vals := make(map[string]string, len(ids)) |
| 19 |
for _, id := range ids { |
| 20 |
vals[id] = prefix + "-" + id |
| 21 |
} |
| 22 |
|
| 23 |
cond.L.Lock() |
| 24 |
cond.Wait() |
| 25 |
cond.L.Unlock() |
| 26 |
|
| 27 |
return vals, nil |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestBatchRequestsForMissingKeysGetDeduplicated(t *testing.T) { |
| 32 |
t.Parallel() |
| 33 |
|
| 34 |
ctx := context.Background() |
| 35 |
capacity := 10000 |
| 36 |
numShards := 100 |
| 37 |
ttl := time.Minute |
| 38 |
evictionPercentage := 10 |
| 39 |
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage) |
| 40 |
|
| 41 |
var calls atomic.Int32 |
| 42 |
cond := sync.NewCond(&sync.Mutex{}) |
| 43 |
|
| 44 |
for i := 0; i < 10; i++ { |
| 45 |
ids := make([]string, 0, 10) |
| 46 |
for j := 0; j < 10; j++ { |
| 47 |
ids = append(ids, strconv.Itoa(i*10+j)) |
| 48 |
} |
| 49 |
|
| 50 |
// Fetch the batch for 5 different sets of key prefixes. We're doing this to ensure |
| 51 |
// that the responses don't get scrambled. The in-flight tracking has to operate with |
| 52 |
// both the IDs and keys, and messing that up would be devastating. |
| 53 |
for j := 0; j < 5; j++ { |
| 54 |
keyPrefix := "foo" + strconv.Itoa(j) |
| 55 |
keyFn := c.BatchKeyFn(keyPrefix) |
| 56 |
batchFn := createBatchFn(keyPrefix, &calls, cond) |
| 57 |
go c.GetOrFetchBatch(ctx, ids, keyFn, batchFn) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// We need to make sure that these batches are in-flight before we make any more requests. |
| 62 |
time.Sleep(500 * time.Millisecond) |
| 63 |
|
| 64 |
if c.NumKeysInflight() != 500 { |
| 65 |
t.Fatalf("expected 500 keys in-flight; got %d", c.NumKeysInflight()) |
| 66 |
} |
| 67 |
|
| 68 |
// Now, while these batches are in-flight, I'm going to create additional requests for the same IDs in a loop. |
| 69 |
// On each iteration, I'm going to randomize five IDs between 0 and 99. This ensures that new requests are able |
| 70 |
// to pick IDs from any of the in-flight batches, and then merge the response. |
| 71 |
var wg sync.WaitGroup |
| 72 |
for i := 0; i < 100; i++ { |
| 73 |
wg.Add(5) |
| 74 |
for j := 0; j < 5; j++ { |
| 75 |
keyPrefix := "foo" + strconv.Itoa(j) |
| 76 |
keyFn := c.BatchKeyFn(keyPrefix) |
| 77 |
batchFn := createBatchFn(keyPrefix, &calls, cond) |
| 78 |
|
| 79 |
// We are going to get a few duplicates here too which the cache should be able to handle. |
| 80 |
ids := make([]string, 0, 5) |
| 81 |
for k := 0; k < 5; k++ { |
| 82 |
ids = append(ids, strconv.Itoa(rand.IntN(99))) |
| 83 |
} |
| 84 |
|
| 85 |
go func() { |
| 86 |
res, err := c.GetOrFetchBatch(ctx, ids, keyFn, batchFn) |
| 87 |
if err != nil { |
| 88 |
t.Errorf("expected no error got %v", err) |
| 89 |
} |
| 90 |
|
| 91 |
for _, id := range ids { |
| 92 |
want := keyPrefix + "-" + id |
| 93 |
if got, ok := res[id]; !ok || got != want { |
| 94 |
t.Errorf("expected %s got %s", want, got) |
| 95 |
} |
| 96 |
} |
| 97 |
wg.Done() |
| 98 |
}() |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Give the goroutines some time to start. |
| 103 |
time.Sleep(500 * time.Millisecond) |
| 104 |
|
| 105 |
// Assert that none of the requests above created any additional in-flight keys. |
| 106 |
if c.NumKeysInflight() != 500 { |
| 107 |
t.Fatalf("expected 500 keys in-flight; got %d", c.NumKeysInflight()) |
| 108 |
} |
| 109 |
|
| 110 |
// Broadcast that it's time for the in-flight batches to resolve. |
| 111 |
cond.Broadcast() |
| 112 |
// Wait for all of the responses to get asserted. |
| 113 |
wg.Wait() |
| 114 |
|
| 115 |
// Assert that we only made 50 calls. |
| 116 |
if got := calls.Load(); got != 50 { |
| 117 |
t.Errorf("got %d calls; wanted 50", got) |
| 118 |
} |
| 119 |
|
| 120 |
// The keys are deleted in a background routine that needs to get a lock. |
| 121 |
// Hence, to assert that they have been deleted properly, we'll have to give |
| 122 |
// that a routine a chance to run. |
| 123 |
time.Sleep(50 * time.Millisecond) |
| 124 |
if c.NumKeysInflight() > 0 { |
| 125 |
t.Errorf("expected no inflight keys; got %d", c.NumKeysInflight()) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestInflightKeysAreRemovedForBatchRequestsThatPanic(t *testing.T) { |
| 130 |
t.Parallel() |
| 131 |
|
| 132 |
ctx := context.Background() |
| 133 |
capacity := 10000 |
| 134 |
numShards := 100 |
| 135 |
ttl := time.Minute |
| 136 |
evictionPercentage := 10 |
| 137 |
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage) |
| 138 |
|
| 139 |
batchFn := func(_ context.Context, _ []string) (map[string]string, error) { |
| 140 |
panic("boom") |
| 141 |
} |
| 142 |
ids := []string{"1", "2", "3", "4", "5"} |
| 143 |
|
| 144 |
_, err := c.GetOrFetchBatch(ctx, ids, c.BatchKeyFn("foo"), batchFn) |
| 145 |
if err == nil { |
| 146 |
t.Errorf("expected an error; got nil") |
| 147 |
} |
| 148 |
|
| 149 |
time.Sleep(50 * time.Millisecond) |
| 150 |
|
| 151 |
if c.NumKeysInflight() > 0 { |
| 152 |
t.Errorf("expected no inflight keys; got %d", c.NumKeysInflight()) |
| 153 |
} |
| 154 |
|
| 155 |
if c.Size() > 0 { |
| 156 |
t.Errorf("expected no keys in cache; got %d", c.Size()) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestRequestsForMissingKeysGetDeduplicated(t *testing.T) { |
| 161 |
t.Parallel() |
| 162 |
|
| 163 |
ctx := context.Background() |
| 164 |
capacity := 100 |
| 165 |
numShards := 2 |
| 166 |
ttl := time.Minute |
| 167 |
evictionPercentage := 10 |
| 168 |
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage) |
| 169 |
|
| 170 |
ch := make(chan string) |
| 171 |
var calls atomic.Int32 |
| 172 |
fn := func(_ context.Context) (string, error) { |
| 173 |
calls.Add(1) |
| 174 |
return <-ch, nil |
| 175 |
} |
| 176 |
|
| 177 |
var wg sync.WaitGroup |
| 178 |
for i := 0; i < 10; i++ { |
| 179 |
wg.Add(1) |
| 180 |
go func() { |
| 181 |
v, err := c.GetOrFetch(ctx, "id-1", fn) |
| 182 |
if err != nil { |
| 183 |
t.Error(err) |
| 184 |
} |
| 185 |
if v != "value1" { |
| 186 |
t.Errorf("got %q; want %q", v, "value1") |
| 187 |
} |
| 188 |
wg.Done() |
| 189 |
}() |
| 190 |
} |
| 191 |
time.Sleep(50 * time.Millisecond) |
| 192 |
if c.NumKeysInflight() != 1 { |
| 193 |
t.Errorf("expected 1 inflight key; got %d", c.NumKeysInflight()) |
| 194 |
} |
| 195 |
ch <- "value1" |
| 196 |
wg.Wait() |
| 197 |
if got := calls.Load(); got != 1 { |
| 198 |
t.Errorf("got %d calls; wanted 1", got) |
| 199 |
} |
| 200 |
if c.NumKeysInflight() > 0 { |
| 201 |
t.Errorf("expected no inflight keys; got %d", c.NumKeysInflight()) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestInflightKeyIsRemovedIfTheRequestPanics(t *testing.T) { |
| 206 |
t.Parallel() |
| 207 |
|
| 208 |
ctx := context.Background() |
| 209 |
capacity := 10000 |
| 210 |
numShards := 100 |
| 211 |
ttl := time.Minute |
| 212 |
evictionPercentage := 10 |
| 213 |
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage) |
| 214 |
|
| 215 |
fetchFn := func(_ context.Context) (string, error) { |
| 216 |
panic("boom") |
| 217 |
} |
| 218 |
_, err := c.GetOrFetch(ctx, "key1", fetchFn) |
| 219 |
if err == nil { |
| 220 |
t.Errorf("expected an error; got nil") |
| 221 |
} |
| 222 |
|
| 223 |
if c.NumKeysInflight() > 0 { |
| 224 |
t.Errorf("expected no inflight keys; got %d", c.NumKeysInflight()) |
| 225 |
} |
| 226 |
|
| 227 |
if c.Size() > 0 { |
| 228 |
t.Errorf("expected no keys in cache; got %d", c.Size()) |
| 229 |
} |
| 230 |
} |