SamSaffron/memory_profiler

Two tests are failing with Ruby 3

Closed this issue · 2 comments

Hi,

there are two tests of the 0.9.14 release failing when run with Ruby 3.0.

  1. test/test_results.rb: The report for Ruby 3 does not contain the last string as expected here:
  def test_normalize_paths_true
    report = normalized_paths_report
    io = StringIO.new
    report.pretty_print(io, normalize_paths: true)
    assert_match(%r!\d+\s{2}longhorn-0.1.0/lib/longhorn.rb:\d+!, io.string)
    assert_match(%r!ruby/lib/set.rb!, io.string)
  end

I think this test (without adjustments) should only been run via:

--- a/test/test_results.rb
+++ b/test/test_results.rb
@@ -80,6 +80,6 @@ class TestResults < Minitest::Test
     io = StringIO.new
     report.pretty_print(io, normalize_paths: true)
     assert_match(%r!\d+\s{2}longhorn-0.1.0/lib/longhorn.rb:\d+!, io.string)
-    assert_match(%r!ruby/lib/set.rb!, io.string)
+    assert_match(%r!ruby/lib/set.rb!, io.string) if RUBY_VERSION < '3'
   end
 end
  1. test/test_reporter.rb: There is a difference in the number of total allocated strings for test_no_strings_retained_report. I added some debugging output to see why. This basically happens for each of the test strings:
[.. Ruby 2.7 ...]
         2  "LONG TEXT #0 12345678901234567890123456789012345678901234567890"
         1  /build/ruby-memory-profiler-5G9Cml/ruby-memory-profiler-0.9.14/test/test_reporter.rb:40
         1  /build/ruby-memory-profiler-5G9Cml/ruby-memory-profiler-0.9.14/test/test_reporter.rb:41
[.. Ruby 3 ...]
         3  "LONG TEXT #0 12345678901234567890123456789012345678901234567890"
         2  /build/ruby-memory-profiler-5G9Cml/ruby-memory-profiler-0.9.14/test/test_reporter.rb:41
         1  /build/ruby-memory-profiler-5G9Cml/ruby-memory-profiler-0.9.14/test/test_reporter.rb:40
[..]

If this is the correct result for Ruby 3 then I suggest changing the assertion to be:

--- a/test/test_reporter.rb
+++ b/test/test_reporter.rb
@@ -232,7 +232,7 @@ class TestReporter < Minitest::Test
       end
     end
 
-    assert_equal(45, results.total_allocated, "45 strings should be allocated")
+    assert_includes([45, 55], results.total_allocated, "45 or 55 strings should be allocated")
     assert_equal(20, results.strings_allocated.size, "20 unique strings should be observed")
     assert_equal(0, results.strings_retained.size, "0 unique strings should be retained")
   end

Actually, with Ruby 3 on i386 the number of strings allocated is even 60: https://salsa.debian.org/ruby-team/ruby-memory-profiler/-/jobs/2157602

I think we are good now!