diff --git a/tests/AbstractLoopTest.php b/tests/AbstractLoopTest.php index 50b3e432..09b458b8 100644 --- a/tests/AbstractLoopTest.php +++ b/tests/AbstractLoopTest.php @@ -20,33 +20,36 @@ public function setUp() abstract public function createLoop(); - public function createStream() - { - return fopen('php://temp', 'r+'); - } - - public function writeToStream($stream, $content) - { - fwrite($stream, $content); - rewind($stream); + public function createSocketPair() + { + $domain = (DIRECTORY_SEPARATOR === '\\') ? STREAM_PF_INET : STREAM_PF_UNIX; + $sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); + + foreach ($sockets as $socket) { + if (function_exists('stream_set_read_buffer')) { + stream_set_read_buffer($socket, 0); + } + } + + return $sockets; } public function testAddReadStream() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); $this->loop->addReadStream($input, $this->expectCallableExactly(2)); - $this->writeToStream($input, "foo\n"); + fwrite($output, "foo\n"); $this->loop->tick(); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->loop->tick(); } public function testAddWriteStream() { - $input = $this->createStream(); + list ($input) = $this->createSocketPair(); $this->loop->addWriteStream($input, $this->expectCallableExactly(2)); $this->loop->tick(); @@ -55,33 +58,33 @@ public function testAddWriteStream() public function testRemoveReadStreamInstantly() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); $this->loop->addReadStream($input, $this->expectCallableNever()); $this->loop->removeReadStream($input); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->loop->tick(); } public function testRemoveReadStreamAfterReading() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); $this->loop->addReadStream($input, $this->expectCallableOnce()); - $this->writeToStream($input, "foo\n"); + fwrite($output, "foo\n"); $this->loop->tick(); $this->loop->removeReadStream($input); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->loop->tick(); } public function testRemoveWriteStreamInstantly() { - $input = $this->createStream(); + list ($input) = $this->createSocketPair(); $this->loop->addWriteStream($input, $this->expectCallableNever()); $this->loop->removeWriteStream($input); @@ -90,7 +93,7 @@ public function testRemoveWriteStreamInstantly() public function testRemoveWriteStreamAfterWriting() { - $input = $this->createStream(); + list ($input) = $this->createSocketPair(); $this->loop->addWriteStream($input, $this->expectCallableOnce()); $this->loop->tick(); @@ -101,60 +104,60 @@ public function testRemoveWriteStreamAfterWriting() public function testRemoveStreamInstantly() { - $input = $this->createStream(); - + list ($input, $output) = $this->createSocketPair(); + $this->loop->addReadStream($input, $this->expectCallableNever()); $this->loop->addWriteStream($input, $this->expectCallableNever()); $this->loop->removeStream($input); - - $this->writeToStream($input, "bar\n"); + + fwrite($output, "bar\n"); $this->loop->tick(); } public function testRemoveStreamForReadOnly() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); $this->loop->addReadStream($input, $this->expectCallableNever()); - $this->loop->addWriteStream($input, $this->expectCallableOnce()); + $this->loop->addWriteStream($output, $this->expectCallableOnce()); $this->loop->removeReadStream($input); - $this->writeToStream($input, "foo\n"); + fwrite($output, "foo\n"); $this->loop->tick(); } public function testRemoveStreamForWriteOnly() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); - $this->writeToStream($input, "foo\n"); + fwrite($output, "foo\n"); $this->loop->addReadStream($input, $this->expectCallableOnce()); - $this->loop->addWriteStream($input, $this->expectCallableNever()); - $this->loop->removeWriteStream($input); + $this->loop->addWriteStream($output, $this->expectCallableNever()); + $this->loop->removeWriteStream($output); $this->loop->tick(); } public function testRemoveStream() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); $this->loop->addReadStream($input, $this->expectCallableOnce()); $this->loop->addWriteStream($input, $this->expectCallableOnce()); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->loop->tick(); $this->loop->removeStream($input); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->loop->tick(); } public function testRemoveInvalid() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); // remove a valid stream from the event loop that was never added in the first place $this->loop->removeReadStream($stream); @@ -171,14 +174,14 @@ public function emptyRunShouldSimplyReturn() /** @test */ public function runShouldReturnWhenNoMoreFds() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); $loop = $this->loop; $this->loop->addReadStream($input, function ($stream) use ($loop) { $loop->removeStream($stream); }); - $this->writeToStream($input, "foo\n"); + fwrite($output, "foo\n"); $this->assertRunFasterThan($this->tickTimeout * 2); } @@ -186,14 +189,14 @@ public function runShouldReturnWhenNoMoreFds() /** @test */ public function stopShouldStopRunningLoop() { - $input = $this->createStream(); + list ($input, $output) = $this->createSocketPair(); $loop = $this->loop; $this->loop->addReadStream($input, function ($stream) use ($loop) { $loop->stop(); }); - $this->writeToStream($input, "foo\n"); + fwrite($output, "foo\n"); $this->assertRunFasterThan($this->tickTimeout * 2); } @@ -219,23 +222,33 @@ function () { public function testIgnoreRemovedCallback() { // two independent streams, both should be readable right away - $stream1 = $this->createStream(); - $stream2 = $this->createStream(); + list ($input1, $output1) = $this->createSocketPair(); + list ($input2, $output2) = $this->createSocketPair(); + + $called = false; $loop = $this->loop; - $loop->addReadStream($stream1, function ($stream) use ($loop, $stream2) { + $loop->addReadStream($input1, function ($stream) use (& $called, $loop, $input2) { // stream1 is readable, remove stream2 as well => this will invalidate its callback $loop->removeReadStream($stream); - $loop->removeReadStream($stream2); + $loop->removeReadStream($input2); + + $called = true; }); // this callback would have to be called as well, but the first stream already removed us - $loop->addReadStream($stream2, $this->expectCallableNever()); - - $this->writeToStream($stream1, "foo\n"); - $this->writeToStream($stream2, "foo\n"); - + $loop->addReadStream($input2, function () use (& $called) { + if ($called) { + $this->fail('Callback 2 must not be called after callback 1 was called'); + } + }); + + fwrite($output1, "foo\n"); + fwrite($output2, "foo\n"); + $loop->run(); + + $this->assertTrue($called); } public function testNextTick() @@ -258,7 +271,7 @@ public function testNextTick() public function testNextTickFiresBeforeIO() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -280,7 +293,7 @@ function () { public function testRecursiveNextTick() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -306,7 +319,7 @@ function () { public function testRunWaitsForNextTickEvents() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -327,7 +340,7 @@ function () { public function testNextTickEventGeneratedByFutureTick() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->futureTick( function () { @@ -382,7 +395,7 @@ public function testFutureTick() public function testFutureTickFiresBeforeIO() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -404,7 +417,7 @@ function () { public function testRecursiveFutureTick() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -432,7 +445,7 @@ function () { public function testRunWaitsForFutureTickEvents() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -453,7 +466,7 @@ function () { public function testFutureTickEventGeneratedByNextTick() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->nextTick( function () { diff --git a/tests/ExtEventLoopTest.php b/tests/ExtEventLoopTest.php index 71f798c1..16a94281 100644 --- a/tests/ExtEventLoopTest.php +++ b/tests/ExtEventLoopTest.php @@ -81,10 +81,10 @@ public function testCanUseReadableStreamWithFeatureFds() $this->loop->addReadStream($input, $this->expectCallableExactly(2)); - $this->writeToStream($input, "foo\n"); + fwrite($input, "foo\n"); $this->loop->tick(); - $this->writeToStream($input, "bar\n"); + fwrite($input, "bar\n"); $this->loop->tick(); } } diff --git a/tests/StreamSelectLoopTest.php b/tests/StreamSelectLoopTest.php index 61b059c1..1457b789 100644 --- a/tests/StreamSelectLoopTest.php +++ b/tests/StreamSelectLoopTest.php @@ -88,7 +88,7 @@ public function testSignalInterruptWithStream($sigName, $signal) $this->loop->addPeriodicTimer(0.01, function() { pcntl_signal_dispatch(); }); // add stream to the loop - list($writeStream, $readStream) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); + list($writeStream, $readStream) = $this->createSocketPair(); $this->loop->addReadStream($readStream, function($stream, $loop) { /** @var $loop LoopInterface */ $read = fgets($stream); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy