def test_write_cache(self): mock_server_status = {'fake server status': random.uniform(1, 2 ** 32)} status_cache = EntitlementStatusCache() status_cache.server_status = mock_server_status cache_dir = tempfile.mkdtemp() cache_file = os.path.join(cache_dir, 'status_cache.json') status_cache.CACHE_FILE = cache_file status_cache.write_cache() # try to load the file 5 times, if # we still can't read it, fail # we don't know when the write_cache thread ends or # when it starts. Need to track the cache threads # but we do not... tries = 0 while tries <= 5: try: new_status_buf = open(cache_file).read() new_status = json.loads(new_status_buf) break except Exception as e: log.exception(e) tries += 1 time.sleep(.1) continue shutil.rmtree(cache_dir) self.assertEqual(new_status, mock_server_status)
def test_write_cache(self): mock_server_status = {'fake server status': random.uniform(1, 2**32)} status_cache = EntitlementStatusCache() status_cache.server_status = mock_server_status cache_dir = tempfile.mkdtemp() cache_file = os.path.join(cache_dir, 'status_cache.json') status_cache.CACHE_FILE = cache_file status_cache.write_cache() def threadActive(name): for thread in threading.enumerate(): if thread.getName() == name: return True return False # If the file exists, and the thread that writes it does not, we know writing has completed while not (os.path.exists(cache_file) and not threadActive("WriteCacheEntitlementStatusCache")): pass try: new_status_buf = open(cache_file).read() new_status = json.loads(new_status_buf) self.assertEquals(new_status, mock_server_status) finally: shutil.rmtree(cache_dir)
def test_write_cache(self): mock_server_status = {'fake server status': random.uniform(1, 2 ** 32)} status_cache = EntitlementStatusCache() status_cache.server_status = mock_server_status cache_dir = tempfile.mkdtemp() cache_file = os.path.join(cache_dir, 'status_cache.json') status_cache.CACHE_FILE = cache_file status_cache.write_cache() def threadActive(name): for thread in threading.enumerate(): if thread.getName() == name: return True return False # If the file exists, and the thread that writes it does not, we know writing has completed while not (os.path.exists(cache_file) and not threadActive("WriteCacheEntitlementStatusCache")): pass try: new_status_buf = open(cache_file).read() new_status = json.loads(new_status_buf) self.assertEquals(new_status, mock_server_status) finally: shutil.rmtree(cache_dir)