Skip to content

Commit

Permalink
Merge pull request #212 from tmm1/remove-mocha
Browse files Browse the repository at this point in the history
Remove mocha / clean up assertions
  • Loading branch information
tenderlove authored Jul 7, 2023
2 parents a995dba + bb92978 commit ef8b134
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion stackprof.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 29 additions & 16 deletions test/test_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'stackprof'
require 'stackprof/middleware'
require 'minitest/autorun'
require 'mocha/setup'
require 'tmpdir'

class StackProf::MiddlewareTest < MiniTest::Test

Expand All @@ -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
Expand Down Expand Up @@ -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'
11 changes: 9 additions & 2 deletions test/test_stackprof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def test_cputime
end

def test_walltime
GC.disable
profile = StackProf.run(mode: :wall) do
idle
end
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef8b134

Please sign in to comment.