Skip to content

Commit 8cf1daf

Browse files
authored
Add test for file-like read() method sending fewer bytes than requested (#644)
1 parent 2c137e7 commit 8cf1daf

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/test_ops.py

+43
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,49 @@ def seek(self, offset: int, whence: int) -> bytes:
10701070
"approximate",
10711071
)
10721072

1073+
@pytest.mark.parametrize("how_much_to_read", ("half", "minus_10"))
1074+
def test_file_like_read_less_than_requested(self, how_much_to_read):
1075+
# Check that reading fewer bytes than requested still works. FFmpeg will
1076+
# figure out how to get the necessary bytes.
1077+
class FileLike:
1078+
def __init__(self, file):
1079+
self._file = file
1080+
1081+
def read(self, size: int) -> bytes:
1082+
if how_much_to_read == "half":
1083+
size = size // 2
1084+
elif how_much_to_read == "minus_10":
1085+
size = size - 10
1086+
else:
1087+
raise ValueError("Check parametrization of this test!")
1088+
1089+
return self._file.read(size)
1090+
1091+
def seek(self, offset: int, whence: int) -> bytes:
1092+
return self._file.seek(offset, whence)
1093+
1094+
decoder_file_like = create_from_file_like(
1095+
FileLike(open(NASA_VIDEO.path, mode="rb", buffering=0))
1096+
)
1097+
add_video_stream(decoder_file_like)
1098+
1099+
decoder_reference = create_from_file(str(NASA_VIDEO.path))
1100+
add_video_stream(decoder_reference)
1101+
1102+
torch.manual_seed(0)
1103+
indices = torch.randint(
1104+
0, len(NASA_VIDEO.frames[NASA_VIDEO.default_stream_index]), size=(50,)
1105+
).tolist()
1106+
1107+
frames_file_like, *_ = get_frames_at_indices(
1108+
decoder_file_like, frame_indices=indices
1109+
)
1110+
frames_references, *_ = get_frames_at_indices(
1111+
decoder_reference, frame_indices=indices
1112+
)
1113+
1114+
torch.testing.assert_close(frames_file_like, frames_references)
1115+
10731116

10741117
class TestAudioEncoderOps:
10751118

0 commit comments

Comments
 (0)