Add connection methods is_connected() and ping_server() (#528)

* Replace connection handle method check_connection() with is_connected() and ping_server()

* When a connections is fetched from a pool perform a validity check which can be one of none, passive or ping.

* Add the methods is_connected() and ping_server() to the connection template class.

* Remove unused #include and add mising #include.

* Add tests for the connection methods is_connected() and ping_server().
This commit is contained in:
MeanSquaredError
2023-09-20 07:38:16 +03:00
committed by GitHub
parent 8d92e7bb81
commit 623b5154d0
15 changed files with 292 additions and 35 deletions
+25 -6
View File
@@ -116,6 +116,24 @@ namespace sqlpp
}
}
template <typename Pool>
void test_conn_check(Pool& pool)
{
auto check_db = [] (typename Pool::_pooled_connection_t db) {
if (db.is_connected() == false)
{
throw std::runtime_error{"is_connected() returned false"};
}
if (db.ping_server() == false)
{
throw std::runtime_error{"ping_server() returned false"};
}
};
check_db(pool.get(connection_check::none));
check_db(pool.get(connection_check::passive));
check_db(pool.get(connection_check::ping));
}
template <typename Pool>
void test_basic(Pool& pool, const std::string& create_table)
{
@@ -250,15 +268,16 @@ namespace sqlpp
void test_connection_pool (typename Pool::_config_ptr_t config, const std::string& create_table, bool test_mt)
{
auto pool = Pool {config, 5};
sqlpp::test::test_conn_move(pool);
sqlpp::test::test_basic(pool, create_table);
sqlpp::test::test_single_connection(pool);
sqlpp::test::test_multiple_connections(pool);
test_conn_move(pool);
test_basic(pool, create_table);
test_conn_check(pool);
test_single_connection(pool);
test_multiple_connections(pool);
if (test_mt)
{
sqlpp::test::test_multithreaded(pool);
test_multithreaded(pool);
}
sqlpp::test::test_destruction_order<Pool>(config);
test_destruction_order<Pool>(config);
}
} // namespace test
} // namespace sqlpp
+69
View File
@@ -0,0 +1,69 @@
#pragma once
/*
Copyright (c) 2023, Vesselin Atanasov
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdexcept>
namespace sqlpp
{
namespace test
{
namespace
{
template <typename Connection>
void test_conn_empty()
{
Connection db;
if (db.is_connected()) {
throw std::runtime_error{"Unexpected is_connected() == true"};
}
if (db.ping_server()) {
throw std::runtime_error{"Unexpected ping_server() == true"};
}
}
template <typename Connection, typename ConfigPtr>
void test_conn_connected(const ConfigPtr& connection_config)
{
Connection db{connection_config};
if (db.is_connected() == false) {
throw std::runtime_error{"Unexpected is_connected() == false"};
}
if (db.ping_server() == false) {
throw std::runtime_error{"Unexpected ping_server() == false"};
}
}
}
template <typename Connection, typename ConfigPtr>
void test_normal_connection(const ConfigPtr& connection_config)
{
test_conn_empty<Connection>();
test_conn_connected<Connection>(connection_config);
}
} // namespace test
} // namespace sqlpp
+1
View File
@@ -40,6 +40,7 @@ set(test_files
Truncated.cpp
Update.cpp
Remove.cpp
Connection.cpp
ConnectionPool.cpp
)
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023, Vesselin Atanasov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../../include/ConnectionTests.h"
#include "make_test_connection.h"
#include <sqlpp11/mysql/connection.h>
#include <sqlpp11/sqlpp11.h>
int Connection(int, char*[])
{
namespace sql = sqlpp::mysql;
namespace test = sqlpp::test;
test::test_normal_connection<sql::connection>(sql::make_test_config());
return 0;
}
+1 -1
View File
@@ -30,8 +30,8 @@ set(test_files
Basic.cpp
BasicConstConfig.cpp
Blob.cpp
Connection.cpp
ConnectionPool.cpp
Constructor.cpp
Date.cpp
DateTime.cpp
Exceptions.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2017, Serge Robyns
* Copyright (c) 2023, Vesselin Atanasov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@@ -23,13 +24,17 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../../include/ConnectionTests.h"
#include "make_test_connection.h"
#include <sqlpp11/postgresql/connection.h>
#include <sqlpp11/sqlpp11.h>
namespace sql = sqlpp::postgresql;
int Constructor(int, char*[])
int Connection(int, char*[])
{
sql::connection db;
namespace sql = sqlpp::postgresql;
namespace test = sqlpp::test;
test::test_normal_connection<sql::connection>(sql::make_test_config());
return 0;
}
+1
View File
@@ -38,6 +38,7 @@ set(test_files
FloatingPoint.cpp
Integral.cpp
Blob.cpp
Connection.cpp
ConnectionPool.cpp
)
+43
View File
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2023, Vesselin Atanasov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../../include/ConnectionTests.h"
#include <sqlpp11/sqlite3/connection.h>
#include <sqlpp11/sqlpp11.h>
int Connection(int, char*[])
{
namespace sql = sqlpp::sqlite3;
namespace test = sqlpp::test;
auto config = std::make_shared<sql::connection_config>();
config->path_to_database = ":memory:";
config->flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
config->debug = true;
test::test_normal_connection<sql::connection>(config);
return 0;
}