Fix retrywrapper when the wrapped stream fails to write or read completely

This commit is contained in:
Robin Appelman 2016-06-02 16:37:52 +02:00
commit d3620e8dc4
2 changed files with 44 additions and 4 deletions

View file

@ -44,7 +44,7 @@ class RetryWrapper extends Wrapper {
$result = parent::stream_read($count); $result = parent::stream_read($count);
$bytesReceived = strlen($result); $bytesReceived = strlen($result);
while ($bytesReceived < $count && !$this->stream_eof()) { while (strlen($result) > 0 && $bytesReceived < $count && !$this->stream_eof()) {
$result .= parent::stream_read($count - $bytesReceived); $result .= parent::stream_read($count - $bytesReceived);
$bytesReceived = strlen($result); $bytesReceived = strlen($result);
} }
@ -54,11 +54,13 @@ class RetryWrapper extends Wrapper {
public function stream_write($data) { public function stream_write($data) {
$bytesToSend = strlen($data); $bytesToSend = strlen($data);
$result = parent::stream_write($data); $bytesWritten = parent::stream_write($data);
$result = $bytesWritten;
while ($result < $bytesToSend && !$this->stream_eof()) { while ($bytesWritten > 0 && $result < $bytesToSend && !$this->stream_eof()) {
$dataLeft = substr($data, $result); $dataLeft = substr($data, $result);
$result += parent::stream_write($dataLeft); $bytesWritten = parent::stream_write($dataLeft);
$result += $bytesWritten;
} }
return $result; return $result;

View file

@ -35,6 +35,32 @@ class PartialWrapper extends \Icewind\Streams\NullWrapper {
} }
} }
class FailWrapper extends \Icewind\Streams\NullWrapper {
/**
* Wraps a stream with the provided callbacks
*
* @param resource $source
* @return resource
*
* @throws \BadMethodCallException
*/
public static function wrap($source) {
$context = stream_context_create(array(
'null' => array(
'source' => $source)
));
return self::wrapSource($source, $context, 'fail', '\Icewind\Streams\Tests\FailWrapper');
}
public function stream_read($count) {
return false;
}
public function stream_write($data) {
return false;
}
}
class RetryWrapper extends Wrapper { class RetryWrapper extends Wrapper {
/** /**
@ -52,4 +78,16 @@ class RetryWrapper extends Wrapper {
public function testRewindDir() { public function testRewindDir() {
$this->markTestSkipped('directories not supported'); $this->markTestSkipped('directories not supported');
} }
public function testFailedRead() {
$source = fopen('data://text/plain,foo', 'r');
$wrapped = \Icewind\Streams\RetryWrapper::wrap(FailWrapper::wrap($source));
$this->assertEquals('', fread($wrapped, 10));
}
public function testFailedWrite() {
$source = fopen('php://temp', 'w');
$wrapped = \Icewind\Streams\RetryWrapper::wrap(FailWrapper::wrap($source));
fwrite($wrapped, 'foo');
}
} }