Ejemplo n.º 1
0
def test_backoff():
    with NsqdIntegrationServer() as server:
        conn = Nsqd(
            address=server.address,
            tcp_port=server.tcp_port,
            http_port=server.http_port,
        )

        for _ in range(500):
            conn.publish_http('test', 'danger zone!')

        reader = Reader(topic='test',
                        channel='test',
                        nsqd_tcp_addresses=[server.tcp_address],
                        max_in_flight=100,
                        message_handler=lambda reader, message: None)

        reader.start(block=False)
        reader.start_backoff()

        assert reader.state == states.THROTTLED
        assert reader.total_in_flight_or_ready <= 1

        reader.complete_backoff()
        assert reader.state == states.RUNNING
Ejemplo n.º 2
0
def test_random_ready_conn():
    conn_1 = Nsqd('nsq1')
    conn_2 = Nsqd('nsq2')
    reader = Reader('test', 'test', 'localhost:4150')

    reader.max_in_flight = 2
    reader.conns = [conn_2]
    reader.last_random_ready = 0
    assert reader.random_ready_conn(conn_1) is conn_1
    assert reader.last_random_ready == 0

    reader.max_in_flight = 1
    reader.conns = [conn_2, conn_2]
    reader.last_random_ready = last_random_ready = time.time()
    assert reader.random_ready_conn(conn_1) is conn_1
    assert reader.last_random_ready == last_random_ready

    reader.max_in_flight = 1
    reader.conns = [conn_2, conn_2]
    reader.last_random_ready = 0
    assert reader.random_ready_conn(conn_1) is conn_2
    assert reader.last_random_ready != 0

    reader.max_in_flight = 1
    reader.conns = [conn_2, conn_2]
    reader.last_random_ready = 0
    conn_2.ready_count = 1
    assert reader.random_ready_conn(conn_1) is conn_1
    assert reader.last_random_ready == 0
Ejemplo n.º 3
0
def test_multipublish():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(11) == 'MPUB topic\n'

        size = nsq.unpack_size(socket.recv(4))
        data = socket.recv(size)

        head, data = data[:4], data[4:]
        assert nsq.unpack_size(head) == 2

        for _ in xrange(2):
            head, data = data[:4], data[4:]
            assert nsq.unpack_size(head) == 3

            head, data = data[:3], data[3:]
            assert head == 'sup'

        assert data == ''

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        conn.multipublish('topic', ['sup', 'sup'])
Ejemplo n.º 4
0
def test_backoff():
    with NsqdIntegrationServer() as server:
        conn = Nsqd(
            address=server.address,
            tcp_port=server.tcp_port,
            http_port=server.http_port,
        )

        for _ in range(500):
            conn.publish_http('test', 'danger zone!')

        reader = Reader(
            topic='test',
            channel='test',
            nsqd_tcp_addresses=[server.tcp_address],
            max_in_flight=100,
            message_handler=lambda reader, message: None
        )

        reader.start(block=False)
        reader.start_backoff()

        assert reader.state == states.THROTTLED
        assert reader.total_in_flight_or_ready <= 1

        reader.complete_backoff()
        assert reader.state == states.RUNNING
Ejemplo n.º 5
0
def test_random_ready_conn():
    conn_1 = Nsqd('nsq1')
    conn_2 = Nsqd('nsq2')
    reader = Reader('test', 'test', 'localhost:4150')

    reader.max_in_flight = 2
    reader.conns = [conn_2]
    reader.last_random_ready = 0
    assert reader.random_ready_conn(conn_1) is conn_1
    assert reader.last_random_ready == 0

    reader.max_in_flight = 1
    reader.conns = [conn_2, conn_2]
    reader.last_random_ready = last_random_ready = time.time()
    assert reader.random_ready_conn(conn_1) is conn_1
    assert reader.last_random_ready == last_random_ready

    reader.max_in_flight = 1
    reader.conns = [conn_2, conn_2]
    reader.last_random_ready = 0
    assert reader.random_ready_conn(conn_1) is conn_2
    assert reader.last_random_ready != 0

    reader.max_in_flight = 1
    reader.conns = [conn_2, conn_2]
    reader.last_random_ready = 0
    conn_2.ready_count = 1
    assert reader.random_ready_conn(conn_1) is conn_1
    assert reader.last_random_ready == 0
