1+ from threading import Event
2+
3+ from compas_eve import Message
4+ from compas_eve import Publisher
5+ from compas_eve import Subscriber
6+ from compas_eve import Topic
7+ from compas_eve import set_default_transport
8+
9+ try :
10+ from compas_eve import ZeroMQTransport
11+ ZEROMQ_AVAILABLE = True
12+ except ImportError :
13+ ZEROMQ_AVAILABLE = False
14+
15+ import pytest
16+
17+
18+ @pytest .mark .skipif (not ZEROMQ_AVAILABLE , reason = "ZeroMQ transport not available" )
19+ def test_zeromq_tcp_pubsub ():
20+ """Test ZeroMQ transport with TCP endpoints."""
21+ tx = ZeroMQTransport ("tcp://localhost:25555" )
22+ event = Event ()
23+ topic = Topic ("/messages_compas_eve_test/tcp_pubsub/" , Message )
24+
25+ Subscriber (topic , lambda m : event .set (), transport = tx ).subscribe ()
26+
27+ # Small delay to ensure subscriber is ready
28+ import time
29+ time .sleep (0.1 )
30+
31+ Publisher (topic , transport = tx ).publish (Message (done = True ))
32+
33+ received = event .wait (timeout = 3 )
34+ assert received , "Message not received"
35+
36+ tx .close ()
37+
38+
39+ @pytest .mark .skipif (not ZEROMQ_AVAILABLE , reason = "ZeroMQ transport not available" )
40+ def test_zeromq_tcp_message_content ():
41+ """Test that message content is preserved correctly with TCP transport."""
42+ tx = ZeroMQTransport ("tcp://localhost:25556" )
43+ event = Event ()
44+ topic = Topic ("/messages_compas_eve_test/tcp_content/" , Message )
45+
46+ result = {}
47+
48+ def callback (msg ):
49+ result ["message" ] = msg
50+ event .set ()
51+
52+ Subscriber (topic , callback , transport = tx ).subscribe ()
53+
54+ # Small delay to ensure subscriber is ready
55+ import time
56+ time .sleep (0.1 )
57+
58+ test_message = Message (
59+ name = "ZeroMQ Test" ,
60+ value = 42 ,
61+ nested = {"key" : "value" , "list" : [1 , 2 , 3 ]}
62+ )
63+ Publisher (topic , transport = tx ).publish (test_message )
64+
65+ received = event .wait (timeout = 3 )
66+ assert received , "Message not received"
67+ assert result ["message" ].name == "ZeroMQ Test"
68+ assert result ["message" ].value == 42
69+ assert result ["message" ].nested ["key" ] == "value"
70+ assert result ["message" ].nested ["list" ] == [1 , 2 , 3 ]
71+
72+ tx .close ()
73+
74+
75+ @pytest .mark .skipif (not ZEROMQ_AVAILABLE , reason = "ZeroMQ transport not available" )
76+ def test_zeromq_tcp_multiple_topics ():
77+ """Test that multiple topics work correctly with TCP transport."""
78+ tx = ZeroMQTransport ("tcp://localhost:25557" )
79+
80+ topic1 = Topic ("/test/topic1" , Message )
81+ topic2 = Topic ("/test/topic2" , Message )
82+
83+ event1 = Event ()
84+ event2 = Event ()
85+
86+ result = {}
87+
88+ def callback1 (msg ):
89+ result ["topic1" ] = msg
90+ event1 .set ()
91+
92+ def callback2 (msg ):
93+ result ["topic2" ] = msg
94+ event2 .set ()
95+
96+ # Subscribe to both topics
97+ Subscriber (topic1 , callback1 , transport = tx ).subscribe ()
98+ Subscriber (topic2 , callback2 , transport = tx ).subscribe ()
99+
100+ # Small delay to ensure subscribers are ready
101+ import time
102+ time .sleep (0.1 )
103+
104+ # Publish to topic1
105+ Publisher (topic1 , transport = tx ).publish (Message (source = "topic1" ))
106+
107+ # Publish to topic2
108+ Publisher (topic2 , transport = tx ).publish (Message (source = "topic2" ))
109+
110+ received1 = event1 .wait (timeout = 3 )
111+ received2 = event2 .wait (timeout = 3 )
112+
113+ assert received1 , "Message 1 not received"
114+ assert received2 , "Message 2 not received"
115+ assert result ["topic1" ].source == "topic1"
116+ assert result ["topic2" ].source == "topic2"
117+
118+ tx .close ()
119+
120+
121+ @pytest .mark .skipif (not ZEROMQ_AVAILABLE , reason = "ZeroMQ transport not available" )
122+ def test_zeromq_tcp_unsubscribe ():
123+ """Test unsubscribe functionality with TCP transport."""
124+ tx = ZeroMQTransport ("tcp://localhost:25558" )
125+ topic = Topic ("/test/unsub" , Message )
126+
127+ result = dict (count = 0 , event = Event ())
128+
129+ def callback (msg ):
130+ result ["count" ] += 1
131+ result ["event" ].set ()
132+
133+ sub = Subscriber (topic , callback , transport = tx )
134+ pub = Publisher (topic , transport = tx )
135+
136+ # Subscribe and receive first message
137+ sub .subscribe ()
138+
139+ # Small delay to ensure subscriber is ready
140+ import time
141+ time .sleep (0.1 )
142+
143+ pub .publish (Message (seq = 1 ))
144+
145+ received = result ["event" ].wait (timeout = 3 )
146+ assert received , "First message not received"
147+ assert result ["count" ] == 1
148+
149+ # Unsubscribe
150+ result ["event" ].clear ()
151+ sub .unsubscribe ()
152+
153+ # Publish second message - should not be received
154+ pub .publish (Message (seq = 2 ))
155+
156+ received = result ["event" ].wait (timeout = 1 )
157+ assert received is False , "Second message received but it should have been unsubscribed"
158+ assert result ["count" ] == 1 , "Message count should still be 1"
159+
160+ tx .close ()
0 commit comments