Skip to content

Commit

Permalink
Merge pull request php-amqp#105 from pinepain/tests_consume_timeout_f…
Browse files Browse the repository at this point in the history
…ails_sometimes

Fix timeout tests that fails sometimes due to inaccurate measurement
  • Loading branch information
lstrojny committed Apr 11, 2014
2 parents 5448b7d + b261046 commit 9c81d3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
25 changes: 18 additions & 7 deletions tests/amqpconnection_construct_with_connect_timeout.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@ try {
echo $e->getMessage(), PHP_EOL;
}

$cnn = new AMQPConnection(array('connect_timeout' => 0));
$timeout = 10.5;

$credentials = array('host' => 'google.com', 'connect_timeout' => 1.5);
// resolve hostname to don't waste time on resolve inside library while resolve operations are not under timings limit (yet)
$credentials = array('host' => gethostbyname('google.com'), 'connect_timeout' => $timeout);
//$credentials = array('host' => 'google.com', 'connect_timeout' => $timeout);
$cnn = new AMQPConnection($credentials);

$t = microtime(true);
$start = microtime(true);

try {
$cnn->connect();
} catch (AMQPConnectionException $e) {
echo $e->getMessage(), PHP_EOL;
$t = microtime(true) - $t;
$end = microtime(true);

echo ($t > 1 && $t < 2) ? 'timings OK' : 'timings failed', PHP_EOL;
$error = $end - $start - $timeout;

$limit = abs(log10($timeout)); // empirical value

echo 'error: ', $error, PHP_EOL;
echo 'limit: ', $limit, PHP_EOL;

echo abs($error) <= $limit ? 'timings OK' : 'timings failed'; // error should be less than 5% of timeout value
}
?>
--EXPECT--
--EXPECTF--
Parameter 'connect_timeout' must be greater than or equal to zero.
Socket error: could not connect to host.
timings OK
error: %f
limit: %f
timings OK
14 changes: 11 additions & 3 deletions tests/amqpqueue_consume_timeout.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ try {
echo PHP_EOL;
}
$end = microtime(true);
echo abs($end - $start - $timeout) < 0.005 ? 'true' : 'false';
$error = $end - $start - $timeout;
$limit = abs(log10($timeout)); // empirical value

echo 'error: ', $error, PHP_EOL;
echo 'limit: ', $limit, PHP_EOL;

echo abs($error) <= $limit ? 'timings OK' : 'timings failed'; // error should be less than 5% of timeout value
$queue->delete();
?>
--EXPECT--
--EXPECTF--
AMQPConnectionException
true
error: %f
limit: %f
timings OK

0 comments on commit 9c81d3b

Please sign in to comment.