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