From: Dave Rolsky Date: Sun, 19 Feb 2012 18:10:19 +0000 (-0600) Subject: Add tests for memory leaks and cycles X-Git-Tag: 2.0500~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=634a98c9fa80d75fc9b0968ce05d788c1cdf19b3;p=gitmo%2FMoose.git Add tests for memory leaks and cycles --- diff --git a/t/bugs/memory_leaks.t b/t/bugs/memory_leaks.t new file mode 100644 index 0000000..e5d969e --- /dev/null +++ b/t/bugs/memory_leaks.t @@ -0,0 +1,103 @@ +use strict; +use warnings; + +use Test::Requires { + 'Test::LeakTrace' => '0.01', + 'Test::Memory::Cycle' => '0', +}; + +use Test::More; + +use Moose (); +use Moose::Util qw( apply_all_roles ); + +{ + package MyRole; + use Moose::Role; + sub myname { "I'm a role" } +} + +no_leaks_ok( + sub { + Moose::Meta::Class->create_anon_class->new_object; + }, + 'anonymous class with no roles is leak-free' +); + +no_leaks_ok( + sub { + Moose::Meta::Role->initialize('MyRole2'); + }, + 'Moose::Meta::Role->initialize is leak-free' +); + +no_leaks_ok( + sub { + Moose::Meta::Class->create('MyClass2')->new_object; + }, + 'creating named class is leak-free' +); + +{ + local $TODO + = 'role application leaks because we end up applying the role more than once to the meta object'; + no_leaks_ok( + sub { + Moose::Meta::Class->create( 'MyClass', roles => ['MyRole'] ); + }, + 'named class with roles is leak-free' + ); + + no_leaks_ok( + sub { + Moose::Meta::Role->create( 'MyRole2', roles => ['MyRole'] ); + }, + 'named role with roles is leak-free' + ); +} + +no_leaks_ok( + sub { + my $object = Moose::Meta::Class->create('MyClass2')->new_object; + apply_all_roles( $object, 'MyRole' ); + }, + 'applying role to an instance is leak-free' +); + +no_leaks_ok( + sub { + Moose::Meta::Role->create_anon_role; + }, + 'anonymous role is leak-free' +); + +{ + local $TODO + = 'Until we eliminate meta objects from being closed over by the immutabilized methods, this will leak'; + no_leaks_ok( + sub { + my $meta = Moose::Meta::Class->create_anon_class; + $meta->make_immutable; + }, + 'making an anon class immutable is leak-free' + ); +} + +{ + my $meta3 = Moose::Meta::Class->create('MyClass3'); + memory_cycle_ok( $meta3, 'named metaclass object is cycle-free' ); + memory_cycle_ok( $meta3->new_object, 'MyClass3 object is cycle-free' ); + + my $anon_class = Moose::Meta::Class->create_anon_class; + memory_cycle_ok($anon_class, 'anon metaclass object is cycle-free' ); + memory_cycle_ok( $anon_class->new_object, 'object from anon metaclass is cycle-free' ); + + $anon_class->make_immutable; + memory_cycle_ok($anon_class, 'immutable anon metaclass object is cycle-free' ); + memory_cycle_ok( $anon_class->new_object, 'object from immutable anon metaclass is cycle-free' ); + + my $anon_role = Moose::Meta::Role->create_anon_role; + memory_cycle_ok($anon_role, 'anon role meta object is cycle-free' ); +} + +done_testing;