Debian Codegraph @

tests/stream/test_stream_2.py

raw symbols SCIP
1 import pytest
2 import sys
3 import lz4.stream
4 import psutil
5 import os
6
7
8 # This test requires allocating a big lump of memory. In order to
9 # avoid a massive memory allocation during byte compilation, we have
10 # to declare a variable for the size of the buffer we're going to
11 # create outside the scope of the function below. See:
12 # https://bugs.python.org/issue21074
13
14 _4GB = 0xffffffff # actually 4GB - 1B, the maximum size on 4 bytes.
15
16 # This test will be killed on Travis due to the 3GB memory limit
17 # there. Unfortunately psutil reports the host memory, not the memory
18 # available to the container, and so can't be used to detect available
19 # memory, so instead, as an ugly hack for detecting we're on Travis we
20 # check for the TRAVIS environment variable being set. This is quite
21 # fragile.
22
23 if os.environ.get('TRAVIS') is not None or sys.maxsize < _4GB or \
24 psutil.virtual_memory().available < _4GB:
25 huge = None
26 else:
27 try:
28 huge = b'\0' * _4GB
29 except (MemoryError, OverflowError):
30 huge = None
31
32
33 @pytest.mark.skipif(
34 os.environ.get('TRAVIS') is not None,
35 reason='Skipping test on Travis due to insufficient memory'
36 )
37 @pytest.mark.skipif(
38 sys.maxsize < _4GB,
39 reason='Py_ssize_t too small for this test'
40 )
41 @pytest.mark.skipif(
42 psutil.virtual_memory().available < _4GB or huge is None,
43 reason='Insufficient system memory for this test'
44 )
45 def test_huge_1():
46 data = b''
47 kwargs = {
48 'strategy': "double_buffer",
49 'buffer_size': lz4.stream.LZ4_MAX_INPUT_SIZE,
50 'store_comp_size': 4,
51 'dictionary': huge,
52 }
53
54 if psutil.virtual_memory().available < 3 * kwargs['buffer_size']:
55 # The internal LZ4 context will request at least 3 times buffer_size
56 # as memory (2 buffer_size for the double-buffer, and 1.x buffer_size
57 # for the output buffer)
58 pytest.skip('Insufficient system memory for this test')
59
60 # Triggering overflow error
61 message = r'^Dictionary too large for LZ4 API$'
62
63 with pytest.raises(OverflowError, match=message):
64 with lz4.stream.LZ4StreamCompressor(**kwargs) as proc:
65 proc.compress(data)
66
67 with pytest.raises(OverflowError, match=message):
68 with lz4.stream.LZ4StreamDecompressor(**kwargs) as proc:
69 proc.decompress(data)
70
71
72 @pytest.mark.skipif(
73 os.environ.get('TRAVIS') is not None,
74 reason='Skipping test on Travis due to insufficient memory'
75 )
76 @pytest.mark.skipif(
77 sys.maxsize < 0xffffffff,
78 reason='Py_ssize_t too small for this test'
79 )
80 @pytest.mark.skipif(
81 psutil.virtual_memory().available < _4GB or huge is None,
82 reason='Insufficient system memory for this test'
83 )
84 def test_huge_2():
85 data = huge
86 kwargs = {
87 'strategy': "double_buffer",
88 'buffer_size': lz4.stream.LZ4_MAX_INPUT_SIZE,
89 'store_comp_size': 4,
90 'dictionary': b'',
91 }
92
93 if psutil.virtual_memory().available < 3 * kwargs['buffer_size']:
94 # The internal LZ4 context will request at least 3 times buffer_size
95 # as memory (2 buffer_size for the double-buffer, and 1.x buffer_size
96 # for the output buffer)
97 pytest.skip('Insufficient system memory for this test')
98
99 # Raising overflow error
100 message = r'^Input too large for LZ4 API$'
101
102 with pytest.raises(OverflowError, match=message):
103 with lz4.stream.LZ4StreamCompressor(**kwargs) as proc:
104 proc.compress(data)
105
106 # On decompression, too large input will raise LZ4StreamError
107 with pytest.raises(lz4.stream.LZ4StreamError):
108 with lz4.stream.LZ4StreamDecompressor(**kwargs) as proc:
109 proc.decompress(data)
110
111
112 @pytest.mark.skipif(
113 os.environ.get('TRAVIS') is not None,
114 reason='Skipping test on Travis due to insufficient memory'
115 )
116 @pytest.mark.skipif(
117 sys.maxsize < 0xffffffff,
118 reason='Py_ssize_t too small for this test'
119 )
120 @pytest.mark.skipif(
121 psutil.virtual_memory().available < _4GB or huge is None,
122 reason='Insufficient system memory for this test'
123 )
124 def test_huge_3():
125 data = huge
126 kwargs = {
127 'strategy': "double_buffer",
128 'buffer_size': lz4.stream.LZ4_MAX_INPUT_SIZE,
129 'store_comp_size': 4,
130 'dictionary': huge,
131 }
132
133 if psutil.virtual_memory().available < 3 * kwargs['buffer_size']:
134 # The internal LZ4 context will request at least 3 times buffer_size
135 # as memory (2 buffer_size for the double-buffer, and 1.x buffer_size
136 # for the output buffer)
137 pytest.skip('Insufficient system memory for this test')
138
139 # Raising overflow error (during initialization because of the dictionary parameter)
140 message = r'^Dictionary too large for LZ4 API$'
141
142 with pytest.raises(OverflowError, match=message):
143 with lz4.stream.LZ4StreamCompressor(**kwargs) as proc:
144 proc.compress(data)
145
146 with pytest.raises(OverflowError, match=message):
147 with lz4.stream.LZ4StreamDecompressor(**kwargs) as proc:
148 proc.decompress(data)
149
150
151 def test_dummy():
152 pass