Skip to content

Commit

Permalink
Merge pull request #14 from graze/addSocketTimeout
Browse files Browse the repository at this point in the history
Add timeout to socket
  • Loading branch information
brendankay authored Jul 30, 2018
2 parents 47029f9 + 85b5d5b commit 4c6b4eb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
],
"require": {
"php": ">5.5.0",
"clue/socket-raw": "^1.2"
"clue/socket-raw": "^1.3"
},
"require-dev": {
"phpunit/phpunit" : "4.*",
Expand Down
5 changes: 3 additions & 2 deletions src/TelnetClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ public function __construct(
* @param string $prompt
* @param string $promptError
* @param string $lineEnding
* @param float|null $timeout
*
* @throws TelnetExceptionInterface
*/
public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = null)
public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = null, $timeout = null)
{
if ($prompt !== null) {
$this->setPrompt($prompt);
Expand All @@ -122,7 +123,7 @@ public function connect($dsn, $prompt = null, $promptError = null, $lineEnding =
}

try {
$socket = $this->socketFactory->createClient($dsn);
$socket = $this->socketFactory->createClient($dsn, $timeout);
} catch (Exception $e) {
throw new TelnetException(sprintf('unable to create socket connection to [%s]', $dsn), 0, $e);
}
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/TelnetClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ class TelnetClientTest extends \PHPUnit_Framework_TestCase
* @param string $prompt
* @param string $promptError
* @param string $lineEnding
* @param float|null $timeout
*
* @return void
*/
public function testConnect($prompt = null, $promptError = null, $lineEnding = null)
public function testConnect($prompt = null, $promptError = null, $lineEnding = null, $timeout = null)
{
$dsn = 'localhost:80';
$socket = m::mock(Socket::class);
$socketFactory = m::mock(SocketFactory::class)
->shouldReceive('createClient')
->with($dsn)
->with($dsn, $timeout)
->andReturn($socket)
->once()
->getMock();
Expand Down Expand Up @@ -75,7 +76,7 @@ public function testConnect($prompt = null, $promptError = null, $lineEnding = n
}

$telnetClient = $telnetClient->getMock()->makePartial();
$telnetClient->connect($dsn, $prompt, $promptError, $lineEnding);
$telnetClient->connect($dsn, $prompt, $promptError, $lineEnding, $timeout);
}

/**
Expand All @@ -85,6 +86,7 @@ public function dataProviderConnect()
{
return [
['PROMPT', 'PROMPTERROR', 'LINENDING'],
['PROMPT', 'PROMPTERROR', 'LINENDING', 5],
[null, null, null]
];
}
Expand Down

0 comments on commit 4c6b4eb

Please sign in to comment.