From 2c678911d2b5a9244667f8baa949f4e3123b5631 Mon Sep 17 00:00:00 2001 From: Guillermo Alonso Date: Mon, 19 Jun 2017 16:00:44 +0200 Subject: [PATCH] Add proposed syntax for handling subscriptions --- osbrain/tests/test_agent_pubsub_topics.py | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/osbrain/tests/test_agent_pubsub_topics.py b/osbrain/tests/test_agent_pubsub_topics.py index 354b87c..3200678 100644 --- a/osbrain/tests/test_agent_pubsub_topics.py +++ b/osbrain/tests/test_agent_pubsub_topics.py @@ -1,9 +1,11 @@ import time import pytest +import zmq from osbrain import run_agent from osbrain.address import AgentAddressSerializer +from osbrain.helper import wait_agent_attr from common import nsproxy # pragma: no flakes @@ -12,6 +14,40 @@ def log_received_to_list(agent, message, topic=None): agent.received_list.append(message) +def test_change_subscription_topics(nsproxy): + ''' + Test for the different options of subscribing/unsubscribing to topics + in the PUB/SUB pattern. + ''' + server = run_agent('server') + client = run_agent('client') + + addr = server.bind('PUB', alias='pub') + client.set_attr(received_list=[]) + client.connect(addr, alias='sub', handler=log_received_to_list) + + # Stablish a connection + server.each(0.1, 'send', 'pub', 'connecting...') + assert wait_agent_attr(client, name='received_list', data='connecting...') + + # By default, client will be subscribed to all topics + server.send('pub', 'hello') + assert wait_agent_attr(client, name='received_list', data='hello') + + # Only subscribe to 'TOP' topic + client.unsubscribe_socket_from_topic('sub', b'') + client.subscribe_socket_to_topic('sub', b'TOP') + + # Message not received since 'TOP' topic not specified in the send call + server.send('pub', 'world') + assert not wait_agent_attr(client, name='received_list', 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_list', data='ten') + + @pytest.mark.parametrize( 'serializer', AgentAddressSerializer.SERIALIZER_SEPARATOR