t/pgconnect: Fix ref leak in test

Apparently 'my sub' captured the $conn variable and held a ref on it
even beyond the parent sub scope. 'my $x = sub {}' doesn't do that.

Getting the ref counts right is important here for the last test to work.

(Found while I was inspecting the refcount effects of the new ->conn()
methods with Devel::Peek)
This commit is contained in:
Yorhel 2025-05-22 09:54:08 +02:00
parent 2083ab2a6f
commit fd8332601b

View file

@ -370,18 +370,18 @@ subtest 'Prepared statement cache', sub {
$conn->cache_size(2);
my $txn = $conn->txn;
$txn->cache;
my sub numexec($sql) {
my $numexec = sub($sql) {
$txn->q('SELECT generic_plans + custom_plans FROM pg_prepared_statements WHERE statement = $1', $sql)->cache(0)->val
}
};
is $txn->q('SELECT 1')->val, 1;
is numexec('SELECT 1'), 1;
is $numexec->('SELECT 1'), 1;
my $sql = 'SELECT $1::int as a, $2::text as b';
ok !defined numexec($sql);
ok !defined $numexec->($sql);
my $params = $txn->q($sql)->param_types;
is_deeply $params, [23, 25];
is numexec($sql), 0;
is $numexec->($sql), 0;
my $cparams = $txn->q($sql)->param_types;
is_deeply $cparams, $params;
@ -391,23 +391,23 @@ subtest 'Prepared statement cache', sub {
is_deeply $ccols, $cols;
$txn->q($sql, 0, '')->exec;
is numexec($sql), 1;
is $numexec->($sql), 1;
$txn->q($sql, 0, '')->exec;
is numexec($sql), 2;
is $numexec->($sql), 2;
is numexec('SELECT 1'), 1;
is $numexec->('SELECT 1'), 1;
$txn->q('SELECT 2')->exec;
ok !defined numexec('SELECT 1');
is numexec('SELECT 2'), 1;
ok !defined $numexec->('SELECT 1');
is $numexec->('SELECT 2'), 1;
$conn->cache_size(1);
ok !defined numexec('SELECT 1');
ok !defined numexec($sql);
is numexec('SELECT 2'), 1;
ok !defined $numexec->('SELECT 1');
ok !defined $numexec->($sql);
is $numexec->('SELECT 2'), 1;
$conn->cache_size(0);
ok !defined numexec($sql);
ok !defined numexec('SELECT 2');
ok !defined $numexec->($sql);
ok !defined $numexec->('SELECT 2');
};