test_stream_2.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import pytest
  2. import sys
  3. import lz4.stream
  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 = 0xffffffff # actually 4GB - 1B, the maximum size on 4 bytes.
  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. if os.environ.get('TRAVIS') is not None or sys.maxsize < _4GB or \
  19. psutil.virtual_memory().available < _4GB:
  20. huge = None
  21. else:
  22. try:
  23. huge = b'\0' * _4GB
  24. except (MemoryError, OverflowError):
  25. huge = None
  26. @pytest.mark.skipif(
  27. os.environ.get('TRAVIS') is not None,
  28. reason='Skipping test on Travis due to insufficient memory'
  29. )
  30. @pytest.mark.skipif(
  31. sys.maxsize < _4GB,
  32. reason='Py_ssize_t too small for this test'
  33. )
  34. @pytest.mark.skipif(
  35. psutil.virtual_memory().available < _4GB or huge is None,
  36. reason='Insufficient system memory for this test'
  37. )
  38. def test_huge_1():
  39. data = b''
  40. kwargs = {
  41. 'strategy': "double_buffer",
  42. 'buffer_size': lz4.stream.LZ4_MAX_INPUT_SIZE,
  43. 'store_comp_size': 4,
  44. 'dictionary': huge,
  45. }
  46. if psutil.virtual_memory().available < 3 * kwargs['buffer_size']:
  47. # The internal LZ4 context will request at least 3 times buffer_size
  48. # as memory (2 buffer_size for the double-buffer, and 1.x buffer_size
  49. # for the output buffer)
  50. pytest.skip('Insufficient system memory for this test')
  51. # Triggering overflow error
  52. message = r'^Dictionary too large for LZ4 API$'
  53. with pytest.raises(OverflowError, match=message):
  54. with lz4.stream.LZ4StreamCompressor(**kwargs) as proc:
  55. proc.compress(data)
  56. with pytest.raises(OverflowError, match=message):
  57. with lz4.stream.LZ4StreamDecompressor(**kwargs) as proc:
  58. proc.decompress(data)
  59. @pytest.mark.skipif(
  60. os.environ.get('TRAVIS') is not None,
  61. reason='Skipping test on Travis due to insufficient memory'
  62. )
  63. @pytest.mark.skipif(
  64. sys.maxsize < 0xffffffff,
  65. reason='Py_ssize_t too small for this test'
  66. )
  67. @pytest.mark.skipif(
  68. psutil.virtual_memory().available < _4GB or huge is None,
  69. reason='Insufficient system memory for this test'
  70. )
  71. def test_huge_2():
  72. data = huge
  73. kwargs = {
  74. 'strategy': "double_buffer",
  75. 'buffer_size': lz4.stream.LZ4_MAX_INPUT_SIZE,
  76. 'store_comp_size': 4,
  77. 'dictionary': b'',
  78. }
  79. if psutil.virtual_memory().available < 3 * kwargs['buffer_size']:
  80. # The internal LZ4 context will request at least 3 times buffer_size
  81. # as memory (2 buffer_size for the double-buffer, and 1.x buffer_size
  82. # for the output buffer)
  83. pytest.skip('Insufficient system memory for this test')
  84. # Raising overflow error
  85. message = r'^Input too large for LZ4 API$'
  86. with pytest.raises(OverflowError, match=message):
  87. with lz4.stream.LZ4StreamCompressor(**kwargs) as proc:
  88. proc.compress(data)
  89. # On decompression, too large input will raise LZ4StreamError
  90. with pytest.raises(lz4.stream.LZ4StreamError):
  91. with lz4.stream.LZ4StreamDecompressor(**kwargs) as proc:
  92. proc.decompress(data)
  93. @pytest.mark.skipif(
  94. os.environ.get('TRAVIS') is not None,
  95. reason='Skipping test on Travis due to insufficient memory'
  96. )
  97. @pytest.mark.skipif(
  98. sys.maxsize < 0xffffffff,
  99. reason='Py_ssize_t too small for this test'
  100. )
  101. @pytest.mark.skipif(
  102. psutil.virtual_memory().available < _4GB or huge is None,
  103. reason='Insufficient system memory for this test'
  104. )
  105. def test_huge_3():
  106. data = huge
  107. kwargs = {
  108. 'strategy': "double_buffer",
  109. 'buffer_size': lz4.stream.LZ4_MAX_INPUT_SIZE,
  110. 'store_comp_size': 4,
  111. 'dictionary': huge,
  112. }
  113. if psutil.virtual_memory().available < 3 * kwargs['buffer_size']:
  114. # The internal LZ4 context will request at least 3 times buffer_size
  115. # as memory (2 buffer_size for the double-buffer, and 1.x buffer_size
  116. # for the output buffer)
  117. pytest.skip('Insufficient system memory for this test')
  118. # Raising overflow error (during initialization because of the dictionary parameter)
  119. message = r'^Dictionary too large for LZ4 API$'
  120. with pytest.raises(OverflowError, match=message):
  121. with lz4.stream.LZ4StreamCompressor(**kwargs) as proc:
  122. proc.compress(data)
  123. with pytest.raises(OverflowError, match=message):
  124. with lz4.stream.LZ4StreamDecompressor(**kwargs) as proc:
  125. proc.decompress(data)
  126. def test_dummy():
  127. pass