11import pytest
22import sys
33from unittest .mock import patch
4+
45from sentry_sdk .integrations .spark .spark_driver import (
56 _set_app_properties ,
67 _start_sentry_listener ,
1819################
1920
2021
21- def test_set_app_properties ():
22- spark_context = SparkContext (appName = "Testing123" )
22+ @pytest .fixture (scope = "function" )
23+ def create_spark_context ():
24+ yield lambda : SparkContext (appName = "Testing123" )
25+ if SparkContext ._active_spark_context :
26+ SparkContext ._active_spark_context .stop ()
27+
28+
29+ def test_set_app_properties (create_spark_context ):
30+ spark_context = create_spark_context ()
2331 _set_app_properties ()
2432
2533 assert spark_context .getLocalProperty ("sentry_app_name" ) == "Testing123"
@@ -30,20 +38,48 @@ def test_set_app_properties():
3038 )
3139
3240
33- def test_start_sentry_listener ():
34- spark_context = SparkContext .getOrCreate ()
35-
41+ def test_start_sentry_listener (create_spark_context ):
42+ spark_context = create_spark_context ()
3643 gateway = spark_context ._gateway
3744 assert gateway ._callback_server is None
3845
39- _start_sentry_listener (spark_context )
46+ _start_sentry_listener ()
4047
4148 assert gateway ._callback_server is not None
4249
4350
44- def test_initialize_spark_integration (sentry_init ):
51+ def test_initialize_spark_integration (
52+ sentry_init , create_spark_context , reset_integrations
53+ ):
4554 sentry_init (integrations = [SparkIntegration ()])
46- SparkContext .getOrCreate ()
55+ create_spark_context ()
56+
57+
58+ @patch ("sentry_sdk.integrations.spark.spark_driver._patch_spark_context_init" )
59+ def test_initialize_spark_integration_before_spark_context_init (
60+ mock_patch_spark_context_init ,
61+ sentry_init ,
62+ create_spark_context ,
63+ ):
64+ sentry_init (integrations = [SparkIntegration ()])
65+ create_spark_context ()
66+
67+ mock_patch_spark_context_init .assert_called_once ()
68+
69+
70+ @patch ("sentry_sdk.integrations.spark.spark_driver._activate_integration" )
71+ @patch ("sentry_sdk.integrations.spark.spark_driver._patch_spark_context_init" )
72+ def test_initialize_spark_integration_after_spark_context_init (
73+ mock_patch_spark_context_init ,
74+ mock_activate_integration ,
75+ create_spark_context ,
76+ sentry_init ,
77+ ):
78+ create_spark_context ()
79+ sentry_init (integrations = [SparkIntegration ()])
80+
81+ mock_activate_integration .assert_called_once ()
82+ mock_patch_spark_context_init .assert_not_called ()
4783
4884
4985@pytest .fixture
0 commit comments