123 lines
3.3 KiB
C++
123 lines
3.3 KiB
C++
#include <gtest/gtest.h>
|
|
#include <boost/lockfree/queue.hpp>
|
|
#include <metricsLogger.hh>
|
|
#include <queue>
|
|
#include <thread>
|
|
|
|
TEST(SingleThread, Configuration) {
|
|
vptyp::MetricsLogger l(std::cout);
|
|
EXPECT_NO_THROW(l.configure({"apprx", "size", "time"}));
|
|
EXPECT_NO_THROW(l.add("apprx", 123));
|
|
EXPECT_ANY_THROW(l.add("not in configuration", 123));
|
|
}
|
|
|
|
void outBufferCheck(std::ostringstream& s,
|
|
std::queue<std::pair<std::string, int64_t>>& prev) {
|
|
std::string_view outBufferView = s.rdbuf()->view();
|
|
std::string_view lineView;
|
|
size_t startPos = 0;
|
|
while (startPos < outBufferView.length() &&
|
|
(lineView = outBufferView.substr(startPos, outBufferView.find('\n')),
|
|
lineView.size() > 0)) {
|
|
std::cout << lineView << std::endl;
|
|
if (prev.empty())
|
|
break;
|
|
startPos = outBufferView.find('\n') + 1;
|
|
size_t res{0};
|
|
std::set<std::string_view> s;
|
|
while (res != lineView.npos && !prev.empty()) {
|
|
auto nextAwaiting = prev.front();
|
|
|
|
res = lineView.find(nextAwaiting.first);
|
|
if (res != lineView.npos) {
|
|
s.emplace(nextAwaiting.first);
|
|
prev.pop();
|
|
}
|
|
std::cout << nextAwaiting.first << std::endl;
|
|
}
|
|
std::cout << "n" << std::endl;
|
|
}
|
|
EXPECT_EQ(prev.empty(), true);
|
|
}
|
|
|
|
TEST(SingleThread, Add) {
|
|
std::ostringstream s;
|
|
std::queue<std::pair<std::string, int64_t>> prev;
|
|
{
|
|
vptyp::MetricsLogger l(s);
|
|
|
|
l.configure({"apprx", "size", "time"});
|
|
|
|
auto decorator = [&l, &prev](std::string field, int value) {
|
|
l.add(field, value);
|
|
prev.emplace(field, value);
|
|
};
|
|
|
|
decorator("size", 1);
|
|
decorator("time", 1);
|
|
sleep(2); // twice of logger sleep
|
|
decorator("apprx", 1);
|
|
decorator("size", 2);
|
|
sleep(2);
|
|
}
|
|
|
|
// results
|
|
outBufferCheck(s, prev);
|
|
}
|
|
|
|
TEST(MultiThread, Configure) {
|
|
static constexpr size_t threadsNumber = 10;
|
|
vptyp::MetricsLogger l(std::cout);
|
|
std::atomic<size_t> howManyConfigured;
|
|
auto fConfig = [&l, &howManyConfigured] {
|
|
bool res{false};
|
|
EXPECT_NO_THROW(res = l.configure({"apprx", "size", "time"}));
|
|
howManyConfigured.fetch_add(static_cast<size_t>(res));
|
|
};
|
|
// simulate race condition configure
|
|
std::vector<std::jthread> threads(threadsNumber);
|
|
for (auto& thread : threads) {
|
|
thread = std::jthread(fConfig);
|
|
}
|
|
threads.clear();
|
|
EXPECT_EQ(howManyConfigured.load(), 1);
|
|
}
|
|
|
|
TEST(MultiThread, Add) {
|
|
static constexpr size_t maxRandNumber = 100;
|
|
static constexpr size_t producerIterations = 5;
|
|
std::ostringstream s;
|
|
std::queue<std::pair<std::string, int64_t>> prev;
|
|
std::mutex mtx;
|
|
{
|
|
vptyp::MetricsLogger l(s);
|
|
l.configure({"apprx", "size", "time"});
|
|
|
|
auto decorator = [&l, &prev, &mtx](std::string field, int value) {
|
|
for (size_t i = 0; i < producerIterations; ++i) {
|
|
l.add(field, value);
|
|
{
|
|
std::lock_guard g(mtx);
|
|
prev.emplace(field, value);
|
|
}
|
|
sleep(1);
|
|
}
|
|
};
|
|
|
|
std::vector<std::jthread> threads(3);
|
|
threads[0] = std::jthread(decorator, "apprx", rand() % maxRandNumber);
|
|
threads[1] = std::jthread(decorator, "size", rand() % maxRandNumber);
|
|
threads[2] = std::jthread(decorator, "time", rand() % maxRandNumber);
|
|
|
|
threads.clear();
|
|
sleep(1);
|
|
}
|
|
|
|
// results
|
|
outBufferCheck(s, prev);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
} |