|
| 1 | +#include "firebird.h" |
| 2 | +#include "boost/test/unit_test.hpp" |
| 3 | +#include <atomic> |
| 4 | +#include <latch> |
| 5 | +#include <memory> |
| 6 | +#include <mutex> |
| 7 | +#include <thread> |
| 8 | +#include <vector> |
| 9 | +#include "../common/status.h" |
| 10 | +#include "../common/classes/fb_string.h" |
| 11 | +#include "../common/config/config.h" |
| 12 | +#include "../lock/lock_proto.h" |
| 13 | +#include "../jrd/lck.h" |
| 14 | + |
| 15 | +using namespace Firebird; |
| 16 | +using namespace Jrd; |
| 17 | + |
| 18 | + |
| 19 | +namespace |
| 20 | +{ |
| 21 | + class LockManagerTestCallbacks : public LockManager::Callbacks |
| 22 | + { |
| 23 | + public: |
| 24 | + ISC_STATUS getCancelState() const |
| 25 | + { |
| 26 | + return 0; |
| 27 | + } |
| 28 | + |
| 29 | + ULONG adjustWait(ULONG wait) const |
| 30 | + { |
| 31 | + return 0; |
| 32 | + } |
| 33 | + |
| 34 | + void checkoutRun(std::function<void()> func) const |
| 35 | + { |
| 36 | + func(); |
| 37 | + } |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +static std::string getUniqueId() |
| 43 | +{ |
| 44 | + static std::atomic<int> lockSuccess{0}; |
| 45 | + |
| 46 | + const auto now = std::chrono::system_clock::now(); |
| 47 | + const auto nowNs = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count(); |
| 48 | + |
| 49 | + return "lm_" + |
| 50 | + std::to_string(nowNs) + "_" + |
| 51 | + std::to_string(lockSuccess.fetch_add(1)); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +BOOST_AUTO_TEST_SUITE(EngineSuite) |
| 56 | +BOOST_AUTO_TEST_SUITE(LockManagerSuite) |
| 57 | +BOOST_AUTO_TEST_SUITE(LockManagerTests) |
| 58 | + |
| 59 | + |
| 60 | +BOOST_AUTO_TEST_CASE(LockUnlockTest, *boost::unit_test::disabled()) |
| 61 | +{ |
| 62 | + constexpr unsigned THREAD_COUNT = 8u; |
| 63 | + constexpr unsigned ITERATION_COUNT = 100'000u; |
| 64 | + |
| 65 | + ConfigFile configFile(ConfigFile::USE_TEXT, "\n"); |
| 66 | + Config config(configFile); |
| 67 | + |
| 68 | + LockManagerTestCallbacks callbacks; |
| 69 | + const string lockManagerId(getUniqueId().c_str()); |
| 70 | + auto lockManager = std::make_unique<LockManager>(lockManagerId, &config); |
| 71 | + |
| 72 | + unsigned lockSuccess = 0u; |
| 73 | + std::atomic_uint lockFail = 0; |
| 74 | + |
| 75 | + std::vector<std::thread> threads; |
| 76 | + std::latch latch(THREAD_COUNT); |
| 77 | + std::mutex mutex; |
| 78 | + |
| 79 | + for (unsigned threadNum = 0u; threadNum < THREAD_COUNT; ++threadNum) |
| 80 | + { |
| 81 | + threads.emplace_back([&]() { |
| 82 | + const UCHAR LOCK_KEY[] = {'1'}; |
| 83 | + FbLocalStatus statusVector; |
| 84 | + LOCK_OWNER_T ownerId = threadNum + 1; |
| 85 | + SLONG ownerHandle = 0; |
| 86 | + |
| 87 | + lockManager->initializeOwner(&statusVector, ownerId, LCK_OWNER_attachment, &ownerHandle); |
| 88 | + |
| 89 | + latch.arrive_and_wait(); |
| 90 | + |
| 91 | + for (unsigned i = 0; i < ITERATION_COUNT; ++i) |
| 92 | + { |
| 93 | + // std::lock_guard mutexGuard(mutex); |
| 94 | + |
| 95 | + const auto lockId = lockManager->enqueue(callbacks, &statusVector, 0, |
| 96 | + LCK_expression, LOCK_KEY, sizeof(LOCK_KEY), LCK_EX, nullptr, nullptr, 0, LCK_WAIT, ownerHandle); |
| 97 | + |
| 98 | + if (lockId) |
| 99 | + { |
| 100 | + ++lockSuccess; |
| 101 | + lockManager->dequeue(lockId); |
| 102 | + } |
| 103 | + else |
| 104 | + ++lockFail; |
| 105 | + } |
| 106 | + |
| 107 | + lockManager->shutdownOwner(callbacks, &ownerHandle); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + for (auto& thread : threads) |
| 112 | + thread.join(); |
| 113 | + |
| 114 | + BOOST_CHECK_EQUAL(lockFail + lockSuccess, THREAD_COUNT * ITERATION_COUNT); |
| 115 | + // BOOST_CHECK_EQUAL(lockFail.load(), 0u); |
| 116 | + // BOOST_CHECK_EQUAL(lockSuccess, THREAD_COUNT * ITERATION_COUNT); |
| 117 | + |
| 118 | + lockManager.reset(); |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +BOOST_AUTO_TEST_SUITE_END() // LockManagerTests |
| 123 | +BOOST_AUTO_TEST_SUITE_END() // LockManagerSuite |
| 124 | +BOOST_AUTO_TEST_SUITE_END() // EngineSuite |
0 commit comments