From bb92978cfc7d1f3ccddb62ae59c67c93c1d3de8b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 7 Jul 2023 12:01:57 -0700 Subject: [PATCH] Remove mocha / clean up assertions This commit removes mocha and cleans up some assertions. I want to reduce the dependencies and get CI green --- .github/workflows/ci.yml | 2 +- stackprof.gemspec | 1 - test/test_middleware.rb | 45 ++++++++++++++++++++++++++-------------- test/test_stackprof.rb | 11 ++++++++-- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fcf12a14..03f2f1e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - ruby: [ ruby-head, '3.2', '3.1', '3.0', '2.7', '2.6', '2.5', '2.4', '2.3', '2.2', truffleruby ] + ruby: [ ruby-head, '3.2', '3.1', '3.0', '2.7', truffleruby ] steps: - name: Checkout uses: actions/checkout@v2 diff --git a/stackprof.gemspec b/stackprof.gemspec index 2964d4c2..4f6b34cf 100644 --- a/stackprof.gemspec +++ b/stackprof.gemspec @@ -29,6 +29,5 @@ Gem::Specification.new do |s| s.license = 'MIT' s.add_development_dependency 'rake-compiler', '~> 0.9' - s.add_development_dependency 'mocha', '~> 0.14' s.add_development_dependency 'minitest', '~> 5.0' end diff --git a/test/test_middleware.rb b/test/test_middleware.rb index afb335aa..b92e1ce1 100644 --- a/test/test_middleware.rb +++ b/test/test_middleware.rb @@ -2,7 +2,7 @@ require 'stackprof' require 'stackprof/middleware' require 'minitest/autorun' -require 'mocha/setup' +require 'tmpdir' class StackProf::MiddlewareTest < MiniTest::Test @@ -19,23 +19,36 @@ def test_path_custom end def test_save_default - StackProf::Middleware.new(Object.new) - - StackProf.stubs(:results).returns({ mode: 'foo' }) - FileUtils.expects(:mkdir_p).with('tmp/') - File.expects(:open).with(regexp_matches(/^tmp\/stackprof-foo/), 'wb') - - StackProf::Middleware.save + middleware = StackProf::Middleware.new(->(env) { 100.times { Object.new } }, + save_every: 1, + enabled: true) + Dir.mktmpdir do |dir| + Dir.chdir(dir) { middleware.call({}) } + dir = File.join(dir, "tmp") + assert File.directory? dir + profile = Dir.entries(dir).reject { |x| File.directory?(x) }.first + assert profile + assert_equal "stackprof", profile.split("-")[0] + assert_equal "cpu", profile.split("-")[1] + assert_equal Process.pid.to_s, profile.split("-")[2] + end end def test_save_custom - StackProf::Middleware.new(Object.new, { path: 'foo/' }) - - StackProf.stubs(:results).returns({ mode: 'foo' }) - FileUtils.expects(:mkdir_p).with('foo/') - File.expects(:open).with(regexp_matches(/^foo\/stackprof-foo/), 'wb') - - StackProf::Middleware.save + middleware = StackProf::Middleware.new(->(env) { 100.times { Object.new } }, + path: "foo/", + save_every: 1, + enabled: true) + Dir.mktmpdir do |dir| + Dir.chdir(dir) { middleware.call({}) } + dir = File.join(dir, "foo") + assert File.directory? dir + profile = Dir.entries(dir).reject { |x| File.directory?(x) }.first + assert profile + assert_equal "stackprof", profile.split("-")[0] + assert_equal "cpu", profile.split("-")[1] + assert_equal Process.pid.to_s, profile.split("-")[2] + end end def test_enabled_should_use_a_proc_if_passed @@ -70,4 +83,4 @@ def test_metadata StackProf::Middleware.new(Object.new, metadata: metadata) assert_equal metadata, StackProf::Middleware.metadata end -end +end unless RUBY_ENGINE == 'truffleruby' diff --git a/test/test_stackprof.rb b/test/test_stackprof.rb index 2aedc0bc..dc9948be 100644 --- a/test/test_stackprof.rb +++ b/test/test_stackprof.rb @@ -93,6 +93,7 @@ def test_cputime end def test_walltime + GC.disable profile = StackProf.run(mode: :wall) do idle end @@ -104,6 +105,8 @@ def test_walltime assert_equal "StackProfTest#idle", frame[:name] end assert_in_delta 200, frame[:samples], 25 + ensure + GC.enable end def test_custom @@ -241,8 +244,12 @@ def test_gc assert marking_frame assert sweeping_frame - assert_equal gc_frame[:total_samples], profile[:gc_samples] - assert_equal profile[:gc_samples], [gc_frame, marking_frame, sweeping_frame].map{|x| x[:samples] }.inject(:+) + # We can't guarantee a certain number of GCs to run, so just assert + # that it's within some kind of delta + assert_in_delta gc_frame[:total_samples], profile[:gc_samples], 2 + + # Lazy marking / sweeping can cause this math to not add up, so also use a delta + assert_in_delta profile[:gc_samples], [gc_frame, marking_frame, sweeping_frame].map{|x| x[:samples] }.inject(:+), 2 assert_operator profile[:gc_samples], :>, 0 assert_operator profile[:missed_samples], :<=, 25