Skip to content

Commit 7d61a47

Browse files
authored
Merge pull request #122 from Flagsmith/fix/requests-timeout
fix: Fix HTTP requests not timing out
2 parents 7a335eb + 17068db commit 7d61a47

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

flagsmith/flagsmith.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(
4949
api_url: typing.Optional[str] = None,
5050
realtime_api_url: typing.Optional[str] = None,
5151
custom_headers: typing.Optional[typing.Dict[str, typing.Any]] = None,
52-
request_timeout_seconds: typing.Optional[int] = None,
52+
request_timeout_seconds: typing.Optional[int] = 10,
5353
enable_local_evaluation: bool = False,
5454
environment_refresh_interval_seconds: typing.Union[int, float] = 60,
5555
retries: typing.Optional[Retry] = None,

tests/test_flagsmith.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,39 @@ def test_non_200_response_raises_flagsmith_api_error(flagsmith: Flagsmith) -> No
278278
# expected exception raised
279279

280280

281+
@pytest.mark.parametrize(
282+
"settings, expected_timeout",
283+
[
284+
({"request_timeout_seconds": 5}, 5), # Arbitrary timeout
285+
({"request_timeout_seconds": None}, None), # No timeout is forced
286+
({}, 10), # Default timeout
287+
],
288+
)
289+
def test_request_times_out_according_to_setting(
290+
mocker: MockerFixture,
291+
api_key: str,
292+
settings: typing.Dict[str, typing.Any],
293+
expected_timeout: typing.Optional[int],
294+
) -> None:
295+
# Given
296+
session = mocker.patch("flagsmith.flagsmith.requests.Session").return_value
297+
flagsmith = Flagsmith(
298+
environment_key=api_key,
299+
enable_local_evaluation=False,
300+
**settings,
301+
)
302+
303+
# When
304+
flagsmith.get_environment_flags()
305+
306+
# Then
307+
session.get.assert_called_once_with(
308+
"https://edge.api.flagsmith.com/api/v1/flags/",
309+
json=None,
310+
timeout=expected_timeout,
311+
)
312+
313+
281314
@responses.activate()
282315
def test_default_flag_is_used_when_no_environment_flags_returned(api_key: str) -> None:
283316
# Given

0 commit comments

Comments
 (0)