From 57ddb386d8d1cbc71856abd48b62441294a175b8 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Wed, 24 Jun 2015 16:50:55 +0300 Subject: [PATCH] Add unit tests for ThreadContext --- test/miscellaneous/thread.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/miscellaneous/thread.cpp b/test/miscellaneous/thread.cpp index 9767bc4b7..f8d91bf97 100644 --- a/test/miscellaneous/thread.cpp +++ b/test/miscellaneous/thread.cpp @@ -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 thread({"Test", ThreadType::Worker, ThreadPriority::Low}, tid); + + thread.invokeWithResult(&TestObject::checkContext, [&] (bool inTestThreadContext) { + EXPECT_EQ(inTestThreadContext, true); + loop.stop(); + }); + }); + + uv_run(uv_default_loop(), UV_RUN_DEFAULT); +}