Skip to content

Commit

Permalink
Mark listen() deprecated; remove bindDatagramSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jun 6, 2019
1 parent 2c2aa8b commit 6419c39
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 92 deletions.
20 changes: 2 additions & 18 deletions src/functions.php
Expand Up @@ -10,6 +10,8 @@
const LOOP_CONNECTOR_IDENTIFIER = Connector::class;

/**
* @deprecated Use Server::listen() instead.
*
* Listen for client connections on the specified server address.
*
* If you want to accept TLS connections, you have to use `yield $socket->setupTls()` after accepting new clients.
Expand All @@ -29,24 +31,6 @@ function listen(string $uri, ?BindContext $context = null): Server
return Server::listen($uri, $context);
}

/**
* Create a new Datagram (UDP server) on the specified server address.
*
* @see DatagramSocket::bind()
*
* @param string $uri URI in scheme://host:port format. UDP is assumed if no scheme is present.
* @param BindContext|null $context Context options for listening.
*
* @return DatagramSocket
*
* @throws SocketException If binding to the specified URI failed.
* @throws \Error If an invalid scheme is given.
*/
function bindDatagramSocket(string $uri, ?BindContext $context = null): DatagramSocket
{
return DatagramSocket::bind($uri, $context);
}

/**
* Set or access the global socket Connector instance.
*
Expand Down
33 changes: 26 additions & 7 deletions test/DatagramSocketTest.php
Expand Up @@ -4,15 +4,34 @@

use Amp\Loop;
use Amp\Socket;
use Amp\Socket\DatagramSocket;
use PHPUnit\Framework\TestCase;
use function Amp\asyncCall;

class DatagramSocketTest extends TestCase
{
/**
* @expectedException \Error
* @expectedExceptionMessage Only udp scheme allowed for datagram creation
*/
public function testBindEndpointInvalidScheme(): void
{
DatagramSocket::bind("invalid://127.0.0.1:0");
}

/**
* @expectedException \Amp\Socket\SocketException
* @expectedExceptionMessageRegExp /Could not create datagram .*: \[Error: #.*\] .*$/
*/
public function testBindEndpointError(): void
{
DatagramSocket::bind('error');
}

