From: Lukasz Kostyra Date: Wed, 7 Jan 2015 14:39:02 +0000 (+0100) Subject: IPC: Convert test synchronization method to Latch X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a83342a7862458d2a6a35fe8889e80cd9a9dc1bb;p=platform%2Fcore%2Fsecurity%2Fvasum.git IPC: Convert test synchronization method to Latch [Bug/Feature] Some tests were using sleep_for in order to synchronize threads [Cause] N/A [Solution] Use Latch instead of atomic_bool set and sleep_for to synchronize threads in tests [Verification] Build, install, run tests. Should pass as they did before. Change-Id: I3067ed1f13cdde047f720c0b6d05ce19ec156dbe --- diff --git a/tests/unit_tests/ipc/ut-ipc.cpp b/tests/unit_tests/ipc/ut-ipc.cpp index 65ee27e..74c18f3 100644 --- a/tests/unit_tests/ipc/ut-ipc.cpp +++ b/tests/unit_tests/ipc/ut-ipc.cpp @@ -36,12 +36,12 @@ #include "ipc/ipc-gsource.hpp" #include "ipc/types.hpp" #include "utils/glib-loop.hpp" +#include "utils/latch.hpp" #include "config/fields.hpp" #include "logger/logger.hpp" #include -#include #include #include #include @@ -592,14 +592,14 @@ BOOST_AUTO_TEST_CASE(AddSignalInRuntime) Client c(socketPath); connect(s, c); - std::atomic_bool isHandlerACalled(false); - auto handlerA = [&isHandlerACalled](const FileDescriptor, std::shared_ptr&) { - isHandlerACalled = true; + utils::Latch latchA; + auto handlerA = [&latchA](const FileDescriptor, std::shared_ptr&) { + latchA.set(); }; - std::atomic_bool isHandlerBCalled(false); - auto handlerB = [&isHandlerBCalled](const FileDescriptor, std::shared_ptr&) { - isHandlerBCalled = true; + utils::Latch latchB; + auto handlerB = [&latchB](const FileDescriptor, std::shared_ptr&) { + latchB.set(); }; c.addSignalHandler(1, handlerA); @@ -610,8 +610,7 @@ BOOST_AUTO_TEST_CASE(AddSignalInRuntime) s.signal(1, data); // Wait for the signals to arrive - std::this_thread::sleep_for(std::chrono::milliseconds(TIMEOUT)); //TODO wait_for - BOOST_CHECK(isHandlerACalled && isHandlerBCalled); + BOOST_CHECK(latchA.wait(TIMEOUT) && latchB.wait(TIMEOUT)); } @@ -620,14 +619,14 @@ BOOST_AUTO_TEST_CASE(AddSignalOffline) Service s(socketPath); Client c(socketPath); - std::atomic_bool isHandlerACalled(false); - auto handlerA = [&isHandlerACalled](const FileDescriptor, std::shared_ptr&) { - isHandlerACalled = true; + utils::Latch latchA; + auto handlerA = [&latchA](const FileDescriptor, std::shared_ptr&) { + latchA.set(); }; - std::atomic_bool isHandlerBCalled(false); - auto handlerB = [&isHandlerBCalled](const FileDescriptor, std::shared_ptr&) { - isHandlerBCalled = true; + utils::Latch latchB; + auto handlerB = [&latchB](const FileDescriptor, std::shared_ptr&) { + latchB.set(); }; c.addSignalHandler(1, handlerA); @@ -642,8 +641,7 @@ BOOST_AUTO_TEST_CASE(AddSignalOffline) s.signal(1, data); // Wait for the signals to arrive - std::this_thread::sleep_for(std::chrono::milliseconds(2 * TIMEOUT)); - BOOST_CHECK(isHandlerACalled && isHandlerBCalled); + BOOST_CHECK(latchA.wait(TIMEOUT) && latchB.wait(TIMEOUT)); } @@ -652,9 +650,9 @@ BOOST_AUTO_TEST_CASE(AddSignalOffline) BOOST_AUTO_TEST_CASE(ServiceGSource) { ScopedGlibLoop loop; - std::atomic_bool isSignalCalled(false); - auto signalHandler = [&isSignalCalled](const FileDescriptor, std::shared_ptr&) { - isSignalCalled = true; + utils::Latch l; + auto signalHandler = [&l](const FileDescriptor, std::shared_ptr&) { + l.set(); }; IPCGSource::Pointer serviceGSource; @@ -672,17 +670,16 @@ BOOST_AUTO_TEST_CASE(ServiceGSource) auto data = std::make_shared(1); c.signal(2, data); - std::this_thread::sleep_for(std::chrono::milliseconds(TIMEOUT)); //TODO wait_for - BOOST_CHECK(isSignalCalled); + BOOST_CHECK(l.wait(TIMEOUT)); } BOOST_AUTO_TEST_CASE(ClientGSource) { ScopedGlibLoop loop; - std::atomic_bool isSignalCalled(false); - auto signalHandler = [&isSignalCalled](const FileDescriptor, std::shared_ptr&) { - isSignalCalled = true; + utils::Latch l; + auto signalHandler = [&l](const FileDescriptor, std::shared_ptr&) { + l.set(); }; Service s(socketPath); @@ -702,8 +699,7 @@ BOOST_AUTO_TEST_CASE(ClientGSource) auto data = std::make_shared(1); s.signal(2, data); - std::this_thread::sleep_for(std::chrono::milliseconds(TIMEOUT)); //TODO wait_for - BOOST_CHECK(isSignalCalled); + BOOST_CHECK(l.wait(TIMEOUT)); } #endif // GLIB_CHECK_VERSION