Skip to content

Commit

Permalink
add (un-called) invariant-checking method to AsyncLRUCache
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Jun 26, 2011
1 parent a1f545a commit 4b4dffd
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions master/buildbot/util/lru.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ def _purge(self):
del cache[k]
del refcount[k]

def inv(self):
"""Check invariants and log if they are not met; used for debugging"""

# the keys of the queue and cache should be identical
cache_keys = set(self.cache.keys())
queue_keys = set(self.queue)
if queue_keys - cache_keys:
log.msg("INV: uncached keys in queue:", queue_keys - cache_keys)
if cache_keys - queue_keys:
log.msg("INV: unqueued keys in cache:", cache_keys - queue_keys)

# refcount should always represent the number of times each key appears
# in the queue
exp_refcount = dict()
for k in self.queue:
exp_refcount[k] = exp_refcount.get(k, 0) + 1
if exp_refcount != self.refcount:
log.msg("INV: refcounts differ:")
log.msg(" expected:", sorted(exp_refcount.items()))
log.msg(" got:", sorted(self.refcount.items()))


def put(self, key, value):
"""
Update the cache with the given key and value, if the key is already in
Expand Down

0 comments on commit 4b4dffd

Please sign in to comment.