async def test_ack_deadline_cache_cache_outdated_true(subscriber_client): cache = AckDeadlineCache( subscriber_client, 'fake_subscription', 0) cache.last_refresh = time.perf_counter() assert cache.cache_outdated() cache = AckDeadlineCache( subscriber_client, 'fake_subscription', 1000) cache.last_refresh = time.perf_counter() cache.ack_deadline = float('inf') assert cache.cache_outdated()
async def test_ack_deadline_cache_get_calls_refresh_first_time( subscriber_client ): cache = AckDeadlineCache( subscriber_client, 'fake_subscription', 1) assert await cache.get() == 42 assert cache.last_refresh
async def test_ack_deadline_cache_get_refreshes_if_outdated( subscriber_client): cache = AckDeadlineCache(subscriber_client, 'fake_subscription', 0) cache.ack_deadline = 33 assert await cache.get() == 42 assert cache.last_refresh subscriber_client.get_subscription.assert_called_once()
async def test_ack_deadline_cache_get_no_call_if_not_outdated( subscriber_client): cache = AckDeadlineCache(subscriber_client, 'fake_subscription', 1000) cache.ack_deadline = 33 cache.last_refresh = time.perf_counter() assert await cache.get() == 33 subscriber_client.get_subscription.assert_not_called()
async def test_ack_deadline_cache_get_call_first_time( subscriber_client ): cache = AckDeadlineCache( subscriber_client, 'fake_subscription', 1000) cache.last_refresh = time.perf_counter() assert await cache.get() == 42 subscriber_client.get_subscription.assert_called()
async def test_ack_deadline_cache_refresh_updates_value_and_last_refresh( subscriber_client): cache = AckDeadlineCache(subscriber_client, 'fake_subscription', 1) await cache.refresh() assert cache.ack_deadline == 42 assert cache.last_refresh subscriber_client.get_subscription.assert_called_once_with( 'fake_subscription')
async def test_ack_deadline_cache_first_get_failed(subscriber_client): f = asyncio.Future() f.set_exception(RuntimeError) subscriber_client.get_subscription = MagicMock(return_value=f) cache = AckDeadlineCache(subscriber_client, 'fake_subscription', 10) assert await cache.get() == float('inf') assert cache.last_refresh subscriber_client.get_subscription.assert_called_once()
async def test_ack_deadline_cache_refresh_is_cool_about_failures( subscriber_client): f = asyncio.Future() f.set_exception(RuntimeError) subscriber_client.get_subscription = MagicMock(return_value=f) cache = AckDeadlineCache(subscriber_client, 'fake_subscription', 1) cache.ack_deadline = 55.0 await cache.refresh() assert cache.ack_deadline == 55.0 assert cache.last_refresh
async def test_ack_deadline_cache_cache_outdated_false(subscriber_client): cache = AckDeadlineCache( subscriber_client, 'fake_subscription', 1000) cache.ack_deadline = 10 cache.last_refresh = time.perf_counter() assert not cache.cache_outdated()
async def test_ack_deadline_cache_defaults(subscriber_client): cache = AckDeadlineCache( subscriber_client, 'fake_subscription', 1) assert cache.cache_timeout == 1 assert cache.ack_deadline == float('inf') assert cache.last_refresh == float('-inf')