Skip to content

Commit df3fce0

Browse files
committed
Improve functional tests to be less fragile by waiting for remote end
1 parent b75608e commit df3fce0

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

tests/FunctionalServerTest.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,38 @@ public function testClosedStreamFromRequestHandlerWillSendEmptyBody()
472472
$socket->close();
473473
}
474474

475+
public function testRequestHandlerWillReceiveCloseEventIfConnectionClosesWhileSendingBody()
476+
{
477+
$loop = Factory::create();
478+
$connector = new Connector($loop);
479+
480+
$once = $this->expectCallableOnce();
481+
$server = new StreamingServer(function (RequestInterface $request) use ($once) {
482+
$request->getBody()->on('close', $once);
483+
});
484+
485+
$socket = new Socket(0, $loop);
486+
$server->listen($socket);
487+
488+
$connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) {
489+
$conn->write("GET / HTTP/1.0\r\nContent-Length: 100\r\n\r\n");
490+
491+
$loop->addTimer(0.001, function() use ($conn) {
492+
$conn->end();
493+
});
494+
});
495+
496+
Block\sleep(0.1, $loop);
497+
498+
$socket->close();
499+
}
500+
475501
public function testStreamFromRequestHandlerWillBeClosedIfConnectionClosesWhileSendingBody()
476502
{
477503
$loop = Factory::create();
478504
$connector = new Connector($loop);
479505

480506
$stream = new ThroughStream();
481-
$stream->on('close', $this->expectCallableOnce());
482507

483508
$server = new StreamingServer(function (RequestInterface $request) use ($stream) {
484509
return new Response(200, array(), $stream);
@@ -487,22 +512,20 @@ public function testStreamFromRequestHandlerWillBeClosedIfConnectionClosesWhileS
487512
$socket = new Socket(0, $loop);
488513
$server->listen($socket);
489514

490-
$result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) {
515+
$connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) {
491516
$conn->write("GET / HTTP/1.0\r\nContent-Length: 100\r\n\r\n");
492517

493-
$loop->addTimer(0.1, function() use ($conn) {
518+
$loop->addTimer(0.001, function() use ($conn) {
494519
$conn->end();
495520
});
496-
497-
return Stream\buffer($conn);
498521
});
499522

500-
$response = Block\await($result, $loop, 1.0);
501-
502-
$this->assertStringStartsWith("HTTP/1.0 200 OK", $response);
503-
$this->assertStringEndsWith("\r\n\r\n", $response);
523+
// stream will be closed within 0.1s
524+
$ret = Block\await(Stream\first($stream, 'close'), $loop, 0.1);
504525

505526
$socket->close();
527+
528+
$this->assertNull($ret);
506529
}
507530

508531
public function testStreamFromRequestHandlerWillBeClosedIfConnectionClosesButWillOnlyBeDetectedOnNextWrite()

0 commit comments

Comments
 (0)