def test_get_most_recent_version_grace_period_lock_acquired(): """Test for _get_most_recent_version when entry is in grace period. When the entry is in grace_period and we acquire the lock, we should go to the provider store """ store = MockProviderStore() name = "material" provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0) provider._get_most_recent_version(TtlActions.GRACE_PERIOD) assert len(provider._cache._cache) == 1 expected_calls = [ ("max_version", name), ("get_or_create_provider", name, 0), ("version_from_material_description", 0), ] assert store.provider_calls == expected_calls
def test_failed_lock_acquisition(): store = MagicMock(__class__=ProviderStore) provider = CachingMostRecentProvider(provider_store=store, material_name="my material", version_ttl=10.0) provider._version = 9 provider._cache.put(provider._version, (time.time(), sentinel.nine)) with provider._lock: test = provider._get_most_recent_version( ttl_action=TtlActions.GRACE_PERIOD) assert test is sentinel.nine assert not store.mock_calls
def test_get_most_recent_version_expired(): """Test for _get_most_recent_version when entry is expired. When the entry is expired, we should check the cache before going to the provider store. """ store = MockProviderStore() name = "material" provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0) provider._cache = MagicMock() provider._cache.get.return_value = (sentinel.timestamp, sentinel.provider) test1 = provider._get_most_recent_version(TtlActions.EXPIRED) assert test1 == sentinel.provider expected_calls = [] assert store.provider_calls == expected_calls
def test_get_most_recent_version_grace_period_lock_not_acquired(): """Test for _get_most_recent_version when entry is in grace period. When the entry is in grace_period and we do not acquire the lock, we should not go to the provider store """ store = MockProviderStore() name = "material" provider = CachingMostRecentProvider(provider_store=store, material_name=name, version_ttl=0.0) provider._cache = MagicMock() provider._cache.get.return_value = (sentinel.timestamp, sentinel.provider) provider._lock = MagicMock() provider._lock.acquire.return_value = False test = provider._get_most_recent_version(TtlActions.GRACE_PERIOD) assert test == sentinel.provider expected_calls = [] assert store.provider_calls == expected_calls