Add unit tests for ThreadContext

This commit is contained in:
Thiago Marcos P. Santos
2015-06-24 16:50:55 +03:00
parent a8b6b67dfe
commit 57ddb386d8

View File

@@ -54,6 +54,12 @@ public:
return string;
}
bool checkContext() const {
return ThreadContext::currentlyOn(ThreadType::Worker)
&& ThreadContext::getName() == "Test"
&& ThreadContext::getPriority() == ThreadPriority::Low;
}
const std::thread::id tid;
};
@@ -100,3 +106,26 @@ TEST(Thread, invoke) {
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}
TEST(Thread, context) {
bool isMainThreadContext = ThreadContext::currentlyOn(ThreadType::Main)
&& ThreadContext::getName() == "Main"
&& ThreadContext::getPriority() == ThreadPriority::Regular;
EXPECT_EQ(isMainThreadContext, true);
const std::thread::id tid = std::this_thread::get_id();
RunLoop loop(uv_default_loop());
loop.invoke([&] {
Thread<TestObject> thread({"Test", ThreadType::Worker, ThreadPriority::Low}, tid);
thread.invokeWithResult<bool>(&TestObject::checkContext, [&] (bool inTestThreadContext) {
EXPECT_EQ(inTestThreadContext, true);
loop.stop();
});
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}