Skip to content

Commit 2f69231

Browse files
committed
Fix error reporting when trying to create invalid sockets
1 parent b1eccee commit 2f69231

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function createClient($address)
2525
$loop = $this->loop;
2626

2727
return $this->resolveAddress($address)->then(function ($address) use ($loop) {
28-
$socket = stream_socket_client($address, $errno, $errstr);
28+
$socket = @stream_socket_client($address, $errno, $errstr);
2929
if (!$socket) {
3030
throw new Exception('Unable to create client socket: ' . $errstr, $errno);
3131
}
@@ -40,7 +40,7 @@ public function createServer($address)
4040
$loop = $this->loop;
4141

4242
return $this->resolveAddress($address)->then(function ($address) use ($loop) {
43-
$socket = stream_socket_server($address, $errno, $errstr, STREAM_SERVER_BIND);
43+
$socket = @stream_socket_server($address, $errno, $errstr, STREAM_SERVER_BIND);
4444
if (!$socket) {
4545
throw new Exception('Unable to create server socket: ' . $errstr, $errno);
4646
}

tests/FactoryTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
<?php
22

33
use React\Datagram\Socket;
4+
use React\Datagram\Factory;
45
use Clue\React\Block;
56

67
class FactoryTest extends TestCase
78
{
9+
private $loop;
10+
private $resolver;
811
private $factory;
912

1013
public function setUp()
1114
{
1215
$this->loop = React\EventLoop\Factory::create();
13-
$this->factory = new React\Datagram\Factory($this->loop, $this->createResolverMock());
16+
$this->resolver = $this->createResolverMock();
17+
$this->factory = new Factory($this->loop, $this->resolver);
1418
}
1519

1620
public function testCreateClient()
@@ -64,4 +68,22 @@ public function testCreateServerRandomPort()
6468

6569
$capturedServer->close();
6670
}
71+
72+
/**
73+
* @expectedException Exception
74+
* @expectedExceptionMessage Unable to create client socket
75+
*/
76+
public function testCreateClientWithInvalidHostnameWillReject()
77+
{
78+
Block\await($this->factory->createClient('/////'), $this->loop);
79+
}
80+
81+
/**
82+
* @expectedException Exception
83+
* @expectedExceptionMessage Unable to create server socket
84+
*/
85+
public function testCreateServerWithInvalidHostnameWillReject()
86+
{
87+
Block\await($this->factory->createServer('/////'), $this->loop);
88+
}
6789
}

0 commit comments

Comments
 (0)