Simple test for getEntries() locking/unlocking. Related to PRs #130 and #132.
(t *testing.T)
| 249 | |
| 250 | // Simple test for getEntries() locking/unlocking. Related to PRs #130 and #132. |
| 251 | func TestCacheGetEntriesLocking(t *testing.T) { |
| 252 | ctx := testlogging.Context(t) |
| 253 | c := NewCache(&Options{ |
| 254 | MaxCachedDirectories: 4, |
| 255 | MaxCachedEntries: 100, |
| 256 | }) |
| 257 | lock := &lockState{l: c.mu} |
| 258 | c.mu = lock // allow checking the lock state below |
| 259 | |
| 260 | if len(c.data) != 0 || c.totalDirectoryEntries != 0 || c.head != nil || c.tail != nil { |
| 261 | t.Errorf("invalid initial state: %v %v %v %v", c.data, c.totalDirectoryEntries, c.head, c.tail) |
| 262 | } |
| 263 | |
| 264 | cs := newCacheSource() |
| 265 | cv := cacheVerifier{cacheSource: cs, cache: c} |
| 266 | |
| 267 | const id1 = "1" |
| 268 | |
| 269 | cs.setEntryCount(id1, 1) |
| 270 | |
| 271 | // fetch non-existing entry, the loader will return an error |
| 272 | actualEs, err := c.getEntries(ctx, id1, expirationTime, cs.get("foo"), identityWrapper) |
| 273 | if err == nil { |
| 274 | t.Fatal("Expected non-nil error when retrieving non-existing cache entry") |
| 275 | } |
| 276 | |
| 277 | const expectedEsLength = 0 |
| 278 | |
| 279 | actualEsLength := len(actualEs) |
| 280 | if actualEsLength != expectedEsLength { |
| 281 | t.Fatal("Expected empty entries, got: ", actualEsLength) |
| 282 | } |
| 283 | // cache must be unlocked at this point: See #130 |
| 284 | if !lock.Unlocked() { |
| 285 | t.Fatal("Cache is locked after returning from getEntries") |
| 286 | } |
| 287 | |
| 288 | // fetch id1 |
| 289 | _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1), identityWrapper) |
| 290 | cv.verifyCacheMiss(t, id1) |
| 291 | // fetch id1 again - cache hit, no change |
| 292 | _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1), identityWrapper) |
| 293 | cv.verifyCacheHit(t, id1) |
| 294 | // cache must be unlocked and there should be no double unlock: See #132 |
| 295 | if !lock.Unlocked() { |
| 296 | t.Fatal("Cache is locked after returning from getEntries") |
| 297 | } |
| 298 | } |
nothing calls this directly
no test coverage detected