|
7 | 7 | import struct |
8 | 8 | import tempfile |
9 | 9 | import unittest |
| 10 | +from unittest import mock |
10 | 11 | from collections import defaultdict |
11 | 12 |
|
12 | 13 | from test.support import captured_stderr |
@@ -1547,6 +1548,34 @@ def export(self, filename): |
1547 | 1548 | self.assertEqual(collector.batches, expected) |
1548 | 1549 |
|
1549 | 1550 |
|
| 1551 | + def test_rle_long_run_splits_batches(self): |
| 1552 | + # Construct a single repeat record larger than the writer's buffer. |
| 1553 | + num_samples = 8193 |
| 1554 | + filename = self.create_binary_file([], compression="none") |
| 1555 | + data = bytearray(pathlib.Path(filename).read_bytes()) |
| 1556 | + record = (struct.pack("=QIB", 1, 0, 0) # STACK_REPEAT |
| 1557 | + + b"\x81\x40" # 8193 as a varint |
| 1558 | + + b"\x01\x00" * num_samples) # delta=1, status=0 |
| 1559 | + data[64:64] = record |
| 1560 | + struct.pack_into("=Q", data, 12, 0) # start timestamp |
| 1561 | + struct.pack_into("=Q", data, 28, num_samples) |
| 1562 | + struct.pack_into("=I", data, 36, 1) # thread count |
| 1563 | + for offset in (40, 48): # string and frame table offsets |
| 1564 | + old_offset = struct.unpack_from("=Q", data, offset)[0] |
| 1565 | + struct.pack_into("=Q", data, offset, old_offset + len(record)) |
| 1566 | + struct.pack_into("=Q", data, len(data) - 24, len(data)) |
| 1567 | + pathlib.Path(filename).write_bytes(data) |
| 1568 | + |
| 1569 | + collector = mock.Mock() |
| 1570 | + with BinaryReader(filename) as reader: |
| 1571 | + count = reader.replay_samples(collector) |
| 1572 | + batches = [call.args[1] for call in collector.collect.call_args_list] |
| 1573 | + self.assertEqual(count, num_samples) |
| 1574 | + self.assertEqual([len(batch) for batch in batches], [8192, 1]) |
| 1575 | + self.assertEqual([ts for batch in batches for ts in batch], |
| 1576 | + list(range(1, num_samples + 1))) |
| 1577 | + |
| 1578 | + |
1550 | 1579 | class TestBinaryStress(BinaryFormatTestBase): |
1551 | 1580 | """Randomized stress tests for binary format.""" |
1552 | 1581 |
|
|
0 commit comments