Skip to content

Commit

Permalink
Use new context module for feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Aug 30, 2023
1 parent e544364 commit f26fc82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
20 changes: 4 additions & 16 deletions bugsnag/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@
from bugsnag.handlers import BugsnagHandler
from bugsnag.sessiontracker import SessionTracker
from bugsnag.utils import to_rfc3339
from bugsnag.context import ContextLocalState

__all__ = ('Client',)


try:
from contextvars import ContextVar
_feature_flag_delegate_context_var = ContextVar(
'bugsnag-client-feature-flag-delegate',
default=None
) # type: ContextVar[Optional[FeatureFlagDelegate]]
except ImportError:
from bugsnag.utils import ThreadContextVar
_feature_flag_delegate_context_var = ThreadContextVar('bugsnag-client-feature-flag-delegate', default=None) # type: ignore # noqa: E501


class Client:
"""
A Bugsnag monitoring and reporting client.
Expand All @@ -45,6 +35,7 @@ def __init__(self, configuration: Optional[Configuration] = None,
self.configuration = configuration or Configuration() # type: Configuration # noqa: E501
self.session_tracker = SessionTracker(self.configuration)
self.configuration.configure(**kwargs)
self._context = ContextLocalState(self)

if install_sys_hook:
self.install_sys_hook()
Expand Down Expand Up @@ -237,14 +228,11 @@ def log_handler(

@property
def _feature_flag_delegate(self) -> FeatureFlagDelegate:
try:
feature_flag_delegate = _feature_flag_delegate_context_var.get()
except LookupError:
feature_flag_delegate = None
feature_flag_delegate = self._context.feature_flag_delegate

if feature_flag_delegate is None:
feature_flag_delegate = FeatureFlagDelegate()
_feature_flag_delegate_context_var.set(feature_flag_delegate)
self._context.feature_flag_delegate = feature_flag_delegate

return feature_flag_delegate

Expand Down
15 changes: 6 additions & 9 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ def setUp(self):
asynchronous=False,
install_sys_hook=False)

self.client.clear_feature_flags()
legacy.clear_feature_flags()

# Initialisation

def test_init_no_configuration(self):
Expand Down Expand Up @@ -1591,16 +1588,16 @@ def test_feature_flags_can_be_added_in_bulk_with_legacy_client(self):

def test_feature_flags_can_be_removed_with_legacy_client(self):
legacy.add_feature_flags([
FeatureFlag('a', '1'),
FeatureFlag('b'),
FeatureFlag('c', '3')
FeatureFlag('x', '1'),
FeatureFlag('y'),
FeatureFlag('z', '3')
])

legacy.clear_feature_flag('b')
legacy.clear_feature_flag('y')

assert legacy.default_client.feature_flags == [
FeatureFlag('a', '1'),
FeatureFlag('c', '3')
FeatureFlag('x', '1'),
FeatureFlag('z', '3')
]

def test_feature_flags_can_be_cleared_with_legacy_client(self):
Expand Down

0 comments on commit f26fc82

Please sign in to comment.