async def test_futures_are_wake_up_no_side_effect_on_cancellation( self, event_loop, cluster, ops): connection = CoroutineMock() connection.fetch_command = CoroutineMock( return_value=(list(ops.keys()), list(ops.keys()), None, None)) node = Mock() connection_context = AsyncMagicMock() connection_context.__aenter__.return_value = connection node.connection.return_value = connection_context cluster.pick_nodes.return_value = {node: list(ops.keys())} # InflightBatches takes the ownership of the dictionary, we concat # all futures into our own list futures = list(itertools.chain(*list(ops.values()))) # Cancel the first one futures[0].cancel() _ = _InflightBatches(ops, cluster, Mock(), event_loop, return_flags=False, return_cas=False, timeout=1.0, max_keys=1) # Wait for the other ones for future in futures[1:]: await future
async def test_timeout_value_used(self, event_loop, mocker, cluster, ops): optimeout_class = mocker.patch("emcache.autobatching.OpTimeout", AsyncMagicMock()) connection = CoroutineMock() connection.fetch_command = CoroutineMock( return_value=(list(ops.keys()), list(ops.keys()), None, None)) node = Mock() connection_context = AsyncMagicMock() connection_context.__aenter__.return_value = connection node.connection.return_value = connection_context cluster.pick_nodes.return_value = {node: list(ops.keys())} # InflightBatches takes the ownership of the dictionary, we concat # all futures into our own list futures = itertools.chain(*list(ops.values())) _ = _InflightBatches(ops, cluster, Mock(), event_loop, return_flags=False, return_cas=False, timeout=1.0, max_keys=1) for future in futures: await future # Check that the timeout value was properly used optimeout_class.assert_called_with(1.0, ANY)
async def test_signal_termination(self, event_loop, mocker, cluster, ops): on_finish = Mock() connection = CoroutineMock() connection.fetch_command = CoroutineMock( return_value=(list(ops.keys()), list(ops.keys()), None, None)) node = Mock() connection_context = AsyncMagicMock() connection_context.__aenter__.return_value = connection node.connection.return_value = connection_context cluster.pick_nodes.return_value = {node: list(ops.keys())} # InflightBatches takes the ownership of the dictionary, we concat # all futures into our own list futures = itertools.chain(*list(ops.values())) inflight_batches = _InflightBatches(ops, cluster, on_finish, event_loop, return_flags=False, return_cas=False, timeout=1.0, max_keys=1) for future in futures: await future # Check that the timeout value was properly used on_finish.assert_called_with(inflight_batches)
async def test_multiple_batches(self, event_loop, mocker, cluster, ops): connection = CoroutineMock() connection.fetch_command = CoroutineMock( return_value=(list(ops.keys()), list(ops.keys()), None, None)) node = Mock() connection_context = AsyncMagicMock() connection_context.__aenter__.return_value = connection node.connection.return_value = connection_context cluster.pick_nodes.return_value = {node: list(ops.keys())} # InflightBatches takes the ownership of the dictionary, we concat # all futures into our own list futures = itertools.chain(*list(ops.values())) # Configure batches of maximum one key per batch _ = _InflightBatches(ops, cluster, Mock(), event_loop, return_flags=False, return_cas=False, timeout=1.0, max_keys=1) for future in futures: await future # Check that two calls to the fetch command were done assert connection.fetch_command.call_count == 2
async def test_timeout_futures_are_wake_up_no_side_effect_on_cancellation( self, event_loop, mocker, cluster, ops): # force to trigger the exception at `fetch_command` level, thought is not the # where the exception will be raised is good enough for knowing if the caller # is managing as is expected the exception. connection = CoroutineMock() connection.fetch_command = CoroutineMock( side_effect=asyncio.TimeoutError) node = Mock() connection_context = AsyncMagicMock() connection_context.__aenter__.return_value = connection node.connection.return_value = connection_context cluster.pick_nodes.return_value = {node: list(ops.keys())} # InflightBatches takes the ownership of the dictionary, we concat # all futures into our own list futures = list(itertools.chain(*list(ops.values()))) # Cancel the first one futures[0].cancel() _ = _InflightBatches(ops, cluster, Mock(), event_loop, return_flags=False, return_cas=False, timeout=1.0, max_keys=1) # Wait for the other ones for future in futures[1:]: with pytest.raises(asyncio.TimeoutError): await future