-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path73cachedkids.t
81 lines (62 loc) · 1.76 KB
/
73cachedkids.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use warnings;
use strict;
use Scalar::Util qw( weaken reftype refaddr blessed );
use DBI;
use B ();
use Tie::Hash ();
use Test::More;
my (%weak_dbhs, %weak_caches);
# past this scope everything should be gone
{
### get two identical connections
my @dbhs = map { DBI->connect('dbi:ExampleP::memory:', undef, undef, { RaiseError => 1 }) } (1,2);
### get weakrefs on both handles
%weak_dbhs = map { refdesc($_) => $_ } @dbhs;
weaken $_ for values %weak_dbhs;
### tie the first one's cache
if (1) {
ok(
tie( my %cache, 'Tie::StdHash'),
refdesc($dbhs[0]) . ' cache tied'
);
$dbhs[0]->{CachedKids} = \%cache;
}
### prepare something on both
$_->prepare_cached( 'SELECT name FROM .' )
for @dbhs;
### get weakrefs of both caches
%weak_caches = map {
sprintf( 'statement cache of %s (%s)',
refdesc($_),
refdesc($_->{CachedKids})
) => $_->{CachedKids}
} @dbhs;
weaken $_ for values %weak_caches;
### check both caches have entries
is (scalar keys %{$weak_caches{$_}}, 1, "One cached statement found in $_")
for keys %weak_caches;
### check both caches have sane refcounts
is ( refcount( $weak_caches{$_} ), 1, "Refcount of $_ correct")
for keys %weak_caches;
### check both dbh have sane refcounts
is ( refcount( $weak_dbhs{$_} ), 1, "Refcount of $_ correct")
for keys %weak_dbhs;
note "Exiting scope";
@dbhs=();
}
# check both $dbh weakrefs are gone
is ($weak_dbhs{$_}, undef, "$_ garbage collected")
for keys %weak_dbhs;
is ($weak_caches{$_}, undef, "$_ garbage collected")
for keys %weak_caches;
sub refdesc {
sprintf '%s%s(0x%x)',
( defined( $_[1] = blessed $_[0]) ? "$_[1]=" : '' ),
reftype $_[0],
refaddr($_[0]),
;
}
sub refcount {
B::svref_2object($_[0])->REFCNT;
}
done_testing;