tags:

views:

250

answers:

1

i created a signal handling class using pcntl_signal which now i want to use for sigalrm

the problem i have is that my phpunit test for testing the signalclass works (where im only using declare ticks in the signalclass), but the testclass for testing the alarm class, which in turn using the signalclass doesnt if i add declare(ticks=1) in my alarmtests it also works

i thought declare ticks is only needed at the signal handling code, which in my case is in the signalclass? but as far as i can see it is also needed for the code who calls signal handling code it doesnt even work in my alarmclass, i have to put it in my alarmtest class!?

altough using strace the signal is delivered independent of ticks

so anyone understands why i have to use declare() in my tests (sometimes)? or why do i need to declare(ticks=1) also in the code which uses it? it would mean a user needs to know how to use declare

+2  A: 

To use ticks in the global scope you have to have it in the beginning of the calling script. This is probably why you have to redeclare it. I cannot say for certain without knowing your code. Below are some examples that do work with unit testing.

You can IIRC declare youre ticks in your code by the following construct

function tick_function() {
    // Do something
}

register_tick_function('tick_function');

declare (ticks=1) {
    // Your code here
}

Or as a working example

function profile_memory()
{
     echo '<!--' . memory_get_usage() . '-->';
}

register_tick_function('profile_memory');
declare (ticks=1)
{
     $pass = md5('qwerty'); /* Tick executed */
     $pass = strrev($pass);  /* Tick executed */
     echo $pass;  /* Tick executed */
}

This is a working example of a self contained tick function that is working within a unit test

class TickTest {
    private function profile_memory() {
        static $i;
        ++$i;
        echo "Tick $i\n";
    }
    public function  __construct() {
    }
    public function doTicks() {
        $register_tick_function = register_tick_function(
                array($this,'profile_memory')
            );
        declare (ticks=1) {
            $pass = md5('qwerty'); /* Tick executed */
            $pass = strrev($pass);  /* Tick executed */
        }
    }
}

And this is the unit test (and yes i'm aware of the fact that it's not a real test)

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../ticks.php';
class TickTestTest extends PHPUnit_Framework_TestCase {
    protected $object;
    protected function setUp() {
        $this->object = new TickTest;
    }
    protected function tearDown() {
    }
    public function testDoTicks() {
        $this->object->doTicks();
    }
}

Looking at the output the tick function is called when executing the unit test.

Some references

Peter Lindqvist
it seems ure rightadding declare(ticks=1); at the top of the testunit class worksso as i see it, if you try to use ticking functionality (pcntl extension) in classes/functions you have to define ticks globally before you call such functions!it didnt even work if i call declare(ticks=1); within a function which in turn called my signal classso either you have a very specific use case for ticks so you can use {} for a tick block or declare it at the top of the scripts using itthx for the help
John Doe