Skip to content

Commit f6431f3

Browse files
committed
test: ensure freeing closure
GitHub: GH-102 This also improves freed closures assertions.
1 parent a0ccc6b commit f6431f3

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

test/fiddle/test_closure.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ def teardown
1010
super
1111
# Ensure freeing all closures.
1212
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
13-
GC.start
14-
assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {})
13+
not_freed_closures = []
14+
ObjectSpace.each_object(Fiddle::Closure) do |closure|
15+
not_freed_closures << closure unless closure.freed?
16+
end
17+
assert_equal([], not_freed_closures)
1518
end
1619

1720
def test_argument_errors

test/fiddle/test_func.rb

+20-16
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,35 @@ def test_strtod
6060
end
6161

6262
def test_qsort1
63-
cb = Class.new(Closure) {
63+
closure_class = Class.new(Closure) do
6464
def call(x, y)
6565
Pointer.new(x)[0] <=> Pointer.new(y)[0]
6666
end
67-
}.new(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP])
67+
end
6868

69-
qsort = Function.new(@libc['qsort'],
70-
[TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP],
71-
TYPE_VOID)
72-
buff = "9341"
73-
qsort.call(buff, buff.size, 1, cb)
74-
assert_equal("1349", buff)
69+
closure_class.create(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP]) do |callback|
70+
qsort = Function.new(@libc['qsort'],
71+
[TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP],
72+
TYPE_VOID)
73+
buff = "9341"
74+
qsort.call(buff, buff.size, 1, callback)
75+
assert_equal("1349", buff)
7576

76-
bug4929 = '[ruby-core:37395]'
77-
buff = "9341"
78-
under_gc_stress do
79-
qsort.call(buff, buff.size, 1, cb)
77+
bug4929 = '[ruby-core:37395]'
78+
buff = "9341"
79+
under_gc_stress do
80+
qsort.call(buff, buff.size, 1, callback)
81+
end
82+
assert_equal("1349", buff, bug4929)
8083
end
81-
assert_equal("1349", buff, bug4929)
8284
ensure
8385
# Ensure freeing all closures.
8486
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
85-
cb = nil
86-
GC.start
87-
assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {})
87+
not_freed_closures = []
88+
ObjectSpace.each_object(Fiddle::Closure) do |closure|
89+
not_freed_closures << closure unless closure.freed?
90+
end
91+
assert_equal([], not_freed_closures)
8892
end
8993

9094
def test_snprintf

0 commit comments

Comments
 (0)