test_block_2.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import pytest
  2. import sys
  3. import lz4.block
  4. import psutil
  5. import os
  6. # This test requires allocating a big lump of memory. In order to
  7. # avoid a massive memory allocation during byte compilation, we have
  8. # to declare a variable for the size of the buffer we're going to
  9. # create outside the scope of the function below. See:
  10. # https://bugs.python.org/issue21074
  11. _4GB = 0x100000000 # 4GB
  12. # This test will be killed on Travis due to the 3GB memory limit
  13. # there. Unfortunately psutil reports the host memory, not the memory
  14. # available to the container, and so can't be used to detect available
  15. # memory, so instead, as an ugly hack for detecting we're on Travis we
  16. # check for the TRAVIS environment variable being set. This is quite
  17. # fragile.
  18. @pytest.mark.skipif(
  19. os.environ.get('TRAVIS') is not None,
  20. reason='Skipping test on Travis due to insufficient memory'
  21. )
  22. @pytest.mark.skipif(
  23. sys.maxsize < 0xffffffff,
  24. reason='Py_ssize_t too small for this test'
  25. )
  26. @pytest.mark.skipif(
  27. psutil.virtual_memory().available < _4GB,
  28. reason='Insufficient system memory for this test'
  29. )
  30. def test_huge():
  31. try:
  32. huge = b'\0' * _4GB
  33. except MemoryError:
  34. pytest.skip('Insufficient system memory for this test')
  35. with pytest.raises(
  36. OverflowError, match='Input too large for LZ4 API'
  37. ):
  38. lz4.block.compress(huge)
  39. with pytest.raises(
  40. OverflowError, match='Dictionary too large for LZ4 API'
  41. ):
  42. lz4.block.compress(b'', dict=huge)
  43. with pytest.raises(
  44. OverflowError, match='Input too large for LZ4 API'
  45. ):
  46. lz4.block.decompress(huge)
  47. with pytest.raises(
  48. OverflowError, match='Dictionary too large for LZ4 API'
  49. ):
  50. lz4.block.decompress(b'', dict=huge)
  51. def test_dummy():
  52. pass