-
Notifications
You must be signed in to change notification settings - Fork 127
raise ClickException when OAuthRefreshException occurs during watch command #2480
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
Open
simonc56
wants to merge
7
commits into
Taxel:main
Choose a base branch
from
simonc56:feat/exit-auth-fail
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−30
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3db4b32
handle Trakt authentication errors with ClickException in watch command
simonc56 ed7cde3
Implement fatal error handling for OAuthRefreshException in watch web…
simonc56 046fbef
fix return statement
simonc56 b2c0bad
fix tests with OAuthRefreshException
simonc56 c32b689
re-raise ClickException in cli.py so that process exits with code 1
simonc56 d95bbf5
refactor EventDispatcher and WebSocketListener to remove fatal_error …
simonc56 83fde48
handle fatal error in BackgroundTask call method to ensure proper shu…
simonc56 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
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,25 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from threading import Lock | ||
|
|
||
|
|
||
| class FatalErrorState: | ||
| def __init__(self): | ||
| self._error = None | ||
| self._lock = Lock() | ||
|
|
||
| def clear(self): | ||
| with self._lock: | ||
| self._error = None | ||
|
|
||
| def set(self, error: Exception): | ||
| with self._lock: | ||
| if self._error is None: | ||
| self._error = error | ||
|
|
||
| def raise_if_set(self): | ||
| with self._lock: | ||
| error = self._error | ||
|
|
||
| if error is not None: | ||
| raise error |
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
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,73 @@ | ||
| #!/usr/bin/env python3 -m pytest | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| from trakt.errors import OAuthRefreshException | ||
|
|
||
| from plextraktsync.queue.BackgroundTask import BackgroundTask | ||
| from plextraktsync.watch.FatalErrorState import FatalErrorState | ||
| from plextraktsync.watch.WebSocketListener import WebSocketListener | ||
| from tests.conftest import make_oauth_refresh_exception | ||
|
|
||
|
|
||
| def test_background_task_records_oauth_refresh_exception(): | ||
| fatal_error = FatalErrorState() | ||
|
|
||
| def task(_queues): | ||
| raise make_oauth_refresh_exception() | ||
|
|
||
| background_task = BackgroundTask(None, task, fatal_error=fatal_error) | ||
| background_task.timed_events() | ||
|
|
||
| with pytest.raises(OAuthRefreshException): | ||
| fatal_error.raise_if_set() | ||
|
|
||
|
|
||
| def test_background_task_continues_without_fatal_error(): | ||
| fatal_error = FatalErrorState() | ||
| calls = [] | ||
|
|
||
| class Queue: | ||
| def __init__(self): | ||
| self.messages = iter([ | ||
| ("test", 1), | ||
| None, | ||
| ]) | ||
|
|
||
| def get(self, timeout): | ||
| return next(self.messages) | ||
|
|
||
| def task(queues): | ||
| calls.append(list(queues["test"])) | ||
|
|
||
| background_task = BackgroundTask(None, task, fatal_error=fatal_error) | ||
| background_task(Queue()) | ||
|
|
||
| assert calls == [[1]] | ||
|
|
||
|
|
||
| def test_websocket_listener_raises_recorded_oauth_refresh_exception(monkeypatch): | ||
| fatal_error = FatalErrorState() | ||
|
|
||
| class Notifier: | ||
| def __init__(self): | ||
| self.calls = 0 | ||
|
|
||
| def is_alive(self): | ||
| self.calls += 1 | ||
| return self.calls == 1 | ||
|
|
||
| class Plex: | ||
| def startAlertListener(self, callback): | ||
| self.callback = callback | ||
| return Notifier() | ||
|
|
||
| def fail_sleep(_interval): | ||
| fatal_error.set(make_oauth_refresh_exception()) | ||
|
|
||
| monkeypatch.setattr("plextraktsync.watch.WebSocketListener.sleep", fail_sleep) | ||
|
|
||
| listener = WebSocketListener(Plex(), poll_interval=0, fatal_error=fatal_error) | ||
|
|
||
| with pytest.raises(OAuthRefreshException): | ||
| listener.listen() |
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.
Uh oh!
There was an error while loading. Please reload this page.