Skip to content

Commit ed822c0

Browse files
authored
Merge pull request #1530 from appsignal/deflake-minutely-probes-thread-test
De-flake the minutely probes thread test
2 parents 4a41953 + 4c08e7c commit ed822c0

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

spec/lib/appsignal/probes_spec.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)