|
14 | 14 |
|
15 | 15 | from __future__ import annotations |
16 | 16 |
|
| 17 | +import sys |
17 | 18 | import uuid |
18 | 19 | from typing import Any, Sequence, cast |
19 | | -from unittest import IsolatedAsyncioTestCase, TestCase, mock |
| 20 | +from unittest import IsolatedAsyncioTestCase, TestCase, mock, skipIf |
20 | 21 |
|
21 | 22 | import aiokafka |
22 | 23 | from aiokafka import ( |
@@ -63,6 +64,10 @@ def test_instrument_api(self) -> None: |
63 | 64 | ) |
64 | 65 |
|
65 | 66 |
|
| 67 | +@skipIf( |
| 68 | + sys.version_info < (3, 10), |
| 69 | + "aiokafka >= 0.13 requires Python 3.10+", |
| 70 | +) |
66 | 71 | class TestAIOKafkaInstrumentation(TestBase, IsolatedAsyncioTestCase): |
67 | 72 | @staticmethod |
68 | 73 | def consumer_record_factory( |
@@ -126,7 +131,7 @@ async def consumer_factory(**consumer_kwargs: Any) -> AIOKafkaConsumer: |
126 | 131 |
|
127 | 132 | @staticmethod |
128 | 133 | async def producer_factory() -> AIOKafkaProducer: |
129 | | - producer = AIOKafkaProducer(api_version="1.0") |
| 134 | + producer = AIOKafkaProducer() |
130 | 135 |
|
131 | 136 | producer.client._wait_on_metadata = mock.AsyncMock() |
132 | 137 | producer.client.bootstrap = mock.AsyncMock() |
@@ -498,37 +503,43 @@ async def test_send_and_wait(self) -> None: |
498 | 503 | AIOKafkaInstrumentor().instrument(tracer_provider=self.tracer_provider) |
499 | 504 |
|
500 | 505 | producer = await self.producer_factory() |
501 | | - add_message_mock: mock.AsyncMock = ( |
502 | | - producer._message_accumulator.add_message |
503 | | - ) |
504 | | - add_message_mock.side_effect = [mock.AsyncMock()(), mock.AsyncMock()()] |
505 | | - |
506 | | - tracer = self.tracer_provider.get_tracer(__name__) |
507 | | - with tracer.start_as_current_span("test_span") as span: |
508 | | - await producer.send_and_wait("topic_1", b"value_1") |
| 506 | + try: |
| 507 | + add_message_mock: mock.AsyncMock = ( |
| 508 | + producer._message_accumulator.add_message |
| 509 | + ) |
| 510 | + add_message_mock.side_effect = [ |
| 511 | + mock.AsyncMock()(), |
| 512 | + mock.AsyncMock()(), |
| 513 | + ] |
509 | 514 |
|
510 | | - add_message_mock.assert_awaited_with( |
511 | | - TopicPartition(topic="topic_1", partition=1), |
512 | | - None, |
513 | | - b"value_1", |
514 | | - 40.0, |
515 | | - timestamp_ms=None, |
516 | | - headers=[("traceparent", mock.ANY)], |
517 | | - ) |
518 | | - assert ( |
519 | | - add_message_mock.call_args_list[0] |
520 | | - .kwargs["headers"][0][1] |
521 | | - .startswith( |
522 | | - f"00-{format_trace_id(span.get_span_context().trace_id)}-".encode() |
| 515 | + tracer = self.tracer_provider.get_tracer(__name__) |
| 516 | + with tracer.start_as_current_span("test_span") as span: |
| 517 | + await producer.send_and_wait("topic_1", b"value_1") |
| 518 | + |
| 519 | + add_message_mock.assert_awaited_with( |
| 520 | + TopicPartition(topic="topic_1", partition=1), |
| 521 | + None, |
| 522 | + b"value_1", |
| 523 | + 40.0, |
| 524 | + timestamp_ms=None, |
| 525 | + headers=[("traceparent", mock.ANY)], |
| 526 | + ) |
| 527 | + assert ( |
| 528 | + add_message_mock.call_args_list[0] |
| 529 | + .kwargs["headers"][0][1] |
| 530 | + .startswith( |
| 531 | + f"00-{format_trace_id(span.get_span_context().trace_id)}-".encode() |
| 532 | + ) |
523 | 533 | ) |
524 | | - ) |
525 | 534 |
|
526 | | - await producer.send_and_wait("topic_2", b"value_2") |
527 | | - add_message_mock.assert_awaited_with( |
528 | | - TopicPartition(topic="topic_2", partition=1), |
529 | | - None, |
530 | | - b"value_2", |
531 | | - 40.0, |
532 | | - timestamp_ms=None, |
533 | | - headers=[("traceparent", mock.ANY)], |
534 | | - ) |
| 535 | + await producer.send_and_wait("topic_2", b"value_2") |
| 536 | + add_message_mock.assert_awaited_with( |
| 537 | + TopicPartition(topic="topic_2", partition=1), |
| 538 | + None, |
| 539 | + b"value_2", |
| 540 | + 40.0, |
| 541 | + timestamp_ms=None, |
| 542 | + headers=[("traceparent", mock.ANY)], |
| 543 | + ) |
| 544 | + finally: |
| 545 | + await producer.stop() |
0 commit comments