Skip to content

Commit 7b482c4

Browse files
committed
tests: enable transports tests poking with request_size for Python 3.12+
I don't know if python 3.12 changed gzip compression or what but we get a flush call for each message we send so since our message is compressed to 10 bytes lower the limit to 9 bytes to pass the check deciding to flush the buffer.
1 parent ad52a76 commit 7b482c4

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

tests/transports/test_base.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,43 +164,46 @@ def test_api_request_time_dynamic(mock_send, caplog, elasticapm_client):
164164
assert mock_send.call_count == 0
165165

166166

167-
@pytest.mark.skipif(sys.version_info >= (3, 12), reason="Failing locally on 3.12.0rc1") # TODO py3.12
167+
def _cleanup_flush_mock_buffers(mock_flush):
168+
args, kwargs = mock_flush.call_args
169+
buffer = args[0]
170+
fileobj = buffer.fileobj
171+
buffer.close()
172+
compressed_data = fileobj.getbuffer()
173+
compressed_data.release()
174+
175+
168176
@mock.patch("elasticapm.transport.base.Transport._flush")
169177
def test_api_request_size_dynamic(mock_flush, caplog, elasticapm_client):
170-
elasticapm_client.config.update(version="1", api_request_size="100b")
178+
elasticapm_client.config.update(version="1", api_request_size="9b")
171179
transport = Transport(client=elasticapm_client, queue_chill_count=1)
172180
transport.start_thread()
173181
try:
174182
with caplog.at_level("DEBUG", "elasticapm.transport"):
175-
# we need to add lots of uncompressible data to fill up the gzip-internal buffer
176-
for i in range(12):
177-
transport.queue("error", "".join(random.choice(string.ascii_letters) for i in range(2000)))
183+
transport.queue("error", "".join(random.choice(string.ascii_letters) for i in range(2000)))
178184
transport._flushed.wait(timeout=0.1)
179185
assert mock_flush.call_count == 1
186+
_cleanup_flush_mock_buffers(mock_flush)
180187
elasticapm_client.config.update(version="1", api_request_size="1mb")
181188
with caplog.at_level("DEBUG", "elasticapm.transport"):
182-
# we need to add lots of uncompressible data to fill up the gzip-internal buffer
183-
for i in range(12):
184-
transport.queue("error", "".join(random.choice(string.ascii_letters) for i in range(2000)))
189+
transport.queue("error", "".join(random.choice(string.ascii_letters) for i in range(2000)))
185190
transport._flushed.wait(timeout=0.1)
186191
# Should be unchanged because our buffer limit is much higher.
187192
assert mock_flush.call_count == 1
188193
finally:
189194
transport.close()
190195

191196

192-
@pytest.mark.skipif(sys.version_info >= (3, 12), reason="Failing locally on 3.12.0rc1") # TODO py3.12
193197
@mock.patch("elasticapm.transport.base.Transport._flush")
194-
@pytest.mark.parametrize("elasticapm_client", [{"api_request_size": "100b"}], indirect=True)
198+
@pytest.mark.parametrize("elasticapm_client", [{"api_request_size": "9b"}], indirect=True)
195199
def test_flush_time_size(mock_flush, caplog, elasticapm_client):
196200
transport = Transport(client=elasticapm_client, queue_chill_count=1)
197201
transport.start_thread()
198202
try:
199203
with caplog.at_level("DEBUG", "elasticapm.transport"):
200-
# we need to add lots of uncompressible data to fill up the gzip-internal buffer
201-
for i in range(12):
202-
transport.queue("error", "".join(random.choice(string.ascii_letters) for i in range(2000)))
204+
transport.queue("error", "".join(random.choice(string.ascii_letters) for i in range(2000)))
203205
transport._flushed.wait(timeout=0.1)
206+
_cleanup_flush_mock_buffers(mock_flush)
204207
assert mock_flush.call_count == 1
205208
finally:
206209
transport.close()

0 commit comments

Comments
 (0)