Skip to content

Commit 98c12df

Browse files
committed
test: log sidecar port retry attempts
1 parent 2d2c9fb commit 98c12df

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

tests/examples/conftest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def run(
5252
*,
5353
timeout: int = 30,
5454
until: list[str] | None = None,
55-
port_bind_retries: int = 1,
55+
port_bind_retries: int = 3,
5656
) -> str:
5757
"""Run a foreground command, block until it finishes, and return output.
5858
@@ -71,11 +71,16 @@ def run(
7171
for attempt in range(attempts):
7272
output = self._run_once(args, timeout=timeout, until=until)
7373
if attempt < attempts - 1 and self._is_dapr_port_bind_failure(output):
74+
print(
75+
'Dapr sidecar failed to bind a random port; '
76+
f'retrying startup after {2**attempt}s '
77+
f'(attempt {attempt + 1}/{attempts})',
78+
flush=True,
79+
)
80+
time.sleep(2**attempt)
7481
continue
7582
return output
7683

77-
return output
78-
7984
def _run_once(self, args: str, *, timeout: int, until: list[str] | None) -> str:
8085
proc = subprocess.Popen(
8186
args=('dapr', 'run', *shlex.split(args)),

tests/examples/test_dapr_runner.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import subprocess
2+
import time
23
from pathlib import Path
34

5+
import pytest
6+
47
from tests.examples.conftest import DaprRunner
58

69

@@ -16,7 +19,9 @@ def wait(self, timeout: int | None = None) -> int:
1619
return self.returncode
1720

1821

19-
def test_run_retries_transient_dapr_port_bind_failure(monkeypatch, tmp_path: Path) -> None:
22+
def test_run_retries_transient_dapr_port_bind_failure(
23+
monkeypatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
24+
) -> None:
2025
outputs = [
2126
(
2227
'level=error msg="Failed to listen for gRPC server on TCP address :33223 '
@@ -33,11 +38,18 @@ def fake_popen(*args, **kwargs) -> FakeProcess:
3338
return FakeProcess(outputs.pop(0))
3439

3540
monkeypatch.setattr(subprocess, 'Popen', fake_popen)
41+
sleeps: list[int] = []
42+
monkeypatch.setattr(time, 'sleep', sleeps.append)
3643

3744
output = DaprRunner(tmp_path).run('--app-id=secretsapp -- python3 example.py', timeout=1)
3845

3946
assert output == "{'secretKey': 'secretValue'}\n"
4047
assert len(popen_calls) == 2
48+
assert sleeps == [1]
49+
assert (
50+
'Dapr sidecar failed to bind a random port; retrying startup after 1s'
51+
in capsys.readouterr().out
52+
)
4153

4254

4355
def test_run_does_not_retry_non_port_bind_failure(monkeypatch, tmp_path: Path) -> None:

0 commit comments

Comments
 (0)