Skip to content

Commit

Permalink
fix: Set the environment for local evaluation mode on init (#76)
Browse files Browse the repository at this point in the history
* Set the environment for local evaluation mode on init

* Make tests pass with the update environment change

* Fix test failure

* Make update environment update local document

* Use responses for polling manager tests

* Lint

---------

Co-authored-by: Matthew Elwell <[email protected]>
  • Loading branch information
zachaysan and matthewelwell authored Mar 14, 2024
1 parent 398be6c commit 5fc5559
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions flagsmith/flagsmith.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ def _initialise_local_evaluation(self) -> None:
self.event_stream_thread.start()

else:
# To ensure that the environment is set before allowing subsequent
# method calls, update the environment manually.
self.update_environment()
self.environment_data_polling_manager_thread = (
EnvironmentDataPollingManager(
main=self,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_flagsmith.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@


def test_flagsmith_starts_polling_manager_on_init_if_enabled(
mocker: MockerFixture, server_api_key: str
mocker: MockerFixture,
server_api_key: str,
requests_session_response_ok: None,
) -> None:
# Given
mock_polling_manager = mocker.MagicMock()
Expand Down
28 changes: 22 additions & 6 deletions tests/test_polling_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import mock

import requests
import responses
from pytest_mock import MockerFixture

from flagsmith import Flagsmith
Expand Down Expand Up @@ -41,35 +42,50 @@ def test_polling_manager_calls_update_environment_on_each_refresh() -> None:
polling_manager.stop()


@responses.activate()
def test_polling_manager_is_resilient_to_api_errors(
mocker: MockerFixture, server_api_key: str
flagsmith: Flagsmith,
environment_json: str,
mocker: MockerFixture,
server_api_key: str,
) -> None:
# Given
session_mock = mocker.patch("requests.Session")
session_mock.get.return_value = mock.MagicMock(status_code=500)
responses.add(method="GET", url=flagsmith.environment_url, body=environment_json)
flagsmith = Flagsmith(
environment_key=server_api_key,
enable_local_evaluation=True,
environment_refresh_interval_seconds=0.1,
)

responses.add(method="GET", url=flagsmith.environment_url, status=500)
polling_manager = flagsmith.environment_data_polling_manager_thread

# Then
assert polling_manager.is_alive()
polling_manager.stop()


@responses.activate()
def test_polling_manager_is_resilient_to_request_exceptions(
mocker: MockerFixture, server_api_key: str
flagsmith: Flagsmith,
environment_json: str,
mocker: MockerFixture,
server_api_key: str,
) -> None:
# Given
session_mock = mocker.patch("requests.Session")
session_mock.get.side_effect = requests.RequestException()
responses.add(method="GET", url=flagsmith.environment_url, body=environment_json)
flagsmith = Flagsmith(
environment_key=server_api_key,
enable_local_evaluation=True,
environment_refresh_interval_seconds=0.1,
)

responses.add(
method="GET",
url=flagsmith.environment_url,
body=requests.RequestException("Some exception"),
status=500,
)
polling_manager = flagsmith.environment_data_polling_manager_thread

# Then
Expand Down

0 comments on commit 5fc5559

Please sign in to comment.