Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.

Commit 5be509c

Browse files
unknownunknown
unknown
authored and
unknown
committed
Testcase for handle leak
1 parent ca02d9e commit 5be509c

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Diff for: Examples/HandleTest/HandleTestDaemon.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
require_once 'config.php';
3+
/**
4+
* Test case for resource leaks on the automatic restart of the Daemon.
5+
* Check for open files using 'lsof handledeamontest*.log'
6+
* The main log will be open two times, one inherited from the first restart.
7+
* The new logfile in the restarted process will get handle id 0,
8+
* that will be considered STDIN and be closed before the next restart.
9+
* Therefore we only see two open logfiles.
10+
*
11+
* The amount of other open log files will graddaly increase.
12+
*
13+
*/
14+
class HandleTestDeamon extends \Core_Daemon {
15+
16+
/** use long interval to allow for restart.*/
17+
protected $loop_interval = 10;
18+
19+
/**
20+
* Exeucte method, fails on second try.
21+
* @throws \Exception
22+
*/
23+
protected function execute() {
24+
static $count=0;
25+
$count++;
26+
$this->log("execute start $count");
27+
$this->openfiles();
28+
//Need a minum time alive to force a restart.
29+
if ($count>1) {
30+
$this->log("execute fail $count");
31+
throw new \Exception("FAIL");
32+
}
33+
}
34+
35+
/**
36+
* Open a set of files and store them in a static to cause a handle leak.
37+
* @staticvar type $files
38+
* @param type $max
39+
*/
40+
protected function openfiles($max = 5)
41+
{
42+
static $files;
43+
if (empty($files)) {
44+
$this->log("opening files");
45+
for ($i=0;$i<$max;$i++) {
46+
$f = fopen("./handledeamontest".$i.".log", "a+");
47+
fwrite($f, "test file pid=".getmypid()."\n");
48+
fflush($f);
49+
$files[]=$f;
50+
}
51+
}
52+
}
53+
54+
protected function log_file() {
55+
//Reuse the same logfile
56+
return "./handledeamontest.log";
57+
}
58+
59+
protected function setup() {
60+
}
61+
}
62+
63+
HandleTestDeamon::getInstance()->run();
64+
65+

Diff for: Examples/HandleTest/config.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
*
5+
* Note: Nothing in this config file is specifically required to run a PHP Simple Daemon application. You can integrate it
6+
* into an existing bootstrap if you want.
7+
*
8+
* Note: When using external tools (like crontab or process managers like supervisord) the working directory may be different
9+
* than what you expect: Using relative paths and "./" based paths may not work as expected. You should always use absolute
10+
* paths when accessing filesystem resources. Setting a constant like we do with BASE_PATH below can be helpful.
11+
*
12+
*/
13+
14+
date_default_timezone_set('America/Los_Angeles');
15+
16+
// The custom error handlers that ship with PHP Simple Daemon respect all PHP INI error settings.
17+
ini_set('error_log', '/var/log/phpcli');
18+
ini_set('display_errors', 0);
19+
20+
// Define a simple Auto Loader:
21+
// Add the current application and the PHP Simple Daemon ./Core library to the existing include path
22+
// Then set an __autoload function that uses Zend Framework naming conventions.
23+
define("BASE_PATH", dirname(__FILE__));
24+
set_include_path(implode(PATH_SEPARATOR, array(
25+
realpath(BASE_PATH),
26+
realpath(BASE_PATH . '/../../'),
27+
realpath(BASE_PATH . '/../../Core'),
28+
get_include_path(),
29+
)));
30+
31+
function __autoload($class_name)
32+
{
33+
$class_name = str_replace('\\', '/', $class_name);
34+
$class_name = str_replace('_', '/', $class_name);
35+
require_once "$class_name.php";
36+
}
37+
38+
function pathify($class_name) {
39+
return str_replace("_", "/", $class_name) . ".php";
40+
}

0 commit comments

Comments
 (0)