diff options
-rw-r--r-- | ext/fiddle/fiddle.gemspec | 4 | ||||
-rw-r--r-- | test/fiddle/helper.rb | 9 | ||||
-rw-r--r-- | test/fiddle/test_cparser.rb | 2 | ||||
-rw-r--r-- | test/fiddle/test_func.rb | 25 | ||||
-rw-r--r-- | test/fiddle/test_function.rb | 36 | ||||
-rw-r--r-- | test/fiddle/test_handle.rb | 11 | ||||
-rw-r--r-- | test/fiddle/test_memory_view.rb | 8 | ||||
-rw-r--r-- | test/fiddle/test_pointer.rb | 21 |
8 files changed, 79 insertions, 37 deletions
diff --git a/ext/fiddle/fiddle.gemspec b/ext/fiddle/fiddle.gemspec index 509bec5ec9..a9c0ec4026 100644 --- a/ext/fiddle/fiddle.gemspec +++ b/ext/fiddle/fiddle.gemspec @@ -58,9 +58,5 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.5.0" - spec.add_development_dependency "bundler" - spec.add_development_dependency "rake" - spec.add_development_dependency "rake-compiler" - spec.metadata["msys2_mingw_dependencies"] = "libffi" end diff --git a/test/fiddle/helper.rb b/test/fiddle/helper.rb index f7143f2422..0ea3bf57f4 100644 --- a/test/fiddle/helper.rb +++ b/test/fiddle/helper.rb @@ -1,4 +1,6 @@ # frozen_string_literal: true + +require 'rbconfig/sizeof' require 'test/unit' require 'fiddle' @@ -165,5 +167,12 @@ module Fiddle GC.start end end + + def under_gc_stress + stress, GC.stress = GC.stress, true + yield + ensure + GC.stress = stress + end end end diff --git a/test/fiddle/test_cparser.rb b/test/fiddle/test_cparser.rb index 24e1800e59..ae319197a4 100644 --- a/test/fiddle/test_cparser.rb +++ b/test/fiddle/test_cparser.rb @@ -326,7 +326,7 @@ module Fiddle def test_signature_variadic_arguments unless Fiddle.const_defined?("TYPE_VARIADIC") - skip "libffi doesn't support variadic arguments" + omit "libffi doesn't support variadic arguments" end assert_equal([ "printf", diff --git a/test/fiddle/test_func.rb b/test/fiddle/test_func.rb index d3604c79c3..44893017e8 100644 --- a/test/fiddle/test_func.rb +++ b/test/fiddle/test_func.rb @@ -15,7 +15,7 @@ module Fiddle begin f = Function.new(@libm['sinf'], [TYPE_FLOAT], TYPE_FLOAT) rescue Fiddle::DLError - skip "libm may not have sinf()" + omit "libm may not have sinf()" end assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001 end @@ -26,14 +26,13 @@ module Fiddle end def test_string - stress, GC.stress = GC.stress, true - f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP) - buff = +"000" - str = f.call(buff, "123") - assert_equal("123", buff) - assert_equal("123", str.to_s) - ensure - GC.stress = stress + under_gc_stress do + f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP) + buff = +"000" + str = f.call(buff, "123") + assert_equal("123", buff) + assert_equal("123", str.to_s) + end end def test_isdigit @@ -76,13 +75,15 @@ module Fiddle bug4929 = '[ruby-core:37395]' buff = "9341" - EnvUtil.under_gc_stress {qsort.call(buff, buff.size, 1, cb)} + under_gc_stress do + qsort.call(buff, buff.size, 1, cb) + end assert_equal("1349", buff, bug4929) end def test_snprintf unless Fiddle.const_defined?("TYPE_VARIADIC") - skip "libffi doesn't support variadic arguments" + omit "libffi doesn't support variadic arguments" end if Fiddle::WINDOWS snprintf_name = "_snprintf" @@ -92,7 +93,7 @@ module Fiddle begin snprintf_pointer = @libc[snprintf_name] rescue Fiddle::DLError - skip "Can't find #{snprintf_name}: #{$!.message}" + omit "Can't find #{snprintf_name}: #{$!.message}" end snprintf = Function.new(snprintf_pointer, [ diff --git a/test/fiddle/test_function.rb b/test/fiddle/test_function.rb index df632925ad..8ac4f60aa3 100644 --- a/test/fiddle/test_function.rb +++ b/test/fiddle/test_function.rb @@ -164,7 +164,7 @@ module Fiddle begin poll = @libc['poll'] rescue Fiddle::DLError - skip 'poll(2) not available' + omit 'poll(2) not available' end f = Function.new(poll, [TYPE_VOIDP, TYPE_INT, TYPE_INT], TYPE_INT) @@ -180,9 +180,37 @@ module Fiddle end def test_no_memory_leak - prep = 'r = Fiddle::Function.new(Fiddle.dlopen(nil)["rb_obj_frozen_p"], [Fiddle::TYPE_UINTPTR_T], Fiddle::TYPE_UINTPTR_T); a = "a"' - code = 'begin r.call(a); rescue TypeError; end' - assert_no_memory_leak(%w[-W0 -rfiddle], "#{prep}\n1000.times{#{code}}", "10_000.times {#{code}}", limit: 1.2) + if respond_to?(:assert_nothing_leaked_memory) + rb_obj_frozen_p_symbol = Fiddle.dlopen(nil)["rb_obj_frozen_p"] + rb_obj_frozen_p = Fiddle::Function.new(rb_obj_frozen_p_symbol, + [Fiddle::TYPE_UINTPTR_T], + Fiddle::TYPE_UINTPTR_T) + a = "a" + n_tries = 100_000 + n_tries.times do + begin + a + 1 + rescue TypeError + end + end + n_arguments = 1 + sizeof_fiddle_generic = Fiddle::SIZEOF_VOIDP # Rough + size_per_try = + (sizeof_fiddle_generic * n_arguments) + + (Fiddle::SIZEOF_VOIDP * (n_arguments + 1)) + assert_nothing_leaked_memory(size_per_try * n_tries) do + n_tries.times do + begin + rb_obj_frozen_p.call(a) + rescue TypeError + end + end + end + else + prep = 'r = Fiddle::Function.new(Fiddle.dlopen(nil)["rb_obj_frozen_p"], [Fiddle::TYPE_UINTPTR_T], Fiddle::TYPE_UINTPTR_T); a = "a"' + code = 'begin r.call(a); rescue TypeError; end' + assert_no_memory_leak(%w[-W0 -rfiddle], "#{prep}\n1000.times{#{code}}", "10_000.times {#{code}}", limit: 1.2) + end end private diff --git a/test/fiddle/test_handle.rb b/test/fiddle/test_handle.rb index c0e85f7076..7e3ff9d844 100644 --- a/test/fiddle/test_handle.rb +++ b/test/fiddle/test_handle.rb @@ -185,7 +185,16 @@ module Fiddle end if /freebsd/=~ RUBY_PLATFORM def test_no_memory_leak - assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true) + if respond_to?(:assert_nothing_leaked_memory) + n_tries = 100_000 + assert_nothing_leaked_memory(SIZEOF_VOIDP * (n_tries / 100)) do + n_tries.times do + Fiddle::Handle.allocate + end + end + else + assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true) + end end if /cygwin|mingw|mswin/ =~ RUBY_PLATFORM diff --git a/test/fiddle/test_memory_view.rb b/test/fiddle/test_memory_view.rb index 8130823fd6..240cda37df 100644 --- a/test/fiddle/test_memory_view.rb +++ b/test/fiddle/test_memory_view.rb @@ -14,7 +14,7 @@ end module Fiddle class TestMemoryView < TestCase def setup - skip "MemoryView is unavailable" unless defined? Fiddle::MemoryView + omit "MemoryView is unavailable" unless defined? Fiddle::MemoryView end def test_null_ptr @@ -49,7 +49,7 @@ module Fiddle end def test_memory_view_multi_dimensional - skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils + omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils buf = [ 1, 2, 3, 4, 5, 6, 7, 8, @@ -71,7 +71,7 @@ module Fiddle end def test_memory_view_multi_dimensional_with_strides - skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils + omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils buf = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ].pack("l!*") @@ -93,7 +93,7 @@ module Fiddle end def test_memory_view_multi_dimensional_with_multiple_members - skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils + omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils buf = [ 1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8].pack("s*") diff --git a/test/fiddle/test_pointer.rb b/test/fiddle/test_pointer.rb index 88dad75138..7d708ee417 100644 --- a/test/fiddle/test_pointer.rb +++ b/test/fiddle/test_pointer.rb @@ -179,16 +179,6 @@ module Fiddle end def test_free= - assert_normal_exit(<<-"End", '[ruby-dev:39269]') - require 'fiddle' - include Fiddle - free = Fiddle::Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID) - ptr = Fiddle::Pointer.malloc(4) - ptr.free = free - free.ptr - ptr.free.ptr - End - free = Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID) ptr = Pointer.malloc(4) ptr.free = free @@ -282,7 +272,16 @@ module Fiddle end def test_no_memory_leak - assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Pointer.allocate}', rss: true) + if respond_to?(:assert_nothing_leaked_memory) + n_tries = 100_000 + assert_nothing_leaked_memory(SIZEOF_VOIDP * (n_tries / 100)) do + n_tries.times do + Fiddle::Pointer.allocate + end + end + else + assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Pointer.allocate}', rss: true) + end end end end if defined?(Fiddle) |