TestStaleCacheOnError verifies that stale cache is returned when registry fails
(t *testing.T)
| 176 | |
| 177 | // TestStaleCacheOnError verifies that stale cache is returned when registry fails |
| 178 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…