Пример #1
0
def test_ConnectionOpenOK_handling():
    conn = Connection()
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    channel_max = 10
    frame_max = 131072
    heartbeat = 100
    payload = amqpframe.methods.ConnectionTune(
        channel_max=channel_max,
        frame_max=frame_max,
        heartbeat=heartbeat,
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    payload = amqpframe.methods.ConnectionOpenOK()
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    assert fut.done() and not fut.cancelled()
    assert conn.alive
Пример #2
0
def test_ConnectionSecure_handling():
    conn = Connection(auth=CustomAuth(b'foo', b'bar'))
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN CUSTOMAUTH',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    payload = amqpframe.methods.ConnectionSecure(challenge=b'123')
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    method_bytes = io.BytesIO()
    method = amqpframe.methods.ConnectionSecureOK(response=b'246')
    method.to_bytestream(method_bytes)

    assert method_bytes.getvalue() in conn.data_to_send()
    assert conn._handshake_properties['secure']['challenge'] == b'123'
    assert conn._handshake_properties['secure']['response'] == b'246'
Пример #3
0
def test_BasicQos(ready_channel):
    method, args = draw_method_example(amqpframe.methods.BasicQos)
    frame = amqpframe.MethodFrame(ready_channel._channel_id, method)

    fut = ready_channel.send_BasicQos(**args)
    method_bytes = io.BytesIO()
    method.to_bytestream(method_bytes)
    assert method_bytes.getvalue() in ready_channel.data_to_send()

    method = amqpframe.methods.BasicQosOK()
    frame = amqpframe.MethodFrame(ready_channel._channel_id, method)
    ready_channel.handle_frame(frame)

    assert fut.done() and not fut.cancelled()
Пример #4
0
 def _send_method(self, method):
     logger.debug('Sending %s [channel_id:%s]', method.__class__.__name__,
                  self._channel_id)
     self._heartbeater.update_sent_time()
     self._fsm.trigger('send_' + method.__class__.__name__)
     frame = amqpframe.MethodFrame(self._channel_id, method)
     frame.to_bytestream(self._buffer)
Пример #5
0
def ready_channel(ready_connection):
    channel = ready_connection.get_channel()
    channel.send_ChannelOpen()

    payload = amqpframe.methods.ChannelOpenOK()
    frame = amqpframe.MethodFrame(channel._channel_id, payload)

    ready_connection.handle_frame(frame)
    return channel
Пример #6
0
def test_incorrect_ConnectionStart_handling(arguments):
    conn = Connection(locale=b'ru_RU')
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(**arguments)
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    with pytest.raises(amqproto.exceptions.UnrecoverableError):
        conn.handle_frame(frame)
Пример #7
0
def test_can_receive_valid_frame(ready_connection):
    stream = io.BytesIO()

    expected_frames = []
    payload = amqpframe.methods.ChannelOpenOK()
    frame = amqpframe.MethodFrame(1, payload)
    frame.to_bytestream(stream)
    expected_frames.append(frame)

    payload = amqpframe.methods.ChannelOpenOK()
    frame = amqpframe.MethodFrame(1, payload)
    frame.to_bytestream(stream)
    expected_frames.append(frame)

    data = stream.getvalue()

    parsed_frames = list(ready_connection.receive_frames(data))
    assert parsed_frames == expected_frames
Пример #8
0
def test_ChannelFlow_sending(ready_channel):
    fut = ready_channel.send_ChannelFlow(active=False)

    method = amqpframe.methods.ChannelFlowOK(active=False)
    frame = amqpframe.MethodFrame(ready_channel._channel_id, method)

    ready_channel.handle_frame(frame)

    assert fut.done() and not fut.cancelled()
    assert not fut.result()
    assert ready_channel.active
Пример #9
0
def test_ConnectionTune_handling(properties):
    conn = Connection(**properties['client'])
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    payload = amqpframe.methods.ConnectionTune(**properties['server'])
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    negotiated = properties['negotiated']
    for prop in ('channel_max', 'frame_max', 'heartbeat'):
        assert conn.properties[prop] == negotiated[prop]

    data_to_send = conn.data_to_send()

    # Make sure ConnectionTuneOK is sent
    stream = io.BytesIO()
    method = amqpframe.methods.ConnectionTuneOK(**conn.properties)
    method.to_bytestream(stream)
    method_bytes = stream.getvalue()

    assert method_bytes in data_to_send

    # Make sure ConnectionOpen is also sent
    stream = io.BytesIO()
    method = amqpframe.methods.ConnectionOpen(virtual_host='/')
    method.to_bytestream(stream)
    method_bytes = stream.getvalue()

    assert method_bytes in data_to_send
Пример #10
0
def test_ChannelFlow_receiving(ready_channel):
    method = amqpframe.methods.ChannelFlow(active=False)
    frame = amqpframe.MethodFrame(ready_channel._channel_id, method)

    ready_channel.handle_frame(frame)

    method_bytes = io.BytesIO()
    method = amqpframe.methods.ChannelFlowOK(active=False)
    method.to_bytestream(method_bytes)

    assert method_bytes.getvalue() in ready_channel.data_to_send()
    assert not ready_channel.active
Пример #11
0
def test_correct_channel_opening(ready_connection):
    channel = ready_connection.get_channel()
    assert channel._channel_id == 1

    channel.send_ChannelOpen()

    payload = amqpframe.methods.ChannelOpenOK()
    frame = amqpframe.MethodFrame(channel._channel_id, payload)

    ready_connection.handle_frame(frame)
    assert channel.alive

    channel2 = ready_connection.get_channel()
    assert channel2._channel_id == 2
Пример #12
0
def test_correct_ChannelClose_sending(ready_channel):
    method, args = draw_method_example(amqpframe.methods.ChannelClose)

    fut = ready_channel.send_ChannelClose(**args)
    method_bytes = io.BytesIO()
    method.to_bytestream(method_bytes)
    assert method_bytes.getvalue() in ready_channel.data_to_send()

    method = amqpframe.methods.ChannelCloseOK()
    frame = amqpframe.MethodFrame(ready_channel._channel_id, method)
    ready_channel.handle_frame(frame)

    assert fut.done() and not fut.cancelled()
    assert not ready_channel.alive
Пример #13
0
 def _send_method(self, method):
     logger.debug('Sending %s [channel_id:%s]', method.__class__.__name__,
                  self._channel_id)
     if getattr(method, 'no_wait', False) or getattr(
             method, 'nowait', False):
         event = 'send_MethodFrame_nowait'
     elif getattr(method, 'content', False):
         event = 'send_MethodFrame_content'
     else:
         event = 'send_MethodFrame'
     self._framing_fsm.trigger(event)
     self._fsm.trigger('send_' + method.__class__.__name__)
     frame = amqpframe.MethodFrame(self._channel_id, method)
     frame.to_bytestream(self._buffer)
Пример #14
0
def ready_connection():
    conn = Connection(heartbeat=1)
    conn.initiate_connection()

    conn._heartbeater.update_received_time()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(0, payload)
    conn.handle_frame(frame)

    conn._heartbeater.update_received_time()

    channel_max = 2
    frame_max = 1000
    heartbeat = 1
    payload = amqpframe.methods.ConnectionTune(
        channel_max=channel_max,
        frame_max=frame_max,
        heartbeat=heartbeat,
    )
    frame = amqpframe.MethodFrame(0, payload)
    conn.handle_frame(frame)

    conn._heartbeater.update_received_time()

    payload = amqpframe.methods.ConnectionOpenOK()
    frame = amqpframe.MethodFrame(0, payload)
    conn.handle_frame(frame)

    conn.data_to_send()
    return conn
Пример #15
0
def test_correct_ConnectionClose_sending(ready_connection):
    fut = ready_connection.send_ConnectionClose(15, b'foo', 0, 0)
    method_bytes = io.BytesIO()
    method = amqpframe.methods.ConnectionClose(reply_code=15,
                                               reply_text=b'foo',
                                               class_id=0,
                                               method_id=0)
    method.to_bytestream(method_bytes)
    assert method_bytes.getvalue() in ready_connection.data_to_send()

    method = amqpframe.methods.ConnectionCloseOK()
    frame = amqpframe.MethodFrame(ready_connection._channel_id, method)
    ready_connection.handle_frame(frame)

    assert fut.done() and not fut.cancelled()
    assert not ready_connection.alive
Пример #16
0
def test_correct_ConnectionStart_handling():
    conn = Connection()
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    # Make sure all properties are handled
    server_properties = conn._handshake_properties['server']
    assert server_properties['properties']
    assert server_properties['locales'] == [b'en_US', b'ru_RU']
    assert server_properties['mechanisms'] == [b'PLAIN', b'AMQPLAIN']

    chosen_properties = conn._handshake_properties['chosen']
    assert chosen_properties['locale'] == b'en_US'
    assert chosen_properties['mechanism'] == b'PLAIN'

    # Make sure ConnectionStartOK is sent
    stream = io.BytesIO()
    PLAIN(b'guest', b'guest').to_bytestream(stream)
    response = stream.getvalue()

    stream = io.BytesIO()
    client_properties = conn._handshake_properties['client']['properties']
    mechanism = conn._handshake_properties['chosen']['mechanism']
    locale = conn._handshake_properties['chosen']['locale']

    method = amqpframe.methods.ConnectionStartOK(
        client_properties=client_properties,
        mechanism=mechanism,
        response=response,
        locale=locale)
    method.to_bytestream(stream)
    method_bytes = stream.getvalue()

    assert method_bytes in conn.data_to_send()
Пример #17
0
def test_correct_ChannelClose_handling(ready_channel):
    method, args = draw_method_example(amqpframe.methods.ChannelClose)
    frame = amqpframe.MethodFrame(ready_channel._channel_id, method)

    with pytest.raises(amqproto.exceptions.ChannelClosed) as excinfo:
        ready_channel.handle_frame(frame)

    method_bytes = io.BytesIO()
    method = amqpframe.methods.ChannelCloseOK()
    method.to_bytestream(method_bytes)
    assert method_bytes.getvalue() in ready_channel.data_to_send()

    assert not ready_channel.alive

    exc = excinfo.value
    assert exc.reply_code == args['reply_code']
    assert exc.reply_text == args['reply_text']
    assert exc.class_id == args['class_id']
    assert exc.method_id == args['method_id']
Пример #18
0
    def do_test(ready_channel):
        method = data.draw(methods(method_cls))
        ok_method = data.draw(methods(ok_method_cls))

        sender = getattr(ready_channel, 'send_' + method.__class__.__name__)
        args = {name: getattr(method, name)
                for name, _ in method.field_info
                if not name.startswith('reserved')}

        fut = sender(**args)

        method_bytes = io.BytesIO()
        method.to_bytestream(method_bytes)
        assert method_bytes.getvalue() in ready_channel.data_to_send()

        if getattr(method, 'no_wait', False):
            assert fut is None
        else:
            frame = amqpframe.MethodFrame(ready_channel._channel_id, ok_method)
            ready_channel.handle_frame(frame)
            assert fut.done() and not fut.cancelled()
Пример #19
0
def test_correct_ConnectionClose_handling(ready_connection):
    method = amqpframe.methods.ConnectionClose(reply_code=15,
                                               reply_text=b'foo',
                                               class_id=100,
                                               method_id=200)
    frame = amqpframe.MethodFrame(ready_connection._channel_id, method)

    with pytest.raises(amqproto.exceptions.ConnectionClosed) as excinfo:
        ready_connection.handle_frame(frame)

    method_bytes = io.BytesIO()
    method = amqpframe.methods.ConnectionCloseOK()
    method.to_bytestream(method_bytes)
    assert method_bytes.getvalue() in ready_connection.data_to_send()

    assert not ready_connection.alive

    exc = excinfo.value
    assert exc.reply_code == 15
    assert exc.reply_text == b'foo'
    assert exc.class_id == 100
    assert exc.method_id == 200