Skip to content

Commit

Permalink
Add proposed syntax for handling subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Alonso committed Jun 19, 2017
1 parent 312bc1b commit 2c67891
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions osbrain/tests/test_agent_pubsub_topics.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down

0 comments on commit 2c67891

Please sign in to comment.