Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement initialize/shutdown on provider registration #213

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions openfeature/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def set_provider(provider: AbstractProvider):
global _provider
if provider is None:
raise GeneralError(error_message="No provider")
if _provider:
_provider.shutdown()
_provider = provider
provider.initialize(_evaluation_context)
beeme1mr marked this conversation as resolved.
Show resolved Hide resolved


def get_provider() -> typing.Optional[AbstractProvider]:
Expand Down Expand Up @@ -63,3 +66,7 @@ def clear_hooks():
def get_hooks() -> typing.List[Hook]:
global _hooks
return _hooks


def shutdown():
_provider.shutdown()
beeme1mr marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions openfeature/provider/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@


class AbstractProvider:
def initialize(self, evaluation_context: EvaluationContext):
pass

def shutdown(self):
pass

@abstractmethod
def get_metadata(self) -> Metadata:
pass
Expand Down
40 changes: 40 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
get_provider_metadata,
set_evaluation_context,
set_provider,
shutdown,
)
from openfeature.evaluation_context import EvaluationContext
from openfeature.exception import ErrorCode, GeneralError
from openfeature.hook import Hook
from openfeature.provider.metadata import Metadata
from openfeature.provider.no_op_provider import NoOpProvider
from openfeature.provider.provider import AbstractProvider


def test_should_not_raise_exception_with_noop_client():
Expand Down Expand Up @@ -56,6 +58,32 @@ def test_should_try_set_provider_and_fail_if_none_provided():
assert ge.value.error_code == ErrorCode.GENERAL


def test_should_invoke_provider_initialize_function_on_newly_registered_provider():
# Given
evaluation_context = EvaluationContext("targeting_key", {"attr1": "val1"})
provider = MagicMock(spec=AbstractProvider)

# When
set_evaluation_context(evaluation_context)
set_provider(provider)

# Then
provider.initialize.assert_called_with(evaluation_context)


def test_should_invoke_provider_shutdown_function_once_provider_is_no_longer_in_use():
# Given
provider_1 = MagicMock(spec=AbstractProvider)
provider_2 = MagicMock(spec=AbstractProvider)

# When
set_provider(provider_1)
set_provider(provider_2)

# Then
assert provider_1.shutdown.called


def test_should_return_a_provider_if_setup_correctly():
# Given
set_provider(NoOpProvider())
Expand Down Expand Up @@ -116,3 +144,15 @@ def test_should_add_hooks_to_api_hooks():

# Then
assert get_hooks() == [hook_1, hook_2]


def test_should_call_provider_shutdown_on_api_shutdown():
# Given
provider = MagicMock(spec=AbstractProvider)
set_provider(provider)

# When
shutdown()

# Then
assert provider.shutdown.called
Loading