-
Notifications
You must be signed in to change notification settings - Fork 13
/
passthrough_test.go
126 lines (108 loc) · 3.53 KB
/
passthrough_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
package sturdyc_test
import (
"context"
"errors"
"testing"
"time"
"github.com/creativecreature/sturdyc"
"github.com/google/go-cmp/cmp"
)
func TestPassthrough(t *testing.T) {
t.Parallel()
ctx := context.Background()
capacity := 10000
numShards := 100
ttl := time.Minute
evictionPercentage := 10
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage,
sturdyc.WithNoContinuousEvictions(),
)
id := "1"
numPassthroughs := 1000
fetchObserver := NewFetchObserver(numPassthroughs + 1)
fetchObserver.Response(id)
res, err := sturdyc.Passthrough(ctx, c, id, fetchObserver.Fetch)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if res != "value1" {
t.Errorf("expected value1, got %v", res)
}
for i := 0; i < numPassthroughs; i++ {
res, passthroughErr := sturdyc.Passthrough(ctx, c, id, fetchObserver.Fetch)
if passthroughErr != nil {
t.Fatalf("expected no error, got %v", passthroughErr)
}
if res != "value1" {
t.Errorf("expected value1, got %v", res)
}
}
for i := 0; i < numPassthroughs; i++ {
<-fetchObserver.FetchCompleted
}
fetchObserver.AssertFetchCount(t, numPassthroughs+1)
fetchObserver.Clear()
fetchObserver.Err(errors.New("error"))
cachedRes, err := sturdyc.Passthrough(ctx, c, id, fetchObserver.Fetch)
<-fetchObserver.FetchCompleted
if err != nil {
t.Fatal(err)
}
if cachedRes != "value1" {
t.Errorf("expected value1, got %v", cachedRes)
}
fetchObserver.AssertFetchCount(t, numPassthroughs+2)
if c.NumKeysInflight() > 0 {
t.Errorf("expected no inflight keys, got %v", c.NumKeysInflight())
}
}
func TestPassthroughBatch(t *testing.T) {
t.Parallel()
ctx := context.Background()
capacity := 10000
numShards := 100
ttl := time.Minute
evictionPercentage := 10
c := sturdyc.New[string](capacity, numShards, ttl, evictionPercentage,
sturdyc.WithNoContinuousEvictions(),
)
idBatch := []string{"1", "2", "3"}
numPassthroughs := 200
fetchObserver := NewFetchObserver(numPassthroughs + 2)
fetchObserver.BatchResponse(idBatch)
res, err := sturdyc.PassthroughBatch(ctx, c, idBatch, c.BatchKeyFn("item"), fetchObserver.FetchBatch)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !cmp.Equal(res, map[string]string{"1": "value1", "2": "value2", "3": "value3"}) {
t.Errorf("expected value1, value2, value3, got %v", res)
}
for i := 0; i < numPassthroughs; i++ {
res, passthroughErr := sturdyc.PassthroughBatch(ctx, c, idBatch, c.BatchKeyFn("item"), fetchObserver.FetchBatch)
if passthroughErr != nil {
t.Fatalf("expected no error, got %v", passthroughErr)
}
if !cmp.Equal(res, map[string]string{"1": "value1", "2": "value2", "3": "value3"}) {
t.Errorf("expected value1, value2, value3, got %v", res)
}
time.Sleep(2 * time.Millisecond)
}
// We can't say exactly how many got through because there are multiple
// goroutines running and it requires a lock to remove an in-flight key.
fetchObserver.AssertMinFetchCount(t, numPassthroughs/2)
fetchObserver.AssertMaxFetchCount(t, numPassthroughs+1)
fetchObserver.Clear()
fetchObserver.Err(errors.New("error"))
cachedRes, err := sturdyc.PassthroughBatch(ctx, c, idBatch, c.BatchKeyFn("item"), fetchObserver.FetchBatch)
<-fetchObserver.FetchCompleted
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(cachedRes, map[string]string{"1": "value1", "2": "value2", "3": "value3"}) {
t.Errorf("expected value1, value2, value3, got %v", cachedRes)
}
fetchObserver.AssertFetchCount(t, numPassthroughs+2)
if c.NumKeysInflight() > 0 {
t.Errorf("expected no inflight keys, got %v", c.NumKeysInflight())
}
}