integrated auto_reset_event into test utils
This commit is contained in:
parent
2022f6f75b
commit
7bb7afa227
|
|
@ -7,6 +7,7 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <condition_variable>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,9 @@ add_executable(tests
|
||||||
FieldTest.cpp
|
FieldTest.cpp
|
||||||
models/recipe.hpp
|
models/recipe.hpp
|
||||||
ValueTest.cpp
|
ValueTest.cpp
|
||||||
ResultTest.cpp)
|
ResultTest.cpp
|
||||||
|
utils/auto_reset_event.hpp
|
||||||
|
utils/auto_reset_event.cpp)
|
||||||
|
|
||||||
target_link_libraries(tests PRIVATE
|
target_link_libraries(tests PRIVATE
|
||||||
Catch2::Catch2WithMain
|
Catch2::Catch2WithMain
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,12 @@
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
#include <matador/sql/connection_pool.hpp>
|
#include "matador/sql/connection_pool.hpp"
|
||||||
#include <matador/sql/noop_connection.hpp>
|
#include "matador/sql/noop_connection.hpp"
|
||||||
|
|
||||||
class AutoResetEvent
|
#include "utils/auto_reset_event.hpp"
|
||||||
{
|
|
||||||
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;
|
using namespace matador::sql;
|
||||||
|
using namespace matador::test::utils;
|
||||||
|
|
||||||
TEST_CASE("Create connection pool", "[connection pool]") {
|
TEST_CASE("Create connection pool", "[connection pool]") {
|
||||||
using pool_t = connection_pool<noop_connection>;
|
using pool_t = connection_pool<noop_connection>;
|
||||||
|
|
@ -145,31 +120,31 @@ TEST_CASE("Try acquire connection", "[connection pool][try acquire]") {
|
||||||
|
|
||||||
pool.release(ptr2);
|
pool.release(ptr2);
|
||||||
|
|
||||||
AutoResetEvent reset_event1;
|
auto_reset_event reset_main_event;
|
||||||
AutoResetEvent reset_event2;
|
auto_reset_event reset_thread_event;
|
||||||
AutoResetEvent reset_event3;
|
|
||||||
|
|
||||||
std::thread t([&reset_event1, &reset_event2, &reset_event3, &pool]() {
|
std::thread t([&reset_main_event, &reset_thread_event, &pool]() {
|
||||||
auto c1 = pool.acquire();
|
auto c1 = pool.acquire();
|
||||||
REQUIRE(c1.valid());
|
REQUIRE(c1.valid());
|
||||||
REQUIRE(c1.id());
|
REQUIRE(c1.id());
|
||||||
REQUIRE(c1.id().value() > 0);
|
REQUIRE(c1.id().value() > 0);
|
||||||
|
|
||||||
reset_event1.Set();
|
reset_main_event.set();
|
||||||
|
|
||||||
|
reset_thread_event.wait_one();
|
||||||
|
|
||||||
reset_event2.WaitOne();
|
|
||||||
pool.release(c1);
|
pool.release(c1);
|
||||||
REQUIRE(!c1.valid());
|
REQUIRE(!c1.valid());
|
||||||
reset_event3.Set();
|
reset_main_event.set();
|
||||||
});
|
});
|
||||||
reset_event1.WaitOne();
|
reset_main_event.wait_one();
|
||||||
|
|
||||||
ptr2 = pool.try_acquire();
|
ptr2 = pool.try_acquire();
|
||||||
REQUIRE(!ptr2.valid());
|
REQUIRE(!ptr2.valid());
|
||||||
|
|
||||||
reset_event2.Set();
|
reset_thread_event.set();
|
||||||
|
|
||||||
reset_event3.WaitOne();
|
reset_main_event.wait_one();
|
||||||
ptr2 = pool.try_acquire();
|
ptr2 = pool.try_acquire();
|
||||||
REQUIRE(ptr2.valid());
|
REQUIRE(ptr2.valid());
|
||||||
REQUIRE(ptr2.id());
|
REQUIRE(ptr2.id());
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include "auto_reset_event.hpp"
|
||||||
|
|
||||||
|
namespace matador::test::utils {
|
||||||
|
|
||||||
|
auto_reset_event::auto_reset_event() : state(false) {}
|
||||||
|
|
||||||
|
void auto_reset_event::wait_one()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(sync);
|
||||||
|
underlying.wait(lock, [this](){return state.load();});
|
||||||
|
state = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void auto_reset_event::set()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(sync);
|
||||||
|
state = true;
|
||||||
|
underlying.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void auto_reset_event::reset()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(sync);
|
||||||
|
state = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef QUERY_AUTO_RESET_EVENT_HPP
|
||||||
|
#define QUERY_AUTO_RESET_EVENT_HPP
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace matador::test::utils {
|
||||||
|
|
||||||
|
class auto_reset_event
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
auto_reset_event();
|
||||||
|
auto_reset_event(const auto_reset_event& other) = delete;
|
||||||
|
|
||||||
|
void wait_one();
|
||||||
|
void set();
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::condition_variable underlying;
|
||||||
|
std::mutex sync;
|
||||||
|
std::atomic<bool> state;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //QUERY_AUTO_RESET_EVENT_HPP
|
||||||
Loading…
Reference in New Issue