Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce max empty writes to 1 #75

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.php_cs.cache
.phpunit.result.cache
build
coverage
composer.lock
Expand Down
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sudo: false
os: linux

language: php

Expand All @@ -10,7 +10,14 @@ php:
- 7.4
- nightly

matrix:
jobs:
include:
- name: macOS
os: osx
language: generic
before_install:
- curl -s http://getcomposer.org/installer | php
- mv composer.phar /usr/local/bin/composer
allow_failures:
- php: nightly
fast_finish: true
Expand Down
4 changes: 2 additions & 2 deletions lib/ResourceOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
final class ResourceOutputStream implements OutputStream
{
const MAX_CONSECUTIVE_EMPTY_WRITES = 3;
const MAX_CONSECUTIVE_EMPTY_WRITES = 1; // EAGAIN should not occur on next watcher invocation.
const LARGE_CHUNK_SIZE = 128 * 1024;

/** @var resource|null */
Expand Down Expand Up @@ -100,7 +100,7 @@ public function __construct($stream, int $chunkSize = null)
// Broken pipes between processes on macOS/FreeBSD do not detect EOF properly.
if ($written === 0 || $written === false) {
if ($emptyWrites++ > self::MAX_CONSECUTIVE_EMPTY_WRITES) {
$message = "Failed to write to stream after multiple attempts";
$message = "Failed to write to stream";
if ($error = \error_get_last()) {
$message .= \sprintf("; %s", $error["message"]);
}
Expand Down
40 changes: 36 additions & 4 deletions test/ResourceOutputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Amp\ByteStream\Test;

use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\ByteStream\StreamException;
use Amp\Delayed;
use Amp\PHPUnit\AsyncTestCase;

class ResourceOutputStreamTest extends AsyncTestCase
Expand Down Expand Up @@ -34,10 +36,10 @@ public function testNotWritable()
public function testBrokenPipe()
{
if (($sockets = @\stream_socket_pair(
\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX,
STREAM_SOCK_STREAM,
STREAM_IPPROTO_IP
)) === false) {
\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX,
STREAM_SOCK_STREAM,
STREAM_IPPROTO_IP
)) === false) {
$this->fail("Failed to create socket pair.");
}

Expand Down Expand Up @@ -68,6 +70,36 @@ public function testClosedRemoteSocket()

// The first write still succeeds somehow...
yield $stream->write("foobar");

// A delay seems required for the OS to realize the socket is indeed closed.
yield new Delayed(10);

yield $stream->write("foobar");
}

/**
* @requires PHPUnit >= 7
*
* @see https://github.com/reactphp/stream/pull/150
*/
public function testUploadBiggerBlockSecure()
{
$size = 2 ** 18; // 256kb

$resource = \stream_socket_client('tls://httpbin.org:443');

$output = new ResourceOutputStream($resource);

$body = \str_repeat('.', $size);

yield $output->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . $body);

$input = new ResourceInputStream($resource);
$buffer = '';
while (null !== ($chunk = yield $input->read())) {
$buffer .= $chunk;
}

$this->assertStringContainsString($body, $buffer);
}
}