Skip to content

Commit c412846

Browse files
authored
Merge pull request #290 from clue-labs/fragile
Improve test suite to be less fragile
2 parents f8f029c + 3ee71c4 commit c412846

3 files changed

Lines changed: 45 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()

tests/Io/MiddlewareRunnerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ public function provideProcessStackMiddlewares()
8080
*/
8181
public function testProcessStack(array $middlewares, $expectedCallCount)
8282
{
83+
// the ProcessStack middleware instances are stateful, so reset these
84+
// before running the test, to not fail with --repeat=100
85+
foreach ($middlewares as $middleware) {
86+
if ($middleware instanceof ProcessStack) {
87+
$middleware->reset();
88+
}
89+
}
90+
8391
$request = new ServerRequest('GET', 'https://example.com/');
8492
$middlewareStack = new MiddlewareRunner($middlewares);
8593

tests/Middleware/ProcessStack.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public function getCallCount()
2525
{
2626
return $this->callCount;
2727
}
28+
29+
public function reset()
30+
{
31+
$this->callCount = 0;
32+
}
2833
}

0 commit comments

Comments
 (0)