11import pytest
22import sys
33from unittest .mock import patch
4+
5+ import sentry_sdk
46from sentry_sdk .integrations .spark .spark_driver import (
57 _set_app_properties ,
68 _start_sentry_listener ,
1820################
1921
2022
21- def test_set_app_properties ():
22- spark_context = SparkContext (appName = "Testing123" )
23+ @pytest .fixture (scope = "function" )
24+ def create_spark_context ():
25+ yield lambda : SparkContext (appName = "Testing123" )
26+ if SparkContext ._active_spark_context :
27+ SparkContext ._active_spark_context .stop ()
28+
29+
30+ def test_set_app_properties (create_spark_context ):
31+ spark_context = create_spark_context ()
2332 _set_app_properties ()
2433
2534 assert spark_context .getLocalProperty ("sentry_app_name" ) == "Testing123"
@@ -30,20 +39,50 @@ def test_set_app_properties():
3039 )
3140
3241
33- def test_start_sentry_listener ():
34- spark_context = SparkContext .getOrCreate ()
35-
42+ def test_start_sentry_listener (create_spark_context ):
43+ spark_context = create_spark_context ()
3644 gateway = spark_context ._gateway
3745 assert gateway ._callback_server is None
3846
39- _start_sentry_listener (spark_context )
47+ _start_sentry_listener ()
4048
4149 assert gateway ._callback_server is not None
4250
4351
44- def test_initialize_spark_integration (sentry_init ):
52+ def test_initialize_spark_integration (
53+ sentry_init , create_spark_context , reset_integrations
54+ ):
4555 sentry_init (integrations = [SparkIntegration ()])
46- SparkContext .getOrCreate ()
56+ create_spark_context ()
57+
58+
59+ @patch ("sentry_sdk.integrations.spark.spark_driver._patch_spark_context_init" )
60+ def test_initialize_spark_integration_before_spark_context_init (
61+ mock_patch_spark_context_init ,
62+ sentry_init ,
63+ create_spark_context ,
64+ ):
65+ sentry_sdk .get_global_scope ().set_client (None )
66+ sentry_init (integrations = [SparkIntegration ()])
67+ create_spark_context ()
68+
69+ mock_patch_spark_context_init .assert_called_once ()
70+
71+
72+ @patch ("sentry_sdk.integrations.spark.spark_driver._activate_integration" )
73+ @patch ("sentry_sdk.integrations.spark.spark_driver._patch_spark_context_init" )
74+ def test_initialize_spark_integration_after_spark_context_init (
75+ mock_patch_spark_context_init ,
76+ mock_activate_integration ,
77+ create_spark_context ,
78+ sentry_init ,
79+ ):
80+ sentry_sdk .get_global_scope ().clear ()
81+ create_spark_context ()
82+ sentry_init (integrations = [SparkIntegration ()])
83+
84+ mock_activate_integration .assert_called_once ()
85+ mock_patch_spark_context_init .assert_not_called ()
4786
4887
4988@pytest .fixture
0 commit comments