async def test_herd(self, r, event_loop): await r.flushdb() now = int(time.time()) cache = HerdCache(r, self.app, default_herd_timeout=1, extend_herd_timeout=1) await cache.set(self.key, self.expensive_work(self.data), self.data) await asyncio.sleep(1, loop=event_loop) # first get identity = cache._gen_identity(self.key, self.data) content = await r.get(identity) content, expect_expire_time = cache._unpack(content) assert now + 1 == expect_expire_time # HerdCach.get await asyncio.sleep(0.1, loop=event_loop) res = await cache.get(self.key, self.data) # first herd get will reset expire time and return None assert res is None # second get identity = cache._gen_identity(self.key, self.data) content = await r.get(identity) content, new_expire_time = cache._unpack(content) assert new_expire_time >= expect_expire_time + 1
async def test_set(self, r): await r.flushdb() cache = HerdCache(r, self.app, default_herd_timeout=1, extend_herd_timeout=1) now = int(time.time()) res = await cache.set(self.key, self.expensive_work(self.data), self.data) assert res identity = cache._gen_identity(self.key, self.data) content = await r.get(identity) content, expect_expire_time = cache._unpack(content) # supposed equal to 1, but may there be latency assert expect_expire_time - now <= 1 assert content == self.data