Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed May 2, 2024
1 parent 91bae3b commit 7b1adb8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/lib/pq/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,18 @@ namespace tao::pq
auto connection::attempt_rollback() const noexcept -> bool
{
switch( transaction_status() ) {
// LCOV_EXCL_START
case transaction_status::idle:
case transaction_status::active:
return false;
// LCOV_EXCL_STOP

case transaction_status::in_transaction:
case transaction_status::error:
case transaction_status::unknown:
return true;
}
TAO_PQ_UNREACHABLE;
TAO_PQ_UNREACHABLE; // LCOV_EXCL_LINE
}

void connection::check_prepared_name( const std::string_view name )
Expand Down Expand Up @@ -237,6 +239,7 @@ namespace tao::pq
get_notifications();
return;

// LCOV_EXCL_START
case poll::status::writable:
return;

Expand All @@ -245,6 +248,7 @@ namespace tao::pq

default:
TAO_PQ_UNREACHABLE;
// LCOV_EXCL_STOP
}
}
}
Expand Down
26 changes: 22 additions & 4 deletions src/test/pq/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@

#include <tao/pq/connection.hpp>

tao::pq::poll::status my_poll( const int /*unused*/, const bool /*unused*/, const int /*unused*/ )
{
TAO_PQ_UNREACHABLE;
}

void run()
{
using namespace std::chrono_literals;

// overwrite the default with an environment variable if needed
const auto connection_string = tao::pq::internal::getenv( "TAOPQ_TEST_DATABASE", "dbname=template1" ); // NOLINT(clang-analyzer-deadcode.DeadStores)

Expand All @@ -22,7 +29,7 @@ void run()

// open a connection
const auto connection = tao::pq::connection::create( connection_string );
connection->set_timeout( std::chrono::seconds( 1 ) );
connection->set_timeout( 1s );

// open a second, independent connection (and discard it immediately)
std::ignore = tao::pq::connection::create( connection_string );
Expand Down Expand Up @@ -120,12 +127,23 @@ void run()
TEST_THROWS( connection->execute( "SELECT $1", "\\xa" ).as< tao::pq::binary >() );
TEST_THROWS( connection->execute( "SELECT $1", "\\xa." ).as< tao::pq::binary >() );

{
using callback_t = tao::pq::poll::status ( * )( int, bool, int );

const auto old_cb = *connection->poll_callback().target< callback_t >();
TEST_ASSERT( old_cb != nullptr );
TEST_ASSERT( *connection->poll_callback().target< callback_t >() != &my_poll );
connection->set_poll_callback( my_poll );
TEST_ASSERT( *connection->poll_callback().target< callback_t >() == &my_poll );
connection->reset_poll_callback();
TEST_ASSERT( *connection->poll_callback().target< callback_t >() == old_cb );
}

connection->reset_timeout();
TEST_EXECUTE( connection->execute( "SELECT pg_sleep( .5 )" ) );
TEST_EXECUTE( connection->execute( "SELECT pg_sleep( 0.2 )" ) );

using namespace std::chrono_literals;
connection->set_timeout( 100ms );
TEST_THROWS( connection->execute( "SELECT pg_sleep( .5 )" ) );
TEST_THROWS( connection->execute( "SELECT pg_sleep( .2 )" ) );
}

auto main() -> int // NOLINT(bugprone-exception-escape)
Expand Down
27 changes: 27 additions & 0 deletions src/test/pq/connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class limited_connection_pool
}
};

tao::pq::poll::status my_poll( const int /*unused*/, const bool /*unused*/, const int /*unused*/ )
{
TAO_PQ_UNREACHABLE;
}

void run()
{
// overwrite the default with an environment variable if needed
Expand Down Expand Up @@ -104,6 +109,28 @@ void run()
}
TEST_ASSERT( pool->size() == 4 );
TEST_ASSERT( pool->attached() == 0 );

{
using callback_t = tao::pq::poll::status ( * )( int, bool, int );

const auto old_cb = *pool->poll_callback().target< callback_t >();
TEST_ASSERT( old_cb != nullptr );
TEST_ASSERT( *pool->poll_callback().target< callback_t >() != &my_poll );
TEST_ASSERT( *pool->connection()->poll_callback().target< callback_t >() != &my_poll );
pool->set_poll_callback( my_poll );
TEST_ASSERT( *pool->poll_callback().target< callback_t >() == &my_poll );
TEST_ASSERT( *pool->connection()->poll_callback().target< callback_t >() == &my_poll );
pool->reset_poll_callback();
TEST_ASSERT( *pool->poll_callback().target< callback_t >() == old_cb );
TEST_ASSERT( *pool->connection()->poll_callback().target< callback_t >() == old_cb );
}

using namespace std::chrono_literals;
pool->set_timeout( 100ms );
TEST_THROWS( pool->execute( "SELECT pg_sleep( .5 )" ) );

pool->reset_timeout();
TEST_EXECUTE( pool->execute( "SELECT pg_sleep( .5 )" ) );
}

auto main() -> int // NOLINT(bugprone-exception-escape)
Expand Down
14 changes: 14 additions & 0 deletions src/test/pq/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ void run()
std::cout << row[ "name" ].as< std::string >() << " is "
<< row[ "age" ].as< unsigned >() << " years old.\n";
}

{
tao::pq::parameter< 1 > p;
p.bind( 1 );
TEST_THROWS( p.bind( 2 ) );
}

{
tao::pq::parameter< 1 > p;
tao::pq::parameter< 1 > p2;
p.bind( 1 );
p2.bind( 1 );
TEST_THROWS( p.bind( p2 ) );
}
}

auto main() -> int // NOLINT(bugprone-exception-escape)
Expand Down

0 comments on commit 7b1adb8

Please sign in to comment.