public function testReceive()
{
Loop::run(function () {
$endpoint = Socket\bindDatagramSocket('127.0.0.1:0');
$endpoint = DatagramSocket::bind('127.0.0.1:0');
Loop::delay(100, [$endpoint, 'close']);

$this->assertInternalType('resource', $endpoint->getResource());
Expand All @@ -37,7 +56,7 @@ public function testReceive()
public function testSend()
{
Loop::run(function () {
$endpoint = Socket\bindDatagramSocket('127.0.0.1:0');
$endpoint = DatagramSocket::bind('127.0.0.1:0');
Loop::delay(100, [$endpoint, 'close']);

$this->assertInternalType('resource', $endpoint->getResource());
Expand Down Expand Up @@ -70,7 +89,7 @@ public function testSendPacketTooLarge()
$this->expectExceptionMessage('Could not send packet on endpoint: stream_socket_sendto(): Message too long');

Loop::run(function () {
$endpoint = Socket\bindDatagramSocket('127.0.0.1:0');
$endpoint = DatagramSocket::bind('127.0.0.1:0');
Loop::delay(100, [$endpoint, 'close']);

$socket = yield Socket\connect('udp://' . $endpoint->getAddress());
Expand All @@ -86,7 +105,7 @@ public function testSendPacketTooLarge()
public function testReceiveThenClose()
{
Loop::run(function () {
$endpoint = Socket\bindDatagramSocket('127.0.0.1:0');
$endpoint = DatagramSocket::bind('127.0.0.1:0');

$promise = $endpoint->receive();

Expand All @@ -99,7 +118,7 @@ public function testReceiveThenClose()
public function testReceiveAfterClose()
{
Loop::run(function () {
$endpoint = Socket\bindDatagramSocket('127.0.0.1:0');
$endpoint = DatagramSocket::bind('127.0.0.1:0');

$endpoint->close();

Expand All @@ -112,7 +131,7 @@ public function testSimultaneousReceive()
$this->expectException(Socket\PendingReceiveError::class);

Loop::run(function () {
$endpoint = Socket\bindDatagramSocket('127.0.0.1:0');
$endpoint = DatagramSocket::bind('127.0.0.1:0');
try {
$promise = $endpoint->receive();
$endpoint->receive();
Expand All @@ -127,7 +146,7 @@ public function testSetChunkSize()
Loop::run(function () {
$context = (new Socket\BindContext())->withChunkSize(1);

$endpoint = Socket\bindDatagramSocket('127.0.0.1:0', $context);
$endpoint = DatagramSocket::bind('127.0.0.1:0', $context);

try {
$socket = yield Socket\connect('udp://' . $endpoint->getAddress());
Expand Down
43 changes: 38 additions & 5 deletions test/ServerTest.php
Expand Up @@ -5,15 +5,48 @@
use Amp\Delayed;
use Amp\Loop;
use Amp\Socket;
use Amp\Socket\Server;
use PHPUnit\Framework\TestCase;
use function Amp\asyncCall;

class ServerTest extends TestCase
{
/**
* @expectedException \Error
* @expectedExceptionMessage Only tcp and unix schemes allowed for server creation
*/
public function testListenInvalidScheme(): void
{
Server::listen("invalid://127.0.0.1:0");
}

/**
* @expectedException \Amp\Socket\SocketException
* @expectedExceptionMessageRegExp /Could not create server .*: \[Error: #.*\] .*$/
*/
public function testListenStreamSocketServerError(): void
{
Server::listen('error');
}

public function testListenIPv6(): void
{
try {
$socket = Server::listen('[::1]:0');
$this->assertRegExp('(\[::1\]:\d+)', (string) $socket->getAddress());
} catch (Socket\SocketException $e) {
if ($e->getMessage() === 'Could not create server tcp://[::1]:0: [Error: #0] Cannot assign requested address') {
$this->markTestSkipped('Missing IPv6 support');
}

throw $e;
}
}

public function testAccept(): void
{
Loop::run(function () {
$server = Socket\listen('127.0.0.1:0');
$server = Server::listen('127.0.0.1:0');

asyncCall(function () use ($server) {
while ($socket = yield $server->accept()) {
Expand All @@ -32,7 +65,7 @@ public function testTls(): void
Loop::run(function () {
$tlsContext = (new Socket\ServerTlsContext)
->withDefaultCertificate(new Socket\Certificate(__DIR__ . '/tls/amphp.org.pem'));
$server = Socket\listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));
$server = Server::listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));

asyncCall(function () use ($server) {
/** @var Socket\ResourceSocket $socket */
Expand Down Expand Up @@ -69,7 +102,7 @@ public function testSniWorksWithCorrectHostName(): void
Loop::run(function () {
$tlsContext = (new Socket\ServerTlsContext)
->withCertificates(['amphp.org' => new Socket\Certificate(__DIR__ . '/tls/amphp.org.pem')]);
$server = Socket\listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));
$server = Server::listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));

asyncCall(function () use ($server) {
/** @var Socket\EncryptableSocket $socket */
Expand Down Expand Up @@ -109,7 +142,7 @@ public function testSniWorksWithMultipleCertificates(): void
'www.amphp.org' => new Socket\Certificate(__DIR__ . '/tls/www.amphp.org.pem'),
]);

$server = Socket\listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));
$server = Server::listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));

asyncCall(function () use ($server) {
/** @var Socket\ResourceSocket $socket */
Expand Down Expand Up @@ -161,7 +194,7 @@ public function testSniWorksWithMultipleCertificatesAndDifferentFilesForCertAndK
'www.amphp.org' => new Socket\Certificate(__DIR__ . '/tls/www.amphp.org.crt', __DIR__ . '/tls/www.amphp.org.key'),
]);

$server = Socket\listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));
$server = Server::listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));

asyncCall(function () use ($server) {
/** @var Socket\ResourceSocket $socket */
Expand Down
3 changes: 2 additions & 1 deletion test/SocketTest.php
Expand Up @@ -4,6 +4,7 @@

use Amp\Loop;
use Amp\Socket;
use Amp\Socket\Server;
use PHPUnit\Framework\TestCase;
use function Amp\asyncCall;
use function Amp\ByteStream\buffer;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function testSocketAddress(): void

public function testEnableCryptoWithoutTlsContext(): void
{
$server = Socket\listen('127.0.0.1:0');
$server = Server::listen('127.0.0.1:0');

asyncCall(function () use ($server) {
yield Socket\connect($server->getAddress());
Expand Down
5 changes: 3 additions & 2 deletions test/TlsFragmentationTest.php
Expand Up @@ -7,6 +7,7 @@
use Amp\Loop;
use Amp\PHPUnit\TestCase;
use Amp\Socket;
use Amp\Socket\Server;
use function Amp\asyncCall;

class TlsFragmentationTest extends TestCase
Expand All @@ -22,11 +23,11 @@ public function testTls(): void
}

Loop::run(function () {
$proxyServer = Socket\listen('127.0.0.1:0');
$proxyServer = Server::listen('127.0.0.1:0');

$tlsContext = (new Socket\ServerTlsContext)
->withDefaultCertificate(new Socket\Certificate(__DIR__ . '/tls/amphp.org.pem'));
$server = Socket\listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));
$server = Server::listen('127.0.0.1:0', (new Socket\BindContext)->withTlsContext($tlsContext));

// Proxy to apply chunking of single bytes
asyncCall(function () use ($proxyServer, $server) {
Expand Down
59 changes: 0 additions & 59 deletions test/functionsTest.php

This file was deleted.

0 comments on commit 6419c39

Please sign in to comment.