xref: /PHP-7.1/ext/pdo_pgsql/tests/bug69752.phpt (revision a037b6de)
1--TEST--
2PDO PgSQL Bug #69752 (memory leak with closeCursor)
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6require dirname(__FILE__) . '/config.inc';
7require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
8PDOTest::skip();
9?>
10--FILE--
11<?php
12require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
13$pdo = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
14
15$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
16
17$pdo->beginTransaction();
18
19$pdo->exec("
20    create table foo (
21        id bigserial not null primary key,
22        field1 text not null,
23        field2 text not null,
24        field3 text not null,
25        field4 int4 not null
26    )
27");
28$stmt = $pdo->prepare("insert into foo (field1, field2, field3, field4) values (:field1, :field2, :field3, :field4)");
29$max = 1000;
30$first_time_usage = null;
31
32for($i = 0; $i < $max; $i++) {
33    $data = array(
34        'field1' => "field1: $i",
35        'field2' => "field2: $i",
36        'field3' => "field3: $i",
37        'field4' => $i
38    );
39    $stmt->execute($data);
40    $stmt->closeCursor();
41    $usage = intval(floor(memory_get_usage() / 1024));
42
43    if ($first_time_usage === null) $first_time_usage = $usage;
44
45    /* Use delta instead of direct comparison here */
46    if (abs($first_time_usage - $usage) > 3){
47        printf("Memory Leak Detected: %d != %d\n", $usage, $first_time_usage);
48        break;
49    }
50}
51$pdo->rollBack();
52echo "done\n"
53?>
54--EXPECTF--
55done
56