1
1
import pytest
2
2
import sys
3
3
from unittest .mock import patch
4
+
5
+ import sentry_sdk
4
6
from sentry_sdk .integrations .spark .spark_driver import (
5
7
_set_app_properties ,
6
8
_start_sentry_listener ,
18
20
################
19
21
20
22
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 ()
23
32
_set_app_properties ()
24
33
25
34
assert spark_context .getLocalProperty ("sentry_app_name" ) == "Testing123"
@@ -30,20 +39,50 @@ def test_set_app_properties():
30
39
)
31
40
32
41
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 ()
36
44
gateway = spark_context ._gateway
37
45
assert gateway ._callback_server is None
38
46
39
- _start_sentry_listener (spark_context )
47
+ _start_sentry_listener ()
40
48
41
49
assert gateway ._callback_server is not None
42
50
43
51
44
- def test_initialize_spark_integration (sentry_init ):
52
+ def test_initialize_spark_integration (
53
+ sentry_init , create_spark_context , reset_integrations
54
+ ):
45
55
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 ()
47
86
48
87
49
88
@pytest .fixture
0 commit comments