Esempio n. 1
0
def test_subscribe(runpath):
    """Test subscribing to a particular data format."""
    server = ZMQServer(name='server',
                       host='127.0.0.1',
                       port=0,
                       message_pattern=zmq.PUB,
                       runpath=os.path.join(runpath, 'server'))

    with server:
        client = ZMQClient(name='client',
                           hosts=[server.host],
                           ports=[server.port],
                           message_pattern=zmq.SUB,
                           runpath=os.path.join(runpath, 'client'))

        with client:
            client.subscribe(b'Hello')
            time.sleep(1)

            data1 = b'Hello World'
            data2 = b'random message'
            server.send(data=data1, timeout=TIMEOUT)
            recv = client.receive(timeout=TIMEOUT)
            assert data1 == recv
            server.send(data=data2, timeout=TIMEOUT)
            with pytest.raises(TimeoutException):
                client.receive(timeout=0.2)
Esempio n. 2
0
def test_many_publish_one_subscribe(runpath):
    """Test connecting one subscribe client to two publish servers."""
    server1 = ZMQServer(name='server1',
                        host='127.0.0.1',
                        port=0,
                        message_pattern=zmq.PUB,
                        runpath=os.path.join(runpath, 'server1'))
    server2 = ZMQServer(name='server2',
                        host='127.0.0.1',
                        port=0,
                        message_pattern=zmq.PUB,
                        runpath=os.path.join(runpath, 'server2'))

    with server1, server2:
        client = ZMQClient(name='client',
                           hosts=[server1.host, server2.host],
                           ports=[server1.port, server2.port],
                           message_pattern=zmq.SUB,
                           runpath=os.path.join(runpath, 'client'))

        with client:
            client.subscribe(b'')

            # The SUB client seems to take longer to connect and results in
            # dropped messages.
            time.sleep(1)

            data1 = b'Hello'
            data2 = b'World'
            server1.send(data=data1, timeout=TIMEOUT)
            recv1 = client.receive(timeout=TIMEOUT)
            server2.send(data=data2, timeout=TIMEOUT)
            recv2 = client.receive(timeout=TIMEOUT)
            assert data1 == recv1
            assert data2 == recv2
Esempio n. 3
0
def test_many_push_one_pull(runpath):
    """Test connecting one pull client to two push servers."""
    server1 = ZMQServer(name='server1',
                        host='127.0.0.1',
                        port=0,
                        message_pattern=zmq.PUSH,
                        runpath=os.path.join(runpath, 'server1'))
    server2 = ZMQServer(name='server2',
                        host='127.0.0.1',
                        port=0,
                        message_pattern=zmq.PUSH,
                        runpath=os.path.join(runpath, 'server2'))

    with server1, server2:
        client = ZMQClient(name='client',
                           hosts=[server1.host, server2.host],
                           ports=[server1.port, server2.port],
                           message_pattern=zmq.PULL,
                           runpath=os.path.join(runpath, 'client'))

        with client:
            time.sleep(1)

            data1 = b'Hello'
            data2 = b'World'
            server1.send(data=data1, timeout=TIMEOUT)
            recv1 = client.receive(timeout=TIMEOUT)
            server2.send(data=data2, timeout=TIMEOUT)
            recv2 = client.receive(timeout=TIMEOUT)
            assert data1 == recv1
            assert data2 == recv2
Esempio n. 4
0
def test_one_publish_many_subscribe(runpath):
    """Test connecting two subscribe clients to one publish server."""
    server = ZMQServer(name='server',
                       host='127.0.0.1',
                       port=0,
                       message_pattern=zmq.PUB,
                       runpath=os.path.join(runpath, 'server'))

    with server:
        client1 = ZMQClient(name='client1',
                            hosts=[server.host],
                            ports=[server.port],
                            message_pattern=zmq.SUB,
                            runpath=os.path.join(runpath, 'client1'))
        client2 = ZMQClient(name='client2',
                            hosts=[server.host],
                            ports=[server.port],
                            message_pattern=zmq.SUB,
                            runpath=os.path.join(runpath, 'client2'))

        with client1, client2:
            for client in (client1, client2):
                client.subscribe(b'')

            # The SUB client seems to take longer to connect and results in
            # dropped messages.
            time.sleep(1)

            data = b'Hello World'
            server.send(data=data, timeout=TIMEOUT)
            recv1 = client1.receive(timeout=TIMEOUT)
            recv2 = client2.receive(timeout=TIMEOUT)

            assert data == recv1
            assert data == recv2
Esempio n. 5
0
def test_flush(runpath):
    """Test flushing the client receive queue."""
    server = ZMQServer(name='server',
                       host='127.0.0.1',
                       port=0,
                       message_pattern=zmq.PAIR,
                       runpath=os.path.join(runpath, 'server'))

    with server:
        client = ZMQClient(name='client',
                           hosts=[server.host],
                           ports=[server.port],
                           message_pattern=zmq.PAIR,
                           runpath=os.path.join(runpath, 'client'))

        with client:
            client.send(data=b'Hello World', timeout=TIMEOUT)
            server.receive(timeout=TIMEOUT)
            server.send(data=b'Hello client', timeout=TIMEOUT)
            client.flush()

            with pytest.raises(TimeoutException):
                client.receive(timeout=0.2)