Ejemplo n.º 6
0
def test_max_concurrency():
    server1 = NsqdIntegrationServer()
    server2 = NsqdIntegrationServer()

    with server1, server2:

        class Accounting(object):
            count = 0
            total = 100
            concurrency = 0
            error = None

        for server in (server1, server2):
            conn = Nsqd(
                address=server.address,
                tcp_port=server.tcp_port,
                http_port=server.http_port,
            )

            for _ in range(Accounting.total // 2):
                conn.publish_http('test', b'danger zone!')

        reader = Reader(
            topic='test',
            channel='test',
            nsqd_tcp_addresses=[
                server1.tcp_address,
                server2.tcp_address,
            ],
            max_in_flight=5,
            max_concurrency=1,
        )

        @reader.on_exception.connect
        def error_handler(reader, message, error):
            if isinstance(error, NSQSocketError):
                return
            Accounting.error = error
            reader.close()

        @reader.on_message.connect
        def handler(reader, message):
            assert message.body == b'danger zone!'
            assert Accounting.concurrency == 0

            Accounting.concurrency += 1
            gevent.sleep()
            Accounting.concurrency -= 1

            Accounting.count += 1
            if Accounting.count == Accounting.total:
                reader.close()

        reader.start()

        if Accounting.error:
            raise Accounting.error

        assert Accounting.count == Accounting.total
Ejemplo n.º 7
0
def test_hashing():
    conn1 = Nsqd('localhost', 1337)
    conn2 = Nsqd('localhost', 1337)
    assert conn1 == conn2
    assert not (conn1 < conn2)
    assert not (conn2 < conn1)

    test = {conn1: True}
    assert conn2 in test
Ejemplo n.º 8
0
    def _(server1, server2):
        class Accounting(object):
            count = 0
            total = 100
            concurrency = 0
            error = None

        for server in (server1, server2):
            conn = Nsqd(
                address=server.address,
                tcp_port=server.tcp_port,
                http_port=server.http_port,
            )

            for _ in xrange(Accounting.total / 2):
                conn.publish_http('test', 'danger zone!')

        reader = Reader(
            topic='test',
            channel='test',
            nsqd_tcp_addresses=[
                server1.tcp_address,
                server2.tcp_address,
            ],
            max_in_flight=5,
            max_concurrency=1,
        )

        @reader.on_exception.connect
        def error_handler(reader, message, error):
            if isinstance(error, NSQSocketError):
                return
            Accounting.error = error
            reader.close()

        @reader.on_message.connect
        def handler(reader, message):
            assert message.body == 'danger zone!'
            assert Accounting.concurrency == 0

            Accounting.concurrency += 1
            gevent.sleep()
            Accounting.concurrency -= 1

            Accounting.count += 1
            if Accounting.count == Accounting.total:
                reader.close()

        try:
            reader.start()
        except NSQSocketError:
            pass

        if Accounting.error:
            raise Accounting.error

        assert Accounting.count == Accounting.total
Ejemplo n.º 9
0
def test_command(command, args, resp):
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(len(resp)) == resp

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        getattr(conn, command)(*args)
Ejemplo n.º 10
0
def test_command(command, args, resp):
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(len(resp)) == resp

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        getattr(conn, command)(*args)
Ejemplo n.º 11
0
def test_lookupd():
    with LookupdIntegrationServer() as lookupd_server:
        server1 = NsqdIntegrationServer(lookupd=lookupd_server.tcp_address)
        server2 = NsqdIntegrationServer(lookupd=lookupd_server.tcp_address)

        with server1, server2:

            class Accounting(object):
                count = 0
                total = 500
                concurrency = 0
                error = None

            for server in (server1, server2):
                conn = Nsqd(
                    address=server.address,
                    tcp_port=server.tcp_port,
                    http_port=server.http_port,
                )

                for _ in range(Accounting.total // 2):
                    conn.publish_http('test', b'danger zone!')

            reader = Reader(
                topic='test',
                channel='test',
                lookupd_http_addresses=lookupd_server.http_address,
                max_in_flight=32,
            )

            @reader.on_exception.connect
            def error_handler(reader, message, error):
                if isinstance(error, NSQSocketError):
                    return
                Accounting.error = error
                reader.close()

            @reader.on_message.connect
            def handler(reader, message):
                assert message.body == b'danger zone!'

                Accounting.count += 1
                if Accounting.count == Accounting.total:
                    reader.close()

            gevent.sleep(0.1)
            reader.start()

            if Accounting.error:
                raise Accounting.error

            assert Accounting.count == Accounting.total
Ejemplo n.º 12
0
def test_publish():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(10) == 'PUB topic\n'

        assert nsq.unpack_size(socket.recv(4)) == 3
        assert socket.recv(3) == 'sup'

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        conn.publish('topic', 'sup')
Ejemplo n.º 13
0
def test_error(error_msg, error, fatal):
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        socket.send(mock_response(nsq.FRAME_TYPE_ERROR, error_msg))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        frame, resp = conn.read_response()
        assert frame == nsq.FRAME_TYPE_ERROR
        assert isinstance(resp, getattr(errors, error))
        assert conn.is_connected != fatal
Ejemplo n.º 14
0
def test_sync_heartbeat():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, b'_heartbeat_'))
        assert socket.recv(4) == b'NOP\n'

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == b'_heartbeat_'
Ejemplo n.º 15
0
def test_error(error_msg, error, fatal):
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        socket.send(mock_response(nsq.FRAME_TYPE_ERROR, error_msg))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        frame, resp = conn.read_response()
        assert frame == nsq.FRAME_TYPE_ERROR
        assert isinstance(resp, getattr(errors, error))
        assert conn.is_connected != fatal
