Skip to content

Commit 0a3273d

Browse files
committed
Add test for verifying memory management.
1 parent 3467c89 commit 0a3273d

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

test/test_node.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,20 @@ def test_set_content
232232
assert_equal("unescaped & string", node.content)
233233
assert_equal("<test>unescaped &amp; string</test>", node.to_s)
234234
end
235+
236+
def test_document_node_marks_document
237+
GC.stress = true
238+
node = create_document_child
239+
assert_equal("child", node.name)
240+
refute_nil(node.doc)
241+
ensure
242+
GC.stress = false
243+
end
244+
245+
private
246+
247+
def create_document_child
248+
doc = LibXML::XML::Document.string("<root><child/></root>")
249+
doc.root.children.first
250+
end
235251
end

test/test_node_edit.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,22 @@ def test_set_base
155155
assert_equal("<test xml:base=\"http://www.rubynet.org/\">\n <num>one</num>\n <num>two</num>\n <num>three</num>\n</test>",
156156
@doc.root.to_s)
157157
end
158+
159+
def test_detached_subtree_keeps_root_alive
160+
GC.stress = true
161+
node = create_detached_subtree_leaf
162+
assert_equal("leaf", node.name)
163+
ensure
164+
GC.stress = false
165+
end
166+
167+
private
168+
169+
def create_detached_subtree_leaf
170+
root = LibXML::XML::Node.new("root")
171+
mid = LibXML::XML::Node.new("mid")
172+
root << mid
173+
mid << LibXML::XML::Node.new("leaf")
174+
root.children.first.children.first
175+
end
158176
end

test/test_reader.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,21 @@ def test_string_encoding
363363
assert_equal(LibXML::XML::Encoding::NONE, reader.encoding)
364364
end
365365
end
366+
367+
def test_expand_gc_after_advance
368+
GC.stress = true
369+
expand_and_advance
370+
ensure
371+
GC.stress = false
372+
end
373+
374+
private
375+
376+
def expand_and_advance
377+
reader = LibXML::XML::Reader.string("<root><a/><b/></root>")
378+
reader.read # root
379+
reader.read # a
380+
reader.expand
381+
reader.next
382+
end
366383
end

0 commit comments

Comments
 (0)