added multi threaded pool test
This commit is contained in:
@@ -34,8 +34,6 @@ add_executable(tests
|
||||
models/location.hpp
|
||||
models/optional.hpp
|
||||
ConvertTest.cpp
|
||||
DummyConnection.hpp
|
||||
DummyConnection.cpp
|
||||
EntityQueryBuilderTest.cpp
|
||||
models/author.hpp
|
||||
models/book.hpp
|
||||
|
||||
+103
-5
@@ -1,15 +1,42 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <matador/sql/connection_pool.hpp>
|
||||
#include <matador/sql/noop_connection.hpp>
|
||||
|
||||
#include "DummyConnection.hpp"
|
||||
class AutoResetEvent
|
||||
{
|
||||
public:
|
||||
AutoResetEvent() : state(false) {}
|
||||
AutoResetEvent(const AutoResetEvent& other) = delete;
|
||||
|
||||
void WaitOne() {
|
||||
std::unique_lock<std::mutex> lock(sync);
|
||||
underlying.wait(lock, [this](){return state.load();});
|
||||
}
|
||||
|
||||
void Set() {
|
||||
std::unique_lock<std::mutex> lock(sync);
|
||||
state = true;
|
||||
underlying.notify_all();
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
std::unique_lock<std::mutex> lock(sync);
|
||||
state = false;
|
||||
}
|
||||
|
||||
private:
|
||||
std::condition_variable underlying;
|
||||
std::mutex sync;
|
||||
std::atomic<bool> state;
|
||||
};
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Create connection pool", "[connection pool]") {
|
||||
using pool_t = connection_pool<matador::test::DummyConnection>;
|
||||
using pool_t = connection_pool<noop_connection>;
|
||||
|
||||
pool_t pool("sqlite://sqlite.db", 4);
|
||||
pool_t pool("noop://noop.db", 4);
|
||||
|
||||
REQUIRE(pool.size() == 4);
|
||||
REQUIRE(pool.idle() == 4);
|
||||
@@ -51,9 +78,9 @@ TEST_CASE("Create connection pool", "[connection pool]") {
|
||||
}
|
||||
|
||||
TEST_CASE("Acquire connection by id", "[connection pool]") {
|
||||
using pool_t = connection_pool<matador::test::DummyConnection>;
|
||||
using pool_t = connection_pool<noop_connection>;
|
||||
|
||||
pool_t pool("sqlite://sqlite.db", 4);
|
||||
pool_t pool("noop://noop.db", 4);
|
||||
|
||||
REQUIRE(pool.size() == 4);
|
||||
REQUIRE(pool.idle() == 4);
|
||||
@@ -78,4 +105,75 @@ TEST_CASE("Acquire connection by id", "[connection pool]") {
|
||||
|
||||
REQUIRE(same_ptr.valid());
|
||||
REQUIRE(same_ptr.id() == connection_id);
|
||||
}
|
||||
|
||||
TEST_CASE("Try acquire connection", "[connection pool][try acquire]") {
|
||||
using pool_t = connection_pool<noop_connection>;
|
||||
|
||||
pool_t pool("noop://noop.db", 1);
|
||||
|
||||
REQUIRE(pool.size() == 1);
|
||||
REQUIRE(pool.idle() == 1);
|
||||
REQUIRE(pool.inuse() == 0);
|
||||
|
||||
auto ptr = pool.try_acquire();
|
||||
REQUIRE(ptr.valid());
|
||||
REQUIRE(ptr.id());
|
||||
REQUIRE(ptr.id().value() > 0);
|
||||
REQUIRE(ptr->is_open());
|
||||
REQUIRE(pool.size() == 1);
|
||||
REQUIRE(pool.idle() == 0);
|
||||
REQUIRE(pool.inuse() == 1);
|
||||
|
||||
auto ptr2 = pool.try_acquire();
|
||||
REQUIRE(!ptr2.valid());
|
||||
|
||||
pool.release(ptr);
|
||||
REQUIRE(!ptr.valid());
|
||||
REQUIRE(pool.size() == 1);
|
||||
REQUIRE(pool.idle() == 1);
|
||||
REQUIRE(pool.inuse() == 0);
|
||||
|
||||
ptr2 = pool.try_acquire();
|
||||
REQUIRE(ptr2.valid());
|
||||
REQUIRE(ptr2.id());
|
||||
REQUIRE(ptr2.id().value() > 0);
|
||||
REQUIRE(ptr2->is_open());
|
||||
REQUIRE(pool.size() == 1);
|
||||
REQUIRE(pool.idle() == 0);
|
||||
REQUIRE(pool.inuse() == 1);
|
||||
|
||||
pool.release(ptr2);
|
||||
|
||||
AutoResetEvent reset_event1;
|
||||
AutoResetEvent reset_event2;
|
||||
AutoResetEvent reset_event3;
|
||||
|
||||
std::thread t([&reset_event1, &reset_event2, &reset_event3, &pool]() {
|
||||
auto c1 = pool.acquire();
|
||||
REQUIRE(c1.valid());
|
||||
REQUIRE(c1.id());
|
||||
REQUIRE(c1.id().value() > 0);
|
||||
|
||||
reset_event1.Set();
|
||||
|
||||
reset_event2.WaitOne();
|
||||
pool.release(c1);
|
||||
REQUIRE(!c1.valid());
|
||||
reset_event3.Set();
|
||||
});
|
||||
reset_event1.WaitOne();
|
||||
|
||||
ptr2 = pool.try_acquire();
|
||||
REQUIRE(!ptr2.valid());
|
||||
|
||||
reset_event2.Set();
|
||||
|
||||
reset_event3.WaitOne();
|
||||
ptr2 = pool.try_acquire();
|
||||
REQUIRE(ptr2.valid());
|
||||
REQUIRE(ptr2.id());
|
||||
REQUIRE(ptr2.id().value() > 0);
|
||||
|
||||
t.join();
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#include "DummyConnection.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
DummyConnection::DummyConnection(sql::connection_info info)
|
||||
: connection_info_(std::move(info))
|
||||
{}
|
||||
|
||||
DummyConnection::DummyConnection(const std::string &dns)
|
||||
: DummyConnection(sql::connection_info::parse(dns))
|
||||
{}
|
||||
|
||||
void DummyConnection::open()
|
||||
{
|
||||
is_open_ = true;
|
||||
}
|
||||
|
||||
void DummyConnection::close()
|
||||
{
|
||||
is_open_ = false;
|
||||
}
|
||||
|
||||
bool DummyConnection::is_open() const
|
||||
{
|
||||
return is_open_;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#ifndef QUERY_DUMMYCONNECTION_HPP
|
||||
#define QUERY_DUMMYCONNECTION_HPP
|
||||
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
class DummyConnection
|
||||
{
|
||||
public:
|
||||
explicit DummyConnection(sql::connection_info info);
|
||||
explicit DummyConnection(const std::string& dns);
|
||||
DummyConnection(const DummyConnection &x) = default;
|
||||
DummyConnection& operator=(const DummyConnection &x) = default;
|
||||
DummyConnection(DummyConnection &&x) noexcept = default;
|
||||
DummyConnection& operator=(DummyConnection &&x) noexcept = default;
|
||||
~DummyConnection() = default;
|
||||
|
||||
void open();
|
||||
void close();
|
||||
[[nodiscard]] bool is_open() const;
|
||||
|
||||
private:
|
||||
sql::connection_info connection_info_;
|
||||
|
||||
bool is_open_{};
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_DUMMYCONNECTION_HPP
|
||||
Reference in New Issue
Block a user