1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/fiddle] test: ensure freeing closure

GitHub: GH-102

This also improves freed closures assertions.

https://github.com/ruby/fiddle/commit/f6431f3cf8
This commit is contained in:
Sutou Kouhei 2022-09-15 06:38:52 +09:00 committed by Hiroshi SHIBATA
parent 255e617bc3
commit 7c33141293
2 changed files with 27 additions and 20 deletions

View file

@ -10,8 +10,11 @@ module Fiddle
super
# Ensure freeing all closures.
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
GC.start
assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {})
not_freed_closures = []
ObjectSpace.each_object(Fiddle::Closure) do |closure|
not_freed_closures << closure unless closure.freed?
end
assert_equal([], not_freed_closures)
end
def test_argument_errors

View file

@ -60,31 +60,35 @@ module Fiddle
end
def test_qsort1
cb = Class.new(Closure) {
closure_class = Class.new(Closure) do
def call(x, y)
Pointer.new(x)[0] <=> Pointer.new(y)[0]
end
}.new(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP])
qsort = Function.new(@libc['qsort'],
[TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP],
TYPE_VOID)
buff = "9341"
qsort.call(buff, buff.size, 1, cb)
assert_equal("1349", buff)
bug4929 = '[ruby-core:37395]'
buff = "9341"
under_gc_stress do
qsort.call(buff, buff.size, 1, cb)
end
assert_equal("1349", buff, bug4929)
closure_class.create(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP]) do |callback|
qsort = Function.new(@libc['qsort'],
[TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP],
TYPE_VOID)
buff = "9341"
qsort.call(buff, buff.size, 1, callback)
assert_equal("1349", buff)
bug4929 = '[ruby-core:37395]'
buff = "9341"
under_gc_stress do
qsort.call(buff, buff.size, 1, callback)
end
assert_equal("1349", buff, bug4929)
end
ensure
# Ensure freeing all closures.
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
cb = nil
GC.start
assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {})
not_freed_closures = []
ObjectSpace.each_object(Fiddle::Closure) do |closure|
not_freed_closures << closure unless closure.freed?
end
assert_equal([], not_freed_closures)
end
def test_snprintf