Skip to content

Commit

Permalink
refactor: use a static property
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek committed Mar 5, 2024
1 parent 8e9116e commit 89e26fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
1 change: 0 additions & 1 deletion tests/DuplexResourceStreamIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use React\Stream\ReadableResourceStream;
use React\EventLoop\ExtEventLoop;
use React\EventLoop\ExtLibeventLoop;
use React\EventLoop\ExtLibevLoop;
use React\EventLoop\LoopInterface;
use React\EventLoop\LibEventLoop;
use React\EventLoop\LibEvLoop;
Expand Down
28 changes: 14 additions & 14 deletions tests/FunctionalInternetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
*/
class FunctionalInternetTest extends TestCase
{
/** @var LoopInterface */
private static $loop;

public static function setUpBeforeClass(): void
{
Loop::set(new StreamSelectLoop());
self::$loop = Loop::get();
}

public function testUploadKilobytePlain()
{
$size = 1000;
$stream = stream_socket_client('tcp://httpbin.org:80');

$loop = Loop::get();
$stream = new DuplexResourceStream($stream);

$buffer = '';
Expand All @@ -36,7 +39,7 @@ public function testUploadKilobytePlain()

$stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size));

$this->awaitStreamClose($stream, $loop);
$this->awaitStreamClose($stream);

$this->assertNotEquals('', $buffer);
}
Expand All @@ -46,7 +49,6 @@ public function testUploadBiggerBlockPlain()
$size = 50 * 1000;
$stream = stream_socket_client('tcp://httpbin.org:80');

$loop = Loop::get();
$stream = new DuplexResourceStream($stream);

$buffer = '';
Expand All @@ -58,7 +60,7 @@ public function testUploadBiggerBlockPlain()

$stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size));

$this->awaitStreamClose($stream, $loop);
$this->awaitStreamClose($stream);

$this->assertNotEquals('', $buffer);
}
Expand All @@ -68,7 +70,6 @@ public function testUploadKilobyteSecure()
$size = 1000;
$stream = stream_socket_client('ssl://httpbin.org:443');

$loop = Loop::get();
$stream = new DuplexResourceStream($stream);

$buffer = '';
Expand All @@ -80,7 +81,7 @@ public function testUploadKilobyteSecure()

$stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size));

$this->awaitStreamClose($stream, $loop);
$this->awaitStreamClose($stream);

$this->assertNotEquals('', $buffer);
}
Expand All @@ -99,7 +100,6 @@ public function testUploadBiggerBlockSecure()
// We work around this by limiting the write chunk size to 8192 bytes
// here to also support older PHP versions.
// See https://github.com/reactphp/socket/issues/105
$loop = Loop::get();
$stream = new DuplexResourceStream(
$stream,
null,
Expand All @@ -115,23 +115,23 @@ public function testUploadBiggerBlockSecure()

$stream->write("POST /post HTTP/1.0\r\nHost: httpbin.org\r\nContent-Length: $size\r\n\r\n" . str_repeat('.', $size));

$this->awaitStreamClose($stream, $loop);
$this->awaitStreamClose($stream);

$this->assertNotEquals('', $buffer);
}

private function awaitStreamClose(DuplexResourceStream $stream, LoopInterface $loop, $timeout = 10.0)
private function awaitStreamClose(DuplexResourceStream $stream, float $timeout = 10.0)
{
$stream->on('close', function () use ($loop) {
$loop->stop();
$stream->on('close', function () {
self::$loop->stop();
});

$that = $this;
$loop->addTimer($timeout, function () use ($loop, $that) {
$loop->stop();
self::$loop->addTimer($timeout, function () use ($that) {
self::$loop->stop();
$that->fail('Timed out while waiting for stream to close');
});

$loop->run();
self::$loop->run();
}
}

0 comments on commit 89e26fa

Please sign in to comment.