From c12c1ec61663fc9bd460fea698361b41afb6e9d1 Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Sat, 2 Mar 2024 00:23:04 +0100 Subject: [PATCH] Add example for size-limited connection pools, see #72 --- src/test/pq/connection_pool.cpp | 73 +++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/src/test/pq/connection_pool.cpp b/src/test/pq/connection_pool.cpp index d2a47ae..028ed44 100644 --- a/src/test/pq/connection_pool.cpp +++ b/src/test/pq/connection_pool.cpp @@ -7,25 +7,80 @@ #include +class limited_connection_pool + : public tao::pq::connection_pool +{ + using tao::pq::connection_pool::connection_pool; + + [[nodiscard]] auto v_create() const -> std::unique_ptr< tao::pq::connection > override + { + if( attached() >= 4 ) { + throw std::runtime_error( "connection limit reached" ); + } + return connection_pool::v_create(); + } +}; + void run() { // overwrite the default with an environment variable if needed const auto connection_string = tao::pq::internal::getenv( "TAOPQ_TEST_DATABASE", "dbname=template1" ); - const auto pool = tao::pq::connection_pool::create( connection_string ); + const auto pool = tao::pq::connection_pool::create< limited_connection_pool >( connection_string ); + + TEST_ASSERT( pool->empty() ); + TEST_ASSERT( pool->size() == 0 ); + TEST_ASSERT( pool->attached() == 0 ); TEST_ASSERT( pool->connection() ); + + TEST_ASSERT( !pool->empty() ); + TEST_ASSERT( pool->size() == 1 ); + TEST_ASSERT( pool->attached() == 0 ); + TEST_ASSERT( pool->connection()->execute( "SELECT 1" ).as< int >() == 1 ); - const auto conn = pool->connection(); - TEST_ASSERT( pool->connection() ); - TEST_ASSERT( conn->execute( "SELECT 2" ).as< int >() == 2 ); + TEST_ASSERT( pool->size() == 1 ); + TEST_ASSERT( pool->attached() == 0 ); - const auto pool2 = tao::pq::connection_pool::create( connection_string ); - TEST_ASSERT( pool->connection()->execute( "SELECT 3" ).as< int >() == 3 ); - TEST_ASSERT( pool2->connection()->execute( "SELECT 4" ).as< int >() == 4 ); - TEST_ASSERT( conn->execute( "SELECT 5" ).as< int >() == 5 ); - TEST_ASSERT( pool2->connection()->execute( "SELECT 6" ).as< int >() == 6 ); + { + const auto conn = pool->connection(); + TEST_ASSERT( pool->connection() ); + TEST_ASSERT( conn->execute( "SELECT 2" ).as< int >() == 2 ); + + TEST_ASSERT( pool->size() == 1 ); + TEST_ASSERT( pool->attached() == 1 ); + + const auto pool2 = tao::pq::connection_pool::create( connection_string ); + TEST_ASSERT( pool->connection()->execute( "SELECT 3" ).as< int >() == 3 ); + TEST_ASSERT( pool2->connection()->execute( "SELECT 4" ).as< int >() == 4 ); + TEST_ASSERT( conn->execute( "SELECT 5" ).as< int >() == 5 ); + TEST_ASSERT( pool2->connection()->execute( "SELECT 6" ).as< int >() == 6 ); + } + + TEST_ASSERT( pool->size() == 2 ); + TEST_ASSERT( pool->attached() == 0 ); + { + const auto c0 = pool->connection(); + const auto c1 = pool->connection(); + TEST_ASSERT( pool->size() == 0 ); + TEST_ASSERT( pool->attached() == 2 ); + { + const auto c2 = pool->connection(); + const auto c3 = pool->connection(); + TEST_ASSERT( pool->size() == 0 ); + TEST_ASSERT( pool->attached() == 4 ); + + TEST_THROWS( pool->connection() ); + + TEST_ASSERT( pool->size() == 0 ); + TEST_ASSERT( pool->attached() == 4 ); + } + TEST_ASSERT( pool->size() == 2 ); + TEST_ASSERT( pool->attached() == 2 ); + } + TEST_ASSERT( pool->size() == 4 ); + TEST_ASSERT( pool->attached() == 0 ); } auto main() -> int // NOLINT(bugprone-exception-escape)