Ejemplo n.º 16
0
def test_sync_heartbeat():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, '_heartbeat_'))
        assert socket.recv(4) == 'NOP\n'

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == '_heartbeat_'
Ejemplo n.º 17
0
def test_messages():
    with NsqdIntegrationServer() as server:

        class Accounting(object):
            count = 0
            total = 500
            error = None

        conn = Nsqd(
            address=server.address,
            tcp_port=server.tcp_port,
            http_port=server.http_port,
        )

        for _ in xrange(Accounting.total):
            conn.publish_http('test', 'danger zone!')

        reader = Reader(
            topic='test',
            channel='test',
            nsqd_tcp_addresses=[server.tcp_address],
            max_in_flight=100,
        )

        @reader.on_exception.connect
        def error_handler(reader, message, error):
            if isinstance(error, NSQSocketError):
                return
            Accounting.error = error
            reader.close()

        @reader.on_message.connect
        def handler(reader, message):
            assert message.body == 'danger zone!'

            Accounting.count += 1
            if Accounting.count == Accounting.total:
                assert not reader.is_starved
                reader.close()

        try:
            reader.start()
        except NSQSocketError:
            pass

        if Accounting.error:
            raise Accounting.error

        assert Accounting.count == Accounting.total
Ejemplo n.º 18
0
def test_identify():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(9) == 'IDENTIFY\n'

        size = nsq.unpack_size(socket.recv(4))
        data = json.loads(socket.recv(size))

        assert 'gnsq' in data['user_agent']
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, 'OK'))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn.identify() is None
Ejemplo n.º 19
0
def test_messages():
    with NsqdIntegrationServer() as server:

        class Accounting(object):
            count = 0
            total = 500
            error = None

        conn = Nsqd(
            address=server.address,
            tcp_port=server.tcp_port,
            http_port=server.http_port,
        )

        for _ in range(Accounting.total):
            conn.publish_http('test', b'danger zone!')

        reader = Reader(
            topic='test',
            channel='test',
            nsqd_tcp_addresses=[server.tcp_address],
            max_in_flight=100,
        )

        @reader.on_exception.connect
        def error_handler(reader, message, error):
            if isinstance(error, NSQSocketError):
                return
            Accounting.error = error
            reader.close()

        @reader.on_message.connect
        def handler(reader, message):
            assert message.body == b'danger zone!'

            Accounting.count += 1
            if Accounting.count == Accounting.total:
                assert not reader.is_starved
                reader.close()

        reader.start()

        if Accounting.error:
            raise Accounting.error

        assert Accounting.count == Accounting.total
