def test_read_bundle(): pad = padded(len('#bundle')) data = struct.pack('>%isQ' % pad, b'#bundle', 1) tests = ( message_1, message_2, message_1, message_2, ) for source, msg, result in tests: msg = struct.pack('>%iB' % len(msg), *msg) assert read_message(msg)[::2] == result data += struct.pack('>i', len(msg)) + msg timetag, messages = read_bundle(data) for test, r in zip(tests, messages): assert (r[0], r[2]) == test[2]
def test_format_bundle(): bundle, stats = format_bundle((message_1[0], message_2[0]), timetag=None) assert struct.pack('>%iB' % len(message_1[1]), *message_1[1]) in bundle assert struct.pack('>%iB' % len(message_2[1]), *message_2[1]) in bundle timetag, messages = read_bundle(bundle) assert timetag == approx(time()) assert len(messages) == 2 assert messages[0][::2] == message_1[2] assert messages[1][::2] == message_2[2] assert stats.calls == 2 assert stats.bytes == 72 assert stats.params == 6 assert stats.types['f'] == 3 assert stats.types['i'] == 2 assert stats.types['s'] == 1
def test_read_broken_bundle(): s = b'not a bundle' data = struct.pack('>%is' % len(s), s) with raises(ValueError): read_bundle(data)