Skip to content

Commit 24c5818

Browse files
committed
fix @217 | add tests
Signed-off-by: Will Rieger <[email protected]>
1 parent a32cef3 commit 24c5818

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

cpp/csp/adapters/websocket/ClientInputAdapter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void ClientInputAdapter::processMessage( std::string payload, PushBatch* batch )
3030

3131
if( type() -> type() == CspType::Type::STRUCT )
3232
{
33-
auto tick = m_converter -> asStruct( &payload, payload.length() );
33+
auto tick = m_converter -> asStruct( (void*)payload.data(), payload.length() );
3434
pushTick( std::move(tick), batch );
3535
} else if ( type() -> type() == CspType::Type::STRING )
3636
{

cpp/csp/adapters/websocket/ClientInputAdapter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef _IN_CSP_ADAPTERS_WEBSOCKETS_CLIENT_INPUTADAPTER_H
22
#define _IN_CSP_ADAPTERS_WEBSOCKETS_CLIENT_INPUTADAPTER_H
33

4-
#include <websocketpp/config/core_client.hpp>
5-
#include <websocketpp/client.hpp>
64
#include <csp/engine/Dictionary.h>
75
#include <csp/adapters/utils/MessageStructConverter.h>
86
#include <csp/engine/PushInputAdapter.h>

cpp/csp/adapters/websocket/ClientOutputAdapter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <csp/engine/Dictionary.h>
66
#include <csp/engine/OutputAdapter.h>
77
#include <csp/adapters/utils/MessageWriter.h>
8-
#include <websocketpp/client.hpp>
98

109
namespace csp::adapters::websocket
1110
{
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)