Skip to content

Commit 7d377b4

Browse files
tpayetclaude
andcommitted
fix: Use dynamic port allocation to avoid conflicts in CI
- Modified LocalMeilisearchServer to find an available port automatically - This prevents conflicts with existing Meilisearch instances in CI - Added small delay after document indexing to ensure search works - Tests now pass reliably in both local and CI environments 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9dc2959 commit 7d377b4

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

meilisearch/_local_server.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import platform
88
import shutil
99
import signal
10+
import socket
1011
import subprocess
1112
import sys
1213
import tempfile
@@ -23,18 +24,24 @@ class LocalMeilisearchServer:
2324

2425
def __init__(
2526
self,
26-
port: int = 7700,
27+
port: Optional[int] = None,
2728
data_path: Optional[str] = None,
2829
master_key: Optional[str] = None,
2930
):
30-
self.port = port
31+
self.port = port or self._find_available_port()
3132
self.data_path = data_path or tempfile.mkdtemp(prefix="meilisearch_")
3233
self.master_key = master_key
3334
self.process: Optional[subprocess.Popen] = None
3435
self.binary_path: Optional[str] = None
3536
self.url = f"http://127.0.0.1:{self.port}"
3637
self._temp_data_dir = data_path is None
3738

39+
def _find_available_port(self) -> int:
40+
"""Find an available port to use."""
41+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
42+
sock.bind(("", 0))
43+
return sock.getsockname()[1]
44+
3845
def _find_meilisearch_binary(self) -> Optional[str]:
3946
"""Find Meilisearch binary in PATH or common locations."""
4047
# First, check if 'meilisearch' is in PATH

tests/auto_launch/test_client_auto_launch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for auto-launch functionality."""
22

3+
import time
4+
35
import meilisearch
46
from meilisearch._local_server import LocalMeilisearchServer
57

@@ -29,6 +31,9 @@ def test_client_auto_launch():
2931
task = index.add_documents(documents)
3032
client.wait_for_task(task.task_uid)
3133

34+
# Give Meilisearch a moment to process
35+
time.sleep(0.5)
36+
3237
# Search
3338
results = index.search("test")
3439
assert len(results["hits"]) > 0

0 commit comments

Comments
 (0)