Skip to content

Commit d895448

Browse files
authored
Merge pull request #121 from clue-labs/async
Use reactphp/async instead of clue/reactphp-block
2 parents d518c59 + 114352e commit d895448

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"react/socket": "^1.12"
1717
},
1818
"require-dev": {
19-
"clue/block-react": "^1.5",
2019
"clue/connection-manager-extra": "^1.3",
2120
"phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36",
21+
"react/async": "^4 || ^3 || ^2",
2222
"react/event-loop": "^1.2",
2323
"react/http": "^1.6"
2424
},

tests/FunctionalTest.php

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Clue\Tests\React\Socks;
44

5-
use Clue\React\Block;
65
use Clue\React\Socks\Client;
76
use Clue\React\Socks\Server;
8-
use React\EventLoop\Loop;
97
use React\Promise\Promise;
108
use React\Socket\ConnectionInterface;
119
use React\Socket\Connector;
@@ -20,6 +18,7 @@ class FunctionalTest extends TestCase
2018
private $connector;
2119
private $client;
2220

21+
private $socket;
2322
private $port;
2423
private $server;
2524

@@ -28,20 +27,28 @@ class FunctionalTest extends TestCase
2827
*/
2928
public function setUpClientServer()
3029
{
31-
$socket = new SocketServer('127.0.0.1:0');
32-
$address = $socket->getAddress();
30+
$this->socket = new SocketServer('127.0.0.1:0');
31+
$address = $this->socket->getAddress();
3332
if (strpos($address, '://') === false) {
3433
$address = 'tcp://' . $address;
3534
}
3635
$this->port = parse_url($address, PHP_URL_PORT);
3736
$this->assertNotEquals(0, $this->port);
3837

3938
$this->server = new Server();
40-
$this->server->listen($socket);
39+
$this->server->listen($this->socket);
4140
$this->connector = new TcpConnector();
4241
$this->client = new Client('127.0.0.1:' . $this->port, $this->connector);
4342
}
4443

44+
/**
45+
* @after
46+
*/
47+
public function closeSocket()
48+
{
49+
$this->socket->close();
50+
}
51+
4552
/** @group internet */
4653
public function testConnection()
4754
{
@@ -117,6 +124,8 @@ public function testConnectionSocksOverTls()
117124
$this->client = new Client(str_replace('tls:', 'sockss:', $socket->getAddress()), $this->connector);
118125

119126
$this->assertResolveStream($this->client->connect('www.google.com:80'));
127+
128+
$socket->close();
120129
}
121130

122131
/**
@@ -146,6 +155,8 @@ public function testConnectionSocksOverTlsUsesPeerNameFromSocksUri()
146155
$this->client = new Client(str_replace('tls:', 'sockss:', $socket->getAddress()), $this->connector);
147156

148157
$this->assertResolveStream($this->client->connect('www.google.com:80'));
158+
159+
$socket->close();
149160
}
150161

151162
/** @group internet */
@@ -166,6 +177,8 @@ public function testConnectionSocksOverUnix()
166177
$this->assertResolveStream($this->client->connect('www.google.com:80'));
167178

168179
unlink($path);
180+
181+
$socket->close();
169182
}
170183

171184
/** @group internet */
@@ -186,6 +199,8 @@ public function testConnectionSocks5OverUnix()
186199
$this->assertResolveStream($this->client->connect('www.google.com:80'));
187200

188201
unlink($path);
202+
203+
$socket->close();
189204
}
190205

191206
/** @group internet */
@@ -206,6 +221,8 @@ public function testConnectionSocksWithAuthenticationOverUnix()
206221
$this->assertResolveStream($this->client->connect('www.google.com:80'));
207222

208223
unlink($path);
224+
225+
$socket->close();
209226
}
210227

211228
/** @group internet */
@@ -220,6 +237,8 @@ public function testConnectionAuthenticationFromUri()
220237
$this->client = new Client('name:pass@127.0.0.1:' . $this->port, $this->connector);
221238

222239
$this->assertResolveStream($this->client->connect('www.google.com:80'));
240+
241+
$socket->close();
223242
}
224243

225244
/** @group internet */
@@ -244,6 +263,8 @@ public function testConnectionAuthenticationCallback()
244263

245264
$this->assertResolveStream($this->client->connect('www.google.com:80'));
246265
$this->assertEquals(1, $called);
266+
267+
$socket->close();
247268
}
248269

249270
/** @group internet */
@@ -257,6 +278,9 @@ public function testConnectionAuthenticationCallbackWillNotBeInvokedIfClientsSen
257278
});
258279

259280
$socket = new SocketServer('127.0.0.1:0');
281+
$socket->on('connection', function () use ($socket) {
282+
$socket->close();
283+
});
260284
$this->server->listen($socket);
261285
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);
262286

