Skip to content

Commit 2af9ed2

Browse files
committed
find port dynamically
1 parent a79256f commit 2af9ed2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

unit_tests/sources/declarative/decoders/test_composite_decoder.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import csv
55
import gzip
66
import json
7+
import socket
78
from http.server import BaseHTTPRequestHandler, HTTPServer
89
from io import BytesIO, StringIO
910
from threading import Thread
@@ -22,6 +23,12 @@
2223
from airbyte_cdk.utils import AirbyteTracedException
2324

2425

26+
def find_available_port() -> int:
27+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
28+
s.bind(('localhost', 0))
29+
return s.getsockname()[1] # type: ignore # this should return a int
30+
31+
2532
def compress_with_gzip(data: str, encoding: str = "utf-8"):
2633
"""
2734
Compress the data using Gzip.
@@ -223,11 +230,12 @@ def test_composite_raw_decoder_csv_parser_without_mocked_response():
223230
# self.wfile.write(bytes('John,Doe,120 jefferson st.,Riverside, NJ, 08075\nJack,McGinnis,220 hobo Av.,Phila, PA,09119\n"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075\nStephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234\n,Blankman,,SomeTown, SD, 00298\n"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123\n', "utf-8"))
224231

225232
# start server
226-
httpd = HTTPServer(("localhost", 8080), TestServer)
233+
port = find_available_port()
234+
httpd = HTTPServer(("localhost", port), TestServer)
227235
thread = Thread(target=httpd.serve_forever, args=())
228236
thread.start()
229237
try:
230-
response = requests.get(f"http://localhost:8080", stream=True)
238+
response = requests.get(f"http://localhost:{port}", stream=True)
231239
result = list(CompositeRawDecoder(parser=CsvParser()).decode(response))
232240

233241
assert len(result) == 1

0 commit comments

Comments
 (0)