mirror of
https://github.com/KjellKod/g3log.git
synced 2024-12-12 18:30:25 +01:00
86473c60e0
* Added "removeSink" method and related functionality to LogWorker API
* Added a new API for plugging in custom Timestamp generator.
* Revert "Added "removeSink" method and related functionality to LogWorker API"
This reverts commit c9cee5d9a0
.
* Dropping shared_ptr<Timestamp> in log messages and using regular vars
* Moving to standard timespec struct from custom Timestamp
* Wiring timespec timestamps with formating routine
* Falling back to clock_gettime
* Reverting g3 API changes
* Optimizing format string generation
* Removed _microseconds from LogMessage
* Implemented sec fractional format key
* Optimization of format string generation
* Adjusting comments
* Refining localtime_formatted by introducing two helper functions
* refactored and simplified code
* fixed up some commented away unit tests
* refactoring message specifics tests to it's own test
* Use gcc 4.9
* C++14 for Linux
* Update .travis.yml
* Update buildAndRunTests.sh
* lower case in `-std=c++14`
* -lrt flag for gcc
* Added support for high precision clock on Linux/gcc (already there now for OSX). Windows is still missing
* intermediate comments
* Clarified for some code readers the mysterious use of assert in an expression that is always true
* refactored + renamed functions and constants. Added unit test for retrieving fractional type
* committing changes previously fixed - finished unit testing for g3::internal::time::GetFractional(..)
* added unit test for fractional to string
* added missing unit tests for localtime_formatted
* fixed? nano / microsec functionality to timer
* test
172 lines
4.1 KiB
C++
172 lines
4.1 KiB
C++
/** ==========================================================================
|
|
* 2012 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
|
|
* with no warranties. This code is yours to share, use and modify with no
|
|
* strings attached and no restrictions or obligations.
|
|
*
|
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
|
* ============================================================================*/
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
#include <future>
|
|
#include <string>
|
|
#include <exception>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include "g3log/time.hpp"
|
|
#include "g3log/future.hpp"
|
|
|
|
|
|
std::future<std::string> sillyFutureReturn()
|
|
{
|
|
std::packaged_task<std::string()> task([](){return std::string("Hello Future");}); // wrap the function
|
|
std::future<std::string> result = task.get_future(); // get a future
|
|
std::thread(std::move(task)).detach(); // launch on a thread
|
|
std::cout << "Waiting...";
|
|
result.wait();
|
|
return result; // already wasted
|
|
}
|
|
|
|
|
|
TEST(Configuration, FutureSilly)
|
|
{
|
|
std::string hello = sillyFutureReturn().get();
|
|
ASSERT_STREQ(hello.c_str(), "Hello Future");
|
|
}
|
|
|
|
struct MsgType
|
|
{
|
|
std::string msg_;
|
|
MsgType(std::string m): msg_(m){};
|
|
std::string msg(){return msg_;}
|
|
};
|
|
|
|
|
|
TEST(TestOf_CopyableCall, Expecting_SmoothSailing)
|
|
{
|
|
using namespace kjellkod;
|
|
const std::string str("Hello from struct");
|
|
MsgType type(str);
|
|
std::unique_ptr<Active> bgWorker(Active::createActive());
|
|
std::future<std::string> fstring =
|
|
g3::spawn_task(std::bind(&MsgType::msg, type), bgWorker.get());
|
|
ASSERT_STREQ(str.c_str(), fstring.get().c_str());
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOf_CopyableLambdaCall, Expecting_AllFine)
|
|
{
|
|
using namespace kjellkod;
|
|
std::unique_ptr<Active> bgWorker(Active::createActive());
|
|
|
|
// lambda task
|
|
const std::string str_standalone("Hello from standalone");
|
|
auto msg_lambda=[=](){return (str_standalone+str_standalone);};
|
|
std::string expected(str_standalone+str_standalone);
|
|
|
|
auto fstring_standalone = g3::spawn_task(msg_lambda, bgWorker.get());
|
|
ASSERT_STREQ(expected.c_str(), fstring_standalone.get().c_str());
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename F>
|
|
std::future<typename std::result_of<F()>::type> ObsoleteSpawnTask(F f)
|
|
{
|
|
typedef typename std::result_of<F()>::type result_type;
|
|
typedef std::packaged_task<result_type()> task_type;
|
|
|
|
task_type task(std::move(f));
|
|
std::future<result_type> result = task.get_future();
|
|
|
|
std::vector<std::function<void()>> vec;
|
|
vec.push_back(g3::MoveOnCopy<task_type>(std::move(task)));
|
|
std::thread(std::move(vec.back())).detach();
|
|
result.wait();
|
|
return std::move(result);
|
|
}
|
|
|
|
TEST(TestOf_ObsoleteSpawnTaskWithStringReturn, Expecting_FutureString)
|
|
{
|
|
std::string str("Hello");
|
|
std::string expected(str+str);
|
|
auto msg_lambda=[=](){return (str+str);};
|
|
auto future_string = ObsoleteSpawnTask(msg_lambda);
|
|
|
|
ASSERT_STREQ(expected.c_str(), future_string.get().c_str());
|
|
}
|
|
// gcc thread example below
|
|
// tests code below copied from mail-list conversion between
|
|
// Lars Gullik Bjønnes and Jonathan Wakely
|
|
// http://gcc.gnu.org/ml/gcc-help/2011-11/msg00052.html
|
|
|
|
// --------------------------------------------------------------
|
|
namespace WORKING
|
|
{
|
|
using namespace g3;
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <iostream>
|
|
#include <future>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
std::vector<std::function<void()>> vec;
|
|
|
|
template<typename F>
|
|
std::future<typename std::result_of<F()>::type> spawn_task(F f)
|
|
{
|
|
typedef typename std::result_of<F()>::type result_type;
|
|
typedef std::packaged_task<result_type()> task_type;
|
|
|
|
task_type task(std::move(f));
|
|
std::future<result_type> res = task.get_future();
|
|
|
|
vec.push_back(
|
|
MoveOnCopy<task_type>(
|
|
std::move(task)));
|
|
|
|
std::thread([]()
|
|
{
|
|
auto task = std::move(vec.back());
|
|
vec.pop_back();
|
|
task();
|
|
}
|
|
).detach();
|
|
|
|
return std::move(res);
|
|
}
|
|
|
|
|
|
|
|
double get_res()
|
|
{
|
|
return 42.2;
|
|
}
|
|
|
|
std::string msg3(){return "msg3";}
|
|
} // WORKING
|
|
|
|
TEST(Yalla, Testar)
|
|
{
|
|
using namespace WORKING;
|
|
auto f = spawn_task(get_res);
|
|
ASSERT_EQ(42.2, f.get());
|
|
|
|
auto f2 = spawn_task(msg3);
|
|
ASSERT_EQ("msg3", f2.get());
|
|
|
|
|
|
ASSERT_TRUE(true);
|
|
}
|
|
|
|
|
|
|
|
|
|
|