MCPcopy
hub / github.com/micro/go-micro / TestStaleCacheOnError

Function TestStaleCacheOnError

registry/cache/cache_test.go:178–220  ·  view source on GitHub ↗

TestStaleCacheOnError verifies that stale cache is returned when registry fails

(t *testing.T)

Source from the content-addressed store, hash-verified

176
177// TestStaleCacheOnError verifies that stale cache is returned when registry fails
178func TestStaleCacheOnError(t *testing.T) {
179 mock := &mockRegistry{
180 services: []*registry.Service{
181 {
182 Name: "test.service",
183 Version: "1.0.0",
184 Nodes: []*registry.Node{
185 {Id: "node1", Address: "localhost:9090"},
186 },
187 },
188 },
189 }
190
191 // Type assertion to *cache is necessary to access internal state for verification
192 c := New(mock, func(o *Options) {
193 o.TTL = 100 * time.Millisecond // Short TTL for testing
194 o.Logger = logger.DefaultLogger
195 }).(*cache)
196
197 // First request - should populate cache
198 services, err := c.GetService("test.service")
199 if err != nil {
200 t.Fatalf("First request failed: %v", err)
201 }
202 if len(services) != 1 {
203 t.Fatalf("Expected 1 service, got %d", len(services))
204 }
205
206 // Wait for cache to expire
207 time.Sleep(150 * time.Millisecond)
208
209 // Configure mock to fail
210 mock.err = errors.New("etcd unavailable")
211
212 // Second request - should return stale cache despite error
213 services, err = c.GetService("test.service")
214 if err != nil {
215 t.Errorf("Should have returned stale cache, got error: %v", err)
216 }
217 if len(services) != 1 {
218 t.Errorf("Expected stale cache with 1 service, got %d", len(services))
219 }
220}
221
222// TestCachePenetrationPrevention verifies the complete flow:
223// 1. Cache populated

Callers

nothing calls this directly

Calls 5

NewFunction · 0.70
GetServiceMethod · 0.65
FatalfMethod · 0.45
SleepMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…