test/test_memory_leak.rb in nokogiri-maglev--1.5.0.1 vs test/test_memory_leak.rb in nokogiri-maglev--1.5.2

- old
+ new

@@ -1,8 +1,20 @@ require "helper" class TestMemoryLeak < Nokogiri::TestCase + def setup + super + @str = <<EOF +<!DOCTYPE HTML> +<html> + <body> + <br /> + </body> +</html> +EOF + end + if ENV['NOKOGIRI_GC'] # turning these off by default for now def test_dont_hurt_em_why content = File.open("#{File.dirname(__FILE__)}/files/dont_hurt_em_why.xml").read ndoc = Nokogiri::XML(content) 2.times do @@ -24,11 +36,11 @@ def test_for_mem_leak_on_io_callbacks io = File.open SNUGGLES_FILE Nokogiri::XML.parse(io) - (10**10).times do + loop do Nokogiri::XML.parse(BadIO.new) rescue nil doc.write BadIO.new rescue nil end end @@ -56,9 +68,77 @@ 2.times { GC.start } count_end = count_object_space_documents assert((count_end - count_start) <= 2, "memory leak detected") rescue LoadError puts "\ndike is not installed, skipping memory leak test" + end + end + + def test_node_set_namespace_mem_leak + xml = Nokogiri::XML "<foo></foo>" + ctx = Nokogiri::XML::XPathContext.new(xml) + loop do + ctx.evaluate("//namespace::*") + end + end + + def test_leak_on_node_replace + loop do + doc = Nokogiri.XML("<root><foo /></root>") + n = Nokogiri::XML::CDATA.new(doc, "bar") + pivot = doc.root.children[0] + pivot.replace(n) + end + end + + def test_sax_parser_context + io = StringIO.new(@str) + + loop do + Nokogiri::XML::SAX::ParserContext.new(@str) + Nokogiri::XML::SAX::ParserContext.new(io) + io.rewind + + Nokogiri::HTML::SAX::ParserContext.new(@str) + Nokogiri::HTML::SAX::ParserContext.new(io) + io.rewind + end + end + + class JumpingSaxHandler < Nokogiri::XML::SAX::Document + def initialize(jumptag) + @jumptag = jumptag + super() + end + + def start_element(name, attrs = []) + throw @jumptag + end + end + + def test_jumping_sax_handler + doc = JumpingSaxHandler.new(:foo) + + loop do + catch(:foo) do + Nokogiri::HTML::SAX::Parser.new(doc).parse(@str) + end + end + end + + def test_in_context_parser_leak + loop do + doc = Nokogiri::XML::Document.new + fragment1 = Nokogiri::XML::DocumentFragment.new(doc, '<foo/>') + node = fragment1.children[0] + node.parse('<bar></bar>') + end + end + + def test_leak_on_xpath_string_function + doc = Nokogiri::XML(@str) + loop do + doc.xpath('name(//node())') end end end # if NOKOGIRI_GC private