|
| 1 | +import os |
| 2 | +import pytz |
| 3 | +import threading |
| 4 | +import unittest |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +import csp |
| 8 | +from csp import ts |
| 9 | + |
| 10 | +if os.environ.get("CSP_TEST_WEBSOCKET"): |
| 11 | + import tornado.ioloop |
| 12 | + import tornado.web |
| 13 | + import tornado.websocket |
| 14 | + |
| 15 | + from csp.adapters.websocket import JSONTextMessageMapper, RawTextMessageMapper, Status, WebsocketAdapterManager |
| 16 | + |
| 17 | + class EchoWebsocketHandler(tornado.websocket.WebSocketHandler): |
| 18 | + def on_message(self, msg): |
| 19 | + return self.write_message(msg) |
| 20 | + |
| 21 | + |
| 22 | +@unittest.skipIf(not os.environ.get("CSP_TEST_WEBSOCKET"), "Skipping websocket adapter tests") |
| 23 | +class TestWebsocket(unittest.TestCase): |
| 24 | + @classmethod |
| 25 | + def setUpClass(cls): |
| 26 | + cls.app = tornado.web.Application([(r"/ws", EchoWebsocketHandler)]) |
| 27 | + cls.app.listen(8000) |
| 28 | + cls.io_loop = tornado.ioloop.IOLoop.current() |
| 29 | + cls.io_thread = threading.Thread(target=cls.io_loop.start) |
| 30 | + cls.io_thread.start() |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def tearDownClass(cls): |
| 34 | + cls.io_loop.add_callback(cls.io_loop.stop) |
| 35 | + if cls.io_thread: |
| 36 | + cls.io_thread.join() |
| 37 | + |
| 38 | + def test_send_recv_msg(self): |
| 39 | + @csp.node |
| 40 | + def send_msg_on_open(status: ts[Status]) -> ts[str]: |
| 41 | + if csp.ticked(status): |
| 42 | + return "Hello, World!" |
| 43 | + |
| 44 | + @csp.graph |
| 45 | + def g(): |
| 46 | + ws = WebsocketAdapterManager("ws://localhost:8000/ws") |
| 47 | + status = ws.status() |
| 48 | + ws.send(send_msg_on_open(status)) |
| 49 | + recv = ws.subscribe(str, RawTextMessageMapper()) |
| 50 | + |
| 51 | + csp.add_graph_output("recv", recv) |
| 52 | + csp.stop_engine(recv) |
| 53 | + |
| 54 | + msgs = csp.run(g, starttime=datetime.now(pytz.UTC), realtime=True) |
| 55 | + assert len(msgs) == 1 |
| 56 | + assert msgs["recv"][0][1] == "Hello, World!" |
| 57 | + |
| 58 | + def test_send_recv_json(self): |
| 59 | + class MsgStruct(csp.Struct): |
| 60 | + a: int |
| 61 | + b: str |
| 62 | + |
| 63 | + @csp.node |
| 64 | + def send_msg_on_open(status: ts[Status]) -> ts[str]: |
| 65 | + if csp.ticked(status): |
| 66 | + return MsgStruct(a=1234, b="im a string").to_json() |
| 67 | + |
| 68 | + @csp.graph |
| 69 | + def g(): |
| 70 | + ws = WebsocketAdapterManager("ws://localhost:8000/ws") |
| 71 | + status = ws.status() |
| 72 | + ws.send(send_msg_on_open(status)) |
| 73 | + recv = ws.subscribe(MsgStruct, JSONTextMessageMapper()) |
| 74 | + |
| 75 | + csp.add_graph_output("recv", recv) |
| 76 | + csp.stop_engine(recv) |
| 77 | + |
| 78 | + msgs = csp.run(g, starttime=datetime.now(pytz.UTC), realtime=True) |
| 79 | + assert len(msgs) == 1 |
| 80 | + obj = msgs["recv"][0][1] |
| 81 | + assert isinstance(obj, MsgStruct) |
| 82 | + assert obj.a == 1234 |
| 83 | + assert obj.b == "im a string" |
0 commit comments