| 1 |
package sturdyc_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/google/go-cmp/cmp" |
| 10 |
"github.com/viccon/sturdyc" |
| 11 |
) |
| 12 |
|
| 13 |
func TestPassthrough(t *testing.T) { |
| 14 |
t.Parallel() |
| 15 |
|
| 16 |
ctx := context.Background() |
| 17 |
capacity := 10000 |
| 18 |
numShards := 100 |
| 19 |
ttl := time.Minute |
| 20 |
evictionPercentage := 10 |
| 21 |
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage, |
| 22 |
sturdyc.WithNoContinuousEvictions(), |
| 23 |
) |
| 24 |
|
| 25 |
id := "1" |
| 26 |
numPassthroughs := 1000 |
| 27 |
fetchObserver := NewFetchObserver(numPassthroughs + 1) |
| 28 |
fetchObserver.Response(id) |
| 29 |
|
| 30 |
res, err := sturdyc.Passthrough(ctx, c, id, fetchObserver.Fetch) |
| 31 |
if err != nil { |
| 32 |
t.Fatalf("expected no error, got %v", err) |
| 33 |
} |
| 34 |
if res != "value1" { |
| 35 |
t.Errorf("expected value1, got %v", res) |
| 36 |
} |
| 37 |
|
| 38 |
for i := 0; i < numPassthroughs; i++ { |
| 39 |
res, passthroughErr := sturdyc.Passthrough(ctx, c, id, fetchObserver.Fetch) |
| 40 |
if passthroughErr != nil { |
| 41 |
t.Fatalf("expected no error, got %v", passthroughErr) |
| 42 |
} |
| 43 |
|
| 44 |
if res != "value1" { |
| 45 |
t.Errorf("expected value1, got %v", res) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
for i := 0; i < numPassthroughs; i++ { |
| 50 |
<-fetchObserver.FetchCompleted |
| 51 |
} |
| 52 |
|
| 53 |
fetchObserver.AssertFetchCount(t, numPassthroughs+1) |
| 54 |
|
| 55 |
fetchObserver.Clear() |
| 56 |
fetchObserver.Err(errors.New("error")) |
| 57 |
cachedRes, err := sturdyc.Passthrough(ctx, c, id, fetchObserver.Fetch) |
| 58 |
<-fetchObserver.FetchCompleted |
| 59 |
if err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
if cachedRes != "value1" { |
| 63 |
t.Errorf("expected value1, got %v", cachedRes) |
| 64 |
} |
| 65 |
fetchObserver.AssertFetchCount(t, numPassthroughs+2) |
| 66 |
if c.NumKeysInflight() > 0 { |
| 67 |
t.Errorf("expected no inflight keys, got %v", c.NumKeysInflight()) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestPassthroughBatch(t *testing.T) { |
| 72 |
t.Parallel() |
| 73 |
|
| 74 |
ctx := context.Background() |
| 75 |
capacity := 10000 |
| 76 |
numShards := 100 |
| 77 |
ttl := time.Minute |
| 78 |
evictionPercentage := 10 |
| 79 |
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage, |
| 80 |
sturdyc.WithNoContinuousEvictions(), |
| 81 |
) |
| 82 |
|
| 83 |
idBatch := []string{"1", "2", "3"} |
| 84 |
numPassthroughs := 200 |
| 85 |
fetchObserver := NewFetchObserver(numPassthroughs + 2) |
| 86 |
fetchObserver.BatchResponse(idBatch) |
| 87 |
|
| 88 |
res, err := sturdyc.PassthroughBatch(ctx, c, idBatch, c.BatchKeyFn("item"), fetchObserver.FetchBatch) |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("expected no error, got %v", err) |
| 91 |
} |
| 92 |
if !cmp.Equal(res, map[string]string{"1": "value1", "2": "value2", "3": "value3"}) { |
| 93 |
t.Errorf("expected value1, value2, value3, got %v", res) |
| 94 |
} |
| 95 |
|
| 96 |
for i := 0; i < numPassthroughs; i++ { |
| 97 |
res, passthroughErr := sturdyc.PassthroughBatch(ctx, c, idBatch, c.BatchKeyFn("item"), fetchObserver.FetchBatch) |
| 98 |
if passthroughErr != nil { |
| 99 |
t.Fatalf("expected no error, got %v", passthroughErr) |
| 100 |
} |
| 101 |
if !cmp.Equal(res, map[string]string{"1": "value1", "2": "value2", "3": "value3"}) { |
| 102 |
t.Errorf("expected value1, value2, value3, got %v", res) |
| 103 |
} |
| 104 |
time.Sleep(2 * time.Millisecond) |
| 105 |
} |
| 106 |
|
| 107 |
// We can't say exactly how many got through because there are multiple |
| 108 |
// goroutines running and it requires a lock to remove an in-flight key. |
| 109 |
fetchObserver.AssertMinFetchCount(t, numPassthroughs/2) |
| 110 |
fetchObserver.AssertMaxFetchCount(t, numPassthroughs+1) |
| 111 |
|
| 112 |
fetchObserver.Clear() |
| 113 |
fetchObserver.Err(errors.New("error")) |
| 114 |
cachedRes, err := sturdyc.PassthroughBatch(ctx, c, idBatch, c.BatchKeyFn("item"), fetchObserver.FetchBatch) |
| 115 |
<-fetchObserver.FetchCompleted |
| 116 |
if err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
if !cmp.Equal(cachedRes, map[string]string{"1": "value1", "2": "value2", "3": "value3"}) { |
| 120 |
t.Errorf("expected value1, value2, value3, got %v", cachedRes) |
| 121 |
} |
| 122 |
fetchObserver.AssertFetchCount(t, numPassthroughs+2) |
| 123 |
if c.NumKeysInflight() > 0 { |
| 124 |
t.Errorf("expected no inflight keys, got %v", c.NumKeysInflight()) |
| 125 |
} |
| 126 |
} |