Skip to content

Commit f052439

Browse files
committed
Merge pull request #11 from clue-labs/error
Fix error reporting when trying to create invalid sockets
2 parents b1eccee + f30223e commit f052439

2 files changed

Lines changed: 58 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: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
<?php
22

33
use React\Datagram\Socket;
4+
use React\Datagram\Factory;
45
use Clue\React\Block;
6+
use React\Promise;
57

68
class FactoryTest extends TestCase
79
{
10+
private $loop;
11+
private $resolver;
812
private $factory;
913

1014
public function setUp()
1115
{
1216
$this->loop = React\EventLoop\Factory::create();
13-
$this->factory = new React\Datagram\Factory($this->loop, $this->createResolverMock());
17+
$this->resolver = $this->createResolverMock();
18+
$this->factory = new Factory($this->loop, $this->resolver);
1419
}
1520

1621
public function testCreateClient()
@@ -64,4 +69,54 @@ public function testCreateServerRandomPort()
6469

6570
$capturedServer->close();
6671
}
72+
73+
public function testCreateClientWithIpWillNotUseResolver()
74+
{
75+
$this->resolver->expects($this->never())->method('resolve');
76+
77+
$client = Block\await($this->factory->createClient('127.0.0.1:0'), $this->loop);
78+
$client->close();
79+
}
80+
81+
public function testCreateClientWithHostnameWillUseResolver()
82+
{
83+
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\resolve('127.0.0.1'));
84+
85+
$client = Block\await($this->factory->createClient('example.com:0'), $this->loop);
86+
$client->close();
87+
}
88+
89+
public function testCreateClientWithHostnameWillRejectIfResolverRejects()
90+
{
91+
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\reject(new \RuntimeException('test')));
92+
93+
$this->setExpectedException('RuntimeException');
94+
Block\await($this->factory->createClient('example.com:0'), $this->loop);
95+
}
96+
97+
public function testCreateClientWithHostnameWillRejectIfNoResolverIsGiven()
98+
{
99+
$this->factory = new Factory($this->loop);
100+
101+
$this->setExpectedException('Exception');
102+
Block\await($this->factory->createClient('example.com:0'), $this->loop);
103+
}
104+
105+
/**
106+
* @expectedException Exception
107+
* @expectedExceptionMessage Unable to create client socket
108+
*/
109+
public function testCreateClientWithInvalidHostnameWillReject()
110+
{
111+
Block\await($this->factory->createClient('/////'), $this->loop);
112+
}
113+
114+
/**
115+
* @expectedException Exception
116+
* @expectedExceptionMessage Unable to create server socket
117+
*/
118+
public function testCreateServerWithInvalidHostnameWillReject()
119+
{
120+
Block\await($this->factory->createServer('/////'), $this->loop);
121+
}
67122
}

0 commit comments

Comments
 (0)