diff options
author | Hwankyu Jhun <h.jhun@samsung.com> | 2022-03-30 10:15:12 +0900 |
---|---|---|
committer | Hwankyu Jhun <h.jhun@samsung.com> | 2022-03-30 10:15:52 +0900 |
commit | 44301c070e7574fc0cb11fabdbdb4c2a88fa273e (patch) | |
tree | 84316404bd855910d8851b8aef2a0aca01a78a80 | |
parent | cae48e076ee84823c1557185ef4353ab36188884 (diff) | |
download | appcore-agent-44301c070e7574fc0cb11fabdbdb4c2a88fa273e.tar.gz appcore-agent-44301c070e7574fc0cb11fabdbdb4c2a88fa273e.tar.bz2 appcore-agent-44301c070e7574fc0cb11fabdbdb4c2a88fa273e.zip |
Fix unit tests
- Use GMainLoop
Change-Id: Ia9761524cbe785dca5defa48764448158f306285
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
-rw-r--r-- | unittests/service_app_test.cc | 64 |
1 files changed, 53 insertions, 11 deletions
diff --git a/unittests/service_app_test.cc b/unittests/service_app_test.cc index 87cdab9..5785ed8 100644 --- a/unittests/service_app_test.cc +++ b/unittests/service_app_test.cc @@ -16,10 +16,13 @@ #include <gmock/gmock.h> #include <dlog.h> +#include <glib.h> #include <stdio.h> #include <chrono> +#include <condition_variable> #include <functional> +#include <mutex> #include <thread> #include <bundle_cpp.h> @@ -43,9 +46,36 @@ class ServiceAppTest : public TestFixture { ServiceAppTest() : TestFixture(std::make_unique<Mocks>()) {} virtual ~ServiceAppTest() {} - virtual void SetUp() {} + virtual void SetUp() { + context_ = g_main_context_new(); + loop_ = g_main_loop_new(context_, false); + } + + virtual void TearDown() { + if (loop_) { + g_main_loop_unref(loop_); + loop_ = nullptr; + } + + if (context_) { + g_main_context_unref(context_); + context_ = nullptr; + } + } + + void LoopRun() { + g_main_loop_run(loop_); + } - virtual void TearDown() {} + void LoopQuit() { + g_main_loop_quit(loop_); + } + + GMainContext* context_ = nullptr; + GMainLoop* loop_ = nullptr; + std::mutex mutex_; + std::condition_variable cond_; + bool started_ = false; }; extern "C" int __dlog_print(log_id_t log_id, int prio, const char* tag, @@ -98,29 +128,41 @@ TEST_F(ServiceAppTest, Basic) { EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER)); EXPECT_CALL(GetMock<AppCoreMock>(), Run(_, _)).Times(1); - bool loop = true; + + std::unique_lock<std::mutex> lock(mutex_); EXPECT_CALL(GetMock<AppCoreMock>(), OnLoopRun()) .WillOnce(testing::Invoke([&]() { - while (loop) { - std::cout << "wait..." << std::endl; - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } + LoopRun(); })); std::thread t([&]() { + { + std::unique_lock<std::mutex> t_lock(mutex_); + GSource* source = g_timeout_source_new(100); + g_source_set_callback(source, [](gpointer user_data) { + auto* h = static_cast<ServiceAppTest*>(user_data); + std::unique_lock<std::mutex> l(h->mutex_); + h->started_ = true; + h->cond_.notify_one(); + return G_SOURCE_REMOVE; + }, this, nullptr); + g_source_attach(source, context_); + g_source_unref(source); + } + callback.create = __service_app_create_cb; - service_app_main(argc, argv, &callback, reinterpret_cast<void*>(&loop)); + service_app_main(argc, argv, &callback, this); }); - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - ret = service_app_main(argc, argv, &callback, &loop); + cond_.wait(lock, [&] { return started_; }); + ret = service_app_main(argc, argv, &callback, this); EXPECT_THAT(ret, testing::Eq(APP_ERROR_ALREADY_RUNNING)); // test service_app_exit EXPECT_CALL(GetMock<AppCoreMock>(), Exit()).Times(1); EXPECT_CALL(GetMock<AppCoreMock>(), OnLoopExit()) .WillOnce(testing::Invoke([&]() { - loop = false; + LoopQuit(); t.join(); })); service_app_exit(); |