-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathPingTimeoutTest.php
More file actions
61 lines (52 loc) · 1.85 KB
/
PingTimeoutTest.php
File metadata and controls
61 lines (52 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace ClickHouseDB\Tests;
use ClickHouseDB\Client;
use PHPUnit\Framework\TestCase;
/**
* Standalone tests for ping() timeout behaviour that do not require a live
* ClickHouse server.
*
* @group PingTimeoutTest
*/
class PingTimeoutTest extends TestCase
{
/**
* Verifies that setTimeout() is respected by ping().
*
* A local TCP server that accepts connections but never sends an HTTP
* response is used so that the TCP handshake succeeds (preventing the
* connect timeout from firing first) while the overall request hangs
* until CURLOPT_TIMEOUT fires.
*/
public function testQueryTimeoutIsRespectedByPing(): void
{
// Bind to an ephemeral port; the OS completes the TCP handshake for
// queued connections, but we never call stream_socket_accept(), so
// the HTTP response never arrives.
$server = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr);
$this->assertNotFalse($server, "Could not start local test server: $errstr");
$address = stream_socket_get_name($server, false);
[$host, $port] = explode(':', $address);
$config = [
'host' => $host,
'port' => (int) $port,
'username' => '',
'password' => '',
];
$start_time = microtime(true);
try {
$db = new Client($config);
// High connect timeout so it does not fire before the query timeout.
$db->setConnectTimeOut(5);
$db->setTimeout(1);
$db->ping();
} catch (\Exception $e) {
// Expected — ping() will throw when the query timeout fires.
} finally {
fclose($server);
}
$elapsed = round(microtime(true) - $start_time);
$this->assertEquals(1, $elapsed);
}
}