From 4c4a34e5ba212c69307ad0c956f94ffaaf23ecfd Mon Sep 17 00:00:00 2001 From: Marcelo Galigniana Date: Thu, 18 Sep 2025 10:10:53 -0500 Subject: [PATCH] test(spark): Improve `test_spark` speed Fixes GH-4623 --- tests/integrations/spark/test_spark.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/integrations/spark/test_spark.py b/tests/integrations/spark/test_spark.py index 91882a0b8f..0ea2770b89 100644 --- a/tests/integrations/spark/test_spark.py +++ b/tests/integrations/spark/test_spark.py @@ -28,11 +28,12 @@ def sentry_init_with_reset(sentry_init): _processed_integrations.discard("spark") -@pytest.fixture(scope="function") +@pytest.fixture(scope="session") def create_spark_context(): conf = SparkConf().set("spark.driver.bindAddress", "127.0.0.1") - yield lambda: SparkContext(conf=conf, appName="Testing123") - SparkContext._active_spark_context.stop() + sc = SparkContext(conf=conf, appName="Testing123") + yield lambda: sc + sc.stop() def test_set_app_properties(create_spark_context): @@ -61,12 +62,18 @@ def test_start_sentry_listener(create_spark_context): def test_initialize_spark_integration_before_spark_context_init( mock_patch_spark_context_init, sentry_init_with_reset, - create_spark_context, ): - sentry_init_with_reset() - create_spark_context() - - mock_patch_spark_context_init.assert_called_once() + # As we are using the same SparkContext connection for the whole session, + # we clean it during this test. + original_context = SparkContext._active_spark_context + SparkContext._active_spark_context = None + + try: + sentry_init_with_reset() + mock_patch_spark_context_init.assert_called_once() + finally: + # Restore the original one. + SparkContext._active_spark_context = original_context @patch("sentry_sdk.integrations.spark.spark_driver._activate_integration")