Skip to content

Commit

Permalink
BugFix: Random test failure when tracking compilation time (#1713)
Browse files Browse the repository at this point in the history
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]`.
  • Loading branch information
CodeMeister authored Jan 31, 2025
1 parent 7771828 commit 9bf88a5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions spec/acceptance/activesupport_instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9bf88a5

Please sign in to comment.