From 9bf88a5648f0479b9bac7d43b753ea5fe5339f69 Mon Sep 17 00:00:00 2001 From: CodeMeister Date: Fri, 31 Jan 2025 11:31:04 +0000 Subject: [PATCH] BugFix: Random test failure when tracking compilation time (#1713) test: "tracks proper time of compiling the factory" source: acceptance/activesupport_instrumentation_spec.rb:99 The test sporadically fails with a complitation time of 0: The issue, is that the "factory_bot.compile_factory" intrumentation is actually triggered twice, not just once. First for :user and secondly for :configuration. The :configuration compilation time, which can indeed be 0, is the last one triggered, so that's the elapsed time recorded by :time_to_execute. By changing :time_to_execute to a hash, and recording the individual compilations, we can check just the :user compilation time with `time_to_execute[:user]`. --- spec/acceptance/activesupport_instrumentation_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/acceptance/activesupport_instrumentation_spec.rb b/spec/acceptance/activesupport_instrumentation_spec.rb index edc21537..96f79d86 100644 --- a/spec/acceptance/activesupport_instrumentation_spec.rb +++ b/spec/acceptance/activesupport_instrumentation_spec.rb @@ -97,13 +97,15 @@ def subscribed(callback, *args) end it "tracks proper time of compiling the factory" do - time_to_execute = 0 - callback = ->(_name, start, finish, _id, _payload) { time_to_execute = finish - start } + time_to_execute = {user: 0} + callback = ->(_name, start, finish, _id, payload) { + time_to_execute[payload[:name]] = (finish - start) + } ActiveSupport::Notifications.subscribed(callback, "factory_bot.compile_factory") do FactoryBot.build(:user) end - expect(time_to_execute).to be > 0 + expect(time_to_execute[:user]).to be > 0 end it "builds the correct payload" do