2016-06-17 13:50:26 -07:00
|
|
|
import os
|
2019-09-26 22:18:28 -07:00
|
|
|
import platform
|
2016-06-17 13:50:26 -07:00
|
|
|
import tempfile
|
2019-09-26 22:18:28 -07:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
import capnp
|
2016-06-17 13:50:26 -07:00
|
|
|
|
|
|
|
this_dir = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def test_capnp():
|
2021-10-01 11:00:22 -07:00
|
|
|
return capnp.load(os.path.join(this_dir, "test_large_read.capnp"))
|
2016-06-17 13:50:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_large_read(test_capnp):
|
|
|
|
f = tempfile.TemporaryFile()
|
|
|
|
|
|
|
|
array = test_capnp.MultiArray.new_message()
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
row = array.init("rows", 1)[0]
|
|
|
|
values = row.init("values", 10000)
|
2016-06-17 13:50:26 -07:00
|
|
|
for i in range(len(values)):
|
|
|
|
values[i] = i
|
|
|
|
|
|
|
|
array.write_packed(f)
|
|
|
|
f.seek(0)
|
|
|
|
|
|
|
|
array = test_capnp.MultiArray.read_packed(f)
|
|
|
|
del f
|
|
|
|
assert array.rows[0].values[9000] == 9000
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2016-06-17 13:50:26 -07:00
|
|
|
def test_large_read_multiple(test_capnp):
|
|
|
|
f = tempfile.TemporaryFile()
|
|
|
|
msg1 = test_capnp.Msg.new_message()
|
|
|
|
msg1.data = [0x41] * 8192
|
|
|
|
msg1.write(f)
|
|
|
|
msg2 = test_capnp.Msg.new_message()
|
|
|
|
msg2.write(f)
|
|
|
|
f.seek(0)
|
|
|
|
|
|
|
|
for m in test_capnp.Msg.read_multiple(f):
|
|
|
|
pass
|
2019-01-28 17:04:34 +00:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2019-01-28 18:09:17 +00:00
|
|
|
def get_two_adjacent_messages(test_capnp):
|
2019-01-28 17:04:34 +00:00
|
|
|
msg1 = test_capnp.Msg.new_message()
|
|
|
|
msg1.data = [0x41] * 8192
|
|
|
|
m1 = msg1.to_bytes()
|
|
|
|
msg2 = test_capnp.Msg.new_message()
|
|
|
|
m2 = msg2.to_bytes()
|
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
return m1 + m2
|
2019-01-28 18:09:17 +00:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2019-01-28 18:09:17 +00:00
|
|
|
def test_large_read_multiple_bytes(test_capnp):
|
|
|
|
data = get_two_adjacent_messages(test_capnp)
|
2019-01-28 17:04:34 +00:00
|
|
|
for m in test_capnp.Msg.read_multiple_bytes(data):
|
|
|
|
pass
|
|
|
|
|
2019-01-31 01:02:11 -08:00
|
|
|
with pytest.raises(capnp.KjException):
|
|
|
|
data = get_two_adjacent_messages(test_capnp)[:-1]
|
|
|
|
for m in test_capnp.Msg.read_multiple_bytes(data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with pytest.raises(capnp.KjException):
|
2021-10-01 11:00:22 -07:00
|
|
|
data = get_two_adjacent_messages(test_capnp) + b" "
|
2019-01-31 01:02:11 -08:00
|
|
|
for m in test_capnp.Msg.read_multiple_bytes(data):
|
|
|
|
pass
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
platform.python_implementation() == "PyPy",
|
|
|
|
reason="PyPy memoryview support is limited",
|
|
|
|
)
|
2019-01-28 18:09:17 +00:00
|
|
|
def test_large_read_mutltiple_bytes_memoryview(test_capnp):
|
|
|
|
data = get_two_adjacent_messages(test_capnp)
|
2019-01-28 17:04:34 +00:00
|
|
|
for m in test_capnp.Msg.read_multiple_bytes(memoryview(data)):
|
|
|
|
pass
|
2019-01-28 18:09:17 +00:00
|
|
|
|
2019-01-31 01:02:11 -08:00
|
|
|
with pytest.raises(capnp.KjException):
|
|
|
|
data = get_two_adjacent_messages(test_capnp)[:-1]
|
|
|
|
for m in test_capnp.Msg.read_multiple_bytes(memoryview(data)):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with pytest.raises(capnp.KjException):
|
2021-10-01 11:00:22 -07:00
|
|
|
data = get_two_adjacent_messages(test_capnp) + b" "
|
2019-01-31 01:02:11 -08:00
|
|
|
for m in test_capnp.Msg.read_multiple_bytes(memoryview(data)):
|
|
|
|
pass
|