Test::Unit leaving phanthom objects in ObjectSpace?

J

james_b

I have some code that examines the objects in ObjectSpace and returns a
set with all those that meet some criteria. I found during unit testing
that, although I have just one (global) varible refering to an object of
a particular type, iterating over ObjectSpace.each_object returns
numerous objects of this class after some test methods have been called.

My unit test code has this:

def setup
$foo = Foo.new( "Test!" )
end

def teardown
$foo = nil
end

After four test methods are called, ObjectSpace still holds two objects
of class Foo, although the code only has that one variable for the one
instance of Foo needed.

Is this a quirk of Test::Unit? Or of ObjectSpace?

I've tried adding a call to ObjectSpace.garbage_collect
before the iteration, but it doesn't change anything.

This appears to be a Test::Unit quirk because if I call the code outside
of the Test::Unit::TestCase subclass then I find no leftover objects.
Direct calls to setup and teardown do not leave any objects around, and
the call to each_object finds the one item that should be there.

How can I ensure that once teardown is called that there are no extra
objects lingering in ObjectSpace, short of doing the setup and teardown
by hand inside of each test method?

I'm using ruby 1.8.0 (2003-05-26) [i386-mswin32] and whatever Test::Unit
that comes with that (there's no version number in unit.rb)

Thanks,

James Britt
 
K

Kent Dahl

james_b said:
I have some code that examines the objects in ObjectSpace and returns a
set with all those that meet some criteria. I found during unit testing
that, although I have just one (global) varible refering to an object of
a particular type, iterating over ObjectSpace.each_object returns
numerous objects of this class after some test methods have been called. [snip]
After four test methods are called, ObjectSpace still holds two objects
of class Foo, although the code only has that one variable for the one
instance of Foo needed.

Is this a quirk of Test::Unit? Or of ObjectSpace?

I've tried adding a call to ObjectSpace.garbage_collect
before the iteration, but it doesn't change anything. [snip]
I'm using ruby 1.8.0 (2003-05-26) [i386-mswin32] and whatever Test::Unit
that comes with that (there's no version number in unit.rb)


I cannot reproduce it using ruby 1.8.0 (2003-07-24) [i686-linux], so
perhaps it was a temporary problem that has been fixed. Suggest you try
a newer version to see if it helps.

[kentda@localhost ruby]$ ruby1.8 -v setup.rb
ruby 1.8.0 (2003-07-24) [i686-linux]
Loaded suite ATest
Started
.....
Finished in 0.003337 seconds.

4 tests, 0 assertions, 0 failures, 0 errors
Foos:4
Foos:0
[kentda@localhost ruby]$ cat setup.rb
require 'test/unit'
class Foo
end
class ATest < Test::Unit::TestCase
def setup
$foo = Foo.new
end
def teardown
$foo = nil
end
def test_a; end
def test_b; end
def test_c; end
def test_d; end
end

def num_foos
n = 0
ObjectSpace.each_object(Foo){|i| n+=1 }
print "Foos:", n, "\n"
end

if __FILE__ == $0
require 'test/unit/ui/console/testrunner'

Test::Unit::UI::Console::TestRunner.run ATest.suite
num_foos
ObjectSpace.garbage_collect
num_foos
end
 
J

james_b

Kent said:
I cannot reproduce it using ruby 1.8.0 (2003-07-24) [i686-linux], so
perhaps it was a temporary problem that has been fixed. Suggest you try
a newer version to see if it helps.


Your example code didn't quite mimick my situation (left-over objects
*during* execution of TestCase methods), but it prompted me to stick
ObjectSpace.garbage_collect inside of teardown, correcting the problem
for the time being.


Thanks,


James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top