Skip to content

Replace Hash#sum with Hash#reduce to fix a rare bug in the RSpec time tracker for a specific project#305

Merged
ArturT merged 7 commits intomainfrom
debug-time-tracker
Jul 25, 2025
Merged

Replace Hash#sum with Hash#reduce to fix a rare bug in the RSpec time tracker for a specific project#305
ArturT merged 7 commits intomainfrom
debug-time-tracker

Conversation

@ArturT
Copy link
Copy Markdown
Member

@ArturT ArturT commented Jul 25, 2025

Description

The Hash#sum method yields an unpredictable result when extracting key and value pairs from a Hash. Use Hash#reduce to get consistent results in a specific customer project.

Issue

time_all_by_group_id_path = {
  "packs/a_spec.rb[1]" => 0.4432561059966247,
  "packs/a_spec.rb[1:5]" => 0.001432769997336436,
  "packs/a_spec.rb[1:6]" => 0.00025801000083447434,
  "packs/a_spec.rb[1:6:1]" => 0.00034806999974534847,
  "packs/a_spec.rb[1:6:2]" => 0.00036408000050869305,
  "packs/a_spec.rb[1:6:3]" => 0.0003385700001672376
}

Code that iterates over the Hash.

example[:path] = "packs/a_spec.rb[1]"
time_all_by_group_id_path.sum do |group_id_path, time|
  puts "group_id_path: #{group_id_path.inspect}"
  puts "time: #{time.inspect}"
  puts
  # :path is a file path (a_spec.rb), sum any before/after(:all) in the file
  next time if group_id_path.start_with?(example[:path])
  # :path is an id path (a_spec.rb[1:1]), sum any before/after(:all) above it
  next time if example[:path].start_with?(group_id_path[0..-2])
  0
end

group_id_path.start_with?(example[:path]) fails with:

E, [2025-07-15T10:06:58.711768 #286] ERROR -- : [knapsack_pro] An unexpected exception happened. RSpec cannot handle it. The exception: #<NoMethodError: undefined method `start_with?' for an instance of Float>
E, [2025-07-15T10:06:58.711810 #286] ERROR -- : [knapsack_pro] Exception message: undefined method `start_with?' for an instance of Float
E, [2025-07-15T10:06:58.711890 #286] ERROR -- : [knapsack_pro] Exception backtrace: /home/app/webapp/vendor/bundle/ruby/3.3.0/gems/knapsack_pro-8.3.2/lib/knapsack_pro/formatters/time_tracker.rb:114:in `block (2 levels) in add_hooks_time'

This error happens in the customer environment but not when reproduced locally in my environment.

In the customer environment, the values are:

group_id_path = 0.4432561059966247 # which is value for packs/a_spec.rb[1]
time = nil

The fix in this PR fixes the issue for the customer.

Checks

  • I added the changes to the UNRELEASED section of the CHANGELOG.md, including the needed bump (i.e., patch, minor, major)
  • I followed the architecture outlined below for RSpec in Queue Mode:
    • Pure: lib/knapsack_pro/pure/queue/rspec_pure.rb contains pure functions that are unit tested.
    • Extension: lib/knapsack_pro/extensions/rspec_extension.rb encapsulates calls to RSpec internals and is integration and E2E tested.
    • Runner: lib/knapsack_pro/runners/queue/rspec_runner.rb invokes the pure code and the extension to produce side effects, which are integration and E2E tested.

@ArturT ArturT added the bug label Jul 25, 2025
@ArturT ArturT merged commit 73d1459 into main Jul 25, 2025
25 checks passed
@ArturT ArturT deleted the debug-time-tracker branch July 25, 2025 10:53
@ArturT ArturT changed the title Fix a Hash#sum bug in the time tracker for RSpec Replace Hash#sum with Hash#reduce to fix a rare bug in the RSpec time tracker for a specific project Jul 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant