mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add: basic mt ut for queue
This commit is contained in:
parent
af96a09e36
commit
f6af394195
@ -11,6 +11,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "libimp/def.h"
|
#include "libimp/def.h"
|
||||||
|
#include "libimp/export.h"
|
||||||
#include "libimp/span.h"
|
#include "libimp/span.h"
|
||||||
|
|
||||||
LIBIMP_NAMESPACE_BEG_
|
LIBIMP_NAMESPACE_BEG_
|
||||||
@ -22,7 +23,7 @@ LIBIMP_NAMESPACE_BEG_
|
|||||||
* \param name the mangled name
|
* \param name the mangled name
|
||||||
* \return std::string a human-readable demangled type name
|
* \return std::string a human-readable demangled type name
|
||||||
*/
|
*/
|
||||||
std::string demangle(span<char const> name) noexcept;
|
LIBIMP_EXPORT std::string demangle(span<char const> name) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Returns an implementation defined string containing the name of the type.
|
* \brief Returns an implementation defined string containing the name of the type.
|
||||||
|
|||||||
@ -1,8 +1,14 @@
|
|||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <atomic>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
#include "libconcur/queue.h"
|
#include "libconcur/queue.h"
|
||||||
#include "libimp/log.h"
|
#include "libimp/log.h"
|
||||||
|
#include "libimp/nameof.h"
|
||||||
|
|
||||||
using namespace concur;
|
using namespace concur;
|
||||||
|
|
||||||
@ -78,7 +84,77 @@ namespace {
|
|||||||
template <typename PR, typename CR>
|
template <typename PR, typename CR>
|
||||||
void test_queue(std::size_t np, std::size_t nc) {
|
void test_queue(std::size_t np, std::size_t nc) {
|
||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
|
log.info("\n\tStart with: [", imp::nameof<PR>(), " - ", imp::nameof<CR>(), "]\n\t\t", np, " producers, ", nc, " consumers...");
|
||||||
|
|
||||||
|
struct Data {
|
||||||
|
std::uint64_t n;
|
||||||
|
std::uint64_t i;
|
||||||
|
};
|
||||||
|
queue<Data, PR, CR> que;
|
||||||
|
|
||||||
|
constexpr static std::uint32_t loop_size = 10'0000;
|
||||||
|
|
||||||
|
std::atomic<std::uint64_t> sum {0};
|
||||||
|
std::atomic<std::size_t> running {np};
|
||||||
|
|
||||||
|
auto prod_call = [&](std::size_t n) {
|
||||||
|
for (std::uint32_t i = 1; i <= loop_size; ++i) {
|
||||||
|
std::this_thread::yield();
|
||||||
|
for (std::uint32_t k = 1; !que.push(Data{n, i}); ++k) {
|
||||||
|
std::this_thread::yield();
|
||||||
|
if (k % (loop_size / 10) == 0) {
|
||||||
|
log.info("[", n, "] put count: ", i, ", retry: ", k);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i % (loop_size / 10) == 0) {
|
||||||
|
log.info("[", n, "] put count: ", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--running;
|
||||||
|
};
|
||||||
|
auto cons_call = [&] {
|
||||||
|
for (;;) {
|
||||||
|
std::this_thread::yield();
|
||||||
|
Data v;
|
||||||
|
while (!que.pop(v)) {
|
||||||
|
if (running == 0) return;
|
||||||
|
std::this_thread::yield();
|
||||||
|
}
|
||||||
|
sum += v.i;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<std::thread> prods(np);
|
||||||
|
for (std::size_t n = 0; n < np; ++n) prods[n] = std::thread(prod_call, n);
|
||||||
|
std::vector<std::thread> conss(nc);
|
||||||
|
for (auto &c : conss) c = std::thread(cons_call);
|
||||||
|
|
||||||
|
for (auto &p : prods) p.join();
|
||||||
|
for (auto &c : conss) c.join();
|
||||||
|
|
||||||
|
EXPECT_EQ(sum, np * (loop_size * std::uint64_t(loop_size + 1)) / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
TEST(queue, multi_thread) {
|
||||||
|
using namespace concur;
|
||||||
|
|
||||||
|
/// \brief 1-1
|
||||||
|
test_queue<relation::single, relation::single>(1, 1);
|
||||||
|
test_queue<relation::single, relation::multi >(1, 1);
|
||||||
|
test_queue<relation::multi , relation::single>(1, 1);
|
||||||
|
test_queue<relation::multi , relation::multi >(1, 1);
|
||||||
|
|
||||||
|
/// \brief 8-1
|
||||||
|
test_queue<relation::multi , relation::single>(8, 1);
|
||||||
|
test_queue<relation::multi , relation::multi >(8, 1);
|
||||||
|
|
||||||
|
/// \brief 1-8
|
||||||
|
test_queue<relation::single, relation::multi >(1, 8);
|
||||||
|
test_queue<relation::multi , relation::multi >(1, 8);
|
||||||
|
|
||||||
|
/// \brief 8-8
|
||||||
|
test_queue<relation::multi , relation::multi >(8, 8);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user