-
Notifications
You must be signed in to change notification settings - Fork 854
fix: improve Socket Mode client stability and correctness #1854
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 1 commit
Commits
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
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,68 @@ | ||
| import logging | ||
| import ssl | ||
| import unittest | ||
| from threading import Lock | ||
| from unittest.mock import patch, MagicMock, create_autospec | ||
|
|
||
| from slack_sdk.socket_mode.client import BaseSocketModeClient | ||
| from slack_sdk.socket_mode.builtin.internals import ( | ||
| _parse_handshake_response, | ||
| _fetch_messages, | ||
| ) | ||
|
|
||
|
|
||
| class TestSocketModeClient(unittest.TestCase): | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def test_connect_to_new_endpoint_does_not_release_lock_on_acquisition_timeout(self): | ||
| client = BaseSocketModeClient.__new__(BaseSocketModeClient) | ||
| client.logger = self.logger | ||
| client.connect_operation_lock = create_autospec(Lock(), acquire=MagicMock(return_value=False)) | ||
|
|
||
| client.connect_to_new_endpoint() | ||
|
|
||
| client.connect_operation_lock.release.assert_not_called() | ||
|
|
||
| def test_connect_to_new_endpoint_releases_lock_on_successful_acquisition(self): | ||
| client = BaseSocketModeClient.__new__(BaseSocketModeClient) | ||
| client.logger = self.logger | ||
| client.connect_operation_lock = Lock() | ||
|
|
||
| with patch.object(client, client.is_connected.__name__, return_value=True): | ||
| client.connect_to_new_endpoint() | ||
|
|
||
| acquired = client.connect_operation_lock.acquire(blocking=False) | ||
| self.assertTrue(acquired) | ||
| client.connect_operation_lock.release() | ||
|
|
||
| def test_parse_handshake_response_preserves_colons_in_header_values(self): | ||
| lines = [ | ||
| "HTTP/1.1 101 Switching Protocols", | ||
| "Upgrade: websocket", | ||
| "Location: https://example.com:8080/path", | ||
| "", | ||
| ] | ||
| with patch( | ||
| "slack_sdk.socket_mode.builtin.internals._read_http_response_line", | ||
| side_effect=lines, | ||
| ): | ||
| status, headers, _ = _parse_handshake_response(MagicMock(spec=ssl.SSLSocket)) | ||
|
|
||
| self.assertEqual(status, 101) | ||
| self.assertEqual(headers["upgrade"], "websocket") | ||
| self.assertEqual(headers["location"], "https://example.com:8080/path") | ||
|
|
||
| def test_parse_handshake_response_parses_standard_headers(self): | ||
| lines = [ | ||
| "HTTP/1.1 200 OK", | ||
| "Content-Type: text/html", | ||
| "", | ||
| ] | ||
| with patch( | ||
| "slack_sdk.socket_mode.builtin.internals._read_http_response_line", | ||
| side_effect=lines, | ||
| ): | ||
| status, headers, _ = _parse_handshake_response(MagicMock(spec=ssl.SSLSocket)) | ||
|
|
||
| self.assertEqual(status, 200) | ||
| self.assertEqual(headers["content-type"], "text/html") | ||
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.
looks like this is where unit tests are failing. this is the fix according to claude
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.
Thats spot on 💯 thanks for sharing the solution 🥇