Ejemplo n.º 20
0
def test_identify():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(9) == b'IDENTIFY\n'

        size = nsq.unpack_size(socket.recv(4))
        data = json.loads(socket.recv(size).decode('utf-8'))

        assert 'gnsq' in data['user_agent']
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK'))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn.identify() is None
Ejemplo n.º 21
0
        def _(server1, server2):
            class Accounting(object):
                count = 0
                total = 500
                concurrency = 0
                error = None

            for server in (server1, server2):
                conn = Nsqd(
                    address=server.address,
                    tcp_port=server.tcp_port,
                    http_port=server.http_port,
                )

                for _ in range(Accounting.total // 2):
                    conn.publish_http('test', b'danger zone!')

            reader = Reader(
                topic='test',
                channel='test',
                lookupd_http_addresses=lookupd_server.http_address,
                max_in_flight=32,
            )

            @reader.on_exception.connect
            def error_handler(reader, message, error):
                if isinstance(error, NSQSocketError):
                    return
                Accounting.error = error
                reader.close()

            @reader.on_message.connect
            def handler(reader, message):
                assert message.body == b'danger zone!'

                Accounting.count += 1
                if Accounting.count == Accounting.total:
                    reader.close()

            gevent.sleep(0.1)
            reader.start()

            if Accounting.error:
                raise Accounting.error

            assert Accounting.count == Accounting.total
Ejemplo n.º 22
0
def test_disconnected():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(1) == b''

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        conn.close_stream()
        assert conn.state == states.DISCONNECTED

        with pytest.raises(errors.NSQSocketError):
            conn.nop()

        with pytest.raises(errors.NSQSocketError):
            conn.read_response()
Ejemplo n.º 23
0
def test_sync_receive_messages():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(9) == 'IDENTIFY\n'

        size = nsq.unpack_size(socket.recv(4))
        data = json.loads(socket.recv(size))

        assert isinstance(data, dict)
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, 'OK'))

        msg = 'SUB topic channel\n'
        assert socket.recv(len(msg)) == msg
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, 'OK'))

        for i in xrange(10):
            assert socket.recv(6) == 'RDY 1\n'

            body = json.dumps({'data': {'test_key': i}})
            ts = i * 1000 * 1000
            socket.send(mock_response_message(ts, i, i, body))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn.identify() is None

        conn.subscribe('topic', 'channel')
        frame, data = conn.read_response()

        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == 'OK'

        for i in xrange(10):
            conn.ready(1)
            frame, msg = conn.read_response()

            assert frame == nsq.FRAME_TYPE_MESSAGE
            assert isinstance(msg, Message)
            assert msg.timestamp == i * 1000 * 1000
            assert msg.id == '%016d' % i
            assert msg.attempts == i
            assert json.loads(msg.body)['data']['test_key'] == i
Ejemplo n.º 24
0
def test_negotiation():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(9) == b'IDENTIFY\n'

        size = nsq.unpack_size(socket.recv(4))
        data = json.loads(socket.recv(size).decode('utf-8'))

        assert 'gnsq' in data['user_agent']
        resp = six.b(json.dumps({'test': 42}))
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, resp))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn.identify()['test'] == 42
Ejemplo n.º 25
0
def test_negotiation():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(9) == b'IDENTIFY\n'

        size = nsq.unpack_size(socket.recv(4))
        data = json.loads(socket.recv(size).decode('utf-8'))

        assert 'gnsq' in data['user_agent']
        resp = six.b(json.dumps({'test': 42}))
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, resp))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn.identify()['test'] == 42
Ejemplo n.º 26
0
def test_auth():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(5) == b'AUTH\n'

        assert nsq.unpack_size(socket.recv(4)) == 6
        assert socket.recv(6) == b'secret'

        resp = six.b(json.dumps({'identity': 'awesome'}))
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, resp))

    with handle as server:
        conn = Nsqd(address='127.0.0.1',
                    tcp_port=server.server_port,
                    auth_secret=b'secret')

        conn.connect()
        resp = conn.auth()
        assert resp['identity'] == 'awesome'
Ejemplo n.º 27
0
def test_disconnected():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(1) == ''

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        conn.close_stream()
        assert conn.state == states.DISCONNECTED

        with pytest.raises(errors.NSQSocketError):
            conn.nop()

        with pytest.raises(errors.NSQSocketError):
            conn.read_response()
