2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2006-03-04 01:28:51 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestObjectSpace < Test::Unit::TestCase
|
|
|
|
def self.deftest_id2ref(obj)
|
|
|
|
/:(\d+)/ =~ caller[0]
|
|
|
|
file = $`
|
|
|
|
line = $1.to_i
|
2006-03-04 01:39:25 -05:00
|
|
|
code = <<"End"
|
2006-03-04 01:28:51 -05:00
|
|
|
define_method("test_id2ref_#{line}") {\
|
|
|
|
o = ObjectSpace._id2ref(obj.object_id);\
|
2006-09-16 11:30:49 -04:00
|
|
|
assert_same(obj, o, "didn't round trip: \#{obj.inspect}");\
|
2006-03-04 01:28:51 -05:00
|
|
|
}
|
|
|
|
End
|
2006-03-04 01:39:25 -05:00
|
|
|
eval code, binding, file, line
|
2006-03-04 01:28:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
deftest_id2ref(-0x4000000000000001)
|
|
|
|
deftest_id2ref(-0x4000000000000000)
|
|
|
|
deftest_id2ref(-0x40000001)
|
|
|
|
deftest_id2ref(-0x40000000)
|
|
|
|
deftest_id2ref(-1)
|
|
|
|
deftest_id2ref(0)
|
|
|
|
deftest_id2ref(1)
|
|
|
|
deftest_id2ref(0x3fffffff)
|
|
|
|
deftest_id2ref(0x40000000)
|
|
|
|
deftest_id2ref(0x3fffffffffffffff)
|
|
|
|
deftest_id2ref(0x4000000000000000)
|
|
|
|
deftest_id2ref(:a)
|
|
|
|
deftest_id2ref(:abcdefghijilkjl)
|
|
|
|
deftest_id2ref(:==)
|
|
|
|
deftest_id2ref(Object.new)
|
|
|
|
deftest_id2ref(self)
|
|
|
|
deftest_id2ref(true)
|
|
|
|
deftest_id2ref(false)
|
|
|
|
deftest_id2ref(nil)
|
2008-04-14 10:26:51 -04:00
|
|
|
|
2019-12-23 01:02:14 -05:00
|
|
|
def test_id2ref_liveness
|
|
|
|
assert_normal_exit <<-EOS
|
|
|
|
ids = []
|
|
|
|
10.times{
|
|
|
|
1_000.times{
|
|
|
|
ids << 'hello'.object_id
|
|
|
|
}
|
|
|
|
objs = ids.map{|id|
|
|
|
|
begin
|
|
|
|
ObjectSpace._id2ref(id)
|
|
|
|
rescue RangeError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
GC.start
|
|
|
|
objs.each{|e| e.inspect}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2020-06-15 12:03:15 -04:00
|
|
|
def test_id2ref_invalid_argument
|
|
|
|
msg = /no implicit conversion/
|
|
|
|
assert_raise_with_message(TypeError, msg) {ObjectSpace._id2ref(nil)}
|
|
|
|
assert_raise_with_message(TypeError, msg) {ObjectSpace._id2ref(false)}
|
|
|
|
assert_raise_with_message(TypeError, msg) {ObjectSpace._id2ref(true)}
|
|
|
|
assert_raise_with_message(TypeError, msg) {ObjectSpace._id2ref(:a)}
|
|
|
|
assert_raise_with_message(TypeError, msg) {ObjectSpace._id2ref("0")}
|
|
|
|
assert_raise_with_message(TypeError, msg) {ObjectSpace._id2ref(Object.new)}
|
|
|
|
end
|
|
|
|
|
2008-04-14 10:26:51 -04:00
|
|
|
def test_count_objects
|
|
|
|
h = {}
|
|
|
|
ObjectSpace.count_objects(h)
|
|
|
|
assert_kind_of(Hash, h)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_empty(h.keys.delete_if {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
|
|
|
|
assert_empty(h.values.delete_if {|x| x.is_a?(Integer) })
|
2008-04-14 10:26:51 -04:00
|
|
|
|
|
|
|
h = ObjectSpace.count_objects
|
|
|
|
assert_kind_of(Hash, h)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_empty(h.keys.delete_if {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
|
|
|
|
assert_empty(h.values.delete_if {|x| x.is_a?(Integer) })
|
2008-04-14 10:26:51 -04:00
|
|
|
|
|
|
|
assert_raise(TypeError) { ObjectSpace.count_objects(1) }
|
2008-05-31 10:03:23 -04:00
|
|
|
|
|
|
|
h0 = {:T_FOO=>1000}
|
|
|
|
h = ObjectSpace.count_objects(h0)
|
|
|
|
assert_same(h0, h)
|
|
|
|
assert_equal(0, h0[:T_FOO])
|
2008-04-14 10:26:51 -04:00
|
|
|
end
|
2008-06-05 10:57:05 -04:00
|
|
|
|
|
|
|
def test_finalizer
|
2019-07-03 01:44:20 -04:00
|
|
|
assert_in_out_err(["-e", <<-END], "", %w(:ok :ok :ok :ok), [])
|
2008-06-05 10:57:05 -04:00
|
|
|
a = []
|
|
|
|
ObjectSpace.define_finalizer(a) { p :ok }
|
|
|
|
b = a.dup
|
|
|
|
ObjectSpace.define_finalizer(a) { p :ok }
|
2010-06-23 01:32:46 -04:00
|
|
|
!b
|
2008-06-05 10:57:05 -04:00
|
|
|
END
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_raise(ArgumentError) { ObjectSpace.define_finalizer([], Object.new) }
|
2013-11-09 13:12:39 -05:00
|
|
|
|
|
|
|
code = proc do |priv|
|
|
|
|
<<-"CODE"
|
|
|
|
fin = Object.new
|
|
|
|
class << fin
|
|
|
|
#{priv}def call(id)
|
|
|
|
puts "finalized"
|
2013-11-09 10:36:46 -05:00
|
|
|
end
|
2013-11-09 13:12:39 -05:00
|
|
|
end
|
|
|
|
ObjectSpace.define_finalizer([], fin)
|
|
|
|
CODE
|
2013-11-09 10:36:46 -05:00
|
|
|
end
|
2019-07-03 01:44:20 -04:00
|
|
|
assert_in_out_err([], code[""], ["finalized"])
|
|
|
|
assert_in_out_err([], code["private "], ["finalized"])
|
2014-09-10 04:22:03 -04:00
|
|
|
c = EnvUtil.labeled_class("C\u{3042}").new
|
|
|
|
o = Object.new
|
|
|
|
assert_raise_with_message(ArgumentError, /C\u{3042}/) {
|
|
|
|
ObjectSpace.define_finalizer(o, c)
|
|
|
|
}
|
2008-06-05 10:57:05 -04:00
|
|
|
end
|
2012-12-03 05:03:25 -05:00
|
|
|
|
2016-06-20 00:37:19 -04:00
|
|
|
def test_finalizer_with_super
|
|
|
|
assert_in_out_err(["-e", <<-END], "", %w(:ok), [])
|
|
|
|
class A
|
|
|
|
def foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class B < A
|
|
|
|
def foo
|
|
|
|
1.times { super }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class C
|
|
|
|
module M
|
|
|
|
end
|
|
|
|
|
|
|
|
FINALIZER = proc do
|
2016-08-09 03:36:14 -04:00
|
|
|
M.module_eval(__FILE__, "", __LINE__) do
|
2016-06-20 00:37:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def define_finalizer
|
|
|
|
ObjectSpace.define_finalizer(self, FINALIZER)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class D
|
|
|
|
def foo
|
|
|
|
B.new.foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
C::M.singleton_class.send :define_method, :module_eval do |src, id, line|
|
|
|
|
end
|
|
|
|
|
|
|
|
GC.stress = true
|
|
|
|
10.times do
|
|
|
|
C.new.define_finalizer
|
|
|
|
D.new.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
p :ok
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2021-07-22 11:32:09 -04:00
|
|
|
def test_exception_in_finalizer
|
|
|
|
assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", [], /finalizing \(RuntimeError\)/)
|
|
|
|
begin;
|
|
|
|
ObjectSpace.define_finalizer(Object.new) {raise "finalizing"}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2021-04-07 14:32:30 -04:00
|
|
|
def test_finalizer_thread_raise
|
|
|
|
GC.disable
|
|
|
|
fzer = proc do |id|
|
|
|
|
sleep 0.2
|
|
|
|
end
|
|
|
|
2.times do
|
|
|
|
o = Object.new
|
|
|
|
ObjectSpace.define_finalizer(o, fzer)
|
|
|
|
end
|
|
|
|
|
|
|
|
my_error = Class.new(RuntimeError)
|
|
|
|
begin
|
|
|
|
main_th = Thread.current
|
|
|
|
Thread.new do
|
|
|
|
sleep 0.1
|
|
|
|
main_th.raise(my_error)
|
|
|
|
end
|
|
|
|
GC.start
|
|
|
|
puts "After GC"
|
|
|
|
sleep(10)
|
|
|
|
assert(false)
|
|
|
|
rescue my_error
|
|
|
|
end
|
2021-08-04 15:33:47 -04:00
|
|
|
ensure
|
|
|
|
GC.enable
|
2021-04-07 14:32:30 -04:00
|
|
|
end
|
|
|
|
|
2012-12-03 05:03:25 -05:00
|
|
|
def test_each_object
|
2015-11-24 22:18:13 -05:00
|
|
|
klass = Class.new
|
|
|
|
new_obj = klass.new
|
|
|
|
|
|
|
|
found = []
|
|
|
|
count = ObjectSpace.each_object(klass) do |obj|
|
|
|
|
found << obj
|
|
|
|
end
|
|
|
|
assert_equal(1, count)
|
|
|
|
assert_equal(1, found.size)
|
|
|
|
assert_same(new_obj, found[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_object_enumerator
|
|
|
|
klass = Class.new
|
|
|
|
new_obj = klass.new
|
|
|
|
|
|
|
|
found = []
|
|
|
|
counter = ObjectSpace.each_object(klass)
|
|
|
|
assert_equal(1, counter.each {|obj| found << obj})
|
|
|
|
assert_equal(1, found.size)
|
|
|
|
assert_same(new_obj, found[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_object_no_gabage
|
2013-05-19 22:22:35 -04:00
|
|
|
assert_separately([], <<-End)
|
2012-12-03 05:03:25 -05:00
|
|
|
GC.disable
|
|
|
|
eval('begin; 1.times{}; rescue; ensure; end')
|
|
|
|
arys = []
|
|
|
|
ObjectSpace.each_object(Array){|ary|
|
|
|
|
arys << ary
|
|
|
|
}
|
|
|
|
GC.enable
|
|
|
|
arys.each{|ary|
|
2012-12-03 06:06:21 -05:00
|
|
|
begin
|
|
|
|
assert_equal(String, ary.inspect.class) # should not cause SEGV
|
|
|
|
rescue RuntimeError
|
|
|
|
# rescue "can't modify frozen File" error.
|
|
|
|
end
|
2012-12-03 05:03:25 -05:00
|
|
|
}
|
2013-05-19 22:22:35 -04:00
|
|
|
End
|
2012-12-03 05:03:25 -05:00
|
|
|
end
|
2014-12-08 20:16:27 -05:00
|
|
|
|
|
|
|
def test_each_object_recursive_key
|
|
|
|
assert_normal_exit(<<-'end;', '[ruby-core:66742] [Bug #10579]')
|
|
|
|
h = {["foo"]=>nil}
|
|
|
|
p Thread.current[:__recursive_key__]
|
|
|
|
end;
|
|
|
|
end
|
2015-07-21 15:04:56 -04:00
|
|
|
|
|
|
|
def test_each_object_singleton_class
|
|
|
|
assert_separately([], <<-End)
|
|
|
|
class C
|
|
|
|
class << self
|
|
|
|
$c = self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
exist = false
|
|
|
|
ObjectSpace.each_object(Class){|o|
|
|
|
|
exist = true if $c == o
|
|
|
|
}
|
|
|
|
assert(exist, 'Bug #11360')
|
|
|
|
End
|
2015-12-22 08:15:58 -05:00
|
|
|
|
|
|
|
klass = Class.new
|
|
|
|
instance = klass.new
|
|
|
|
sclass = instance.singleton_class
|
|
|
|
meta = klass.singleton_class
|
|
|
|
assert_kind_of(meta, sclass)
|
|
|
|
assert_include(ObjectSpace.each_object(meta).to_a, sclass)
|
2015-07-21 15:04:56 -04:00
|
|
|
end
|
2021-03-12 14:36:58 -05:00
|
|
|
|
|
|
|
def test_each_object_with_allocation
|
|
|
|
assert_normal_exit(<<-End)
|
|
|
|
list = []
|
|
|
|
ObjectSpace.each_object { |o| list << Object.new }
|
|
|
|
End
|
|
|
|
end
|
2006-03-04 01:28:51 -05:00
|
|
|
end
|