Example #1
0
def test_read_size():
    conn = _get_test_conn()
    body_size = 6
    body_size_packed = struct_pack('>l', body_size)
    conn._read_size(body_size_packed)
    conn.stream.read_bytes.assert_called_once_with(body_size, conn._read_body)

    # now test that we get the right behavior when we get malformed data
    # for this, we'll want to stick on mock on conn.close
    conn.stream.read_bytes.reset_mock()
    with patch.object(conn, 'close', autospec=True) as mock_close:
        conn._read_size('asdfasdf')
        mock_close.assert_called_once_with()
        assert not conn.stream.read_bytes.called
Example #2
0
def pytest_generate_tests(metafunc):
    identify_dict_ascii = {'a': 1, 'b': 2}
    identify_dict_unicode = {'c': u'w\xc3\xa5\xe2\x80\xa0'}
    identify_body_ascii = to_bytes(json.dumps(identify_dict_ascii))
    identify_body_unicode = to_bytes(json.dumps(identify_dict_unicode))

    msgs = [b'asdf', b'ghjk', b'abcd']
    mpub_body = struct_pack('>l', len(msgs)) + b''.join(struct_pack('>l', len(m)) + m for m in msgs)
    if metafunc.function == test_command:
        for cmd_method, kwargs, result in [
                (protocol.identify,
                    {'data': identify_dict_ascii},
                    b'IDENTIFY\n' + struct_pack('>l', len(identify_body_ascii)) +
                    to_bytes(identify_body_ascii)),
                (protocol.identify,
                    {'data': identify_dict_unicode},
                    b'IDENTIFY\n' + struct_pack('>l', len(identify_body_unicode)) +
                    to_bytes(identify_body_unicode)),
                (protocol.subscribe,
                    {'topic': 'test_topic', 'channel': 'test_channel'},
                    b'SUB test_topic test_channel\n'),
                (protocol.finish,
                    {'id': 'test'},
                    b'FIN test\n'),
                (protocol.finish,
                    {'id': u'\u2020est \xfcn\xee\xe7\xf8\u2202\xe9'},
                    b'FIN \xe2\x80\xa0est \xc3\xbcn\xc3\xae\xc3\xa7\xc3\xb8\xe2\x88\x82\xc3\xa9\n'),
                (protocol.requeue,
                    {'id': 'test'},
                    b'REQ test 0\n'),
                (protocol.requeue,
                    {'id': 'test', 'time_ms': 60},
                    b'REQ test 60\n'),
                (protocol.touch,
                    {'id': 'test'},
                    b'TOUCH test\n'),
                (protocol.ready,
                    {'count': 100},
                    b'RDY 100\n'),
                (protocol.nop,
                    {},
                    b'NOP\n'),
                (protocol.pub,
                    {'topic': 'test', 'data': msgs[0]},
                    b'PUB test\n' + struct_pack('>l', len(msgs[0])) + to_bytes(msgs[0])),
                (protocol.mpub,
                    {'topic': 'test', 'data': msgs},
                    b'MPUB test\n' + struct_pack('>l', len(mpub_body)) + to_bytes(mpub_body))
                ]:
            metafunc.addcall(funcargs=dict(cmd_method=cmd_method, kwargs=kwargs, result=result))
Example #3
0
def mock_response_write_message(c, timestamp, attempts, id, body):
    timestamp_packed = struct_pack('>q', timestamp)
    attempts_packed = struct_pack('>h', attempts)
    id = b"%016d" % id
    mock_response_write(
        c, protocol.FRAME_TYPE_MESSAGE, timestamp_packed + attempts_packed + id + body)
Example #4
0
def mock_response_write(c, frame_type, data):
    body_size = 4 + len(data)
    body_size_packed = struct_pack('>l', body_size)
    frame_type_packed = struct_pack('>l', frame_type)
    mock_write(c, body_size_packed + frame_type_packed + data)