@@ -208,27 +208,31 @@ def call
208208 end
209209
210210 it "ensures only one minutely probes thread is active at a time" do
211- alive_thread_counter = proc { Thread . list . reject { |t | t . status == "dead" } . length }
212211 probe = MockProbe . new
213212 Appsignal ::Probes . register :my_probe , probe
214- expect do
215- Appsignal ::Probes . start
216- end . to change { alive_thread_counter . call } . by ( 1 )
213+
214+ Appsignal ::Probes . start
215+ first_thread = Appsignal ::Probes . instance_variable_get ( :@thread )
216+ expect ( first_thread ) . to be_alive
217217
218218 wait_for ( "enough probe calls" ) { probe . calls >= 2 }
219219 expect ( Appsignal ::Probes ) . to have_received ( :initial_wait_time ) . once
220220 expect ( Appsignal ::Probes ) . to have_received ( :wait_time ) . at_least ( :once )
221221 expect ( log ) . to contains_log ( :debug , "Gathering minutely metrics with 1 probe" )
222222 expect ( log ) . to contains_log ( :debug , "Gathering minutely metrics with 'my_probe' probe" )
223223
224- # Starting twice in this spec, so expecting it more than once
225- expect ( Appsignal ::Probes ) . to have_received ( :initial_wait_time ) . once
226- expect do
227- # Fetch old thread
228- thread = Appsignal ::Probes . instance_variable_get ( :@thread )
229- Appsignal ::Probes . start
230- thread &.join # Wait for old thread to exit
231- end . to_not ( change { alive_thread_counter . call } )
224+ # Starting again replaces the probe thread rather than leaving the
225+ # previous one running: `start` kills the old thread and assigns a new
226+ # one in its place. Assert on the thread objects directly instead of
227+ # counting every live thread in the process, which is fragile to
228+ # unrelated background threads starting or stopping mid-example.
229+ Appsignal ::Probes . start
230+ second_thread = Appsignal ::Probes . instance_variable_get ( :@thread )
231+ first_thread . join # Wait for the killed old thread to finish exiting
232+
233+ expect ( second_thread ) . not_to be ( first_thread )
234+ expect ( first_thread ) . not_to be_alive
235+ expect ( second_thread ) . to be_alive
232236 end
233237
234238 context "with thread already started" do
0 commit comments