pytest-kafka-broker 0.3.1__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,9 @@ import asyncio
2
2
  import subprocess
3
3
  from collections.abc import AsyncGenerator
4
4
  from dataclasses import dataclass
5
+ from errno import EADDRINUSE
5
6
  from pathlib import Path
7
+ from socket import socket
6
8
  from tarfile import TarFile
7
9
  from tempfile import TemporaryDirectory
8
10
  from uuid import uuid4
@@ -75,6 +77,36 @@ config
75
77
  """
76
78
 
77
79
 
80
+ def _unused_tcp_port(default: int = 0) -> int:
81
+ with socket() as sock:
82
+ try:
83
+ sock.bind(("127.0.0.1", default))
84
+ except OSError as e:
85
+ if e.errno != EADDRINUSE:
86
+ raise
87
+ sock.bind(("127.0.0.1", 0))
88
+ _, port = sock.getsockname()
89
+ return port
90
+
91
+
92
+ @pytest.fixture
93
+ def find_unused_tcp_port():
94
+ """Unused TCP port factory.
95
+
96
+ This is similar to `unused_tcp_port_factory` from pytest_asyncio, but it
97
+ supports a default port argument, and is not session-scoped.
98
+ """
99
+ used = set()
100
+
101
+ def factory(default: int = 0) -> int:
102
+ while (port := _unused_tcp_port(default)) in used:
103
+ pass
104
+ used.add(port)
105
+ return port
106
+
107
+ return factory
108
+
109
+
78
110
  @dataclass
79
111
  class KafkaBrokerContext:
80
112
  """Information and convenience methods for a temporary Kafka cluster.
@@ -120,7 +152,7 @@ del _doc
120
152
 
121
153
  @pytest_asyncio.fixture
122
154
  async def kafka_broker(
123
- kafka_home, tmp_path, unused_tcp_port_factory
155
+ kafka_home, tmp_path, find_unused_tcp_port
124
156
  ) -> AsyncGenerator[KafkaBrokerContext]:
125
157
  """Pytest fixture to run a local, temporary Kafka broker."""
126
158
  kafka_storage = kafka_home / "bin" / "kafka-storage.sh"
@@ -131,8 +163,8 @@ async def kafka_broker(
131
163
  log_path = tmp_path / "log"
132
164
  log_path.mkdir()
133
165
  env = {"LOG_DIR": str(log_path)}
134
- plaintext_port = unused_tcp_port_factory()
135
- controller_port = unused_tcp_port_factory()
166
+ plaintext_port = find_unused_tcp_port(9092)
167
+ controller_port = find_unused_tcp_port(9093)
136
168
  config_path.write_text(
137
169
  f"""
138
170
  process.roles=broker,controller
@@ -183,7 +215,9 @@ async def kafka_broker(
183
215
  port.cancel()
184
216
  raise RuntimeError("Kafka broker terminated unexpectedly")
185
217
  try:
186
- yield KafkaBrokerContext(f"127.0.0.1:{plaintext_port}")
218
+ bootstrap_server = f"127.0.0.1:{plaintext_port}"
219
+ print(f"Kafka broker running at {bootstrap_server}")
220
+ yield KafkaBrokerContext(bootstrap_server)
187
221
  finally:
188
222
  with Status("Stopping Kafka broker"):
189
223
  try:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-kafka-broker
3
- Version: 0.3.1
3
+ Version: 0.5.0
4
4
  Summary: Pytest plugin to run a single-broker Kafka cluster
5
5
  Author-email: Leo Singer <leo.singer@ligo.org>
6
6
  License-Expression: Apache-2.0
@@ -0,0 +1,7 @@
1
+ pytest_kafka_broker/__init__.py,sha256=N7nTmm22Dp8TCWDImJysySVDs0eq9k-DrW1swiwBNOs,7415
2
+ pytest_kafka_broker/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ pytest_kafka_broker-0.5.0.dist-info/METADATA,sha256=UQHkABMoQmBsZptt6rPAHnQpnuO5-sMbF7K_oLBJkpc,683
4
+ pytest_kafka_broker-0.5.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
5
+ pytest_kafka_broker-0.5.0.dist-info/entry_points.txt,sha256=mjFsRbu6FOiZkUOlSaXpo4QdLHwniNB-p_NEIgRbDnw,46
6
+ pytest_kafka_broker-0.5.0.dist-info/top_level.txt,sha256=nTrYx9xVeK5hsqbhBRL2bgBV_ea-J66_f4Dk8eD-Ci0,20
7
+ pytest_kafka_broker-0.5.0.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- pytest_kafka_broker/__init__.py,sha256=1-dEGoWlajumAl7AWsYARbxi9wKlBwTh56sg1KSLC5g,6511
2
- pytest_kafka_broker/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- pytest_kafka_broker-0.3.1.dist-info/METADATA,sha256=_31JrO09YK79CS6ly65rCWZdQEuaFcF_dUBG7lKx-KE,683
4
- pytest_kafka_broker-0.3.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
5
- pytest_kafka_broker-0.3.1.dist-info/entry_points.txt,sha256=mjFsRbu6FOiZkUOlSaXpo4QdLHwniNB-p_NEIgRbDnw,46
6
- pytest_kafka_broker-0.3.1.dist-info/top_level.txt,sha256=nTrYx9xVeK5hsqbhBRL2bgBV_ea-J66_f4Dk8eD-Ci0,20
7
- pytest_kafka_broker-0.3.1.dist-info/RECORD,,