Skip to content

Commit

Permalink
helper method for splitting time into seconds and microseconds, and u…
Browse files Browse the repository at this point in the history
…se it in both places where needed
  • Loading branch information
ricardclau committed Jan 14, 2014
1 parent 8292f5f commit ca1093b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
13 changes: 13 additions & 0 deletions PhpAmqpLib/Helper/MiscHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ public static function saveBytes($bytes)
fclose($fh);
}

/**
* Gets a number (either int or float) and returns an array containing its integer part as first element and its
* decimal part mutliplied by 10^6. Useful for some PHP stream functions that need seconds and microseconds as
* different arguments
*
* @param $number
* @return array
*/
public static function splitSecondsMicroseconds($number)
{
return array(floor($number), ($number - floor($number)) * 1000000);
}

/**
* View any string as a hexdump.
*
Expand Down
35 changes: 35 additions & 0 deletions PhpAmqpLib/Tests/Unit/Helper/MiscHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace PhpAmqpLib\Tests\Unit\Helper\Protocol;

use PhpAmqpLib\Helper\MiscHelper;

class MiscHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getInputOutputForSplitSecondsMicroseconds
* @param mixed $input
* @param array $expected
*/
public function testSplitSecondsMicroseconds($input, $expected)
{
$this->assertEquals($expected, MiscHelper::splitSecondsMicroseconds($input));
}

public function getInputOutputForSplitSecondsMicroseconds()
{
return array(
array(0, array(0, 0)),
array(0.3, array(0, 300000)),
array("0.3", array(0, 300000)),
array(3, array(3, 0)),
array("3", array(3, 0)),
array(3.0, array(3, 0)),
array("3.0", array(3, 0)),
array(3.1, array(3, 100000)),
array("3.1", array(3, 100000)),
array(3.123456, array(3, 123456)),
array("3.123456", array(3, 123456)),
);
}
}
4 changes: 3 additions & 1 deletion PhpAmqpLib/Wire/AMQPReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpAmqpLib\Wire;

use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Helper\MiscHelper;
use PhpAmqpLib\Wire\AMQPDecimal;
use PhpAmqpLib\Exception\AMQPRuntimeException;
use PhpAmqpLib\Exception\AMQPOutOfBoundsException;
Expand Down Expand Up @@ -102,7 +103,8 @@ protected function wait()
}

// wait ..
$result = $this->io->select($this->timeout, 0);
list($sec, $usec) = MiscHelper::splitSecondsMicroseconds($this->timeout);
$result = $this->io->select($sec, $usec);

if ($result === false) {
throw new AMQPRuntimeException(sprintf("An error occurs", $this->timeout));
Expand Down
4 changes: 3 additions & 1 deletion PhpAmqpLib/Wire/IO/StreamIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpAmqpLib\Exception\AMQPRuntimeException;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Helper\MiscHelper;

class StreamIO extends AbstractIO
{
Expand All @@ -27,7 +28,8 @@ public function __construct($host, $port, $connection_timeout, $read_write_timeo
throw new AMQPRuntimeException("Error Connecting to server($errno): $errstr ");
}

if(!stream_set_timeout($this->sock, floor($read_write_timeout), ($read_write_timeout - floor($read_write_timeout)) * 1000000)) {
list($sec, $usec) = MiscHelper::splitSecondsMicroseconds($read_write_timeout);
if(!stream_set_timeout($this->sock, $sec, $usec)) {
throw new AMQPIOException("Timeout could not be set");
}

Expand Down

0 comments on commit ca1093b

Please sign in to comment.