Skip to content

Commit

Permalink
end2end tests: fix race condition in local http server startup
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed May 19, 2024
1 parent 0956491 commit a55019a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
import sys
from functools import wraps
import os
from pathlib import Path
import sys
import time
from typing import Iterator, Optional, TypeVar

import pytest
import requests


def has_x() -> bool:
Expand Down Expand Up @@ -46,7 +48,18 @@ def tmp_popen(*args, **kwargs):
def local_http_server(path: Path, *, port: int) -> Iterator[str]:
address = '127.0.0.1'
with tmp_popen([sys.executable, '-m', 'http.server', '--directory', path, '--bind', address, str(port)]) as popen:
yield f'http://{address}:{port}'
endpoint = f'http://{address}:{port}'

# meh.. but not sure if there is a better way to find out whether it's ready to serve requests
for attempt in range(50):
try:
requests.get(endpoint)
except:
time.sleep(0.05)
continue
else:
break
yield endpoint


T = TypeVar('T')
Expand Down

0 comments on commit a55019a

Please sign in to comment.