Skip to content

Commit

Permalink
Added clearing of socket before sending data (in case previous respon…
Browse files Browse the repository at this point in the history
…se timed out and data is left on the socket
  • Loading branch information
marius-meissner committed Mar 25, 2018
1 parent f67d4cb commit c76a520
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(Socket $socket = null)
*/
public function sendRaw(Request $request): Response
{
$this->socket->clear();
$this->socket->send($request->encode());
$this->requestStarted = microtime(true);

Expand Down
10 changes: 10 additions & 0 deletions src/Network/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ public function __destruct()
{
socket_close($this->connection);
}

/**
* Clears all data currently in queue
*/
public function clear()
{
socket_set_nonblock($this->connection);
socket_read($this->connection, 65536);
socket_set_block($this->connection);
}
}
32 changes: 22 additions & 10 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function test_sendRaw_headerNotComplete()

$request = new DefaultRequest(8, 21, 1500, $responseStructure);

$this->socket->expects(self::at(0))
$this->socket->expects(self::at(1))
->method('send')
->with(self::equalTo($request->encode()));

$this->socket->expects(self::at(1))
$this->socket->expects(self::at(2))
->method('listen')
->willReturn('ABCDEFGHIJKLMN');

$this->socket->expects(self::at(2))
$this->socket->expects(self::at(3))
->method('listen')
->willReturn('OP');

Expand All @@ -105,23 +105,23 @@ public function test_sendRaw_incompleteDataException()

$request = new DefaultRequest(8, 21, 1500, $responseStructure);

$this->socket->expects(self::at(0))
$this->socket->expects(self::at(1))
->method('send')
->with(self::equalTo($request->encode()));

$this->socket->expects(self::at(1))
$this->socket->expects(self::at(2))
->method('listen')
->willReturn('ABCDEFGHIJKLMN');

$this->socket->expects(self::at(2))
$this->socket->expects(self::at(3))
->method('listen')
->willReturn('OP');

$this->socket->expects(self::at(3))
$this->socket->expects(self::at(4))
->method('listen')
->willReturn('QR');

$this->socket->expects(self::at(4))
$this->socket->expects(self::at(5))
->method('listen')
->willReturn('ST');

Expand All @@ -146,14 +146,14 @@ public function test_sendRaw_timeoutOccurredOnSocketLevel()

public function test_sendRaw_correctTimeoutCalculated()
{
$this->socket->expects(self::at(1))
$this->socket->expects(self::at(2))
->method('listen')
->will(self::returnCallback(function () {
usleep(10000);
return 'ABCDEFGHIJKLMN';
}));

$this->socket->expects(self::at(2))
$this->socket->expects(self::at(3))
->method('listen')
->will(self::returnCallback(function (int $timeout) {
self::assertGreaterThan(80000, $timeout);
Expand All @@ -174,4 +174,16 @@ public function test_sendRaw_calculatedTimeoutLessThenZero()
$request = new DefaultRequest(8, 21, 1500, new DefaultResponseStructure(), 1);
$this->service->sendRaw($request);
}

public function test_sendRaw_socketClearedBeforeSendingData()
{
$this->socket->expects(self::at(0))
->method('clear');

$this->socket->expects(self::once())
->method('listen')
->willReturn('ABCDEFGHIJKLMNOP');

$this->service->sendRaw(new DefaultRequest(8, 21, 1500));
}
}

0 comments on commit c76a520

Please sign in to comment.