Skip to content

Commit

Permalink
Merge pull request #111 from clue-labs/sigtest
Browse files Browse the repository at this point in the history
Make signal handling tests more robust and increase test timeouts
  • Loading branch information
WyriHaximus authored Oct 14, 2017
2 parents f0117e4 + adf1079 commit ddee079
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions tests/StreamSelectLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public function signalProvider()
];
}

private $_signalHandled = false;

/**
* Test signal interrupt when no stream is attached to the loop
* @dataProvider signalProvider
Expand All @@ -59,20 +57,24 @@ public function testSignalInterruptNoStream($signal)
$this->markTestSkipped('"pcntl" extension is required to run this test.');
}

// dispatch signal handler once before signal is sent and once after
$this->loop->addTimer(0.01, function() { pcntl_signal_dispatch(); });
$this->loop->addTimer(0.03, function() { pcntl_signal_dispatch(); });
if (defined('HHVM_VERSION')) {
// hhvm startup is slow so we need to add another handler much later
$this->loop->addTimer(0.5, function() { pcntl_signal_dispatch(); });
}
// dispatch signal handler every 10ms for 0.1s
$check = $this->loop->addPeriodicTimer(0.01, function() {
pcntl_signal_dispatch();
});
$this->loop->addTimer(0.1, function () use ($check) {
$this->loop->cancelTimer($check);
});

$this->setUpSignalHandler($signal);
$handled = false;
$this->assertTrue(pcntl_signal(constant($signal), function () use (&$handled) {
$handled = true;
}));

// spawn external process to send signal to current process id
$this->forkSendSignal($signal);

$this->loop->run();
$this->assertTrue($this->_signalHandled);
$this->assertTrue($handled);
}

/**
Expand All @@ -86,7 +88,9 @@ public function testSignalInterruptWithStream($signal)
}

// dispatch signal handler every 10ms
$this->loop->addPeriodicTimer(0.01, function() { pcntl_signal_dispatch(); });
$this->loop->addPeriodicTimer(0.01, function() {
pcntl_signal_dispatch();
});

// add stream to the loop
list($writeStream, $readStream) = $this->createSocketPair();
Expand All @@ -97,27 +101,21 @@ public function testSignalInterruptWithStream($signal)
$loop->stop();
}
});
$this->loop->addTimer(0.05, function() use ($writeStream) {
$this->loop->addTimer(0.1, function() use ($writeStream) {
fwrite($writeStream, "end loop\n");
});

$this->setUpSignalHandler($signal);
$handled = false;
$this->assertTrue(pcntl_signal(constant($signal), function () use (&$handled) {
$handled = true;
}));

// spawn external process to send signal to current process id
$this->forkSendSignal($signal);

$this->loop->run();

$this->assertTrue($this->_signalHandled);
}

/**
* add signal handler for signal
*/
protected function setUpSignalHandler($signal)
{
$this->_signalHandled = false;
$this->assertTrue(pcntl_signal(constant($signal), function() { $this->_signalHandled = true; }));
$this->assertTrue($handled);
}

/**
Expand Down

0 comments on commit ddee079

Please sign in to comment.