1 package DBICTest::Util;
7 use Scalar::Util qw/isweak weaken blessed reftype refaddr/;
10 our @EXPORT_OK = qw/stacktrace populate_weakregistry assert_empty_weakregistry/;
17 while (@frame = caller($frame++)) {
18 push @stack, [@frame[3,1,2]];
21 return undef unless @stack;
24 return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack;
28 sub populate_weakregistry {
29 my ($reg, $target, $slot) = @_;
31 croak 'Target is not a reference' unless defined ref $target;
33 $slot ||= (sprintf '%s%s(0x%x)', # so we don't trigger stringification
34 (defined blessed $target) ? blessed($target) . '=' : '',
39 if (defined $reg->{$slot}{weakref}) {
40 if ( refaddr($reg->{$slot}{weakref}) != (refaddr $target) ) {
41 print STDERR "Bail out! Weak Registry slot collision: $reg->{$slot}{weakref} / $target\n";
47 weaken( $reg->{$slot}{weakref} = $target );
48 $reg->{$slot}{stacktrace} = stacktrace(1);
55 sub assert_empty_weakregistry {
56 my ($weak_registry, $quiet) = @_;
58 croak 'Expecting a registry hashref' unless ref $weak_registry eq 'HASH';
60 return unless keys %$weak_registry;
62 my $tb = eval { Test::Builder->new }
63 or croak 'Calling test_weakregistry without a loaded Test::Builder makes no sense';
65 for my $slot (sort keys %$weak_registry) {
66 next if ! defined $weak_registry->{$slot}{weakref};
67 $tb->BAILOUT("!!!! WEAK REGISTRY SLOT $slot IS NOT A WEAKREF !!!!")
68 unless isweak( $weak_registry->{$slot}{weakref} );
72 for my $slot (sort keys %$weak_registry) {
73 ! defined $weak_registry->{$slot}{weakref} and next if $quiet;
75 $tb->ok (! defined $weak_registry->{$slot}{weakref}, "No leaks of $slot") or do {
80 $diag .= Devel::FindRef::track ($weak_registry->{$slot}{weakref}, 20) . "\n"
81 if ( $ENV{TEST_VERBOSE} && eval { require Devel::FindRef });
83 if (my $stack = $weak_registry->{$slot}{stacktrace}) {
84 $diag .= " Reference first seen$stack";
87 $tb->diag($diag) if $diag;
93 if ($INC{'Test/Builder.pm'}) {
94 my $tb = Test::Builder->new;
96 # we check for test passage - a leak may be a part of a TODO
97 if ($leaks_found and !$tb->is_passing) {
100 "\n\n%s\n%s\n\nInstall Devel::FindRef and re-run the test with set "
101 . '$ENV{TEST_VERBOSE} (prove -v) to see a more detailed leak-report'
102 . "\n\n%s\n%s\n\n", ('#' x 16) x 4
103 ) if ( !$ENV{TEST_VERBOSE} or !$INC{'Devel/FindRef.pm'} );
107 $tb->note("Auto checked $refs_traced references for leaks - none detected");