-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathtyped_aside_test.go
165 lines (140 loc) · 4.35 KB
/
typed_aside_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
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
package rueidisaside
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
"github.com/redis/rueidis"
)
type testStruct struct {
ID int `json:"id"`
Name string `json:"name"`
}
func TestTypedCacheAsideClient_Get(t *testing.T) {
baseClient := makeClient(t, addr)
t.Cleanup(baseClient.Close)
serializer := func(v *testStruct) (string, error) {
if v == nil {
return "nilTestStruct", nil
}
b, err := json.Marshal(v)
return string(b), err
}
deserializer := func(s string) (*testStruct, error) {
if s == "nilTestStruct" {
return nil, nil
}
var v testStruct
err := json.Unmarshal([]byte(s), &v)
return &v, err
}
client := NewTypedCacheAsideClient[testStruct](baseClient, serializer, deserializer)
t.Run("successful get and cache", func(t *testing.T) {
expected := &testStruct{ID: 1, Name: "test"}
key := randStr()
val, err := client.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (*testStruct, error) {
return expected, nil
})
if err != nil {
t.Fatal(err)
}
if val.ID != expected.ID || val.Name != expected.Name {
t.Fatalf("expected %v, got %v", expected, val)
}
// Test cached value
val2, err := client.Get(context.Background(), time.Second, key, nil)
if err != nil {
t.Fatal(err)
}
if val.ID != expected.ID || val.Name != expected.Name {
t.Fatalf("cached value mismatch: expected %v, got %v", expected, val2)
}
})
t.Run("serialization error", func(t *testing.T) {
badSerializer := func(v *testStruct) (string, error) {
return "", errors.New("serialization error")
}
clientWithBadSerializer := NewTypedCacheAsideClient[testStruct](baseClient, badSerializer, deserializer)
key := randStr()
_, err := clientWithBadSerializer.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (*testStruct, error) {
return &testStruct{ID: 1, Name: "test"}, nil
})
if err == nil {
t.Fatal("expected serialization error")
}
})
t.Run("deserialization error", func(t *testing.T) {
badDeserializer := func(s string) (*testStruct, error) {
return nil, errors.New("deserialization error")
}
clientWithBadDeserializer := NewTypedCacheAsideClient[testStruct](baseClient, serializer, badDeserializer)
key := randStr()
_, err := clientWithBadDeserializer.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (*testStruct, error) {
return &testStruct{ID: 1, Name: "test"}, nil
})
if err == nil {
t.Fatal("expected deserialization error")
}
})
t.Run("nil value handling", func(t *testing.T) {
key := randStr()
val, err := client.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (*testStruct, error) {
return nil, nil
})
if err != nil {
t.Fatalf("nil valud should not return error: %v", err)
}
if val != nil {
t.Fatalf("expected nil value, got %v", val)
}
})
}
func TestTypedCacheAsideClient_Del(t *testing.T) {
baseClient := makeClient(t, addr)
t.Cleanup(baseClient.Close)
serializer := func(v *testStruct) (string, error) {
b, err := json.Marshal(v)
return string(b), err
}
deserializer := func(s string) (*testStruct, error) {
var v testStruct
err := json.Unmarshal([]byte(s), &v)
return &v, err
}
client := NewTypedCacheAsideClient[testStruct](baseClient, serializer, deserializer)
// Set a value first
key := randStr()
testVal := &testStruct{ID: 1, Name: "test"}
_, err := client.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (*testStruct, error) {
return testVal, nil
})
if err != nil {
t.Fatal(err)
}
// Verify it's cached
_, err = client.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (*testStruct, error) {
t.Fatal("this function should not be called because the value should be cached")
return testVal, nil
})
if err != nil {
t.Fatal(err)
}
// Delete the value
err = client.Del(context.Background(), key)
if err != nil {
t.Fatal(err)
}
// Verify it's deleted
called := false
_, err = client.Get(context.Background(), time.Second, key, func(ctx context.Context, key string) (val *testStruct, err error) {
called = true
return testVal, nil
})
if err != nil && !rueidis.IsRedisNil(err) {
t.Fatal("expected error for deleted key")
}
if !called {
t.Fatal("expected function to be called because the value should be deleted")
}
}