Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: Random test failure when tracking compilation time #1713

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CodeMeister
Copy link

Summary

When running the full test suite, it would randomly fail an activesupport_instrumentation test. On re-running the suite several times, everything would pass.

Test:

  • "tracks proper time of compiling the factory"

Source:

  • acceptance/activesupport_instrumentation_spec.rb:99

The Problem:

it "tracks proper time of compiling the factory" do
    time_to_execute = 0
    callback = ->(_name, start, finish, _id, _payload) { time_to_execute = finish - start }
    ActiveSupport::Notifications.subscribed(callback, "factory_bot.compile_factory") do
      FactoryBot.build(:user)
    end

    expect(time_to_execute).to be > 0
  end

The test would fail roughly once in every 30 runs , always with a :time_to_execute of 0:

The Cause

The cause, was that "factory_bot.compile_factory" instrumentation triggered twice, not just once.

  • First for :user
  • Second 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.

The Solution

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].

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)
  }
  ActiveSupport::Notifications.subscribed(callback, "factory_bot.compile_factory") do
    FactoryBot.build(:user)
  end

  expect(time_to_execute[:user]).to be > 0
end

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]`.
@CodeMeister CodeMeister changed the title BugFix for random test failure when tracking compilation time BugFix: Random test failure when tracking compilation time Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant