From 1f736eafe35103cfa836967cc6616b3451468009 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Tue, 13 Sep 2022 11:56:44 -0600 Subject: [PATCH 1/3] Make test_flushed_arg_with_wait less flaky --- tests/transports/test_base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/transports/test_base.py b/tests/transports/test_base.py index e4c0e88fc..460a43e8b 100644 --- a/tests/transports/test_base.py +++ b/tests/transports/test_base.py @@ -266,11 +266,12 @@ def test_flushed_arg(sending_elasticapm_client): assert sending_elasticapm_client.httpserver.requests[0].args["flushed"] == "true" +@pytest.mark.flaky(reruns=3) # Trying to test automatic flushes is inherently flaky @pytest.mark.parametrize("sending_elasticapm_client", [{"api_request_time": "100ms"}], indirect=True) def test_flushed_arg_with_wait(sending_elasticapm_client): sending_elasticapm_client.begin_transaction("test_type") sending_elasticapm_client.end_transaction("test") - time.sleep(0.2) + time.sleep(0.3) sending_elasticapm_client._transport.flush() assert sending_elasticapm_client.httpserver.requests[1].args["flushed"] == "true" From 075bb76fdd0585e0e1dff8cfd14f9e7db6c804c7 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Tue, 13 Sep 2022 12:26:26 -0600 Subject: [PATCH 2/3] Fix flaky test_send_remote_failover_sync --- tests/client/client_tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/client/client_tests.py b/tests/client/client_tests.py index e7d64bef2..862ef207b 100644 --- a/tests/client/client_tests.py +++ b/tests/client/client_tests.py @@ -254,6 +254,7 @@ def test_send_remote_failover_sync(should_try, sending_elasticapm_client, caplog sending_elasticapm_client.httpserver.code = 202 sending_elasticapm_client.capture_message("bar", handled=False) sending_elasticapm_client.close() + time.sleep(0.2) assert not sending_elasticapm_client._transport.state.did_fail() From 24a410186af0bf488748077e27d5c4c4136f016c Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Wed, 14 Sep 2022 11:56:09 -0600 Subject: [PATCH 3/3] Fix the actual race condition and a small bug with forced flushes --- elasticapm/transport/base.py | 6 +++++- tests/client/client_tests.py | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/elasticapm/transport/base.py b/elasticapm/transport/base.py index f0485c4b2..3151ded05 100644 --- a/elasticapm/transport/base.py +++ b/elasticapm/transport/base.py @@ -180,7 +180,11 @@ def _process_queue(self): # No data on buffer, but due to manual flush we should send # an empty payload with flushed=true query param, but only # to a local APM server (or lambda extension) - self.send("", forced_flush=True) + try: + self.send("", forced_flush=True) + self.handle_transport_success() + except Exception as e: + self.handle_transport_fail(e) self._last_flush = timeit.default_timer() buffer = self._init_buffer() buffer_written = False diff --git a/tests/client/client_tests.py b/tests/client/client_tests.py index 862ef207b..d4619a428 100644 --- a/tests/client/client_tests.py +++ b/tests/client/client_tests.py @@ -246,7 +246,7 @@ def test_send_remote_failover_sync(should_try, sending_elasticapm_client, caplog # test error with caplog.at_level("ERROR", "elasticapm.transport"): sending_elasticapm_client.capture_message("foo", handled=False) - sending_elasticapm_client._transport.flush() + sending_elasticapm_client._transport._flushed.wait(timeout=1) assert sending_elasticapm_client._transport.state.did_fail() assert_any_record_contains(caplog.records, "go away") @@ -254,7 +254,6 @@ def test_send_remote_failover_sync(should_try, sending_elasticapm_client, caplog sending_elasticapm_client.httpserver.code = 202 sending_elasticapm_client.capture_message("bar", handled=False) sending_elasticapm_client.close() - time.sleep(0.2) assert not sending_elasticapm_client._transport.state.did_fail()