Ejemplo n.º 28
0
def test_multipublish():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(11) == b'MPUB topic\n'

        size = nsq.unpack_size(socket.recv(4))
        data = socket.recv(size)

        head, data = data[:4], data[4:]
        assert nsq.unpack_size(head) == 2

        for _ in range(2):
            head, data = data[:4], data[4:]
            assert nsq.unpack_size(head) == 3

            head, data = data[:3], data[3:]
            assert head == b'sup'

        assert data == b''

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        conn.multipublish('topic', [b'sup', b'sup'])
Ejemplo n.º 29
0
def test_identify_auth():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(9) == 'IDENTIFY\n'

        size = nsq.unpack_size(socket.recv(4))
        data = json.loads(socket.recv(size))
        assert 'gnsq' in data['user_agent']

        resp = json.dumps({'auth_required': True})
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, resp))

        assert socket.recv(5) == 'AUTH\n'
        assert nsq.unpack_size(socket.recv(4)) == 6
        assert socket.recv(6) == 'secret'

        resp = json.dumps({'identity': 'awesome'})
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, resp))

    with handle as server:
        conn = Nsqd(
            address='127.0.0.1',
            tcp_port=server.server_port,
            auth_secret='secret'
        )

        @conn.on_auth.connect
        def assert_auth(conn, response):
            assert assert_auth.was_called is False
            assert_auth.was_called = True
            assert response['identity'] == 'awesome'

        assert_auth.was_called = False
        conn.connect()
        resp = conn.identify()

        assert resp['auth_required']
        assert assert_auth.was_called
Ejemplo n.º 30
0
def test_auth():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(5) == 'AUTH\n'

        assert nsq.unpack_size(socket.recv(4)) == 6
        assert socket.recv(6) == 'secret'

        resp = json.dumps({'identity': 'awesome'})
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, resp))

    with handle as server:
        conn = Nsqd(
            address='127.0.0.1',
            tcp_port=server.server_port,
            auth_secret='secret'
        )

        conn.connect()
        resp = conn.auth()
        assert resp['identity'] == 'awesome'
Ejemplo n.º 31
0
def test_cls_error():
    with NsqdIntegrationServer() as server:
        conn = Nsqd(address=server.address, tcp_port=server.tcp_port)

        conn.connect()
        assert conn.state == states.CONNECTED

        conn.close()
        frame, error = conn.read_response()
        assert frame == nsq.FRAME_TYPE_ERROR
        assert isinstance(error, errors.NSQInvalid)
Ejemplo n.º 32
0
def test_read(body):
    @mock_server
    def handle(socket, address):
        socket.send(struct.pack('>l', len(body)))
        socket.send(body)

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn._read_response() == body
        conn.close_stream()
Ejemplo n.º 33
0
def test_connection():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(1) == b''

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        assert conn.state == states.INIT

        conn.connect()
        assert conn.state == states.CONNECTED

        conn.connect()
        assert conn.state == states.CONNECTED

        conn.close_stream()
        assert conn.state == states.DISCONNECTED
Ejemplo n.º 34
0
def test_cls_error():
    with NsqdIntegrationServer() as server:
        conn = Nsqd(address=server.address, tcp_port=server.tcp_port)

        conn.connect()
        assert conn.state == states.CONNECTED

        conn.close()
        frame, error = conn.read_response()
        assert frame == nsq.FRAME_TYPE_ERROR
        assert isinstance(error, errors.NSQInvalid)
Ejemplo n.º 35
0
def test_read(body):
    @mock_server
    def handle(socket, address):
        socket.send(struct.pack('>l', len(body)))
        socket.send(body)

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn._read_response() == body
        conn.close_stream()
Ejemplo n.º 36
0
def test_publish():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(10) == b'PUB topic\n'

        assert nsq.unpack_size(socket.recv(4)) == 3
        assert socket.recv(3) == b'sup'

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()
        conn.publish('topic', b'sup')
Ejemplo n.º 37
0
def test_connection():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == '  V2'
        assert socket.recv(1) == ''

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        assert conn.state == states.INIT

        conn.connect()
        assert conn.state == states.CONNECTED

        conn.connect()
        assert conn.state == states.CONNECTED

        conn.close_stream()
        assert conn.state == states.DISCONNECTED
