From 9f40d8038db0e22a1210607678f4ce5d9b24c048 Mon Sep 17 00:00:00 2001 From: CodeMeister Date: Tue, 5 Nov 2024 16:43:41 +0000 Subject: [PATCH 1/2] BugFix for random test failure when tracking compilation time 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..854d4130 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 From 7add9dad66bf059df162b3570018afcb9aea7183 Mon Sep 17 00:00:00 2001 From: Neil Carvalho Date: Thu, 30 Jan 2025 13:32:34 -0300 Subject: [PATCH 2/2] Update spec/acceptance/activesupport_instrumentation_spec.rb --- spec/acceptance/activesupport_instrumentation_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/acceptance/activesupport_instrumentation_spec.rb b/spec/acceptance/activesupport_instrumentation_spec.rb index 854d4130..96f79d86 100644 --- a/spec/acceptance/activesupport_instrumentation_spec.rb +++ b/spec/acceptance/activesupport_instrumentation_spec.rb @@ -98,8 +98,8 @@ def subscribed(callback, *args) it "tracks proper time of compiling the factory" do time_to_execute = {user: 0} - callback = ->(_name, start, finish, _id, _payload) { - time_to_execute[_payload[:name]] = (finish - start) + 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)