test_dataset_swmr.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import numpy as np
  2. import pytest
  3. import h5py
  4. from .common import TestCase
  5. class TestDatasetSwmrRead(TestCase):
  6. """ Testing SWMR functions when reading a dataset.
  7. Skip this test if the HDF5 library does not have the SWMR features.
  8. """
  9. def setUp(self):
  10. TestCase.setUp(self)
  11. self.data = np.arange(13).astype('f')
  12. self.dset = self.f.create_dataset('data', chunks=(13,), maxshape=(None,), data=self.data)
  13. fname = self.f.filename
  14. self.f.close()
  15. self.f = h5py.File(fname, 'r', swmr=True)
  16. self.dset = self.f['data']
  17. def test_initial_swmr_mode_on(self):
  18. """ Verify that the file is initially in SWMR mode"""
  19. self.assertTrue(self.f.swmr_mode)
  20. def test_read_data(self):
  21. self.assertArrayEqual(self.dset, self.data)
  22. def test_refresh(self):
  23. self.dset.refresh()
  24. def test_force_swmr_mode_on_raises(self):
  25. """ Verify when reading a file cannot be forcibly switched to swmr mode.
  26. When reading with SWMR the file must be opened with swmr=True."""
  27. with self.assertRaises(Exception):
  28. self.f.swmr_mode = True
  29. self.assertTrue(self.f.swmr_mode)
  30. def test_force_swmr_mode_off_raises(self):
  31. """ Switching SWMR write mode off is only possible by closing the file.
  32. Attempts to forcibly switch off the SWMR mode should raise a ValueError.
  33. """
  34. with self.assertRaises(ValueError):
  35. self.f.swmr_mode = False
  36. self.assertTrue(self.f.swmr_mode)
  37. @pytest.mark.thread_unsafe(reason="Can't enable global SWMR flag twice")
  38. class TestDatasetSwmrWrite(TestCase):
  39. """ Testing SWMR functions when reading a dataset.
  40. Skip this test if the HDF5 library does not have the SWMR features.
  41. """
  42. def setUp(self):
  43. """ First setup a file with a small chunked and empty dataset.
  44. No data written yet.
  45. """
  46. # Note that when creating the file, the swmr=True is not required for
  47. # write, but libver='latest' is required.
  48. self.f = h5py.File(self.mktemp(), 'w', libver='latest')
  49. self.data = np.arange(4).astype('f')
  50. self.dset = self.f.create_dataset('data', shape=(0,), dtype=self.data.dtype, chunks=(2,), maxshape=(None,))
  51. def test_initial_swmr_mode_off(self):
  52. """ Verify that the file is not initially in SWMR mode"""
  53. self.assertFalse(self.f.swmr_mode)
  54. def test_switch_swmr_mode_on(self):
  55. """ Switch to SWMR mode and verify """
  56. self.f.swmr_mode = True
  57. self.assertTrue(self.f.swmr_mode)
  58. def test_switch_swmr_mode_off_raises(self):
  59. """ Switching SWMR write mode off is only possible by closing the file.
  60. Attempts to forcibly switch off the SWMR mode should raise a ValueError.
  61. """
  62. self.f.swmr_mode = True
  63. self.assertTrue(self.f.swmr_mode)
  64. with self.assertRaises(ValueError):
  65. self.f.swmr_mode = False
  66. self.assertTrue(self.f.swmr_mode)
  67. def test_extend_dset(self):
  68. """ Extend and flush a SWMR dataset
  69. """
  70. self.f.swmr_mode = True
  71. self.assertTrue(self.f.swmr_mode)
  72. self.dset.resize( self.data.shape )
  73. self.dset[:] = self.data
  74. self.dset.flush()
  75. # Refresh and read back data for assertion
  76. self.dset.refresh()
  77. self.assertArrayEqual(self.dset, self.data)
  78. def test_extend_dset_multiple(self):
  79. self.f.swmr_mode = True
  80. self.assertTrue(self.f.swmr_mode)
  81. self.dset.resize( (4,) )
  82. self.dset[0:] = self.data
  83. self.dset.flush()
  84. # Refresh and read back 1st data block for assertion
  85. self.dset.refresh()
  86. self.assertArrayEqual(self.dset, self.data)
  87. self.dset.resize( (8,) )
  88. self.dset[4:] = self.data
  89. self.dset.flush()
  90. # Refresh and read back 1st data block for assertion
  91. self.dset.refresh()
  92. self.assertArrayEqual(self.dset[0:4], self.data)
  93. self.assertArrayEqual(self.dset[4:8], self.data)