Ejemplo n.º 38
0
def test_sync_receive_messages():
    @mock_server
    def handle(socket, address):
        assert socket.recv(4) == b'  V2'
        assert socket.recv(9) == b'IDENTIFY\n'

        size = nsq.unpack_size(socket.recv(4))
        data = json.loads(socket.recv(size).decode('utf-8'))

        assert isinstance(data, dict)
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK'))

        msg = b'SUB topic channel\n'
        assert socket.recv(len(msg)) == msg
        socket.send(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK'))

        for i in range(10):
            assert socket.recv(6) == b'RDY 1\n'

            body = six.b(json.dumps({'data': {'test_key': i}}))
            ts = i * 1000 * 1000
            socket.send(mock_response_message(ts, i, i, body))

    with handle as server:
        conn = Nsqd(address='127.0.0.1', tcp_port=server.server_port)
        conn.connect()

        assert conn.identify() is None

        conn.subscribe('topic', 'channel')
        frame, data = conn.read_response()

        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == b'OK'

        for i in range(10):
            conn.ready(1)
            frame, msg = conn.read_response()

            assert frame == nsq.FRAME_TYPE_MESSAGE
            assert isinstance(msg, Message)
            assert msg.timestamp == i * 1000 * 1000
            assert msg.id == six.b('%016d' % i)
            assert msg.attempts == i
            assert json.loads(
                msg.body.decode('utf-8'))['data']['test_key'] == i
Ejemplo n.º 39
0
def test_socket_upgrades(tls, deflate, snappy):
    with NsqdIntegrationServer() as server:
        options = {
            'address': server.address,
            'tcp_port': server.tcp_port,
            'deflate': deflate,
            'snappy': snappy,
        }

        if tls:
            options.update({
                'tls_v1': True,
                'tls_options': {
                    'keyfile': server.tls_key,
                    'certfile': server.tls_cert,
                }
            })

        conn = Nsqd(**options)
        conn.connect()
        assert conn.state == states.CONNECTED

        if deflate and snappy:
            with pytest.raises(errors.NSQErrorCode):
                conn.identify()
            return

        if tls and BAD_GEVENT:
            with pytest.raises(AttributeError):
                conn.identify()
            return

        if tls and server.version < (0, 2, 28):
            with pytest.raises(ssl.SSLError):
                conn.identify()
            return

        resp = conn.identify()
        assert isinstance(resp, dict)

        assert resp['tls_v1'] is tls
        assert resp['deflate'] is deflate
        assert resp['snappy'] is snappy

        if tls and (deflate or snappy):
            assert isinstance(conn.stream.socket._socket, SSLSocket)
        elif tls:
            assert isinstance(conn.stream.socket, SSLSocket)

        if deflate:
            assert isinstance(conn.stream.socket, DefalteSocket)

        if snappy:
            assert isinstance(conn.stream.socket, SnappySocket)

        conn.publish('topic', 'sup')
        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == 'OK'

        conn.subscribe('topic', 'channel')
        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == 'OK'

        conn.ready(1)
        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_MESSAGE
        assert data.body == 'sup'

        conn.close_stream()
Ejemplo n.º 40
0
def test_socket_upgrades(tls, deflate, snappy):
    with NsqdIntegrationServer() as server:
        options = {
            'address': server.address,
            'tcp_port': server.tcp_port,
            'deflate': deflate,
            'snappy': snappy,
        }

        if tls:
            options.update({
                'tls_v1': True,
                'tls_options': {
                    'keyfile': server.tls_key,
                    'certfile': server.tls_cert,
                }
            })

        conn = Nsqd(**options)
        conn.connect()
        assert conn.state == states.CONNECTED

        if deflate and snappy:
            with pytest.raises(errors.NSQErrorCode):
                conn.identify()
            return

        if tls and BAD_GEVENT:
            with pytest.raises(AttributeError):
                conn.identify()
            return

        if tls and server.version < (0, 2, 28):
            with pytest.raises(ssl.SSLError):
                conn.identify()
            return

        resp = conn.identify()
        assert isinstance(resp, dict)

        assert resp['tls_v1'] is tls
        assert resp['deflate'] is deflate
        assert resp['snappy'] is snappy

        if tls and (deflate or snappy):
            assert isinstance(conn.stream.socket._socket, SSLSocket)
        elif tls:
            assert isinstance(conn.stream.socket, SSLSocket)

        if deflate:
            assert isinstance(conn.stream.socket, DefalteSocket)

        if snappy:
            assert isinstance(conn.stream.socket, SnappySocket)

        conn.publish('topic', b'sup')
        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == b'OK'

        conn.subscribe('topic', 'channel')
        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_RESPONSE
        assert data == b'OK'

        conn.ready(1)
        frame, data = conn.read_response()
        assert frame == nsq.FRAME_TYPE_MESSAGE
        assert data.body == b'sup'

        conn.close_stream()