MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / TestValkeyDataCache

Function TestValkeyDataCache

lib/valkeycache/cache_test.go:78–160  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

76}
77
78func TestValkeyDataCache(t *testing.T) {
79 cache := getTestValkey(t)
80 ctx := context.Background()
81
82 testKey1 := "test-key-1"
83 testValue1 := []byte("test-value")
84
85 t.Run("cache miss", func(t *testing.T) {
86 result, err := cache.Get(ctx, testKey1)
87 if !errors.Is(err, cachetypes.ErrCachedDataNotFound) {
88 t.Errorf("invalid error %v", err)
89 }
90 if result != nil {
91 t.Error("expected null result")
92 }
93 })
94
95 t.Run("cache hit", func(t *testing.T) {
96 // Store result
97 err := cache.Cache(ctx, testKey1, testValue1)
98 if !errors.Is(err, nil) {
99 t.Errorf("invalid error storing value %v", err)
100 }
101
102 // Get result.
103 result, err := cache.Get(ctx, testKey1)
104 if !errors.Is(err, nil) {
105 t.Errorf("invalid error getting value %v", err)
106 }
107 if !reflect.DeepEqual(result, testValue1) {
108 t.Error("expected result")
109 }
110
111 // Wait for TTL
112 time.Sleep(getDefatulTTL() * 2)
113 result, err = cache.Get(ctx, testKey1)
114 if !errors.Is(err, cachetypes.ErrCachedDataNotFound) {
115 t.Errorf("invalid error getting expired result %v", err)
116 }
117 if result != nil {
118 t.Error("expected null result")
119 }
120
121 })
122
123 t.Run("cache hit with custom ttl", func(t *testing.T) {
124 // Store result with custom ttl
125 err := cache.Cache(ctx, testKey1, testValue1, cachetypes.WithTTL(getDefatulTTL()*4))
126 if !errors.Is(err, nil) {
127 t.Errorf("invalid error storing value %v", err)
128 }
129
130 // Get result.
131 result, err := cache.Get(ctx, testKey1)
132 if !errors.Is(err, nil) {
133 t.Errorf("invalid error getting value %v", err)
134 }
135 if !reflect.DeepEqual(result, testValue1) {

Callers

nothing calls this directly

Calls 6

getTestValkeyFunction · 0.85
getDefatulTTLFunction · 0.85
RunMethod · 0.65
GetMethod · 0.65
CacheMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected