Compare commits

...

2 Commits

Author SHA1 Message Date
Sascha Kuehl 7bb7afa227 integrated auto_reset_event into test utils 2024-04-14 11:41:36 +02:00
Sascha Kuehl 2022f6f75b updated catch2 to 3.5.4 2024-04-14 11:41:09 +02:00
8 changed files with 77 additions and 44 deletions

View File

@ -3,7 +3,7 @@ Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
GIT_TAG v3.5.4 # or a later release
)
FetchContent_MakeAvailable(Catch2)

View File

@ -3,7 +3,7 @@ Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
GIT_TAG v3.5.4 # or a later release
)
FetchContent_MakeAvailable(Catch2)

View File

@ -3,7 +3,7 @@ Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
GIT_TAG v3.5.4 # or a later release
)
FetchContent_MakeAvailable(Catch2)

View File

@ -7,6 +7,7 @@
#include <mutex>
#include <string>
#include <optional>
#include <condition_variable>
#include <thread>
#include <unordered_map>

View File

@ -3,7 +3,7 @@ Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # or a later release
GIT_TAG v3.5.4 # or a later release
)
FetchContent_MakeAvailable(Catch2)
@ -40,7 +40,9 @@ add_executable(tests
FieldTest.cpp
models/recipe.hpp
ValueTest.cpp
ResultTest.cpp)
ResultTest.cpp
utils/auto_reset_event.hpp
utils/auto_reset_event.cpp)
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain

View File

@ -1,37 +1,12 @@
#include <catch2/catch_test_macros.hpp>
#include <matador/sql/connection_pool.hpp>
#include <matador/sql/noop_connection.hpp>
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/noop_connection.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;
};
#include "utils/auto_reset_event.hpp"
using namespace matador::sql;
using namespace matador::test::utils;
TEST_CASE("Create connection pool", "[connection pool]") {
using pool_t = connection_pool<noop_connection>;
@ -145,31 +120,31 @@ TEST_CASE("Try acquire connection", "[connection pool][try acquire]") {
pool.release(ptr2);
AutoResetEvent reset_event1;
AutoResetEvent reset_event2;
AutoResetEvent reset_event3;
auto_reset_event reset_main_event;
auto_reset_event reset_thread_event;
std::thread t([&reset_event1, &reset_event2, &reset_event3, &pool]() {
std::thread t([&reset_main_event, &reset_thread_event, &pool]() {
auto c1 = pool.acquire();
REQUIRE(c1.valid());
REQUIRE(c1.id());
REQUIRE(c1.id().value() > 0);
reset_event1.Set();
reset_main_event.set();
reset_thread_event.wait_one();
reset_event2.WaitOne();
pool.release(c1);
REQUIRE(!c1.valid());
reset_event3.Set();
reset_main_event.set();
});
reset_event1.WaitOne();
reset_main_event.wait_one();
ptr2 = pool.try_acquire();
REQUIRE(!ptr2.valid());
reset_event2.Set();
reset_thread_event.set();
reset_event3.WaitOne();
reset_main_event.wait_one();
ptr2 = pool.try_acquire();
REQUIRE(ptr2.valid());
REQUIRE(ptr2.id());

View File

@ -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;
}
}

View File

@ -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