Mercurial > hg
comparison tests/test-lrucachedict.py @ 39566:bd9d3a89f07b
util: add a popoldest() method to lrucachedict
This allows consumers to prune the oldest item from the cache. This
could be useful for e.g. a consumer that wishes for the size of
items tracked by the cache to remain under a high water mark.
Differential Revision: https://phab.mercurial-scm.org/D4501
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 05 Sep 2018 23:15:20 -0700 |
parents | 2dcc68c7d25b |
children | ee087f0d7db5 |
comparison
equal
deleted
inserted
replaced
39565:2dcc68c7d25b | 39566:bd9d3a89f07b |
---|---|
170 # Original should remain unchanged. | 170 # Original should remain unchanged. |
171 for key in ('a', 'b', 'c', 'd'): | 171 for key in ('a', 'b', 'c', 'd'): |
172 self.assertIn(key, d) | 172 self.assertIn(key, d) |
173 self.assertEqual(d[key], 'v%s' % key) | 173 self.assertEqual(d[key], 'v%s' % key) |
174 | 174 |
175 def testpopoldest(self): | |
176 d = util.lrucachedict(4) | |
177 d['a'] = 'va' | |
178 d['b'] = 'vb' | |
179 | |
180 self.assertEqual(len(d), 2) | |
181 self.assertEqual(d.popoldest(), ('a', 'va')) | |
182 self.assertEqual(len(d), 1) | |
183 self.assertEqual(d.popoldest(), ('b', 'vb')) | |
184 self.assertEqual(len(d), 0) | |
185 self.assertIsNone(d.popoldest()) | |
186 | |
187 d['a'] = 'va' | |
188 d['b'] = 'vb' | |
189 d['c'] = 'vc' | |
190 d['d'] = 'vd' | |
191 | |
192 self.assertEqual(d.popoldest(), ('a', 'va')) | |
193 self.assertEqual(len(d), 3) | |
194 for key in ('b', 'c', 'd'): | |
195 self.assertEqual(d[key], 'v%s' % key) | |
196 | |
197 d['a'] = 'va' | |
198 self.assertEqual(d.popoldest(), ('b', 'vb')) | |
199 | |
175 if __name__ == '__main__': | 200 if __name__ == '__main__': |
176 silenttestrunner.main(__name__) | 201 silenttestrunner.main(__name__) |