File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -268,6 +268,7 @@ def close
268268 end
269269
270270 if client = get_current_client
271+ client . configuration . run_after_close_callbacks
271272 client . flush
272273
273274 if client . configuration . include_local_variables
Original file line number Diff line number Diff line change @@ -461,7 +461,8 @@ def after(event, &block)
461461 def callbacks
462462 @callbacks ||= {
463463 initialize : { before : [ ] , after : [ ] } ,
464- configured : { before : [ ] , after : [ ] }
464+ configured : { before : [ ] , after : [ ] } ,
465+ closed : { before : [ ] , after : [ ] }
465466 }
466467 end
467468
@@ -798,6 +799,11 @@ def error_messages
798799 @errors . join ( ", " )
799800 end
800801
802+ # @api private
803+ def run_after_close_callbacks
804+ run_callbacks ( :after , :closed )
805+ end
806+
801807 private
802808
803809 def init_dsn ( dsn_string )
Original file line number Diff line number Diff line change @@ -569,6 +569,20 @@ class SentryConfigurationSample < Sentry::Configuration
569569 end
570570 end
571571
572+ describe '#run_after_close_callbacks' do
573+ it 'calls hooks registered with after(:closed)' do
574+ called = false
575+
576+ config = Class . new ( Sentry ::Configuration ) do
577+ after ( :closed ) do
578+ called = true
579+ end
580+ end . new
581+
582+ expect { config . run_after_close_callbacks } . to change { called } . from ( false ) . to ( true )
583+ end
584+ end
585+
572586 describe "#skip_rake_integration" do
573587 it "returns false by default" do
574588 expect ( subject . skip_rake_integration ) . to eq ( false )
Original file line number Diff line number Diff line change @@ -8,5 +8,13 @@ class Configuration
88 Sentry ::Yabeda . collector = Sentry ::Yabeda ::Collector . new ( self )
99 end
1010 end
11+
12+ after ( :closed ) do
13+ if ( collector = Sentry ::Yabeda . collector )
14+ collector . run
15+ collector . kill
16+ Sentry ::Yabeda . collector = nil
17+ end
18+ end
1119 end
1220end
Original file line number Diff line number Diff line change 2121 end
2222 end
2323
24+ describe "on close" do
25+ it "performs a final collect before shutting down" do
26+ expect ( ::Yabeda ) . to receive ( :collect! )
27+ Sentry . close
28+ end
29+
30+ it "kills the collector and sets it to nil" do
31+ collector = Sentry ::Yabeda . collector
32+ expect ( collector ) . to receive ( :kill ) . and_call_original
33+ Sentry . close
34+ expect ( Sentry ::Yabeda . collector ) . to be_nil
35+ end
36+
37+ it "does nothing when no collector is running" do
38+ Sentry ::Yabeda . collector = nil
39+ expect { Sentry . close } . not_to raise_error
40+ end
41+ end
42+
2443 describe "auto-start" do
2544 it "starts automatically when Sentry is initialized with enable_metrics" do
2645 expect ( Sentry ::Yabeda . collector ) . to be_a ( described_class )
2746 end
2847
2948 it "does not start when enable_metrics is false" do
3049 Sentry . close
31- Sentry ::Yabeda . collector = nil
3250
3351 Sentry . init do |config |
3452 config . dsn = DUMMY_DSN
4058 expect ( Sentry ::Yabeda . collector ) . to be_nil
4159 end
4260
43- it "replaces an existing collector on re-initialization" do
61+ it "replaces an existing collector on re-initialization via close " do
4462 first = Sentry ::Yabeda . collector
4563
4664 Sentry . close
4967 expect ( Sentry ::Yabeda . collector ) . to be_a ( described_class )
5068 expect ( Sentry ::Yabeda . collector ) . not_to equal ( first )
5169 end
70+
71+ it "replaces an existing collector on re-initialization without close" do
72+ first = Sentry ::Yabeda . collector
73+ expect ( first ) . to receive ( :kill ) . and_call_original
74+
75+ perform_basic_setup
76+
77+ expect ( Sentry ::Yabeda . collector ) . to be_a ( described_class )
78+ expect ( Sentry ::Yabeda . collector ) . not_to equal ( first )
79+ end
5280 end
5381end
You can’t perform that action at this time.
0 commit comments