Skip to content

Commit d50928e

Browse files
authored
Throws TimeoutException when TransferTimeout is exceeded (#382)
1 parent 370e7a1 commit d50928e

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

src/Connection/Http1Connection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ private function readResponse(
481481
}
482482

483483
$originalCancellation->throwIfRequested();
484+
$readingCancellation->throwIfRequested();
484485

485486
throw new SocketException(\sprintf(
486487
"Receiving the response headers for '%s' failed, because the socket to '%s' @ '%s' closed early with %d bytes received within %0.3f seconds",
@@ -499,11 +500,11 @@ private function readResponse(
499500
// Throw original cancellation if it was requested.
500501
$originalCancellation->throwIfRequested();
501502

502-
throw new TimeoutException(
503-
'Inactivity timeout exceeded, more than ' . $timeout . ' seconds elapsed from last data received',
504-
0,
505-
$e
506-
);
503+
if ($readingCancellation->isRequested()) {
504+
throw new TimeoutException('Allowed transfer timeout exceeded, took longer than ' . $request->getTransferTimeout() . ' s', 0, $e);
505+
}
506+
507+
throw new TimeoutException('Inactivity timeout exceeded, more than ' . $timeout . ' seconds elapsed from last data received', 0, $e);
507508
} catch (\Throwable $e) {
508509
$this->close();
509510
throw new SocketException('Receiving the response headers failed: ' . $e->getMessage(), 0, $e);

test/TimeoutTest.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testTimeoutDuringBody(): void
6060

6161
public function testTimeoutDuringConnect(): void
6262
{
63-
$this->setTimeout(0.6);
63+
$this->setTimeout(1);
6464

6565
$connector = $this->createMock(Socket\SocketConnector::class);
6666
$connector->method('connect')
@@ -99,7 +99,7 @@ public function testTimeoutDuringTlsEnable(): void
9999
}
100100
});
101101

102-
$this->setTimeout(0.6);
102+
$this->setTimeout(1);
103103

104104
try {
105105
$uri = "https://" . $server->getAddress() . "/";
@@ -223,7 +223,7 @@ public function testTimeoutDuringTlsEnableInterceptor(): void
223223
}
224224
});
225225

226-
$this->setTimeout(0.6);
226+
$this->setTimeout(1);
227227

228228
try {
229229
$uri = "https://" . $server->getAddress() . "/";
@@ -275,4 +275,46 @@ public function testTimeoutDuringInactivity(): void
275275
$server->close();
276276
}
277277
}
278+
279+
public function testTransferTimeoutDuringRequest(): void
280+
{
281+
$server = listen("tcp://127.0.0.1:0");
282+
283+
$this->setTimeout(1);
284+
285+
try {
286+
$uri = "http://" . $server->getAddress() . "/";
287+
288+
$request = new Request($uri);
289+
$request->setTransferTimeout(0.1);
290+
291+
$this->expectException(TimeoutException::class);
292+
$this->expectExceptionMessage("Allowed transfer timeout exceeded, took longer than 0.1 s");
293+
294+
$this->client->request($request);
295+
} finally {
296+
$server->close();
297+
}
298+
}
299+
300+
public function testInactivityTimeoutDuringRequest(): void
301+
{
302+
$server = listen("tcp://127.0.0.1:0");
303+
304+
$this->setTimeout(1);
305+
306+
try {
307+
$uri = "http://" . $server->getAddress() . "/";
308+
309+
$request = new Request($uri);
310+
$request->setInactivityTimeout(0.1);
311+
312+
$this->expectException(TimeoutException::class);
313+
$this->expectExceptionMessage("Inactivity timeout exceeded, more than 0.1 seconds elapsed from last data received");
314+
315+
$this->client->request($request);
316+
} finally {
317+
$server->close();
318+
}
319+
}
278320
}

0 commit comments

Comments
 (0)