Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::testing::mock_connection_pool Class Reference

Mock connection pool for testing pool-related functionality. More...

#include <mock_connection_pool.h>

Collaboration diagram for database::testing::mock_connection_pool:
Collaboration graph

Classes

struct  config
 
struct  stats
 Get current pool statistics. More...
 

Public Member Functions

 mock_connection_pool (const config &cfg={})
 
 ~mock_connection_pool ()
 
mock_databaseacquire ()
 Acquire a connection from the pool.
 
mock_databaseacquire (std::chrono::milliseconds timeout)
 Acquire a connection with timeout.
 
void release (mock_database *conn)
 Release a connection back to the pool.
 
stats get_stats () const
 
void simulate_exhaustion ()
 Simulate pool exhaustion.
 
void reset ()
 Reset pool to normal operation.
 
void configure_all (std::function< void(mock_database &)> configurator)
 Configure all connections with same expectation.
 

Private Member Functions

void create_connection ()
 

Private Attributes

config config_
 
std::vector< std::unique_ptr< mock_database > > connections_
 
std::queue< mock_database * > available_
 
std::set< mock_database * > in_use_
 
std::mutex mutex_
 
std::condition_variable cv_
 
std::atomic< bool > exhausted_ {false}
 
std::atomic< size_t > total_acquisitions_ {0}
 
std::atomic< size_t > total_releases_ {0}
 
std::atomic< size_t > acquisition_timeouts_ {0}
 

Detailed Description

Mock connection pool for testing pool-related functionality.

Features:

  • Configurable pool size
  • Simulated connection acquisition delays
  • Connection leak detection
  • Pool exhaustion simulation

Definition at line 28 of file mock_connection_pool.h.

Constructor & Destructor Documentation

◆ mock_connection_pool()

database::testing::mock_connection_pool::mock_connection_pool ( const config & cfg = {})
inlineexplicit

◆ ~mock_connection_pool()

database::testing::mock_connection_pool::~mock_connection_pool ( )
inlinedefault

Member Function Documentation

◆ acquire() [1/2]

mock_database * database::testing::mock_connection_pool::acquire ( )
inline

Acquire a connection from the pool.

Returns
Pointer to mock_database, or nullptr if pool is exhausted

Definition at line 164 of file mock_connection_pool.h.

164 {
166}
mock_database * acquire()
Acquire a connection from the pool.

References acquire(), database::testing::mock_connection_pool::config::acquire_timeout, and config_.

Referenced by acquire().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ acquire() [2/2]

mock_database * database::testing::mock_connection_pool::acquire ( std::chrono::milliseconds timeout)
inline

Acquire a connection with timeout.

Parameters
timeoutMaximum time to wait
Returns
Pointer to mock_database, or nullptr if timeout

Definition at line 168 of file mock_connection_pool.h.

168 {
169 std::unique_lock<std::mutex> lock(mutex_);
170
171 if (exhausted_) {
173 return nullptr;
174 }
175
176 // Wait for available connection or create new one
177 auto deadline = std::chrono::steady_clock::now() + timeout;
178 while (available_.empty()) {
179 if (connections_.size() < config_.max_size) {
181 break;
182 }
183
184 if (cv_.wait_until(lock, deadline) == std::cv_status::timeout) {
186 return nullptr;
187 }
188
189 if (exhausted_) {
191 return nullptr;
192 }
193 }
194
195 if (available_.empty()) {
197 return nullptr;
198 }
199
200 auto* conn = available_.front();
201 available_.pop();
202 in_use_.insert(conn);
204
205 // Simulate slow acquisition if configured
207 lock.unlock();
208 std::this_thread::sleep_for(config_.slow_acquire_delay);
209 }
210
211 return conn;
212}
std::queue< mock_database * > available_
std::vector< std::unique_ptr< mock_database > > connections_

References acquisition_timeouts_, available_, config_, connections_, create_connection(), cv_, exhausted_, in_use_, database::testing::mock_connection_pool::config::max_size, mutex_, database::testing::mock_connection_pool::config::simulate_slow_acquire, database::testing::mock_connection_pool::config::slow_acquire_delay, database::timeout, and total_acquisitions_.

Here is the call graph for this function:

◆ configure_all()

void database::testing::mock_connection_pool::configure_all ( std::function< void(mock_database &)> configurator)
inline

Configure all connections with same expectation.

Definition at line 250 of file mock_connection_pool.h.

250 {
251 std::lock_guard<std::mutex> lock(mutex_);
252 for (auto& conn : connections_) {
253 configurator(*conn);
254 }
255}

