Skip to content

Commit fb9fdaa

Browse files
author
Nijat K
committed
Remove print statements, add more to websocket tests
Signed-off-by: Nijat K <[email protected]>
1 parent d3b6676 commit fb9fdaa

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

cpp/csp/adapters/websocket/ClientAdapterManager.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ void ClientAdapterManager::stop() {
537537

538538
PushInputAdapter* ClientAdapterManager::getInputAdapter(CspTypePtr & type, PushMode pushMode, const Dictionary & properties)
539539
{
540-
std::cout << "Adapter manage m_dynamic " << m_dynamic << "\n";
541540
if ( m_dynamic ){
542541
auto caller_id = properties.get<int64_t>("caller_id");
543542
size_t validated_id = validateCallerId(caller_id);

cpp/csp/adapters/websocket/ClientInputAdapter.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ void ClientInputAdapter::processMessage( std::tuple<std::string, void*> data, si
5252
{
5353
// Extract the source string and data pointer from tuple
5454
std::string source = std::get<0>(data);
55-
std::cout << source << "\n";
5655
void* c = std::get<1>(data);
5756
// std::cout << "Got data pointer: " << c << "\n";
5857
// std::cout << "dynamic??\n";
@@ -78,7 +77,6 @@ void ClientInputAdapter::processMessage( std::tuple<std::string, void*> data, si
7877
auto msg = std::string((char const*)c, t);
7978
// std::cout << "got message " << msg << "\n";
8079
actual_type.meta()->field("msg")->setValue( true_val.get(), msg );
81-
std::cout << msg << "\n";
8280

8381
pushTick( std::move(true_val), batch );
8482
}

csp/adapters/websocket.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
from csp.impl.wiring.delayed_node import DelayedNodeWrapperDef
2222
from csp.lib import _websocketadapterimpl
2323

24-
from .websocket_types import ActionType # noqa
25-
from .websocket_types import ConnectionRequest, WebsocketHeaderUpdate
24+
from .websocket_types import ActionType, ConnectionRequest, WebsocketHeaderUpdate, WebsocketStatus # noqa
2625

2726
# InternalConnectionRequest,
2827
_ = (

csp/tests/adapters/test_websocket.py

+46-27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
RawTextMessageMapper,
2424
Status,
2525
WebsocketAdapterManager,
26+
WebsocketStatus,
2627
)
2728

2829
class EchoWebsocketHandler(tornado.websocket.WebSocketHandler):
@@ -263,34 +264,52 @@ def g():
263264
csp.run(g, starttime=datetime.now(pytz.UTC), endtime=timedelta(seconds=1), realtime=True)
264265

265266
def test_dynamic_multiple_subscribers(self):
266-
with tornado_server():
267+
@csp.node
268+
def send_on_status(status: ts[Status], uri: str, val: str) -> ts[str]:
269+
if csp.ticked(status):
270+
if uri in status.msg and status.status_code == WebsocketStatus.ACTIVE.value:
271+
return val
267272

268-
@csp.graph
269-
def g():
270-
ws = WebsocketAdapterManager(dynamic=True)
271-
conn_request1 = csp.const(
272-
ConnectionRequest(uri="ws://localhost:8000/", on_connect_payload="hey world from 8000")
273-
)
274-
conn_request2 = csp.const(
275-
ConnectionRequest(uri="ws://localhost:8001/", on_connect_payload="hey world from 8001")
276-
)
277-
recv = ws.subscribe(str, RawTextMessageMapper(), connection_request=conn_request1)
278-
recv2 = ws.subscribe(str, RawTextMessageMapper(), connection_request=conn_request2)
279-
280-
csp.add_graph_output("recv", recv)
281-
csp.add_graph_output("recv2", recv2)
282-
283-
merged = csp.flatten([recv, recv2])
284-
stop = csp.filter(csp.count(merged) == 2, merged)
285-
csp.stop_engine(stop)
286-
287-
msgs = csp.run(g, starttime=datetime.now(pytz.UTC), endtime=timedelta(seconds=5), realtime=True)
288-
assert len(msgs["recv"]) == 1
289-
assert msgs["recv"][0][1].msg == "hey world from 8000"
290-
assert msgs["recv"][0][1].uri == "ws://localhost:8000/"
291-
assert len(msgs["recv2"]) == 1
292-
assert msgs["recv2"][0][1].msg == "hey world from 8001"
293-
assert msgs["recv2"][0][1].uri == "ws://localhost:8001/"
273+
with tornado_server():
274+
# We do this to only spawn the tornado server once for both options
275+
for use_on_connect_payload in [True, False]:
276+
277+
@csp.graph
278+
def g():
279+
ws = WebsocketAdapterManager(dynamic=True)
280+
if use_on_connect_payload:
281+
conn_request1 = csp.const(
282+
ConnectionRequest(uri="ws://localhost:8000/", on_connect_payload="hey world from 8000")
283+
)
284+
conn_request2 = csp.const(
285+
ConnectionRequest(uri="ws://localhost:8001/", on_connect_payload="hey world from 8001")
286+
)
287+
else:
288+
conn_request1 = csp.const(ConnectionRequest(uri="ws://localhost:8000/"))
289+
conn_request2 = csp.const(ConnectionRequest(uri="ws://localhost:8001/"))
290+
status = ws.status()
291+
to_send = send_on_status(status, "ws://localhost:8000/", "hey world from 8000")
292+
to_send2 = send_on_status(status, "ws://localhost:8001/", "hey world from 8001")
293+
ws.send(to_send, connection_request=conn_request1)
294+
ws.send(to_send2, connection_request=conn_request2)
295+
296+
recv = ws.subscribe(str, RawTextMessageMapper(), connection_request=conn_request1)
297+
recv2 = ws.subscribe(str, RawTextMessageMapper(), connection_request=conn_request2)
298+
299+
csp.add_graph_output("recv", recv)
300+
csp.add_graph_output("recv2", recv2)
301+
302+
merged = csp.flatten([recv, recv2])
303+
stop = csp.filter(csp.count(merged) == 2, merged)
304+
csp.stop_engine(stop)
305+
306+
msgs = csp.run(g, starttime=datetime.now(pytz.UTC), endtime=timedelta(seconds=5), realtime=True)
307+
assert len(msgs["recv"]) == 1
308+
assert msgs["recv"][0][1].msg == "hey world from 8000"
309+
assert msgs["recv"][0][1].uri == "ws://localhost:8000/"
310+
assert len(msgs["recv2"]) == 1
311+
assert msgs["recv2"][0][1].msg == "hey world from 8001"
312+
assert msgs["recv2"][0][1].uri == "ws://localhost:8001/"
294313

295314
@pytest.mark.parametrize("dynamic", [False, True])
296315
def test_send_recv_burst_json(self, dynamic):

0 commit comments

Comments
 (0)