-
Notifications
You must be signed in to change notification settings - Fork 45
chore: Support flag change listeners in contract tests #410
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c6978e9
chore: Support flag change listeners in contract tests
devin-ai-integration[bot] 3c946fe
chore: Remove unused Optional import
devin-ai-integration[bot] 095913a
fix: Increase timeout for flaky FDv1 fallback test on Windows
devin-ai-integration[bot] e21aa3d
fix: Fix race condition in value change listener registration
devin-ai-integration[bot] 0424bc8
fix: Move add_listener inside lock in register_flag_change_listener
devin-ai-integration[bot] 41e2c6c
fix: Make FDv1 fallback test more robust against timing issues
devin-ai-integration[bot] c251fe4
bump to alpha.4
keelerm84 62ca8b5
code review feedback
keelerm84 e61d84c
ignoring default value
keelerm84 d1a7783
more locking
keelerm84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import logging | ||
| import threading | ||
| from typing import Callable, Dict | ||
|
|
||
| import requests | ||
|
|
||
| from ldclient.context import Context | ||
| from ldclient.interfaces import FlagChange, FlagTracker, FlagValueChange | ||
|
|
||
| log = logging.getLogger('testservice') | ||
|
|
||
|
|
||
| class ListenerRegistry: | ||
| """Manages all active flag change listener registrations for a single SDK client entity.""" | ||
|
|
||
| def __init__(self, tracker: FlagTracker): | ||
| self._tracker = tracker | ||
| self._lock = threading.Lock() | ||
| # Maps listener_id -> (sdk_listener callable, cleanup function) | ||
| self._listeners: Dict[str, Callable] = {} | ||
|
|
||
| def register_flag_change_listener(self, listener_id: str, callback_uri: str): | ||
| """Register a general flag change listener that fires on any flag configuration change.""" | ||
| def on_flag_change(flag_change: FlagChange): | ||
| payload = { | ||
| 'listenerId': listener_id, | ||
| 'flagKey': flag_change.key, | ||
| } | ||
| try: | ||
| requests.post(callback_uri, json=payload) | ||
| except Exception as e: | ||
| log.warning('Failed to post flag change notification: %s', e) | ||
|
|
||
| with self._lock: | ||
| # If a listener with this ID already exists, unregister the old one first | ||
| if listener_id in self._listeners: | ||
| self._tracker.remove_listener(self._listeners[listener_id]) | ||
|
|
||
| self._tracker.add_listener(on_flag_change) | ||
| self._listeners[listener_id] = on_flag_change | ||
|
|
||
| def register_flag_value_change_listener( | ||
| self, | ||
| listener_id: str, | ||
| flag_key: str, | ||
| context: Context, | ||
| callback_uri: str, | ||
| ): | ||
| """Register a flag value change listener that fires when the evaluated value changes.""" | ||
| def on_value_change(change: FlagValueChange): | ||
| payload = { | ||
| 'listenerId': listener_id, | ||
| 'flagKey': change.key, | ||
| 'oldValue': change.old_value, | ||
| 'newValue': change.new_value, | ||
| } | ||
| try: | ||
| requests.post(callback_uri, json=payload) | ||
| except Exception as e: | ||
| log.warning('Failed to post flag value change notification: %s', e) | ||
|
|
||
| # add_flag_value_change_listener returns the underlying listener | ||
| # that must be passed to remove_listener to unsubscribe | ||
| with self._lock: | ||
| if listener_id in self._listeners: | ||
| self._tracker.remove_listener(self._listeners[listener_id]) | ||
|
|
||
| underlying_listener = self._tracker.add_flag_value_change_listener(flag_key, context, on_value_change) | ||
| self._listeners[listener_id] = underlying_listener | ||
|
|
||
| def unregister(self, listener_id: str) -> bool: | ||
| """Unregister a previously registered listener. Returns False if not found.""" | ||
| with self._lock: | ||
| listener = self._listeners.pop(listener_id, None) | ||
| if listener is None: | ||
| return False | ||
|
|
||
| self._tracker.remove_listener(listener) | ||
| return True | ||
|
|
||
| def close_all(self): | ||
| """Unregister all listeners. Called when the SDK client entity shuts down.""" | ||
| with self._lock: | ||
| for listener in self._listeners.values(): | ||
| self._tracker.remove_listener(listener) | ||
| self._listeners.clear() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.