|
2 | 2 | import pytest
|
3 | 3 | import pytz
|
4 | 4 | import threading
|
| 5 | +import tornado.ioloop |
| 6 | +import tornado.web |
| 7 | +import tornado.websocket |
5 | 8 | from contextlib import contextmanager
|
6 | 9 | from datetime import datetime, timedelta
|
7 | 10 | from typing import List, Optional, Type
|
8 | 11 |
|
9 | 12 | import csp
|
10 | 13 | from csp import ts
|
11 |
| - |
12 |
| -if os.environ.get("CSP_TEST_WEBSOCKET"): |
13 |
| - import tornado.ioloop |
14 |
| - import tornado.web |
15 |
| - import tornado.websocket |
16 |
| - |
17 |
| - from csp.adapters.websocket import ( |
18 |
| - ActionType, |
19 |
| - ConnectionRequest, |
20 |
| - JSONTextMessageMapper, |
21 |
| - RawTextMessageMapper, |
22 |
| - Status, |
23 |
| - WebsocketAdapterManager, |
24 |
| - WebsocketHeaderUpdate, |
25 |
| - WebsocketStatus, |
26 |
| - ) |
27 |
| - |
28 |
| - class EchoWebsocketHandler(tornado.websocket.WebSocketHandler): |
29 |
| - def on_message(self, msg): |
30 |
| - # Carve-out to allow inspecting the headers |
31 |
| - if msg == "header1": |
32 |
| - msg = self.request.headers.get(msg, "") |
33 |
| - elif not isinstance(msg, str) and msg.decode("utf-8") == "header1": |
34 |
| - # Need this for bytes |
35 |
| - msg = self.request.headers.get("header1", "") |
36 |
| - return self.write_message(msg) |
37 |
| - |
38 |
| - @contextmanager |
39 |
| - def create_tornado_server(port: int): |
40 |
| - """Base context manager for creating a Tornado server in a thread""" |
41 |
| - ready_event = threading.Event() |
42 |
| - io_loop = None |
43 |
| - app = None |
44 |
| - io_thread = None |
45 |
| - |
46 |
| - def run_io_loop(): |
47 |
| - nonlocal io_loop, app |
48 |
| - io_loop = tornado.ioloop.IOLoop() |
49 |
| - io_loop.make_current() |
50 |
| - app = tornado.web.Application([(r"/", EchoWebsocketHandler)]) |
51 |
| - app.listen(port) |
52 |
| - ready_event.set() |
53 |
| - io_loop.start() |
54 |
| - |
55 |
| - io_thread = threading.Thread(target=run_io_loop) |
56 |
| - io_thread.start() |
57 |
| - ready_event.wait() |
58 |
| - |
59 |
| - try: |
60 |
| - yield io_loop, app, io_thread |
61 |
| - finally: |
62 |
| - io_loop.add_callback(io_loop.stop) |
63 |
| - if io_thread: |
64 |
| - io_thread.join(timeout=5) |
65 |
| - if io_thread.is_alive(): |
66 |
| - raise RuntimeError("IOLoop failed to stop") |
67 |
| - |
68 |
| - @contextmanager |
69 |
| - def tornado_server(port: int = 8001): |
70 |
| - """Simplified context manager that uses the base implementation""" |
71 |
| - with create_tornado_server(port) as (_io_loop, _app, _io_thread): |
72 |
| - yield |
| 14 | +from csp.adapters.websocket import ( |
| 15 | + ActionType, |
| 16 | + ConnectionRequest, |
| 17 | + JSONTextMessageMapper, |
| 18 | + RawTextMessageMapper, |
| 19 | + Status, |
| 20 | + WebsocketAdapterManager, |
| 21 | + WebsocketHeaderUpdate, |
| 22 | + WebsocketStatus, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +class EchoWebsocketHandler(tornado.websocket.WebSocketHandler): |
| 27 | + def on_message(self, msg): |
| 28 | + # Carve-out to allow inspecting the headers |
| 29 | + if msg == "header1": |
| 30 | + msg = self.request.headers.get(msg, "") |
| 31 | + elif not isinstance(msg, str) and msg.decode("utf-8") == "header1": |
| 32 | + # Need this for bytes |
| 33 | + msg = self.request.headers.get("header1", "") |
| 34 | + return self.write_message(msg) |
| 35 | + |
| 36 | + |
| 37 | +@contextmanager |
| 38 | +def create_tornado_server(port: int): |
| 39 | + """Base context manager for creating a Tornado server in a thread""" |
| 40 | + ready_event = threading.Event() |
| 41 | + io_loop = None |
| 42 | + app = None |
| 43 | + io_thread = None |
| 44 | + |
| 45 | + def run_io_loop(): |
| 46 | + nonlocal io_loop, app |
| 47 | + io_loop = tornado.ioloop.IOLoop() |
| 48 | + io_loop.make_current() |
| 49 | + app = tornado.web.Application([(r"/", EchoWebsocketHandler)]) |
| 50 | + app.listen(port) |
| 51 | + ready_event.set() |
| 52 | + io_loop.start() |
| 53 | + |
| 54 | + io_thread = threading.Thread(target=run_io_loop) |
| 55 | + io_thread.start() |
| 56 | + ready_event.wait() |
| 57 | + |
| 58 | + try: |
| 59 | + yield io_loop, app, io_thread |
| 60 | + finally: |
| 61 | + io_loop.add_callback(io_loop.stop) |
| 62 | + if io_thread: |
| 63 | + io_thread.join(timeout=5) |
| 64 | + if io_thread.is_alive(): |
| 65 | + raise RuntimeError("IOLoop failed to stop") |
| 66 | + |
| 67 | + |
| 68 | +@contextmanager |
| 69 | +def tornado_server(port: int = 8001): |
| 70 | + """Simplified context manager that uses the base implementation""" |
| 71 | + with create_tornado_server(port) as (_io_loop, _app, _io_thread): |
| 72 | + yield |
73 | 73 |
|
74 | 74 |
|
75 |
| -@pytest.mark.skipif(os.environ.get("CSP_TEST_WEBSOCKET") is None, reason="'CSP_TEST_WEBSOCKET' env variable is not set") |
76 | 75 | class TestWebsocket:
|
77 | 76 | @pytest.fixture(scope="class", autouse=True)
|
78 | 77 | def setup_tornado(self, request):
|
|
0 commit comments