Skip to content

ExtUvLoop: Fix reporting connection refused errors #207

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

Merged
merged 3 commits into from
Dec 31, 2019
Merged
Changes from 1 commit
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
Next Next commit
Add TCP/IP connection tests
  • Loading branch information
clue committed Dec 28, 2019
commit 06ecbb73accd87ac66a3fd2e9dc251e41c440628
106 changes: 106 additions & 0 deletions tests/AbstractLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace React\Tests\EventLoop;

use React\EventLoop\StreamSelectLoop;

abstract class AbstractLoopTest extends TestCase
{
/**
Expand Down Expand Up @@ -36,6 +38,110 @@ public function createSocketPair()
return $sockets;
}

public function testAddReadStreamTriggersWhenSocketReceivesData()
{
list ($input, $output) = $this->createSocketPair();

$loop = $this->loop;
$timeout = $loop->addTimer(0.1, function () use ($input, $loop) {
$loop->removeReadStream($input);
});

$called = 0;
$this->loop->addReadStream($input, function () use (&$called, $loop, $input, $timeout) {
++$called;
$loop->removeReadStream($input);
$loop->cancelTimer($timeout);
});

fwrite($output, "foo\n");

$this->loop->run();

$this->assertEquals(1, $called);
}

public function testAddReadStreamTriggersWhenSocketCloses()
{
list ($input, $output) = $this->createSocketPair();

$loop = $this->loop;
$timeout = $loop->addTimer(0.1, function () use ($input, $loop) {
$loop->removeReadStream($input);
});

$called = 0;
$this->loop->addReadStream($input, function () use (&$called, $loop, $input, $timeout) {
++$called;
$loop->removeReadStream($input);
$loop->cancelTimer($timeout);
});

fclose($output);

$this->loop->run();

$this->assertEquals(1, $called);
}

public function testAddWriteStreamTriggersWhenSocketConnectionSucceeds()
{
$server = stream_socket_server('127.0.0.1:0');

$errno = $errstr = null;
$connecting = stream_socket_client(stream_socket_get_name($server, false), $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);

$loop = $this->loop;
$timeout = $loop->addTimer(0.1, function () use ($connecting, $loop) {
$loop->removeWriteStream($connecting);
});

$called = 0;
$this->loop->addWriteStream($connecting, function () use (&$called, $loop, $connecting, $timeout) {
++$called;
$loop->removeWriteStream($connecting);
$loop->cancelTimer($timeout);
});

$this->loop->run();

$this->assertEquals(1, $called);
}

public function testAddWriteStreamTriggersWhenSocketConnectionRefused()
{
// @link https://github.com/reactphp/event-loop/issues/206
if ($this->loop instanceof StreamSelectLoop && DIRECTORY_SEPARATOR === '\\') {
$this->markTestIncomplete('StreamSelectLoop does not currently support detecting connection refused errors on Windows');
}

// first verify the operating system actually refuses the connection and no firewall is in place
// use higher timeout because Windows retires multiple times and has a noticeable delay
// @link https://stackoverflow.com/questions/19440364/why-do-failed-attempts-of-socket-connect-take-1-sec-on-windows
$errno = $errstr = null;
if (@stream_socket_client('127.0.0.1:1', $errno, $errstr, 10.0) !== false || $errno !== SOCKET_ECONNREFUSED) {
$this->markTestSkipped('Expected host to refuse connection, but got error ' . $errno . ': ' . $errstr);
}

$connecting = stream_socket_client('127.0.0.1:1', $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);

$loop = $this->loop;
$timeout = $loop->addTimer(10.0, function () use ($connecting, $loop) {
$loop->removeWriteStream($connecting);
});

$called = 0;
$this->loop->addWriteStream($connecting, function () use (&$called, $loop, $connecting, $timeout) {
++$called;
$loop->removeWriteStream($connecting);
$loop->cancelTimer($timeout);
});

$this->loop->run();

$this->assertEquals(1, $called);
}

public function testAddReadStream()
{
list ($input, $output) = $this->createSocketPair();
Expand Down
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