def test_refetch_config(self): config_fetcher = ConfigFetcherMock() config_cache = InMemoryConfigCache() cache_policy = AutoPollingCachePolicy(config_fetcher, config_cache, cache_key, 2, 1, None) time.sleep(1.5) config = cache_policy.get() self.assertEqual(config, TEST_JSON) self.assertEqual(config_fetcher.get_call_count, 1) self.assertEqual(config_fetcher.get_force_fetch_count, 1) time.sleep(1.5) config = cache_policy.get() self.assertEqual(config, TEST_JSON) self.assertEqual(config_fetcher.get_call_count, 2) self.assertEqual(config_fetcher.get_force_fetch_count, 1) try: # Clear the cache cache_policy._lock.acquire_write() cache_policy._config_cache.set(cache_key, None) finally: cache_policy._lock.release_write() time.sleep(1.5) self.assertEqual(config_fetcher.get_call_count, 3) self.assertEqual(config_fetcher.get_force_fetch_count, 2) config = cache_policy.get() self.assertEqual(config, TEST_JSON) cache_policy.stop()
def test_updated_values(self): config_fetcher = ConfigFetcherCountMock() config_cache = InMemoryConfigCache() cache_policy = AutoPollingCachePolicy(config_fetcher, config_cache, cache_key, 2, 5, None) config = cache_policy.get() self.assertEqual(config, 10) time.sleep(2.200) config = cache_policy.get() self.assertEqual(config, 20) cache_policy.stop()
def test_init_wait_time_ok(self): config_fetcher = ConfigFetcherWaitMock(0) config_cache = InMemoryConfigCache() cache_policy = AutoPollingCachePolicy(config_fetcher, config_cache, cache_key, 60, 5, None) config = cache_policy.get() self.assertEqual(config, TEST_JSON) cache_policy.stop()
def test_wrong_params(self): config_fetcher = ConfigFetcherMock() config_cache = InMemoryConfigCache() cache_policy = AutoPollingCachePolicy(config_fetcher, config_cache, cache_key, 0, -1, None) time.sleep(2) config = cache_policy.get() self.assertEqual(config, TEST_JSON) cache_policy.stop()
def test_exception(self): config_fetcher = ConfigFetcherWithErrorMock(Exception("error")) config_cache = InMemoryConfigCache() cache_policy = AutoPollingCachePolicy(config_fetcher, config_cache, cache_key, 60, 1) # Get value from Config Store, which indicates a config_fetcher call value = cache_policy.get() self.assertEqual(value, None) cache_policy.stop()
def test_fetch_call_count(self): config_fetcher = ConfigFetcherMock() config_cache = InMemoryConfigCache() cache_policy = AutoPollingCachePolicy(config_fetcher, config_cache, cache_key, 2, 1, None) time.sleep(3) self.assertEqual(config_fetcher.get_call_count, 2) config = cache_policy.get() self.assertEqual(config, TEST_JSON) cache_policy.stop()
def test_init_wait_time_timeout(self): config_fetcher = ConfigFetcherWaitMock(5) config_cache = InMemoryConfigCache() start_time = time.time() cache_policy = AutoPollingCachePolicy(config_fetcher, config_cache, cache_key, 60, 1, None) config = cache_policy.get() end_time = time.time() elapsed_time = end_time - start_time self.assertEqual(config, None) self.assertTrue(elapsed_time > 1) self.assertTrue(elapsed_time < 2) cache_policy.stop()