2013-08-21 07:40:39 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <atomic>
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2013-11-04 05:46:19 +01:00
|
|
|
|
2013-08-21 07:40:39 +02:00
|
|
|
#include "testing_helpers.h"
|
2013-11-04 05:46:19 +01:00
|
|
|
#include "g2logmessage.hpp"
|
2013-10-05 06:14:35 +02:00
|
|
|
#include "g2logworker.hpp"
|
2013-07-14 01:57:26 +02:00
|
|
|
|
2013-08-21 07:40:39 +02:00
|
|
|
using namespace testing_helpers;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
TEST(Sink, OneSink) {
|
2013-11-04 05:46:19 +01:00
|
|
|
AtomicBoolPtr flag = make_shared < atomic<bool >> (false);
|
|
|
|
AtomicIntPtr count = make_shared < atomic<int >> (0);
|
|
|
|
{
|
|
|
|
auto worker = g2LogWorker::createWithNoSink();
|
|
|
|
auto handle = worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
|
|
|
|
EXPECT_FALSE(flag->load());
|
|
|
|
EXPECT_TRUE(0 == count->load());
|
|
|
|
//worker->save("this message should trigger an atomic increment at the sink");
|
2013-11-12 07:07:34 +01:00
|
|
|
g2::LogMessage message("test", 0, "test", DEBUG);
|
2013-11-04 05:46:19 +01:00
|
|
|
message.stream() << "this message should trigger an atomic increment at the sink";
|
|
|
|
worker->save(message);
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(flag->load());
|
|
|
|
EXPECT_TRUE(1 == count->load());
|
2013-08-21 07:40:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-01 08:44:30 +02:00
|
|
|
namespace {
|
2013-11-04 05:46:19 +01:00
|
|
|
typedef std::shared_ptr<std::atomic<bool >> AtomicBoolPtr;
|
|
|
|
typedef std::shared_ptr<std::atomic<int >> AtomicIntPtr;
|
|
|
|
typedef vector<AtomicBoolPtr> BoolList;
|
|
|
|
typedef vector<AtomicIntPtr> IntVector;
|
2013-09-01 08:44:30 +02:00
|
|
|
}
|
2013-11-04 05:46:19 +01:00
|
|
|
|
2013-09-01 08:44:30 +02:00
|
|
|
TEST(ConceptSink, OneHundredSinks) {
|
2013-11-04 05:46:19 +01:00
|
|
|
BoolList flags;
|
|
|
|
IntVector counts;
|
|
|
|
|
|
|
|
size_t NumberOfItems = 100;
|
|
|
|
for (size_t index = 0; index < NumberOfItems; ++index) {
|
|
|
|
flags.push_back(make_shared < atomic<bool >> (false));
|
|
|
|
counts.push_back(make_shared < atomic<int >> (0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-11-08 09:45:10 +01:00
|
|
|
RestoreFileLogger logger{"/tmp"};
|
|
|
|
auto worker = logger._scope->get(); //g2LogWorker::createWithNoSink();
|
2013-11-04 05:46:19 +01:00
|
|
|
size_t index = 0;
|
|
|
|
for (auto& flag : flags) {
|
|
|
|
auto& count = counts[index++];
|
|
|
|
// ignore the handle
|
|
|
|
worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
|
|
|
|
}
|
|
|
|
LOG(DEBUG) << "start message";
|
2013-11-12 07:07:34 +01:00
|
|
|
g2::LogMessage message("test", 0, "test", DEBUG);
|
2013-11-04 05:46:19 +01:00
|
|
|
auto& stream = message.stream();
|
|
|
|
stream << "Hello to 100 receivers :)";
|
|
|
|
worker->save(message);
|
|
|
|
stream << "Hello to 100 receivers :)";
|
|
|
|
worker->save(message);
|
|
|
|
//worker->save("Hello to 100 receivers :)");
|
|
|
|
//worker->save("Hello to 100 receivers :)");
|
|
|
|
LOG(INFO) << "end message";
|
|
|
|
}
|
|
|
|
// at the curly brace above the ScopedLogger will go out of scope and all the
|
|
|
|
// 100 logging receivers will get their message to exit after all messages are
|
|
|
|
// are processed
|
2013-09-01 08:44:30 +02:00
|
|
|
size_t index = 0;
|
2013-11-04 05:46:19 +01:00
|
|
|
for (auto& flag : flags) {
|
2013-09-01 08:44:30 +02:00
|
|
|
auto& count = counts[index++];
|
2013-11-04 05:46:19 +01:00
|
|
|
ASSERT_TRUE(flag->load()) << ", count : " << (index - 1);
|
|
|
|
ASSERT_TRUE(4 == count->load()) << ", count : " << (index - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "test one hundred sinks is finished finished\n";
|
|
|
|
}
|