@@ -278,6 +302,8 @@ public function testConnectionAuthenticationFromUriEncoded()
278302
$this->client = new Client(rawurlencode('name') . ':' . rawurlencode('p@ss:w0rd') . '@127.0.0.1:' . $this->port, $this->connector);
279303

280304
$this->assertResolveStream($this->client->connect('www.google.com:80'));
305+
306+
$socket->close();
281307
}
282308

283309
/** @group internet */
@@ -292,6 +318,8 @@ public function testConnectionAuthenticationFromUriWithOnlyUserAndNoPassword()
292318
$this->client = new Client('empty@127.0.0.1:' . $this->port, $this->connector);
293319

294320
$this->assertResolveStream($this->client->connect('www.google.com:80'));
321+
322+
$socket->close();
295323
}
296324

297325
/** @group internet */
@@ -306,6 +334,8 @@ public function testConnectionAuthenticationEmptyPassword()
306334
$this->client = new Client('user@127.0.0.1:' . $this->port, $this->connector);
307335

308336
$this->assertResolveStream($this->client->connect('www.google.com:80'));
337+
338+
$socket->close();
309339
}
310340

311341
/** @group internet */
@@ -321,6 +351,9 @@ public function testConnectionInvalidNoAuthenticationOverLegacySocks4()
321351
$this->server = new Server(null, null, array('name' => 'pass'));
322352

323353
$socket = new SocketServer('127.0.0.1:0');
354+
$socket->on('connection', function () use ($socket) {
355+
$socket->close();
356+
});
324357
$this->server->listen($socket);
325358
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);
326359

@@ -334,6 +367,9 @@ public function testConnectionInvalidNoAuthentication()
334367
$this->server = new Server(null, null, array('name' => 'pass'));
335368

336369
$socket = new SocketServer('127.0.0.1:0');
370+
$socket->on('connection', function () use ($socket) {
371+
$socket->close();
372+
});
337373
$this->server->listen($socket);
338374
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);
339375

@@ -347,6 +383,9 @@ public function testConnectionInvalidAuthenticationMismatch()
347383
$this->server = new Server(null, null, array('name' => 'pass'));
348384

349385
$socket = new SocketServer('127.0.0.1:0');
386+
$socket->on('connection', function () use ($socket) {
387+
$socket->close();
388+
});
350389
$this->server->listen($socket);
351390
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);
352391

@@ -362,6 +401,9 @@ public function testConnectionInvalidAuthenticatorReturnsFalse()
362401
});
363402

364403
$socket = new SocketServer('127.0.0.1:0');
404+
$socket->on('connection', function () use ($socket) {
405+
$socket->close();
406+
});
365407
$this->server->listen($socket);
366408
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);
367409

@@ -377,6 +419,9 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal
377419
});
378420

379421
$socket = new SocketServer('127.0.0.1:0');
422+
$socket->on('connection', function () use ($socket) {
423+
$socket->close();
424+
});
380425
$this->server->listen($socket);
381426
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);
382427

@@ -388,10 +433,13 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal
388433
public function testConnectionInvalidAuthenticatorReturnsPromiseRejected()
389434
{
390435
$this->server = new Server(null, null, function () {
391-
return \React\Promise\reject();
436+
return \React\Promise\reject(new \RuntimeException());
392437
});
393438

394439
$socket = new SocketServer('127.0.0.1:0');
440+
$socket->on('connection', function () use ($socket) {
441+
$socket->close();
442+
});
395443
$this->server->listen($socket);
396444
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);
397445

@@ -457,7 +505,12 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateFailsWit
457505

458506
$ssl = new SecureConnector($this->client, null, array('verify_peer' => true));
459507

460-
$this->assertRejectPromise($ssl->connect($socket->getAddress()));
508+
$promise = $ssl->connect($socket->getAddress());
509+
$promise->then(null, function () use ($socket) {
510+
$socket->close();
511+
});
512+
513+
$this->assertRejectPromise($promise);
461514
}
462515

463516
public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWithoutVerifyPeer()
@@ -480,6 +533,8 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWit
480533

481534
$this->assertResolveStream($ssl->connect($socket->getAddress()));
482535
$this->assertResolveStream($promise);
536+
537+
$socket->close();
483538
}
484539

485540
/** @group internet */
@@ -511,7 +566,7 @@ private function assertResolveStream($promise)
511566
$stream->close();
512567
});
513568

514-
Block\await($promise, Loop::get(), 2.0);
569+
\React\Async\await($promise);
515570
}
516571

517572
private function assertRejectPromise($promise, $message = null, $code = null)
@@ -528,6 +583,6 @@ private function assertRejectPromise($promise, $message = null, $code = null)
528583
$this->setExpectedException('Exception', $message, $code);
529584
}
530585

531-
Block\await($promise, Loop::get(), 2.0);
586+
\React\Async\await($promise);
532587
}
533588
}

0 commit comments

Comments
 (0)