forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_connect.cpp
More file actions
93 lines (78 loc) · 2.69 KB
/
Copy pathtest_connect.cpp
File metadata and controls
93 lines (78 loc) · 2.69 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <thread>
#include <atomic>
#include <Poco/Net/StreamSocket.h>
#include <Common/Exception.h>
#include <Common/ShellCommand.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/copyData.h>
/** In a loop it connects to the server and immediately breaks the connection.
* Using the SO_LINGER option, we ensure that the connection is terminated by sending a RST packet (not FIN).
* Long time ago this behavior caused a bug in the TCPServer implementation in the Poco library.
*/
int main(int argc, char ** argv)
try
{
using namespace DB;
size_t num_iterations = 1;
size_t num_threads = 1;
std::string host = "localhost";
uint16_t port = 9000;
if (argc >= 2)
num_iterations = parse<size_t>(argv[1]);
if (argc >= 3)
num_threads = parse<size_t>(argv[2]);
if (argc >= 4)
host = argv[3];
if (argc >= 5)
port = parse<uint16_t>(argv[4]);
WriteBufferFromFileDescriptor out(STDERR_FILENO);
std::atomic_bool cancel{false};
std::vector<std::thread> threads(num_threads);
for (auto & thread : threads)
{
thread = std::thread([&]
{
for (size_t i = 0; i < num_iterations && !cancel.load(std::memory_order_relaxed); ++i)
{
std::cerr << ".";
try
{
Poco::Net::SocketAddress address(host, port);
Poco::Net::StreamSocket socket;
//socket.setLinger(1, 0);
socket.connectNB(address);
if (!socket.poll(Poco::Timespan(1000000),
Poco::Net::Socket::SELECT_READ | Poco::Net::Socket::SELECT_WRITE | Poco::Net::Socket::SELECT_ERROR))
{
/// Allow to debug the server.
/* auto command = ShellCommand::execute("kill -STOP $(pidof clickhouse-server)");
copyData(command->err, out);
copyData(command->out, out);
command->wait();*/
std::cerr << "Timeout\n";
/* cancel = true;
break;*/
}
}
catch (const Poco::Exception & e)
{
std::cerr << e.displayText() << "\n";
cancel = true;
break;
}
}
});
}
for (auto & thread : threads)
thread.join();
std::cerr << "\n";
}
catch (const Poco::Exception & e)
{
std::cerr << e.displayText() << "\n";
}