xref: /PHP-5.5/ext/mysql/tests/bug55473.phpt (revision a2d8ae9a)
1--TEST--
2Bug #5547 (mysql_pconnect leaks file descriptors on reconnect)
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
8	die("skip Test doesn't work on Windows");
9}
10
11if (!($output = @exec("lsof -np " . getmypid())))
12	die("skip Test can't find command line tool lsof");
13?>
14--INI--
15mysql.max_persistent=30
16mysql.allow_persistent=1
17--FILE--
18<?php
19	include "connect.inc";
20
21	$tmp    = NULL;
22	$link   = NULL;
23
24 	if ($socket)
25        $host = sprintf("%s:%s", $host, $socket);
26    else if ($port)
27        $host = sprintf("%s:%s", $host, $port);
28
29	function connect($host, $user, $passwd) {
30		$conn = mysql_pconnect($host, $user, $passwd);
31
32		if (!$conn)
33			die(sprintf("[001] %s\n", mysql_error()));
34
35		if (!mysql_query("set wait_timeout=1", $conn))
36			printf("[002] [%d] %s\n", mysql_errno($conn), mysql_error($conn));
37
38		return $conn;
39	}
40
41	$conn = connect($host, $user, $passwd);
42	$opened_files = -1;
43
44	for ($i = 0; $i < 4; $i++) {
45		/* wait while mysql closes connection */
46		sleep(3);
47
48		if (!mysql_ping($conn)) {
49			printf("[003] reconnect %d\n", $i);
50			$conn = connect($host, $user, $passwd);
51		}
52
53		$r = mysql_query('select 1', $conn);
54		if (!$r)
55			printf("[004] [%d] %s\n", mysql_errno($conn), mysql_error($conn));
56
57
58		if ($opened_files == -1) {
59			$opened_files = trim(exec("lsof -np " . getmypid() . " | wc -l"));
60			printf("[005] Setting openened files...\n");
61		} else if (($tmp = trim(exec("lsof -np " . getmypid() . " | wc -l"))) != $opened_files) {
62			printf("[006] [%d] different number of opened_files : expected %d, got %d", $i, $opened_files, $tmp);
63		} else {
64			printf("[007] Opened files as expected\n");
65		}
66	}
67
68	print "done!";
69?>
70--EXPECTF--
71Deprecated: mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
72
73Warning: mysql_ping(): MySQL server has gone away in %s on line %d
74[003] reconnect 0
75
76Deprecated: mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
77[005] Setting openened files...
78
79Warning: mysql_ping(): MySQL server has gone away in %s on line %d
80[003] reconnect 1
81
82Deprecated: mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
83[007] Opened files as expected
84
85Warning: mysql_ping(): MySQL server has gone away in %s on line %d
86[003] reconnect 2
87
88Deprecated: mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
89[007] Opened files as expected
90
91Warning: mysql_ping(): MySQL server has gone away in %s on line %d
92[003] reconnect 3
93
94Deprecated: mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
95[007] Opened files as expected
96done!
97