add valgrind rake task to ensure nothing leaks

This commit is contained in:
Sam 2016-05-12 20:05:56 +10:00
parent e2794a2764
commit ff78df0b39
2 changed files with 82 additions and 0 deletions

View File

@ -13,3 +13,42 @@ task :default => :test
gem = Gem::Specification.load( File.dirname(__FILE__) + '/mini_racer.gemspec' )
Rake::ExtensionTask.new( 'mini_racer_extension', gem )
# via http://blog.flavorjon.es/2009/06/easily-valgrind-gdb-your-ruby-c.html
namespace :test do
# partial-loads-ok and undef-value-errors necessary to ignore
# spurious (and eminently ignorable) warnings from the ruby
# interpreter
VALGRIND_BASIC_OPTS = "--num-callers=50 --error-limit=no \
--partial-loads-ok=yes --undef-value-errors=no"
desc "run test suite under valgrind with basic ruby options"
task :valgrind => :compile do
cmdline = "valgrind #{VALGRIND_BASIC_OPTS} ruby test/test_leak.rb"
puts cmdline
system cmdline
end
desc "run test suite under valgrind with leak-check=full"
task :valgrind_leak_check => :compile do
cmdline = "valgrind #{VALGRIND_BASIC_OPTS} --leak-check=full ruby test/test_leak.rb"
puts cmdline
require 'open3'
_, stderr = Open3.capture3(cmdline)
section = ""
stderr.split("\n").each do |line|
if line =~ /==.*==\s*$/
if (section =~ /mini_racer|SUMMARY/)
puts
puts section
puts
end
section = ""
else
section << line << "\n"
end
end
end
end

43
test/test_leak.rb Normal file
View File

@ -0,0 +1,43 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'mini_racer'
require 'objspace'
def has_contexts
ObjectSpace.each_object(MiniRacer::Context).count > 0
end
def clear
while has_contexts
GC.start
end
end
def test
context = MiniRacer::Context.new
context.attach("add", proc{|a,b| a+b})
context.eval('1+1')
context.eval('"1"')
context.eval('a=function(){}')
context.eval('a={a: 1}')
context.eval('a=[1,2,"3"]')
context.eval('add(1,2)')
end
def start
n = 100
puts "Running #{n} contexts"
n.times do
test
clear
end
puts "Ensuring garbage is collected"
puts "Done"
end
start