-
Notifications
You must be signed in to change notification settings - Fork 855
test: fix intermittent CI hang in the unit test matrix #1921
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,11 @@ | |
| import logging | ||
| import threading | ||
| from http import HTTPStatus | ||
| from http.server import HTTPServer, SimpleHTTPRequestHandler | ||
| from typing import Type | ||
| from http.server import SimpleHTTPRequestHandler | ||
| from unittest import TestCase | ||
|
|
||
| from tests.mock_web_api_server.mock_server_thread import MockServerThread | ||
|
|
||
|
|
||
| class MockHandler(SimpleHTTPRequestHandler): | ||
| protocol_version = "HTTP/1.1" | ||
|
|
@@ -62,34 +63,14 @@ def do_POST(self): | |
| self._handle() | ||
|
|
||
|
|
||
| class MockServerThread(threading.Thread): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeps implementation DRY |
||
| def __init__(self, test: TestCase, handler: Type[SimpleHTTPRequestHandler] = MockHandler): | ||
| threading.Thread.__init__(self) | ||
| self.handler = handler | ||
| self.test = test | ||
|
|
||
| def run(self): | ||
| self.server = HTTPServer(("localhost", 8888), self.handler) | ||
| self.test.server_url = "http://localhost:8888" | ||
| self.test.host, self.test.port = self.server.socket.getsockname() | ||
| self.test.server_started.set() # threading.Event() | ||
|
|
||
| self.test = None | ||
| try: | ||
| self.server.serve_forever(0.05) | ||
| finally: | ||
| self.server.server_close() | ||
|
|
||
| def stop(self): | ||
| self.server.shutdown() | ||
| self.join() | ||
|
|
||
|
|
||
| def setup_mock_web_api_server(test: TestCase): | ||
| test.server_started = threading.Event() | ||
| test.thread = MockServerThread(test) | ||
| test.thread = MockServerThread(test=test, handler=MockHandler) | ||
| test.thread.start() | ||
| test.server_started.wait() | ||
| if not test.server_started.wait(timeout=5): | ||
| raise RuntimeError( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fails with noise |
||
| f"Mock web API server failed to start on port {test.thread.port} within 5s (port already in use?)" | ||
| ) | ||
|
|
||
|
|
||
| def cleanup_mock_web_api_server(test: TestCase): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,9 @@ | ||
| import asyncio | ||
| import json | ||
| import logging | ||
| from queue import Queue | ||
| import re | ||
| import threading | ||
| import time | ||
| from http import HTTPStatus | ||
| from http.server import HTTPServer, SimpleHTTPRequestHandler | ||
| from typing import Type, Union | ||
| from unittest import TestCase | ||
| from http.server import SimpleHTTPRequestHandler | ||
| from urllib.parse import urlparse, parse_qs | ||
|
|
||
|
|
||
|
|
@@ -258,37 +253,3 @@ def do_GET(self): | |
|
|
||
| def do_POST(self): | ||
| self._handle() | ||
|
|
||
|
|
||
| class MockServerThread(threading.Thread): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeps implementations DRY |
||
| def __init__( | ||
| self, queue: Union[Queue, asyncio.Queue], test: TestCase, handler: Type[SimpleHTTPRequestHandler] = MockHandler | ||
| ): | ||
| threading.Thread.__init__(self) | ||
| self.handler = handler | ||
| self.test = test | ||
| self.queue = queue | ||
|
|
||
| def run(self): | ||
| self.server = HTTPServer(("localhost", 8888), self.handler) | ||
| self.server.queue = self.queue | ||
| self.test.server_url = "http://localhost:8888" | ||
| self.test.host, self.test.port = self.server.socket.getsockname() | ||
| self.test.server_started.set() # threading.Event() | ||
|
|
||
| self.test = None | ||
| try: | ||
| self.server.serve_forever(0.05) | ||
| finally: | ||
| self.server.server_close() | ||
|
|
||
| def stop(self): | ||
| with self.server.queue.mutex: | ||
| del self.server.queue | ||
| self.server.shutdown() | ||
| self.join() | ||
|
|
||
| def stop_unsafe(self): | ||
| del self.server.queue | ||
| self.server.shutdown() | ||
| self.join() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| start_socket_mode_server, | ||
| socket_mode_envelopes, | ||
| socket_mode_hello_message, | ||
| stop_socket_mode_server, | ||
| ) | ||
| from tests.slack_sdk.socket_mode.mock_web_api_handler import MockHandler | ||
| from tests.mock_web_api_server import setup_mock_web_api_server_async, cleanup_mock_web_api_server_async | ||
|
|
@@ -35,7 +36,10 @@ def setUp(self): | |
| start_socket_mode_server(self, 3001) | ||
|
|
||
| def tearDown(self): | ||
| cleanup_mock_web_api_server_async(self) | ||
| try: | ||
| cleanup_mock_web_api_server_async(self) | ||
| finally: | ||
| stop_socket_mode_server(self) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were not always stopping the socket mode server I think this was leading to test hanging sometimes |
||
|
|
||
| @async_test | ||
| async def test_interactions(self): | ||
|
|
||
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.
The
ThreadingHTTPServeris only available in python 3.7+, this is why we were usingHTTPServer