|
1 | 1 | # MIT License |
2 | 2 | # |
3 | | -# Copyright (c) 2018-2022 Tskit Developers |
| 3 | +# Copyright (c) 2018-2023 Tskit Developers |
4 | 4 | # |
5 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | 6 | # of this software and associated documentation files (the "Software"), to deal |
|
35 | 35 | import traceback |
36 | 36 |
|
37 | 37 | import pytest |
| 38 | +import tszip |
38 | 39 | from pytest import fixture |
39 | 40 |
|
40 | 41 | import tskit |
@@ -308,3 +309,59 @@ def verify_stream(self, ts_list, client_fd): |
308 | 309 | def test_single_then_multi(self, ts_fixture, replicate_ts_fixture, client_fd): |
309 | 310 | self.verify_stream([ts_fixture], client_fd) |
310 | 311 | self.verify_stream(replicate_ts_fixture, client_fd) |
| 312 | + |
| 313 | + |
| 314 | +def write_to_fifo(path, file_path): |
| 315 | + with open(path, "wb") as fifo: |
| 316 | + with open(file_path, "rb") as file: |
| 317 | + fifo.write(file.read()) |
| 318 | + |
| 319 | + |
| 320 | +def read_from_fifo(path, expected_exception, error_text, read_func): |
| 321 | + with open(path) as fifo: |
| 322 | + with pytest.raises(expected_exception, match=error_text): |
| 323 | + read_func(fifo) |
| 324 | + |
| 325 | + |
| 326 | +def write_and_read_from_fifo(fifo_path, file_path, expected_exception, error_text): |
| 327 | + os.mkfifo(fifo_path) |
| 328 | + for read_func in [tskit.load, tskit.TableCollection.load]: |
| 329 | + read_process = multiprocessing.Process( |
| 330 | + target=read_from_fifo, |
| 331 | + args=(fifo_path, expected_exception, error_text, read_func), |
| 332 | + ) |
| 333 | + read_process.start() |
| 334 | + write_process = multiprocessing.Process( |
| 335 | + target=write_to_fifo, args=(fifo_path, file_path) |
| 336 | + ) |
| 337 | + write_process.start() |
| 338 | + write_process.join(timeout=3) |
| 339 | + read_process.join(timeout=3) |
| 340 | + |
| 341 | + |
| 342 | +@pytest.mark.skipif(IS_WINDOWS, reason="No FIFOs on Windows") |
| 343 | +class TestBadStream: |
| 344 | + def test_bad_stream(self, tmp_path): |
| 345 | + fifo_path = tmp_path / "fifo" |
| 346 | + bad_file_path = tmp_path / "bad_file" |
| 347 | + bad_file_path.write_bytes(b"bad data") |
| 348 | + write_and_read_from_fifo( |
| 349 | + fifo_path, bad_file_path, tskit.FileFormatError, "not in kastore format" |
| 350 | + ) |
| 351 | + |
| 352 | + def test_legacy_stream(self, tmp_path): |
| 353 | + fifo_path = tmp_path / "fifo" |
| 354 | + legacy_file_path = os.path.join( |
| 355 | + os.path.dirname(__file__), "data", "hdf5-formats", "msprime-0.3.0_v2.0.hdf5" |
| 356 | + ) |
| 357 | + write_and_read_from_fifo( |
| 358 | + fifo_path, legacy_file_path, tskit.FileFormatError, "not in kastore format" |
| 359 | + ) |
| 360 | + |
| 361 | + def test_tszip_stream(self, tmp_path, ts_fixture): |
| 362 | + fifo_path = tmp_path / "fifo" |
| 363 | + zip_file_path = tmp_path / "tszip_file" |
| 364 | + tszip.compress(ts_fixture, zip_file_path) |
| 365 | + write_and_read_from_fifo( |
| 366 | + fifo_path, zip_file_path, tskit.FileFormatError, "not in kastore format" |
| 367 | + ) |
0 commit comments