Hi,
I am trying to add webhook support to the otherwise command-line application using Waitress web server. Webhook endpoints are defined as Pyramid routes. I also wish to test this functionality using pytest.
The server gets created fine, but on pytest shutdown, I am getting an error: OSError: [Errno 9] Bad file descriptor. What would be the nice way to spin Waitress up and down on demand?
How do I create the server instance:
def create_webhook_server(host: str, port: int, username: str, password: str, queue: Queue) -> MultiSocketServer:
app = create_pyramid_app(username, password, queue, production=False)
server = create_server(app, host=host, port=port)
logger.info("Webhook server will spawn at %s:%d", host, port)
return server
How do I start and test the server in the unit test:
def test_auth_ok():
"""Username and password allow to access the webhook"""
queue = Queue()
server = create_webhook_server("127.0.0.1", 5000, "test", "test", queue)
server_url = "
http://test:te...@127.0.0.1:5000"
webhook_thread = Thread(target=server.run)
webhook_thread.start()
# Test home view
resp = requests.get(server_url)
assert resp.status_code == 200
server.close()
The exception I am getting:
tests/test_webhook_auth.py::test_auth_ok
/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/_pytest/threadexception.py:75: PytestUnhandledThreadExceptionWarning: Exception in thread Thread-1
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/waitress/server.py", line 322, in run
self.asyncore.loop(
File "/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/waitress/wasyncore.py", line 245, in loop
poll_fun(timeout, map)
File "/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/waitress/wasyncore.py", line 172, in poll
r, w, e = select.select(r, w, e, timeout)
OSError: [Errno 9] Bad file descriptor
warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))
tests/test_webhook_auth.py::test_auth_failed
/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/_pytest/threadexception.py:75: PytestUnhandledThreadExceptionWarning: Exception in thread Thread-2
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/waitress/server.py", line 322, in run
self.asyncore.loop(
File "/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/waitress/wasyncore.py", line 245, in loop
poll_fun(timeout, map)
File "/Users/moo/Library/Caches/pypoetry/virtualenvs/tradeexecutor-13XfP0tc-py3.9/lib/python3.9/site-packages/waitress/wasyncore.py", line 172, in poll
r, w, e = select.select(r, w, e, timeout)
OSError: [Errno 9] Bad file descriptor
warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))