From 17cf2ee910f2da4d7330ea1714650aecca618df6 Mon Sep 17 00:00:00 2001 From: KjellKod Date: Sun, 18 Aug 2013 23:17:46 -0600 Subject: [PATCH] Added simple test for sinks --- g2log/test_unit/test_sink.cpp | 143 ---------------------------------- 1 file changed, 143 deletions(-) diff --git a/g2log/test_unit/test_sink.cpp b/g2log/test_unit/test_sink.cpp index 3b252fc..139597f 100644 --- a/g2log/test_unit/test_sink.cpp +++ b/g2log/test_unit/test_sink.cpp @@ -1,145 +1,2 @@ -#include - -#include -#include -#include -#include -#include - -#include "testing_helpers.h" -#include "std2_make_unique.hpp" -#include "g2sink.h" -#include "g2sinkwrapper.h" -#include "g2sinkhandle.h" -#include "g2logmessage.hpp" -using namespace std; -using namespace std2; - -class CoutSink { - stringstream buffer; - unique_ptr scope_ptr; - - CoutSink() : scope_ptr(std2::make_unique(&buffer)) { } -public: - void clear() { buffer.str(""); } - std::string string() { return buffer.str(); } - void save(g2::internal::LogEntry msg) { std::cout << msg; } - - virtual ~CoutSink() final { } - - static std::unique_ptr createSink() - { return std::unique_ptr(new CoutSink); } -}; - - -namespace { - typedef std::shared_ptr SinkWrapperPtr; - typedef g2::internal::LogEntry LogEntry; -} - -namespace g2 { - - class Worker { - std::vector _container; // should be hidden in a pimple with a bg active object - std::unique_ptr _bg; - - void bgSave(LogEntry msg) { - for (auto& sink : _container) { - sink->send(msg); - } - } - - public: - - Worker() : _bg { - kjellkod::Active::createActive() - } - { - } - - ~Worker() { - _bg->send([this] { - _container.clear(); - }); - } - - void save(LogEntry msg) { - _bg->send([this, msg] { - bgSave(msg); - }); - } // will this be copied? - //this is guaranteed to work std::bind(&Worker::bgSave, this, msg)); } - - template - std::unique_ptr< SinkHandle > addSink(std::unique_ptr unique, DefaultLogCall call) { - auto shared = std::shared_ptr(unique.release()); - auto sink = std::make_shared < internal::Sink > (shared, call); - auto add_sink_call = [this, sink] { _container.push_back(sink); - - }; - auto wait_result = g2::spawn_task(add_sink_call, _bg.get()); - wait_result.wait(); - - auto handle = std2::make_unique< SinkHandle >(sink); - return handle; - } - }; - -} // g2 - - - - using namespace g2; - using namespace g2::internal; - - TEST(Sink, CreateHandle) { - Worker worker; - auto handle = worker.addSink(CoutSink::createSink(), &CoutSink::save); - ASSERT_NE(nullptr, handle.get()); - } - - TEST(Sink, OneSink__VerifyMsgIn) { - Worker worker; - auto handle = worker.addSink(CoutSink::createSink(), &CoutSink::save); - worker.save("Hello World!"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - auto output = handle->call(&CoutSink::string); - ASSERT_EQ("Hello World!", output.get()); - } - - struct StringSink { - std::string raw; - void append(LogEntry entry) { raw.append(entry); } - std::string string(){return raw; } - }; - - - TEST(Sink, DualSink__VerifyMsgIn) { - Worker worker; - auto h1 = worker.addSink(CoutSink::createSink(), &CoutSink::save); - auto h2 = worker.addSink(std2::make_unique(), &StringSink::append); - worker.save("Hello World!"); - - - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - auto first = h1->call(&CoutSink::string); - auto second = h2->call(&StringSink::string); - - - ASSERT_EQ("Hello World!", first.get()); - ASSERT_EQ("Hello World!", second.get()); - } - - - - TEST(Sink, DeletedSink__Exptect_badweak_ptr___exception) { - auto worker = std2::make_unique(); - auto h1 = worker->addSink(CoutSink::createSink(), &CoutSink::save); - worker->save("Hello World!"); - worker.reset(); - - auto first = h1->call(&CoutSink::string); - EXPECT_THROW(first.get(), std::bad_weak_ptr); -}