1
1
#include < clickhouse/client.h>
2
2
3
+ #include " clickhouse/base/socket.h"
3
4
#include " readonly_client_test.h"
4
5
#include " connection_failed_client_test.h"
6
+ #include " ut/utils_comparison.h"
5
7
#include " utils.h"
8
+ #include " ut/roundtrip_column.h"
9
+ #include " ut/value_generators.h"
6
10
7
11
#include < gtest/gtest.h>
8
12
13
+ #include < memory>
9
14
#include < optional>
15
+ #include < string_view>
10
16
#include < thread>
11
17
#include < chrono>
12
18
13
19
using namespace clickhouse ;
14
20
21
+
22
+ template <typename T>
23
+ std::shared_ptr<T> createTableWithOneColumn (Client & client, const std::string & table_name, const std::string & column_name)
24
+ {
25
+ auto col = std::make_shared<T>();
26
+ const auto type_name = col->GetType ().GetName ();
27
+
28
+ client.Execute (" DROP TEMPORARY TABLE IF EXISTS " + table_name + " ;" );
29
+ client.Execute (" CREATE TEMPORARY TABLE IF NOT EXISTS " + table_name + " ( " + column_name + " " + type_name + " )" );
30
+
31
+ return col;
32
+ }
33
+
15
34
// Use value-parameterized tests to run same tests with different client
16
35
// options.
17
36
class ClientCase : public testing ::TestWithParam<ClientOptions> {
@@ -27,13 +46,9 @@ class ClientCase : public testing::TestWithParam<ClientOptions> {
27
46
template <typename T>
28
47
std::shared_ptr<T> createTableWithOneColumn (Block & block)
29
48
{
30
- auto col = std::make_shared<T>();
31
- const auto type_name = col->GetType ().GetName ();
32
-
33
- client_->Execute (" DROP TEMPORARY TABLE IF EXISTS " + table_name + " ;" );
34
- client_->Execute (" CREATE TEMPORARY TABLE IF NOT EXISTS " + table_name + " ( " + column_name + " " + type_name + " )" );
49
+ auto col = ::createTableWithOneColumn<T>(*client_, table_name, column_name);
35
50
36
- block.AppendColumn (" test_column " , col);
51
+ block.AppendColumn (column_name , col);
37
52
38
53
return col;
39
54
}
@@ -1352,3 +1367,60 @@ INSTANTIATE_TEST_SUITE_P(ResetConnectionClientTest, ResetConnectionTestCase,
1352
1367
.SetRetryTimeout (std::chrono::seconds (1 ))
1353
1368
}
1354
1369
));
1370
+
1371
+ struct CountingSocketFactoryAdapter : public SocketFactory {
1372
+ struct Counters
1373
+ {
1374
+ size_t connect_count = 0 ;
1375
+
1376
+ void Reset () {
1377
+ *this = Counters ();
1378
+ }
1379
+ };
1380
+
1381
+ SocketFactory & socket_factory;
1382
+ Counters& counters;
1383
+
1384
+ CountingSocketFactoryAdapter (SocketFactory & socket_factory, Counters& counters)
1385
+ : socket_factory(socket_factory)
1386
+ , counters(counters)
1387
+ {}
1388
+
1389
+ std::unique_ptr<SocketBase> connect (const ClientOptions& opts, const Endpoint& endpoint) {
1390
+ ++counters.connect_count ;
1391
+ return socket_factory.connect (opts, endpoint);
1392
+ }
1393
+
1394
+ void sleepFor (const std::chrono::milliseconds& duration) {
1395
+ return socket_factory.sleepFor (duration);
1396
+ }
1397
+
1398
+ };
1399
+
1400
+ TEST (SimpleClientTest, issue_335) {
1401
+ auto vals = MakeStrings ();
1402
+ auto col = std::make_shared<ColumnString>(vals);
1403
+
1404
+ CountingSocketFactoryAdapter::Counters counters;
1405
+ std::unique_ptr<SocketFactory> wrapped_socket_factory = std::make_unique<NonSecureSocketFactory>();
1406
+ std::unique_ptr<SocketFactory> socket_factory = std::make_unique<CountingSocketFactoryAdapter>(*wrapped_socket_factory, counters);
1407
+
1408
+ Client client (ClientOptions (LocalHostEndpoint)
1409
+ .SetSendRetries (0 ), // <<=== crucial for reproducing https://github.com/ClickHouse/clickhouse-cpp/issues/335
1410
+ std::move (socket_factory));
1411
+
1412
+ EXPECT_EQ (1u , counters.connect_count );
1413
+ EXPECT_TRUE (CompareRecursive (vals, *RoundtripColumnValuesTyped (client, col)));
1414
+
1415
+ counters.Reset ();
1416
+
1417
+ client.ResetConnection ();
1418
+ EXPECT_EQ (1u , counters.connect_count );
1419
+ EXPECT_TRUE (CompareRecursive (vals, *RoundtripColumnValuesTyped (client, col)));
1420
+
1421
+ counters.Reset ();
1422
+
1423
+ client.ResetConnectionEndpoint ();
1424
+ EXPECT_EQ (1u , counters.connect_count );
1425
+ EXPECT_TRUE (CompareRecursive (vals, *RoundtripColumnValuesTyped (client, col)));
1426
+ }
0 commit comments