def test_request_object_backup(self): cache = Cache(100) cache.set_fetcher(self.Fetcher) cache.request_object(1, "fresh", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.3) self.failUnlessEqual(self.event,("success", 1, "value1", "new")) self.event = None self.force_error = True cache.request_object(1, "new", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.3) self.failUnlessEqual(self.event,("error", 1, "Forced error")) self.event = None self.force_error = True cache.request_object(1, "new", self.object_handler, self.error_handler, self.timeout_handler, backup_state = "stale") self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.3) self.failUnlessEqual(self.event,("success", 1, "value1", "stale"))
def test_request_object_failure(self): cache = Cache(100) cache.set_fetcher(self.Fetcher) cache.request_object(-1, "fresh", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) cache.tick() self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.3) cache.tick() self.failUnlessEqual(self.event,("error", -1, "Negative address"))
def setUp(self): self.cache = Cache(100) self.event = None
def test_request_object_timeout(self): cache = Cache(100) cache.set_fetcher(self.Fetcher) cache.request_object(1, "fresh", self.object_handler, self.error_handler, self.timeout_handler, timeout=timedelta(seconds=0.1)) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) cache.tick() self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) cache.tick() sleep(0.05) cache.tick() sleep(0.05) cache.tick() sleep(0.3) cache.tick() self.failUnlessEqual(self.event,("timeout", 1))
def test_request_object_success(self): cache = Cache(100) cache.set_fetcher(self.Fetcher) cache.request_object(1, "fresh", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.3) self.failUnlessEqual(self.event,("success", 1, "value1", "new")) self.event = None cache.request_object(1, "fresh", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,("success", 1, "value1", "fresh")) self.event = None cache.request_object(2, "fresh", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.05) cache.tick() self.failUnlessEqual(self.event,None,"Early event: %r" % (self.event,)) sleep(0.3) cache.tick() self.failUnlessEqual(self.event,("success", 2, "value2", "new")) self.event = None cache.request_object(2, "fresh", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,("success", 2, "value2", "fresh")) cache.tick() self.event = None cache.request_object(1, "fresh", self.object_handler, self.error_handler, self.timeout_handler) self.failUnlessEqual(self.event,("success", 1, "value1", "fresh"))
def test_item_expiration(self): cache = Cache(100) item = CacheItem("test_addr","test_value", sec(0.1), sec(0.2), sec(0.3)) cache.add_item(item) gitem = cache.get_item("test_addr","fresh") self.failUnlessEqual(gitem,item) sleep(0.01) gitem = cache.get_item("test_addr","fresh") self.failUnlessEqual(gitem,item) sleep(0.1) gitem = cache.get_item("test_addr","fresh") self.failUnlessEqual(gitem,None) cache.tick() gitem = cache.get_item("test_addr","old") self.failUnlessEqual(gitem,item) sleep(0.3) cache.tick() gitem = cache.get_item("test_addr","stale") self.failUnlessEqual(gitem,None) self.failUnlessEqual(cache.num_items(),0,"number of items: "+`cache.num_items()`)
def test_max_items(self): cache = Cache(10) self.failUnlessEqual(cache.num_items(),0,"number of items: "+`cache.num_items()`) for i in range(10): item = CacheItem(i,i, sec(1), sec(2), sec(3)) cache.add_item(item) self.failUnlessEqual(cache.num_items(),10,"number of items: "+`cache.num_items()`) for i in range(10,30): item = CacheItem(i,i, sec(1), sec(2), sec(3)) cache.add_item(item) self.failUnless(cache.num_items() > 7,"number of items: "+`cache.num_items()`) self.failUnless(cache.num_items() <= 10,"number of items: "+`cache.num_items()`) self.failUnlessEqual(cache.num_items(),len(cache._items), "number of items: %r, in dict: %r" % (cache.num_items(), cache._items))
def test_add_get_item(self): cache = Cache(100) item1 = CacheItem("test_addr1","test_value1", sec(1), sec(2), sec(3)) cache.add_item(item1) item2 = CacheItem("test_addr2","test_value2", sec(1), sec(2), sec(3)) cache.add_item(item2) item3 = CacheItem("test_addr3","test_value3", sec(1), sec(2), sec(3)) cache.add_item(item3) gitem2 = cache.get_item("test_addr2","fresh") self.failUnless(gitem2 is item2, "gitem2=%r item2=%r" % (gitem2, item2)) gitem1 = cache.get_item("test_addr1","old") self.failUnless(gitem1 is item1, "gitem2=%r item2=%r" % (gitem2, item2)) gitem3 = cache.get_item("test_addr3","stale") self.failUnless(gitem3 is item3, "gitem2=%r item2=%r" % (gitem2, item2)) gitem1 = cache.get_item("test_addr1","new") self.failUnless(gitem1 is None, "gitem1=%r" % (gitem1,))