-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add support for provider events. #12
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd3b19c
feat: Add support for provider events.
kinyoklion cf7b4d6
Attempt to appease linter.
kinyoklion 62b7e3e
Update tests/test_provider.py
kinyoklion f68b01c
Update tests/test_provider.py
kinyoklion c790ec3
Decrease indent.
kinyoklion b0b131e
Cleanup.
kinyoklion 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,134 @@ | ||
| import threading | ||
| import time | ||
| from typing import Optional | ||
|
|
||
| from ldclient import Config | ||
| from ldclient.integrations.test_data import TestData | ||
| from ldclient.interfaces import UpdateProcessor, DataSourceUpdateSink, DataSourceState, DataSourceErrorInfo, \ | ||
| DataSourceErrorKind | ||
| from ldclient.versioned_data_kind import FEATURES | ||
|
|
||
|
|
||
| class FailingDataSource(UpdateProcessor): | ||
| def __init__(self, config: Config, store, ready: threading.Event): | ||
| self._data_source_update_sink: Optional[DataSourceUpdateSink] = config.data_source_update_sink | ||
| self._ready = ready | ||
|
|
||
| def start(self): | ||
| if self._data_source_update_sink is None: | ||
| return | ||
|
|
||
| self._ready.set() | ||
|
|
||
| self._data_source_update_sink.update_status( | ||
| DataSourceState.OFF, | ||
| DataSourceErrorInfo( | ||
| DataSourceErrorKind.ERROR_RESPONSE, | ||
| 401, | ||
| time.time(), | ||
| str("Bad things") | ||
| ) | ||
| ) | ||
|
|
||
| def stop(self): | ||
| pass | ||
|
|
||
| def is_alive(self): | ||
| return False | ||
|
|
||
| def initialized(self): | ||
| return False | ||
|
|
||
|
|
||
| class DelayedFailingDataSource(UpdateProcessor): | ||
| def __init__(self, config: Config, store, ready: threading.Event): | ||
| self._data_source_update_sink: Optional[DataSourceUpdateSink] = config.data_source_update_sink | ||
| self._ready = ready | ||
|
|
||
| def start(self): | ||
| if self._data_source_update_sink is None: | ||
| return | ||
|
|
||
| self._ready.set() | ||
|
|
||
| def data_source_failure(): | ||
| self._data_source_update_sink.update_status( | ||
| DataSourceState.OFF, | ||
| DataSourceErrorInfo( | ||
| DataSourceErrorKind.ERROR_RESPONSE, | ||
| 401, | ||
| time.time(), | ||
| str("Bad things") | ||
| ) | ||
| ) | ||
|
|
||
| threading.Timer(0.1, data_source_failure).start() | ||
|
|
||
| def stop(self): | ||
| pass | ||
|
|
||
| def is_alive(self): | ||
| return False | ||
|
|
||
| def initialized(self): | ||
| return False | ||
|
|
||
|
|
||
| class StaleDataSource(UpdateProcessor): | ||
| def __init__(self, config: Config, store, ready: threading.Event): | ||
| self._data_source_update_sink: Optional[DataSourceUpdateSink] = config.data_source_update_sink | ||
| self._ready = ready | ||
|
|
||
| def start(self): | ||
| self._ready.set() | ||
| self._data_source_update_sink.update_status(DataSourceState.VALID, None) | ||
|
|
||
| def data_source_interrupted(): | ||
| self._data_source_update_sink.update_status( | ||
| DataSourceState.INTERRUPTED, | ||
| DataSourceErrorInfo( | ||
| DataSourceErrorKind.ERROR_RESPONSE, | ||
| 408, | ||
| time.time(), | ||
| str("Less bad things") | ||
| ) | ||
| ) | ||
|
|
||
| threading.Timer(0.1, data_source_interrupted).start() | ||
|
|
||
| def stop(self): | ||
| pass | ||
|
|
||
| def is_alive(self): | ||
| return False | ||
|
|
||
| def initialized(self): | ||
| return True | ||
|
|
||
|
|
||
| class UpdatingDataSource(UpdateProcessor): | ||
| def __init__(self, config: Config, store, ready: threading.Event): | ||
| self._data_source_update_sink: Optional[DataSourceUpdateSink] = config.data_source_update_sink | ||
| self._ready = ready | ||
|
|
||
| def start(self): | ||
| self._ready.set() | ||
| self._data_source_update_sink.init({}) | ||
| self._data_source_update_sink.update_status(DataSourceState.VALID, None) | ||
|
|
||
| def update_data(): | ||
| # The test_data_source is only used to access the flag builder. | ||
| # We call _build here, once TestData supports change handlers we should remove this. | ||
| self._data_source_update_sink.upsert(FEATURES, | ||
| TestData().data_source().flag("potato").on(True)._build(1)) | ||
|
|
||
| threading.Timer(0.1, update_data).start() | ||
|
|
||
| def stop(self): | ||
| pass | ||
|
|
||
| def is_alive(self): | ||
| return False | ||
|
|
||
| def initialized(self): | ||
| return True |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should handle the cases where the client failed or succeeded within the start_wait period, or where it exceeded that period and the data source later becomes VALID or OFF.