|
| 1 | +"""Round-trip test for StreamInlet.pull_chunk. |
| 2 | +
|
| 3 | +Pushes a known chunk and pulls it back, asserting the extracted data is |
| 4 | +identical in value, shape, and type. Covers both paths the bulk-slice |
| 5 | +extraction must preserve: a multi-channel numeric chunk and a |
| 6 | +variable-length string ("Markers"-style) chunk. |
| 7 | +""" |
| 8 | + |
| 9 | +import time |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +import pylsl |
| 14 | + |
| 15 | +# (channel_format, samples) — distinct values per channel/sample, including |
| 16 | +# empty and multi-byte strings to exercise variable-length decoding. |
| 17 | +CASES = { |
| 18 | + "double64": ( |
| 19 | + pylsl.cf_double64, |
| 20 | + [[1.0, 2.5, -3.25], [4.0, 5.5, 6.75], [7.0, 8.5, 9.25]], |
| 21 | + ), |
| 22 | + "string": ( |
| 23 | + pylsl.cf_string, |
| 24 | + [["a", "bb", ""], ["ccc", "dddd", "e"], ["", "f", "ééé"]], |
| 25 | + ), |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize("channel_format,samples", CASES.values(), ids=CASES.keys()) |
| 30 | +def test_pull_chunk_roundtrip(channel_format: int, samples: list): |
| 31 | + n_samples = len(samples) |
| 32 | + n_channels = len(samples[0]) |
| 33 | + |
| 34 | + info = pylsl.StreamInfo( |
| 35 | + name="test_pull_chunk", |
| 36 | + type="test", |
| 37 | + channel_count=n_channels, |
| 38 | + nominal_srate=0, |
| 39 | + channel_format=channel_format, |
| 40 | + source_id="test_pull_chunk_id", |
| 41 | + ) |
| 42 | + outlet = pylsl.StreamOutlet(info) |
| 43 | + |
| 44 | + streams = pylsl.resolve_byprop("source_id", "test_pull_chunk_id", timeout=2) |
| 45 | + assert streams, "outlet was not discovered" |
| 46 | + inlet = pylsl.StreamInlet(streams[0]) |
| 47 | + |
| 48 | + # Subscribe before pushing so the only chunk isn't sent before the inlet's |
| 49 | + # data connection is established. |
| 50 | + inlet.open_stream(timeout=2) |
| 51 | + time.sleep(0.5) |
| 52 | + outlet.push_chunk(samples) |
| 53 | + |
| 54 | + # Data may arrive in more than one chunk; collect until complete. |
| 55 | + got_samples: list = [] |
| 56 | + got_ts: list = [] |
| 57 | + deadline = time.time() + 5 |
| 58 | + while len(got_samples) < n_samples and time.time() < deadline: |
| 59 | + chunk, stamps = inlet.pull_chunk(timeout=1.0) |
| 60 | + if chunk: |
| 61 | + assert isinstance(stamps, list) |
| 62 | + assert all(isinstance(row, list) for row in chunk) |
| 63 | + got_samples.extend(chunk) |
| 64 | + got_ts.extend(stamps) |
| 65 | + |
| 66 | + assert got_samples == samples |
| 67 | + assert len(got_ts) == n_samples |
0 commit comments