-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finer topic handling #143
Finer topic handling #143
Changes from 6 commits
ce7149e
dda74d5
985f086
d03ec09
df99372
75110ae
8415517
d3dc08b
938341d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -898,6 +898,51 @@ def _connect_and_register(self, client_address, alias=None, handler=None, | |
self.register(socket, register_as, alias, handler) | ||
return client_address | ||
|
||
def unsubscribe_from_topic(self, | ||
socket: Union[AgentAddress, str, zmq.Socket], | ||
topic: bytes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (maybe this should be considered in the tests as well: test with bytes and test with string). |
||
''' | ||
Unsubscribe a socket from a given topic. | ||
|
||
Parameters | ||
---------- | ||
socket | ||
Identifier of the socket. Must be a valid entry of `self.socket` | ||
for the PUB/SUB pattern and a valid entry of `self.address` for | ||
the SYNC_PUB/SYNC_SUB pattern | ||
topic | ||
topic which we want to unsubscribe from | ||
''' | ||
if isinstance(self.address[socket], AgentAddress): | ||
self.socket[socket].setsockopt(zmq.UNSUBSCRIBE, topic) | ||
elif isinstance(self.address[socket], AgentChannel): | ||
channel = self.address[socket] | ||
socket = channel.receiver | ||
treated_topic = channel.uuid + topic | ||
self.socket[socket].setsockopt(zmq.UNSUBSCRIBE, treated_topic) | ||
|
||
def subscribe_to_topic(self, socket: Union[AgentAddress, str, zmq.Socket], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
topic: bytes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (maybe this should be considered in the tests as well: test with bytes and test with string). |
||
''' | ||
Subscribe a socket to a given topic. | ||
|
||
Parameters | ||
---------- | ||
socket | ||
Identifier of the socket. Must be a valid entry of `self.socket` | ||
for the PUB/SUB pattern and a valid entry of `self.address` for | ||
the SYNC_PUB/SYNC_SUB pattern | ||
topic | ||
topic which we want to subscribe to | ||
''' | ||
if isinstance(self.address[socket], AgentAddress): | ||
self.socket[socket].setsockopt(zmq.SUBSCRIBE, topic) | ||
elif isinstance(self.address[socket], AgentChannel): | ||
channel = self.address[socket] | ||
socket = channel.receiver | ||
treated_topic = channel.uuid + topic | ||
self.socket[socket].setsockopt(zmq.SUBSCRIBE, treated_topic) | ||
|
||
def _handle_async_requests(self, data): | ||
address_uuid, uuid, response = data | ||
if uuid not in self._pending_requests: | ||
|
@@ -932,7 +977,7 @@ def _subscribe(self, alias: str, handlers: Dict[Union[bytes, str], Any]): | |
curated_handlers = topics_to_bytes(handlers) | ||
# Subscribe to topics | ||
for topic in curated_handlers.keys(): | ||
self.socket[alias].setsockopt(zmq.SUBSCRIBE, topic) | ||
self.subscribe_to_topic(alias, topic) | ||
# Reset handlers | ||
self._set_handler(self.socket[alias], curated_handlers) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,41 @@ def on_error(agent): | |
agent.error_log.append('error') | ||
|
||
|
||
@pytest.mark.parametrize('socket_type', ['PUB', 'SYNC_PUB']) | ||
def test_change_subscription_topics_sync(nsproxy, socket_type): | ||
''' | ||
Test for the different options of subscribing/unsubscribing to topics | ||
in the SYNC_PUB/SYNC_SUB pattern. | ||
''' | ||
server = run_agent('server') | ||
client = run_agent('client') | ||
|
||
addr = server.bind(socket_type, alias='pub', handler=lambda: None) | ||
client.set_attr(received=[]) | ||
client.connect(addr, alias='sub', handler=append_received) | ||
|
||
# Stablish a connection | ||
server.each(0.1, 'send', 'pub', 'connecting...') | ||
assert wait_agent_attr(client, name='received', data='connecting...') | ||
|
||
# By default, client will be subscribed to all topics | ||
server.send('pub', 'hello') | ||
assert wait_agent_attr(client, name='received', data='hello') | ||
|
||
# Only subscribe to 'TOP' topic | ||
client.unsubscribe_from_topic('sub', b'') | ||
client.subscribe_to_topic('sub', b'TOP') | ||
|
||
# Message not received since 'TOP' topic not specified in the send call | ||
server.send('pub', 'world') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again we find synchronization problems here. What do you think about this? We create a function only for tests What you think? (This would apply for other tests we have around there, until we implement heart-beating or some other fancy stuff). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is a good idea, however I would still need to set a handler other than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I'm just throwing random ideas... 😂 |
||
assert not wait_agent_attr(client, name='received', data='world', | ||
timeout=1) | ||
|
||
# Receive message with the topic we are subscribed to | ||
server.send('pub', 'ten', topic='TOP') | ||
assert wait_agent_attr(client, name='received', data='ten') | ||
|
||
|
||
@pytest.mark.parametrize('server', [Server, PubServer]) | ||
def test_simple_pub_single_handler(nsproxy, server): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
Union[AgentAddress, AgentChannel, str]
?AgentChannel should be there, and I would remove
zmq.Socket`, as the user should never be working with raw sockets in osBrain.