References connections_, and mutex_.

◆ create_connection()

void database::testing::mock_connection_pool::create_connection ( )
inlineprivate

Definition at line 158 of file mock_connection_pool.h.

158 {
159 auto conn = std::make_unique<mock_database>();
160 available_.push(conn.get());
161 connections_.push_back(std::move(conn));
162}

References available_, and connections_.

Referenced by acquire(), and mock_connection_pool().

Here is the caller graph for this function:

◆ get_stats()

mock_connection_pool::stats database::testing::mock_connection_pool::get_stats ( ) const
inline

Definition at line 223 of file mock_connection_pool.h.

223 {
224 std::lock_guard<std::mutex> lock(mutex_);
225 return {
226 connections_.size(),
227 available_.size(),
228 in_use_.size(),
229 total_acquisitions_.load(),
230 total_releases_.load(),
233 };
234}

References acquisition_timeouts_, available_, connections_, in_use_, mutex_, total_acquisitions_, and total_releases_.

◆ release()

void database::testing::mock_connection_pool::release ( mock_database * conn)
inline

Release a connection back to the pool.

Parameters
connConnection to release

Definition at line 214 of file mock_connection_pool.h.

214 {
215 std::lock_guard<std::mutex> lock(mutex_);
216 if (in_use_.erase(conn)) {
217 available_.push(conn);
219 cv_.notify_one();
220 }
221}

References available_, cv_, in_use_, mutex_, and total_releases_.

Referenced by database::testing::scoped_connection::~scoped_connection().

Here is the caller graph for this function:

◆ reset()

void database::testing::mock_connection_pool::reset ( )
inline

Reset pool to normal operation.

Definition at line 240 of file mock_connection_pool.h.

240 {
241 std::lock_guard<std::mutex> lock(mutex_);
242 exhausted_ = false;
243 // Return all in-use to available
244 for (auto* conn : in_use_) {
245 available_.push(conn);
246 }
247 in_use_.clear();
248}

References available_, exhausted_, in_use_, and mutex_.

◆ simulate_exhaustion()

void database::testing::mock_connection_pool::simulate_exhaustion ( )
inline

Simulate pool exhaustion.

Definition at line 236 of file mock_connection_pool.h.

236 {
237 exhausted_ = true;
238}

References exhausted_.

Member Data Documentation

◆ acquisition_timeouts_

std::atomic<size_t> database::testing::mock_connection_pool::acquisition_timeouts_ {0}
private

Definition at line 101 of file mock_connection_pool.h.

101{0};

Referenced by acquire(), and get_stats().

◆ available_

std::queue<mock_database*> database::testing::mock_connection_pool::available_
private

Definition at line 92 of file mock_connection_pool.h.

Referenced by acquire(), create_connection(), get_stats(), release(), and reset().

◆ config_

config database::testing::mock_connection_pool::config_
private

Definition at line 90 of file mock_connection_pool.h.

Referenced by acquire(), acquire(), and mock_connection_pool().

◆ connections_

std::vector<std::unique_ptr<mock_database> > database::testing::mock_connection_pool::connections_
private

Definition at line 91 of file mock_connection_pool.h.

Referenced by acquire(), configure_all(), create_connection(), and get_stats().

◆ cv_

std::condition_variable database::testing::mock_connection_pool::cv_
private

Definition at line 95 of file mock_connection_pool.h.

Referenced by acquire(), and release().

◆ exhausted_

std::atomic<bool> database::testing::mock_connection_pool::exhausted_ {false}
private

Definition at line 96 of file mock_connection_pool.h.

96{false};

Referenced by acquire(), reset(), and simulate_exhaustion().

◆ in_use_

std::set<mock_database*> database::testing::mock_connection_pool::in_use_
private

Definition at line 93 of file mock_connection_pool.h.

Referenced by acquire(), get_stats(), release(), and reset().

◆ mutex_

std::mutex database::testing::mock_connection_pool::mutex_
mutableprivate

Definition at line 94 of file mock_connection_pool.h.

Referenced by acquire(), configure_all(), get_stats(), release(), and reset().

◆ total_acquisitions_

std::atomic<size_t> database::testing::mock_connection_pool::total_acquisitions_ {0}
private

Definition at line 99 of file mock_connection_pool.h.

99{0};

Referenced by acquire(), and get_stats().

◆ total_releases_

std::atomic<size_t> database::testing::mock_connection_pool::total_releases_ {0}
private

Definition at line 100 of file mock_connection_pool.h.

100{0};

Referenced by get_stats(), and release().


The documentation for this class was generated from the following file: