tizen 2.4 release
[framework/web/wrt-commons.git] / tests / unused / test_shm.cpp
index 20eed04..04638a6 100644 (file)
@@ -32,6 +32,7 @@
 #include <dpl/shared_object.h>
 #include <dpl/shared_property.h>
 #include <memory>
+#include <mutex>
 
 RUNNER_TEST_GROUP_INIT(DPL)
 
@@ -68,15 +69,15 @@ size_t g_delegateCalls = 0;
 
 void Wait(DPL::WaitableEvent& event, bool expectedTimeout = false)
 {
-    LogDebug("WaitForSingleHandle...");
+    WrtLogD("WaitForSingleHandle...");
     DPL::WaitableHandleIndexList list = DPL::WaitForSingleHandle(
             event.GetHandle(),
             expectedTimeout ?
             EXPECTED_WAITABLE_TIMEOUT : UNEXPECTED_WAITABLE_TIMEOUT);
     if (list.size() == 0) {
-        LogDebug("...timeout.");
+        WrtLogD("...timeout.");
     } else {
-        LogDebug("...signaled.");
+        WrtLogD("...signaled.");
         event.Reset();
     }
 
@@ -165,14 +166,14 @@ void ListeningController<SharedType>::OnEventReceived(const int& event)
         OnEvent(event);
         m_waitable->Signal();
     } else if (event == DESTROY_EVENT) {
-        LogDebug("Destroying shared object");
+        WrtLogD("Destroying shared object");
         OnEvent(event);
 
         // deregister, destroy ad notify main thread
         m_so.Reset();
-        LogDebug("4");
+        WrtLogD("4");
         m_waitable->Signal();
-        LogDebug("5");
+        WrtLogD("5");
     } else {
         OnEvent(event);
     }
@@ -199,7 +200,7 @@ class TestSharedObject4 : public SharedObject<SharedTypeList>
 
     ~TestSharedObject4()
     {
-        LogDebug("dtor");
+        WrtLogD("dtor");
     }
 
   protected:
@@ -574,12 +575,12 @@ class EnumTestSO1 : public TestSharedObject
 void EnumTestSO1::PropertyChanged(size_t propertyEnum)
 {
     if (propertyEnum == 1) {
-        LogDebug("Property enum " << propertyEnum << " correctly set");
+        WrtLogD("Property enum %i correctly set", propertyEnum);
         g_enumTestCorrect = true;
     }
     if (propertyEnum == 4) {
         // This is bad. We only have 4 types
-        LogError("Property enum " << propertyEnum << " should be skipped");
+        WrtLogE("Property enum %i should be skipped", propertyEnum);
         g_enumTestIncorrect = true;
     }
     // confirm property change notification
@@ -627,7 +628,7 @@ RUNNER_TEST(SharedMemory_025_InvalidEnumTest)
     // create listening controller and wait until it registers
     EnumController controller(&waitable);
     Wait(waitable);
-    LogDebug("Listening controller created");
+    WrtLogD("Listening controller created");
 
     // create writing shared object
     std::shared_ptr<EnumTestSO2> sho2 =
@@ -695,7 +696,7 @@ void ShmController::OnEventReceived(const int& event)
         m_so = SharedObjectFactory<MultiThreadSO>::Create(SHM_KEY, SEM_NAME);
         PostEvent(2);
     } else if (event == DESTROY_EVENT) {
-        LogDebug("Destroying shared object");
+        WrtLogD("Destroying shared object");
         // deregister, destroy ad notify main thread
         m_so.Reset();
         m_waitable->Signal();
@@ -703,7 +704,7 @@ void ShmController::OnEventReceived(const int& event)
         m_so->TestAndSetProperty();
         m_counter++;
         if (m_counter >= TEST_AND_SET_REPEATS) {
-            LogDebug("Max tests reached. Finishing thread");
+            WrtLogD("Max tests reached. Finishing thread");
             PostEvent(DESTROY_EVENT);
             return;
         }
@@ -735,8 +736,8 @@ RUNNER_TEST(SharedMemory_030_MultithreadTest)
     }
 
     int value = sho->GetProperty<0, int>();
-    LogDebug("Final value is " << value << ", expected " <<
-             MAX_THREADS * TEST_AND_SET_REPEATS);
+    WrtLogD("Final value is %i, expected %i",
+             value, MAX_THREADS * TEST_AND_SET_REPEATS);
     RUNNER_ASSERT(value == MAX_THREADS * TEST_AND_SET_REPEATS);
 }
 
@@ -792,7 +793,7 @@ void ShmController3::OnValueChanged(const DPL::PropertyEvent<bool>& event)
         // change back
         m_model->boolValue.Set(false);
     } else {
-        LogError("Expected value = true, got false");
+        WrtLogE("Expected value = true, got false");
     }
 
     m_waitable->Signal();
@@ -884,7 +885,7 @@ void SPController::OnValueChanged1(const DPL::PropertyEvent<int>& event)
         return;
     }
 
-    LogDebug("[1] Value changed to " << event.value);
+    WrtLogD("[1] Value changed to %s", event.value);
     m_repeats++;
     m_model1->counter.Set(event.value + 1);
 }
@@ -896,7 +897,7 @@ void SPController::OnValueChanged2(const DPL::PropertyEvent<int>& event)
         return;
     }
 
-    LogDebug("[2] Value changed to " << event.value);
+    WrtLogD("[2] Value changed to %i", event.value);
     m_repeats++;
     m_model2->counter.Set(event.value + 1);
 }
@@ -919,7 +920,7 @@ RUNNER_TEST(SharedMemory_060_SingleProcess)
 
     int value = sho->GetProperty<0, int>();
 
-    LogDebug("final value: " << value);
+    WrtLogD("final value: %i", value);
 
     // check value
     RUNNER_ASSERT(value == SINGLE_PROCESS_REPEATS);
@@ -976,7 +977,7 @@ void ListenerTestController::ValueChanged(size_t propertyEnum,
                                           const int& value,
                                           const void* /*info*/)
 {
-    LogDebug("ValueChanged(int) " << propertyEnum << " " << value);
+    WrtLogD("ValueChanged(int) %u %i", propertyEnum, value);
     if ((propertyEnum == 0 &&
          value == 1) || (propertyEnum == 1 && value == 2))
     {
@@ -991,7 +992,7 @@ void ListenerTestController::ValueChanged(size_t propertyEnum,
                                           const char& value,
                                           const void* /*info*/)
 {
-    LogDebug("ValueChanged(char) " << propertyEnum << " " << value);
+    WrtLogD("ValueChanged(char) %u %i", propertyEnum, value);
     if (propertyEnum == 2 && value == 'c') {
         g_values[propertyEnum]++;
         if (g_values[propertyEnum] == 3) {
@@ -1004,7 +1005,7 @@ void ListenerTestController::ValueChanged(size_t propertyEnum,
                                           const int(&value)[64],
                                           const void* /*info*/)
 {
-    LogDebug("ValueChanged(int[64]) " << propertyEnum << " " << value[5]);
+    WrtLogD("ValueChanged(int[64]) %u %i", propertyEnum, value[5]);
     if (propertyEnum == 3 && value[5] == 5) {
         g_values[propertyEnum]++;
         if (g_values[propertyEnum] == 3) {
@@ -1246,18 +1247,18 @@ RUNNER_TEST(SharedMemory_100_LazyInit)
 bool SetCondition(const int& readValue, int& setValue);
 bool SetCondition(const int& readValue, int& setValue)
 {
-    LogDebug("Condition delegate called with read value = " << readValue <<
-             " and set value = " << setValue);
+    WrtLogD("Condition delegate called with read value = %i and set value = %i",
+        readValue, setValue);
 
     if (readValue > 3) {
-        LogDebug("Condition is false");
+        WrtLogD("Condition is false");
         return false;
     }
 
-    LogDebug("Condition is true");
+    WrtLogD("Condition is true");
     if (4 == setValue) {
         setValue = 10;
-        LogDebug("Changing set value to " << setValue);
+        WrtLogD("Changing set value to %i", setValue);
     }
     return true;
 }
@@ -1265,7 +1266,7 @@ bool SetCondition(const int& readValue, int& setValue)
 void SetDelegate(const int& readValue);
 void SetDelegate(const int& readValue)
 {
-    LogDebug("Set delegate called " << readValue);
+    WrtLogD("Set delegate called %i", readValue);
     g_delegateCalls++;
 }
 
@@ -1349,15 +1350,15 @@ class SharedObjectSingleton
 
   private:
     static MTSharedObjectPtr m_sho;
-    static DPL::Mutex m_mutex;
+    static std::mutex m_mutex;
 };
 
 MTSharedObjectPtr SharedObjectSingleton::m_sho;
-DPL::Mutex SharedObjectSingleton::m_mutex;
+std::mutex SharedObjectSingleton::m_mutex;
 
 MTSharedObjectPtr SharedObjectSingleton::Instance()
 {
-    DPL::Mutex::ScopedLock lock(&m_mutex);
+    std::lock_guard<std::mutex> lock(m_mutex);
     if (!m_sho) {
         m_sho = SharedObjectFactory<MTSharedObject>::Create(SHM_KEY, SEM_NAME);
     }
@@ -1366,7 +1367,7 @@ MTSharedObjectPtr SharedObjectSingleton::Instance()
 
 void SharedObjectSingleton::Destroy()
 {
-    DPL::Mutex::ScopedLock lock(&m_mutex);
+    std::lock_guard<std::mutex> lock(m_mutex);
     m_sho.Reset();
 }
 
@@ -1416,7 +1417,7 @@ void ShmController4::ValueChanged(size_t propertyEnum,
                                   const int& value,
                                   const void* /*info*/)
 {
-    LogDebug("ValueChanged(int) " << propertyEnum << " " << value);
+    WrtLogD("ValueChanged(int) %u %i", propertyEnum, value);
     if ((propertyEnum == 0 && value == 1) ||
         (propertyEnum == 1 && value == 11))
     {
@@ -1428,7 +1429,7 @@ void ShmController4::ValueChanged(size_t propertyEnum,
                                   const char& value,
                                   const void* /*info*/)
 {
-    LogDebug("ValueChanged(char) " << propertyEnum << " " << value);
+    WrtLogD("ValueChanged(char) %u %i", propertyEnum, value);
     if (propertyEnum == 2 && value == 'a') {
         m_waitable->Signal();
     }
@@ -1438,7 +1439,7 @@ void ShmController4::ValueChanged(size_t propertyEnum,
                                   const int(&value)[64],
                                   const void* /*info*/)
 {
-    LogDebug("ValueChanged(int[64]) " << propertyEnum << " " << value[5]);
+    WrtLogD("ValueChanged(int[64]) %u %i", propertyEnum, value[5]);
     if (propertyEnum == 3 && value[0] == 0 && value[1] == 1 && value[2] == 2) {
         m_waitable->Signal();
     }
@@ -1459,7 +1460,7 @@ void ShmController4::OnEventReceived(const int& event)
         break;
 
     case DESTROY_EVENT:
-        LogDebug("Destroying shared object");
+        WrtLogD("Destroying shared object");
         // deregister, destroy and notify main thread
         m_so.Reset();
         m_waitable->Signal();
@@ -1497,7 +1498,7 @@ void ShmController4::OnEventReceived(const int& event)
         break;
 
     default:
-        LogError("Unsupported event received: " << event);
+        WrtLogE("Unsupported event received: %i", event);
     }
 }
 
@@ -1516,8 +1517,8 @@ void MultipleWait(DPL::WaitableEvent(&event)[MAX_THREADS])
 #define LISTENER_ASSERT(property) \
     Try { \
         singleton->RemoveListener<(property)>(controller[i]); \
-        LogError("Controller " << i << " is still listening for property " \
-                               << #property); \
+        WrtLogE("Controller %i is still listening for property %s" \
+            i,  #property); \
         RUNNER_ASSERT_MSG(false, "No listeners expected"); \
     } \
     Catch(MTSharedObject::Exception::ListenerNotFound) { \
@@ -1540,7 +1541,7 @@ RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
 
     // Create and wait for notification. Make sure that the thread/controller 0
     // is created first
-    LogDebug("Creating controllers");
+    WrtLogD("Creating controllers");
     for (size_t i = 0; i < MAX_THREADS; ++i) {
         controller[i] = new ShmController4(&waitable[i]);
         Wait(waitable[i]);
@@ -1550,12 +1551,12 @@ RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
     MTSharedObjectPtr singleton = SharedObjectSingleton::Instance();
 
     for (size_t repeats = 0; repeats < SINGLETON_TEST_REPEATS; ++repeats) {
-        LogDebug("%%%%%%%%%%%%%%%%%%%%%");
-        LogDebug("Iteration " << repeats + 1 << " of " << SINGLETON_TEST_REPEATS);
+        WrtLogD("%%%%%%%%%%%%%%%%%%%%%");
+        WrtLogD("Iteration %u of %u", repeats + 1, SINGLETON_TEST_REPEATS);
         singleton->Clear();
 
         // add listeners
-        LogDebug("Adding listeners");
+        WrtLogD("Adding listeners");
         for (size_t i = 0; i < MAX_THREADS; ++i) {
             controller[i]->PostEvent(ShmController4::ADD_LISTENERS);
         }
@@ -1573,31 +1574,31 @@ RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
         RUNNER_ASSERT(checkArray[2] == 0);
 
         // change
-        LogDebug("Setting property 0");
+        WrtLogD("Setting property 0");
         sho->SetProperty<0>(1);
         // wait for confirmations
         MultipleWait(waitable);
 
         // change
-        LogDebug("Setting property 1");
+        WrtLogD("Setting property 1");
         sho->SetProperty<1>(11);
         // wait for confirmations
         MultipleWait(waitable);
 
         // change
-        LogDebug("Setting property 2");
+        WrtLogD("Setting property 2");
         sho->SetProperty<2>('a');
         // wait for confirmations
         MultipleWait(waitable);
 
         // change
-        LogDebug("Setting property 3");
+        WrtLogD("Setting property 3");
         sho->SetProperty<3>(array);
         // wait for confirmations
         MultipleWait(waitable);
 
         // remove listeners
-        LogDebug("Removing listeners");
+        WrtLogD("Removing listeners");
         for (size_t i = 0; i < MAX_THREADS; ++i) {
             controller[i]->PostEvent(ShmController4::REMOVE_LISTENERS);
         }
@@ -1605,7 +1606,7 @@ RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
         MultipleWait(waitable);
 
         // check if listeners array is empty
-        LogDebug("Checking listeners");
+        WrtLogD("Checking listeners");
         for (size_t i = 0; i < MAX_THREADS; ++i) {
             LISTENER_ASSERT(0);
             LISTENER_ASSERT(1);
@@ -1626,7 +1627,7 @@ RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
 
     // Destroy controllers and wait for confirmation. Make sure that
     // thread/controller 0 is destroyed in the end
-    LogDebug("Destroying controllers");
+    WrtLogD("Destroying controllers");
     for (int i = MAX_THREADS - 1; i >= 0; --i) {
         controller[i]->PostEvent(DESTROY_EVENT);
         Wait(waitable[i]);
@@ -1636,7 +1637,7 @@ RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
              * This is to properly close all waitable handles opened by
              * SharedObject in thread 0.
              */
-            LogDebug("Destroying singleton");
+            WrtLogD("Destroying singleton");
             controller[i]->PostEvent(ShmController4::DESTROY_SINGLETON);
             Wait(waitable[i]);
         }