def test_socket_manager_subscribe(get_by_name):
    ("SocketManager().subscribe() should yield the topic and ")

    # Background: wait_until_ready is mocked to return the socket
    socket = get_by_name.return_value
    socket.recv_multipart.return_value = ['metrics:whatevs', 'the-data']

    # Given a zmq mock
    zmq = Mock()

    # And a context
    context = Mock()

    # And a serializer
    serializer = Mock(name='serializer')

    # And a socket manager
    manager = SocketManager(zmq, context, serialization_backend=serializer)

    # And a socket
    socket = manager.create('foobar', zmq.REP)

    # When I perform one iteration in the subscriber
    topic, payload = next(manager.subscribe('foobar'))

    # Then it should have unpacked the payload after receiving
    serializer.unpack.assert_called_once_with('the-data')

    # And the result should be the unpacked value
    payload.should.equal(serializer.unpack.return_value)

    # And the topic should be the expected
    topic.should.equal('metrics:whatevs')
def test_socket_manager_subscribe_invalid_callable(get_by_name):
    (
        "SocketManager().subscribe() should raise TypeError "
        "if the keep_polling callable is not callable"
    )

    # Background: wait_until_ready is mocked to return the socket
    socket = get_by_name.return_value
    socket.recv_multipart.return_value = ["metrics:whatevs", "the-data"]

    # Given a zmq mock
    zmq = Mock()

    # And a context
    context = Mock()

    # And a serializer
    serializer = Mock(name="serializer")

    # And a socket manager
    manager = SocketManager(zmq, context, serialization_backend=serializer)

    # And a socket
    socket = manager.create("foobar", zmq.REP)

    # When I perform one iteration in the subscriber
    when_called = list.when.called_with(
        manager.subscribe("foobar", keep_polling="not a callable")
    )
    when_called.should.have.raised(
        TypeError,
        "SocketManager.subscribe parameter keep_polling must be a function or callable that returns a boolean",
    )
def test_socket_manager_subscribe(get_by_name):
    ("SocketManager().subscribe() should yield the topic and ")

    # Background: wait_until_ready is mocked to return the socket
    socket = get_by_name.return_value
    socket.recv_multipart.return_value = ['metrics:whatevs', 'the-data']

    # Given a zmq mock
    zmq = Mock()

    # And a context
    context = Mock()

    # And a serializer
    serializer = Mock(name='serializer')

    # And a socket manager
    manager = SocketManager(zmq, context, serialization_backend=serializer)

    # And a socket
    socket = manager.create('foobar', zmq.REP)

    # When I perform one iteration in the subscriber
    topic, payload = next(manager.subscribe('foobar'))

    # Then it should have unpacked the payload after receiving
    serializer.unpack.assert_called_once_with('the-data')

    # And the result should be the unpacked value
    payload.should.equal(serializer.unpack.return_value)

    # And the topic should be the expected
    topic.should.equal('metrics:whatevs')
Beispiel #4
0
def test_socket_manager_subscribe():
    ("SocketManager.subscribe should subscribe from a topic")

    # Given a manager
    manager = SocketManager(zmq, context)

    # And a couple of sockets
    manager.ensure_and_bind('foo', zmq.PUB, 'inproc://test.publisher.2', zmq.POLLOUT)
    manager.create('bar', zmq.SUB)
    manager.connect('bar', 'inproc://test.publisher.2', zmq.POLLIN)

    gevent.spawn(manager.publish_safe, 'foo', 'some-topic', {'some': 'value'})

    # Then it should have received
    topic, result = next(manager.subscribe('bar'))
    topic.should.equal('some-topic')
    result.should.equal({'some': 'value'})
def test_socket_manager_subscribe(get_by_name):
    ("SocketManager().subscribe() should yield the topic and yield an Event")

    # Background: wait_until_ready is mocked to return the socket
    socket = Mock(name='<socket(name="foobar")>')
    get_by_name.side_effect = [socket, socket, socket, socket, socket, None]

    socket.recv_multipart.side_effect = [
        ["metrics:whatevs", "the-data"],
        ["action", "test"],
    ]

    # Given a zmq mock
    zmq = Mock()

    # And a context
    context = Mock()

    # And a serializer
    serializer = Mock(name="serializer")

    # And a socket manager
    manager = SocketManager(zmq, context, serialization_backend=serializer)

    # And a socket
    socket = manager.create("foobar", zmq.REP)

    # When I perform one iteration in the subscriber
    events = list(manager.subscribe("foobar"))
    events.should.have.length_of(2)
    event1, event2 = events
    topic1, payload1 = event1.topic, event1.data

    # Then it should have unpacked the payload after receiving
    serializer.unpack.assert_has_calls([call("the-data"), call("test")])

    # And the result should be the unpacked value
    payload1.should.equal(serializer.unpack.return_value)

    # And the topic should be the expected
    topic1.should.equal("metrics:whatevs")

    event2.should.be.an(Event)