|
| 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 | + |
0 commit comments