From e7bc391611778d7a1ee31d9959f9df86761cba89 Mon Sep 17 00:00:00 2001 From: martinschroeder Date: Mon, 9 Jan 2017 12:06:19 +0100 Subject: [PATCH 1/3] Switched to socket pairs in test cases. Removed deprecated code. --- tests/AbstractLoopTest.php | 103 +++++++++++++++++++-------------- tests/ExtEventLoopTest.php | 4 +- tests/StreamSelectLoopTest.php | 2 +- 3 files changed, 61 insertions(+), 48 deletions(-) diff --git a/tests/AbstractLoopTest.php b/tests/AbstractLoopTest.php index 8c175a67..d054648a 100644 --- a/tests/AbstractLoopTest.php +++ b/tests/AbstractLoopTest.php @@ -20,33 +20,36 @@ public function setUp() abstract public function createLoop(); - public function createStream() + public function createSocketPair() { - return fopen('php://temp', 'r+'); - } + $domain = (DIRECTORY_SEPARATOR === '\\') ? STREAM_PF_INET : STREAM_PF_UNIX; + $sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); - public function writeToStream($stream, $content) - { - fwrite($stream, $content); - rewind($stream); + 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->tickLoop($this->loop); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->tickLoop($this->loop); } public function testAddWriteStream() { - $input = $this->createStream(); + list ($input) = $this->createSocketPair(); $this->loop->addWriteStream($input, $this->expectCallableExactly(2)); $this->tickLoop($this->loop); @@ -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->tickLoop($this->loop); } 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->tickLoop($this->loop); $this->loop->removeReadStream($input); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->tickLoop($this->loop); } 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->tickLoop($this->loop); @@ -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->tickLoop($this->loop); } 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->tickLoop($this->loop); } 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->tickLoop($this->loop); } 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->tickLoop($this->loop); $this->loop->removeStream($input); - $this->writeToStream($input, "bar\n"); + fwrite($output, "bar\n"); $this->tickLoop($this->loop); } 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()); + $loop->addReadStream($input2, function () use (& $called) { + if ($called) { + $this->fail('Callback 2 must not be called after callback 1 was called'); + } + }); - $this->writeToStream($stream1, "foo\n"); - $this->writeToStream($stream2, "foo\n"); + fwrite($output1, "foo\n"); + fwrite($output2, "foo\n"); $loop->run(); + + $this->assertTrue($called); } public function testFutureTickEventGeneratedByFutureTick() @@ -275,7 +288,7 @@ public function testFutureTick() public function testFutureTickFiresBeforeIO() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -297,7 +310,7 @@ function () { public function testRecursiveFutureTick() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, @@ -325,7 +338,7 @@ function () { public function testRunWaitsForFutureTickEvents() { - $stream = $this->createStream(); + list ($stream) = $this->createSocketPair(); $this->loop->addWriteStream( $stream, diff --git a/tests/ExtEventLoopTest.php b/tests/ExtEventLoopTest.php index 761ff962..52c65309 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->tickLoop($this->loop); - $this->writeToStream($input, "bar\n"); + fwrite($input, "bar\n"); $this->tickLoop($this->loop); } } 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); From 94f984ffe6386dc60637e79d6a17424c11298add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schr=C3=B6der?= Date: Wed, 11 Jan 2017 09:16:27 +0100 Subject: [PATCH 2/3] Provide only signal constant names in data provider. --- tests/StreamSelectLoopTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/StreamSelectLoopTest.php b/tests/StreamSelectLoopTest.php index 1457b789..5cb957b7 100644 --- a/tests/StreamSelectLoopTest.php +++ b/tests/StreamSelectLoopTest.php @@ -40,9 +40,9 @@ public function testStreamSelectTimeoutEmulation() public function signalProvider() { return [ - ['SIGUSR1', SIGUSR1], - ['SIGHUP', SIGHUP], - ['SIGTERM', SIGTERM], + ['SIGUSR1'], + ['SIGHUP'], + ['SIGTERM'], ]; } @@ -52,7 +52,7 @@ public function signalProvider() * Test signal interrupt when no stream is attached to the loop * @dataProvider signalProvider */ - public function testSignalInterruptNoStream($sigName, $signal) + public function testSignalInterruptNoStream($signal) { if (!extension_loaded('pcntl')) { $this->markTestSkipped('"pcntl" extension is required to run this test.'); @@ -78,7 +78,7 @@ public function testSignalInterruptNoStream($sigName, $signal) * Test signal interrupt when a stream is attached to the loop * @dataProvider signalProvider */ - public function testSignalInterruptWithStream($sigName, $signal) + public function testSignalInterruptWithStream($signal) { if (!extension_loaded('pcntl')) { $this->markTestSkipped('"pcntl" extension is required to run this test.'); @@ -116,7 +116,7 @@ public function testSignalInterruptWithStream($sigName, $signal) protected function setUpSignalHandler($signal) { $this->_signalHandled = false; - $this->assertTrue(pcntl_signal($signal, function() { $this->_signalHandled = true; })); + $this->assertTrue(pcntl_signal(constant($signal), function() { $this->_signalHandled = true; })); } /** @@ -125,7 +125,7 @@ protected function setUpSignalHandler($signal) protected function resetSignalHandlers() { foreach($this->signalProvider() as $signal) { - pcntl_signal($signal[1], SIG_DFL); + pcntl_signal(constant($signal[0]), SIG_DFL); } } @@ -141,7 +141,7 @@ protected function forkSendSignal($signal) } else if ($childPid === 0) { // this is executed in the child process usleep(20000); - posix_kill($currentPid, $signal); + posix_kill($currentPid, constant($signal)); die(); } } From 473c25a3d1329638980fdb890e4a2ea49508ebf7 Mon Sep 17 00:00:00 2001 From: Maciej Mazur Date: Mon, 9 Jan 2017 10:52:07 +0100 Subject: [PATCH 3/3] ExtEventLoop: No longer suppress all errors --- src/ExtEventLoop.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ExtEventLoop.php b/src/ExtEventLoop.php index 51704475..d94e65db 100644 --- a/src/ExtEventLoop.php +++ b/src/ExtEventLoop.php @@ -175,8 +175,7 @@ public function run() break; } - // @-suppression: https://github.com/reactphp/react/pull/234#discussion-diff-7759616R226 - @$this->eventBase->loop($flags); + $this->eventBase->loop($flags); } } 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