Skip to content

Commit

Permalink
Added test for AsyncSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
edefimov committed Jul 25, 2015
1 parent 9a6a475 commit e94ce38
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Socket/AsyncSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function select($seconds, $usec = null)

$read = $this->getSocketsForOperation(OperationInterface::OPERATION_READ);
$write = $this->getSocketsForOperation(OperationInterface::OPERATION_WRITE);
$attempts = floor(($seconds * 1E6 + $usec) / self::ATTEMPT_DELAY) + 1;
$attempts = ceil(($seconds * 1E6 + $usec) / self::ATTEMPT_DELAY);

do {
$result = $this->doStreamSelect($seconds, $usec, $read, $write);
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/Socket/AsyncSelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,31 @@ public function testChangeSocketOperation($operation, $countRead, $countWrite)
$this->verifySocketSelectOperation($countRead, $countWrite);
}

/**
* testThatIfSelectFailedIncompleteSleepWillBeCalled
*
* @return void
* @expectedException \AsyncSockets\Exception\TimeoutException
*/
public function testThatIfSelectFailedIncompleteSleepWillBeCalled()
{
$usleep = $this->getMock('Countable', ['count']);
$usleep->expects(self::exactly(1))->method('count')->with(AsyncSelector::ATTEMPT_DELAY);

PhpFunctionMocker::getPhpFunctionMocker('stream_select')->setCallable(function () {
return 1;
});

PhpFunctionMocker::getPhpFunctionMocker('stream_socket_recvfrom')->setCallable(function () {
return false;
});

PhpFunctionMocker::getPhpFunctionMocker('usleep')->setCallable([$usleep, 'count']);

$this->selector->addSocketOperation($this->socket, OperationInterface::OPERATION_READ);
$this->selector->select(0, 2 * AsyncSelector::ATTEMPT_DELAY);
}

/**
* socketOperationDataProvider
*
Expand Down Expand Up @@ -270,6 +295,7 @@ protected function tearDown()
{
PhpFunctionMocker::getPhpFunctionMocker('stream_select')->restoreNativeHandler();
PhpFunctionMocker::getPhpFunctionMocker('stream_socket_recvfrom')->restoreNativeHandler();
PhpFunctionMocker::getPhpFunctionMocker('usleep')->restoreNativeHandler();
$this->socket->close();
}

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/Socket/Io/AbstractClientIoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,40 @@ function () {
$this->object->write('data');
}

/**
* testThatIoWillNotTryWriteIfNotSelected
*
* @return void
*/
public function testThatIoWillNotTryWriteIfNotSelected()
{
$this->prepareFor(__FUNCTION__);
$this->setConnectedStateForTestObject(true);
foreach (['stream_socket_sendto', 'fwrite'] as $f) {
PhpFunctionMocker::getPhpFunctionMocker($f)->setCallable(
function () use ($f) {
self::fail("{$f} shouldn't be called");
}
);
}

$mocker = PhpFunctionMocker::getPhpFunctionMocker('stream_select');
$mocker->setCallable(function (array &$read = null, array &$write = null) {
$read = [];
$write = [];
return 0;
});

$this->ensureSocketIsOpened();
$this->object->write('data');
}

/** {@inheritdoc} */
protected function tearDown()
{
parent::tearDown();
PhpFunctionMocker::getPhpFunctionMocker('stream_socket_sendto')->restoreNativeHandler();
PhpFunctionMocker::getPhpFunctionMocker('fwrite')->restoreNativeHandler();
PhpFunctionMocker::getPhpFunctionMocker('stream_select')->restoreNativeHandler();
}

Expand Down

0 comments on commit e94ce38

Please sign in to comment.