当前位置: 首页>>代码示例>>Python>>正文


Python AIOKafkaClient._maybe_wait_metadata方法代码示例

本文整理汇总了Python中aiokafka.client.AIOKafkaClient._maybe_wait_metadata方法的典型用法代码示例。如果您正苦于以下问题:Python AIOKafkaClient._maybe_wait_metadata方法的具体用法?Python AIOKafkaClient._maybe_wait_metadata怎么用?Python AIOKafkaClient._maybe_wait_metadata使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在aiokafka.client.AIOKafkaClient的用法示例。


在下文中一共展示了AIOKafkaClient._maybe_wait_metadata方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_fetcher_offsets_for_times

# 需要导入模块: from aiokafka.client import AIOKafkaClient [as 别名]
# 或者: from aiokafka.client.AIOKafkaClient import _maybe_wait_metadata [as 别名]
    def test_fetcher_offsets_for_times(self):
        client = AIOKafkaClient(
            loop=self.loop,
            bootstrap_servers=[])
        client.ready = mock.MagicMock()
        client.ready.side_effect = asyncio.coroutine(lambda a: True)
        client._maybe_wait_metadata = mock.MagicMock()
        client._maybe_wait_metadata.side_effect = asyncio.coroutine(
            lambda: False)
        client.cluster.leader_for_partition = mock.MagicMock()
        client.cluster.leader_for_partition.return_value = 0
        client._api_version = (0, 10, 1)

        subscriptions = SubscriptionState(loop=self.loop)
        fetcher = Fetcher(client, subscriptions, loop=self.loop)
        tp0 = TopicPartition("topic", 0)
        tp1 = TopicPartition("topic", 1)

        # Timeouting will result in KafkaTimeoutError
        with mock.patch.object(fetcher, "_proc_offset_requests") as mocked:
            mocked.side_effect = asyncio.TimeoutError

            with self.assertRaises(KafkaTimeoutError):
                yield from fetcher.get_offsets_by_times({tp0: 0}, 1000)

        # Broker returns UnsupportedForMessageFormatError
        with mock.patch.object(client, "send") as mocked:
            @asyncio.coroutine
            def mock_send(node_id, request):
                return OffsetResponse[1]([
                    ("topic", [(0, 43, -1, -1)]),
                    ("topic", [(1, 0, 1000, 9999)])
                ])
            mocked.side_effect = mock_send
            offsets = yield from fetcher.get_offsets_by_times(
                {tp0: 0, tp1: 0}, 1000)
            self.assertEqual(offsets, {
                tp0: None,
                tp1: OffsetAndTimestamp(9999, 1000),
            })
        # Brokers returns NotLeaderForPartitionError
        with mock.patch.object(client, "send") as mocked:
            @asyncio.coroutine
            def mock_send(node_id, request):
                return OffsetResponse[1]([
                    ("topic", [(0, 6, -1, -1)]),
                ])
            mocked.side_effect = mock_send
            with self.assertRaises(NotLeaderForPartitionError):
                yield from fetcher._proc_offset_request(
                    0, {"topic": (0, 1000)})

        # Broker returns UnknownTopicOrPartitionError
        with mock.patch.object(client, "send") as mocked:
            @asyncio.coroutine
            def mock_send(node_id, request):
                return OffsetResponse[1]([
                    ("topic", [(0, 3, -1, -1)]),
                ])
            mocked.side_effect = mock_send
            with self.assertLogs("aiokafka.consumer.fetcher", "WARN") as cm:
                with self.assertRaises(UnknownTopicOrPartitionError):
                    yield from fetcher._proc_offset_request(
                        0, {"topic": (0, 1000)})
            if cm is not None:
                self.assertIn(
                    "Received unknown topic or partition error", cm.output[0])
开发者ID:aio-libs,项目名称:aiokafka,代码行数:69,代码来源:test_fetcher.py


注:本文中的aiokafka.client.AIOKafkaClient._maybe_wait_metadata方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。