Skip to content

Commit

Permalink
Add example for size-limited connection pools, see #72
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Mar 1, 2024
1 parent a74fb14 commit c12c1ec
Showing 1 changed file with 64 additions and 9 deletions.
73 changes: 64 additions & 9 deletions src/test/pq/connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,80 @@

#include <tao/pq/connection_pool.hpp>

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)
Expand Down

0 comments on commit c12c1ec

Please sign in to comment.