def setUp(self): super(LRUCacheTestCase, self).setUp() self.size = 5 self.cache = ExpiringLRUCache(size=self.size) self.saved_now = lru_cache._now_seconds self.now_ts = 0 lru_cache._now_seconds = lambda: self.now_ts
class LRUCacheTestCase(unittest.TestCase): def setUp(self): super(LRUCacheTestCase, self).setUp() self.size = 5 self.cache = ExpiringLRUCache(size=self.size) self.saved_now = lru_cache._now_seconds self.now_ts = 0 lru_cache._now_seconds = lambda: self.now_ts def tearDown(self): lru_cache._now_seconds = self.saved_now def test_non_existent_key(self): self.assertEqual(None, self.cache.get("abc")) def test_purges_least_recently_used(self): for i in range(self.size + 1): self.cache[i] = "a" self.assertEqual(None, self.cache.get(0)) def test_get_key(self): self.cache[1] = "a" self.assertEqual("a", self.cache[1]) def test_bumps_recently_used(self): for i in range(2 * self.size): self.cache[i] = "a" self.cache[0] = "a" self.assertEqual("a", self.cache[0]) def test_doesnt_expire_if_no_expiration(self): self.cache[1] = "a" self.now_ts = sys.maxsize self.assertEqual("a", self.cache[1]) def test_expiration(self): self.cache = ExpiringLRUCache(size=self.size, expiration_seconds=1) self.cache[1] = "a" self.now_ts = 2 self.assertEqual(None, self.cache.get(1))
def setUp(self): self.client_stub = FlawlessServiceStub() self.saved_get_get_service = flawless.client.client._get_service setattr( flawless.client.client, "_get_service", lambda: (self.client_stub, TransportStub(), flawless.client.client. HOSTPORT_INFO[0])) self.saved_config = copy.deepcopy(flawless.lib.config.get().__dict__) self.test_config = flawless.lib.config.get() self.test_config.__dict__ = dict( (o.name, o.default) for o in flawless.lib.config.OPTIONS) flawless.client.set_hostports(["localhost:9028"]) flawless.client.client.ERROR_CACHE = ExpiringLRUCache( size=flawless.client.client.LRU_CACHE_SIZE)
def __init__(self, *args, **kwargs): super(FlawlessThriftServiceHandler, self).__init__(*args, **kwargs) self.number_of_git_blames_running = 0 self.repository = get_repository( open_process_func=self.open_process_func) self.emailed_errors_cache = ExpiringLRUCache(size=1000, expiration_seconds=60 * 5) self.lock = threading.RLock() self.errors_seen = None self._refresh_errors_seen() self.persist_thread = self.thread_cls( target=self._run_background_update_thread) self.persist_thread.daemon = True self.persist_thread.start()
from flawless.server.api import Flawless log = logging.getLogger(__name__) config = flawless.lib.config.get() MAX_VARIABLE_REPR = 250 MAX_LOCALS = 100 NUM_FRAMES_TO_SAVE = 20 HOSTPORT_INFO = list() SCRUBBED_VARIABLES_REGEX = None CACHE_ERRORS_AFTER_N_OCCURRENCES = 10 REPORT_AFTER_N_MILLIS = 10 * 60 * 1000 # 10 minutes LRU_CACHE_SIZE = 200 ERROR_CACHE = ExpiringLRUCache(size=LRU_CACHE_SIZE) class HostportInfo(object): def __init__(self, hostport): host, port = hostport.split(":") self.host = host self.port = int(port) self.backoff_ms = 0 self.consecutive_connection_errors = 0 def increment_backoff(self): self.consecutive_connection_errors = max( 12, self.consecutive_connection_errors + 1) backoff = 1000 * random.randint(1, 2** self.consecutive_connection_errors)
def test_expiration(self): self.cache = ExpiringLRUCache(size=self.size, expiration_seconds=1) self.cache[1] = "a" self.now_ts = 2 self.assertEqual(None, self.cache.get(1))