2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
20 * @brief Implementation file for test cases for shared data framework
26 #include <dpl/semaphore.h>
27 #include <dpl/test_runner.h>
28 #include <dpl/thread.h>
29 #include <dpl/controller.h>
30 #include <dpl/generic_event.h>
32 #include <dpl/shared_object.h>
33 #include <dpl/shared_property.h>
36 RUNNER_TEST_GROUP_INIT(DPL)
41 const SharedMemory::Key SHM_KEY = 12345;
42 const char* SEM_NAME = "/wrt_engine_shared_object_semaphore";
43 const size_t VERSION = 1;
45 const size_t MAX_THREADS = 10;
46 const size_t TEST_AND_SET_REPEATS = 100;
48 const size_t SHARED_PROP_REPEATS = 3;
50 const size_t SINGLETON_TEST_REPEATS = 3;
52 // maximum random delay in singleton listener addition/removal
53 const size_t MAX_SINGLETON_LISTENER_DELAY = 50;
55 const int SINGLE_PROCESS_REPEATS = 50;
58 * 5 seconds expected timeout for waitable events
59 * 30 seconds unexpected timeout for waitable events
60 * We don't want to block tests
62 const size_t EXPECTED_WAITABLE_TIMEOUT = 5 * 1000;
63 const size_t UNEXPECTED_WAITABLE_TIMEOUT = 30 * 1000;
65 bool g_enumTestCorrect = false;
66 bool g_enumTestIncorrect = false;
67 size_t g_delegateCalls = 0;
69 void Wait(DPL::WaitableEvent& event, bool expectedTimeout = false)
71 LogDebug("WaitForSingleHandle...");
72 DPL::WaitableHandleIndexList list = DPL::WaitForSingleHandle(
75 EXPECTED_WAITABLE_TIMEOUT : UNEXPECTED_WAITABLE_TIMEOUT);
76 if (list.size() == 0) {
77 LogDebug("...timeout.");
79 LogDebug("...signaled.");
83 if (expectedTimeout) {
84 RUNNER_ASSERT(list.size() == 0);
86 RUNNER_ASSERT(list.size() == 1);
93 SharedMemory::Remove(SHM_KEY);
95 Catch(SharedMemory::Exception::RemoveFailed) {
100 DPL::Semaphore::Remove(SEM_NAME);
102 Catch(DPL::Semaphore::Exception::RemoveFailed) {
107 typedef DPL::TypeListDecl<int, int, char, int[64]>::Type TestTypeList;
108 typedef DPL::TypeListDecl<int, int, char, int[63]>::Type TestTypeList2;
109 typedef DPL::TypeListDecl<int, int, char, int[63], int>::Type TestTypeList3;
111 typedef SharedObject<TestTypeList> TestSharedObject;
112 typedef SharedObject<TestTypeList2> TestSharedObject2;
113 typedef SharedObject<TestTypeList3> TestSharedObject3;
115 typedef std::shared_ptr<TestSharedObject> TestSharedObjectPtr;
117 const int INIT_EVENT = 0;
118 const int DESTROY_EVENT = 1;
120 int g_values[TestTypeList::Size];
123 * helper listening controller
125 template <typename SharedType>
126 class ListeningController :
127 public DPL::Controller<DPL::TypeListDecl<int>::Type>
130 explicit ListeningController(DPL::WaitableEvent* waitable);
131 ~ListeningController();
133 virtual void OnEventReceived(const int &event);
135 virtual void OnEvent(const int /*event*/) {}
138 std::shared_ptr<SharedType> m_so;
139 DPL::Thread m_thread;
140 DPL::WaitableEvent* m_waitable;
143 template <typename SharedType>
144 ListeningController<SharedType>::ListeningController(
145 DPL::WaitableEvent* waitable) :
150 SwitchToThread(&m_thread);
151 PostEvent(INIT_EVENT);
154 template <typename SharedType>
155 ListeningController<SharedType>::~ListeningController()
160 template <typename SharedType>
161 void ListeningController<SharedType>::OnEventReceived(const int& event)
163 if (event == INIT_EVENT) {
164 m_so = SharedObjectFactory<SharedType>::Create(SHM_KEY, SEM_NAME);
166 m_waitable->Signal();
167 } else if (event == DESTROY_EVENT) {
168 LogDebug("Destroying shared object");
171 // deregister, destroy ad notify main thread
174 m_waitable->Signal();
181 typedef DPL::TypeListDecl<size_t, bool>::Type SharedTypeList;
183 class TestSharedObject4;
184 typedef std::shared_ptr<TestSharedObject4> TestSharedObject4Ptr;
186 class TestSharedObject4 : public SharedObject<SharedTypeList>
195 static TestSharedObject4Ptr Create()
197 return SharedObjectFactory<TestSharedObject4>::Create(SHM_KEY, SEM_NAME);
206 explicit TestSharedObject4(const std::string& semaphore) :
207 SharedObject<SharedTypeList>(semaphore)
213 SetPropertyInternal<BOOLEAN>(false);
215 friend class SharedObjectFactory<TestSharedObject4>;
217 } // anonymus namespace
219 //////////////////////////////////////////////
221 RUNNER_TEST(SharedMemory_002_AccessByType)
225 SharedData<TestTypeList> str;
228 str.Embedded<0, int>::value = 4;
229 str.Embedded<1, int>::value = 5;
230 str.Embedded<2, char>::value = 'd';
231 str.Embedded<3, int[64]>::value[0] = 1;
232 str.Embedded<3, int[64]>::value[1] = 20;
234 RUNNER_ASSERT((str.Embedded<0, int>::value) == 4);
235 RUNNER_ASSERT((str.Embedded<1, int>::value) == 5);
236 RUNNER_ASSERT((str.Embedded<2, char>::value) == 'd');
237 RUNNER_ASSERT((str.Embedded<3, int[64]>::value[0]) == 1);
238 RUNNER_ASSERT((str.Embedded<3, int[64]>::value[1]) == 20);
241 //////////////////////////////////////////////
243 RUNNER_TEST(SharedMemory_003_AccessByIndex)
247 SharedData<TestTypeList> str;
249 str.Embedded<0, TestTypeList::Element<0>::Type>::value = 4;
250 str.Embedded<1, TestTypeList::Element<1>::Type>::value = 5;
251 str.Embedded<2, TestTypeList::Element<2>::Type>::value = 'd';
252 str.Embedded<3, TestTypeList::Element<3>::Type>::value[0] = 1;
253 str.Embedded<3, TestTypeList::Element<3>::Type>::value[1] = 20;
256 (str.Embedded<0, TestTypeList::Element<0>::Type>::value) == 4);
258 (str.Embedded<1, TestTypeList::Element<1>::Type>::value) == 5);
260 (str.Embedded<2, TestTypeList::Element<2>::Type>::value) == 'd');
262 (str.Embedded<3, TestTypeList::Element<3>::Type>::value[0]) == 1);
264 (str.Embedded<3, TestTypeList::Element<3>::Type>::value[1]) == 20);
267 //////////////////////////////////////////////
269 RUNNER_TEST(SharedMemory_004_SimplifiedAccess)
273 SharedData<TestTypeList> str;
275 // access via PropertyRef
276 str.PropertyRef<1>() = 3;
277 RUNNER_ASSERT(str.PropertyRef<1>() == 3);
279 int (&array)[64] = str.PropertyRef<3>();
281 RUNNER_ASSERT(str.PropertyRef<3>()[0] == 2);
283 str.PropertyRef<3>()[1] = 19;
284 RUNNER_ASSERT(str.PropertyRef<3>()[1] == 19);
287 str.SHARED_PROPERTY(0) = 2;
288 RUNNER_ASSERT(str.SHARED_PROPERTY(0) == 2);
290 str.SHARED_PROPERTY(2) = 'c';
291 RUNNER_ASSERT(str.SHARED_PROPERTY(2) == 'c');
293 str.SHARED_PROPERTY(3)[2] = 10;
294 RUNNER_ASSERT(str.SHARED_PROPERTY(3)[2] == 10);
297 RUNNER_ASSERT((str.Embedded<0, int>::value) == 2);
298 RUNNER_ASSERT((str.Embedded<1, int>::value) == 3);
299 RUNNER_ASSERT((str.Embedded<2, char>::value) == 'c');
300 RUNNER_ASSERT((str.Embedded<3, int[64]>::value[0]) == 2);
301 RUNNER_ASSERT((str.Embedded<3, int[64]>::value[1]) == 19);
302 RUNNER_ASSERT((str.Embedded<3, int[64]>::value[2]) == 10);
305 //////////////////////////////////////////////
315 typedef std::shared_ptr<SharedMemory::Segment<SharedStruct> > SharedStructPtr;
317 RUNNER_TEST(SharedMemory_010_BaseShmTest)
321 typedef std::unique_ptr<SharedMemory> SharedMemoryPtr;
326 shm.Reset(SharedMemory::Create<SharedStruct>(SHM_KEY, false));
328 Catch(SharedMemory::Exception::NotFound) {
329 shm.Reset(SharedMemory::Create<SharedStruct>(SHM_KEY, true, true));
332 SharedStructPtr str = shm->Attach<SharedStruct>();
336 str->Data()->c = '3';
337 str->Data()->d[0] = 4;
338 str->Data()->d[1] = 5;
341 SharedMemoryPtr shm2;
343 shm2.Reset(SharedMemory::Create<SharedStruct>(SHM_KEY, false));
345 Catch(SharedMemory::Exception::NotFound) {
346 shm2.Reset(SharedMemory::Create<SharedStruct>(SHM_KEY, true, true));
349 SharedStructPtr str2 = shm2->Attach<SharedStruct>();
350 SharedStructPtr str3 = shm2->Attach<SharedStruct>();
352 RUNNER_ASSERT(str2->Data()->a == 1);
353 RUNNER_ASSERT(str2->Data()->b == 2);
354 RUNNER_ASSERT(str2->Data()->c == '3');
355 RUNNER_ASSERT(str2->Data()->d[0] == 4);
356 RUNNER_ASSERT(str2->Data()->d[1] == 5);
358 RUNNER_ASSERT(str3->Data()->a == 1);
359 RUNNER_ASSERT(str3->Data()->b == 2);
360 RUNNER_ASSERT(str3->Data()->c == '3');
361 RUNNER_ASSERT(str3->Data()->d[0] == 4);
362 RUNNER_ASSERT(str3->Data()->d[1] == 5);
365 str2->Data()->c = 'c';
366 str2->Data()->d[0] = 0;
367 RUNNER_ASSERT(str3->Data()->a == 1);
368 RUNNER_ASSERT(str3->Data()->b == 4);
369 RUNNER_ASSERT(str3->Data()->c == 'c');
370 RUNNER_ASSERT(str3->Data()->d[0] == 0);
371 RUNNER_ASSERT(str3->Data()->d[1] == 5);
374 //////////////////////////////////////////////
376 RUNNER_TEST(SharedMemory_020_SharedObjectTest)
380 typedef SharedObject<SharedTypeList> MySharedObj;
382 MySharedObj::Ptr so =
383 SharedObjectFactory<MySharedObj>::Create(SHM_KEY, SEM_NAME);
385 RUNNER_ASSERT((so->GetProperty<0, size_t>()) == 0);
386 so->SetProperty<0, size_t>(4);
387 RUNNER_ASSERT((so->GetProperty<0, size_t>()) == 4);
390 //////////////////////////////////////////////
392 class InitTestSharedObject : public TestSharedObject
395 explicit InitTestSharedObject(const std::string& semaphore) :
396 TestSharedObject(semaphore) {}
398 virtual void Init(); // from SharedObject
401 friend class SharedObjectFactory<InitTestSharedObject>;
404 void InitTestSharedObject::Init()
406 SetPropertyInternal<0>(1);
407 SetPropertyInternal<1>(2);
408 SetPropertyInternal<2>('c');
411 RUNNER_TEST(SharedMemory_021_InitTest)
413 RemoveIpcs(); // we need non existing shm
415 std::shared_ptr<InitTestSharedObject> sho =
416 SharedObjectFactory<InitTestSharedObject>::Create(
418 RUNNER_ASSERT((sho->GetProperty<0, int>()) == 1);
419 RUNNER_ASSERT((sho->GetProperty<1, int>()) == 2);
420 RUNNER_ASSERT((sho->GetProperty<2, char>()) == 'c');
423 //////////////////////////////////////////////
425 class VersionTestSO1 : public TestSharedObject
428 explicit VersionTestSO1(const std::string& semaphore) :
429 TestSharedObject(semaphore) {}
431 virtual SizeType GetVersion() const
434 } // from SharedObject
437 friend class SharedObjectFactory<VersionTestSO1>;
440 class VersionTestSO2 : public TestSharedObject
443 explicit VersionTestSO2(const std::string& semaphore) :
444 TestSharedObject(semaphore) {}
446 virtual SizeType GetVersion() const
449 } // from SharedObject
452 friend class SharedObjectFactory<VersionTestSO2>;
455 RUNNER_TEST(SharedMemory_022_InvalidVersionTest)
457 RemoveIpcs(); // we need non existing shm
459 std::shared_ptr<VersionTestSO1> sho =
460 SharedObjectFactory<VersionTestSO1>::Create(SHM_KEY, SEM_NAME);
463 std::shared_ptr<VersionTestSO2> sho2 =
464 SharedObjectFactory<VersionTestSO2>::Create(SHM_KEY, SEM_NAME);
466 RUNNER_ASSERT_MSG(false, "Invalid shm version has been accepted");
468 Catch(SharedObjectBase::Exception::InvalidVersion) {
473 //////////////////////////////////////////////
475 RUNNER_TEST(SharedMemory_023_InvalidSizeTest)
477 RemoveIpcs(); // we need non existing shm
479 typedef SharedObject<TestTypeList> SO1;
480 typedef SharedObject<TestTypeList2> SO2;
482 SO1::Ptr sho = SharedObjectFactory<SO1>::Create(SHM_KEY, SEM_NAME);
485 SO2::Ptr sho2 = SharedObjectFactory<SO2>::Create(SHM_KEY, SEM_NAME);
487 RUNNER_ASSERT_MSG(false, "Invalid shm size has been accepted");
489 Catch(SharedObjectBase::Exception::InvalidSize) {
494 //////////////////////////////////////////////
496 class MagicTestSO1 : public TestSharedObject
499 explicit MagicTestSO1(const std::string& semaphore) :
500 TestSharedObject(semaphore) {}
503 virtual MagicType GetMagicNumber() const
509 friend class SharedObjectFactory<MagicTestSO1>;
512 class MagicTestSO2 : public TestSharedObject
515 explicit MagicTestSO2(const std::string& semaphore) :
516 TestSharedObject(semaphore) {}
519 virtual MagicType GetMagicNumber() const
525 friend class SharedObjectFactory<MagicTestSO2>;
528 RUNNER_TEST(SharedMemory_024_InvalidMagicTest)
530 RemoveIpcs(); // we need non existing shm
532 std::shared_ptr<MagicTestSO1> sho =
533 SharedObjectFactory<MagicTestSO1>::Create(SHM_KEY, SEM_NAME);
536 std::shared_ptr<MagicTestSO2> sho2 =
537 SharedObjectFactory<MagicTestSO2>::Create(SHM_KEY, SEM_NAME);
539 RUNNER_ASSERT_MSG(false, "Invalid shm magic number has been accepted");
541 Catch(SharedObjectBase::Exception::InvalidMagicNumber) {
546 //////////////////////////////////////////////
549 * Listening shared object
551 class EnumTestSO1 : public TestSharedObject
554 void SetWaitable(DPL::WaitableEvent* waitable)
556 m_waitable = waitable;
560 explicit EnumTestSO1(const std::string& semaphore) :
561 TestSharedObject(semaphore),
566 virtual void PropertyChanged(size_t propertyEnum);
569 friend class SharedObjectFactory<EnumTestSO1>;
571 DPL::WaitableEvent* m_waitable;
574 void EnumTestSO1::PropertyChanged(size_t propertyEnum)
576 if (propertyEnum == 1) {
577 LogDebug("Property enum " << propertyEnum << " correctly set");
578 g_enumTestCorrect = true;
580 if (propertyEnum == 4) {
581 // This is bad. We only have 4 types
582 LogError("Property enum " << propertyEnum << " should be skipped");
583 g_enumTestIncorrect = true;
585 // confirm property change notification
586 m_waitable->Signal();
589 class EnumController : public ListeningController<EnumTestSO1>
592 explicit EnumController(DPL::WaitableEvent* waitable) :
593 ListeningController<EnumTestSO1>(waitable) {}
595 virtual void OnEvent(const int event);
598 void EnumController::OnEvent(const int event)
600 if (event == INIT_EVENT) {
601 m_so->SetWaitable(m_waitable);
606 * Writing shared object with correct size but different number of types
608 class EnumTestSO2 : public TestSharedObject3
611 explicit EnumTestSO2(const std::string& semaphore) :
612 TestSharedObject3(semaphore) {}
615 friend class SharedObjectFactory<EnumTestSO2>;
618 RUNNER_TEST(SharedMemory_025_InvalidEnumTest)
620 RemoveIpcs(); // we need non existing shm
622 g_enumTestCorrect = false;
623 g_enumTestIncorrect = false;
625 DPL::WaitableEvent waitable;
627 // create listening controller and wait until it registers
628 EnumController controller(&waitable);
630 LogDebug("Listening controller created");
632 // create writing shared object
633 std::shared_ptr<EnumTestSO2> sho2 =
634 SharedObjectFactory<EnumTestSO2>::Create(SHM_KEY, SEM_NAME);
635 DPL::WaitableHandleIndexList list;
637 // write property and wait for confirmation
638 sho2->SetProperty<1>(2);
641 // write incorrect property and wait for confirmation
643 sho2->SetProperty<4>(2);
644 Wait(waitable, true);
646 // schedule listener deregistration and wait for confirmation
647 controller.PostEvent(DESTROY_EVENT);
651 RUNNER_ASSERT(g_enumTestCorrect == true);
652 RUNNER_ASSERT(g_enumTestIncorrect == false);
655 //////////////////////////////////////////////
657 class MultiThreadSO : public TestSharedObject
660 void TestAndSetProperty();
663 explicit MultiThreadSO(const std::string& semaphore) :
664 TestSharedObject(semaphore) {}
667 friend class SharedObjectFactory<MultiThreadSO>;
670 void MultiThreadSO::TestAndSetProperty()
672 ScopedFlaggedLock lock(*this);
674 int value = PropertyRef<0, int>();
675 DPL::Thread::MicroSleep(100);
676 SetPropertyInternal<0>(value + 1);
679 class ShmController : public ListeningController<MultiThreadSO>
682 explicit ShmController(DPL::WaitableEvent* event) :
683 ListeningController<MultiThreadSO>(event), m_counter(0)
686 virtual void OnEventReceived(const int& event);
692 void ShmController::OnEventReceived(const int& event)
694 if (event == INIT_EVENT) {
695 m_so = SharedObjectFactory<MultiThreadSO>::Create(SHM_KEY, SEM_NAME);
697 } else if (event == DESTROY_EVENT) {
698 LogDebug("Destroying shared object");
699 // deregister, destroy ad notify main thread
701 m_waitable->Signal();
702 } else if (event == 2) {
703 m_so->TestAndSetProperty();
705 if (m_counter >= TEST_AND_SET_REPEATS) {
706 LogDebug("Max tests reached. Finishing thread");
707 PostEvent(DESTROY_EVENT);
714 RUNNER_TEST(SharedMemory_030_MultithreadTest)
716 RemoveIpcs(); // we need non existing shm
718 typedef SharedObject<TestTypeList> SHO;
719 SHO::Ptr sho = SharedObjectFactory<SHO>::Create(SHM_KEY, SEM_NAME);
721 ShmController* controller[MAX_THREADS];
722 DPL::WaitableEvent finalEvent[MAX_THREADS];
724 for (size_t i = 0; i < MAX_THREADS; ++i) {
725 controller[i] = new ShmController(&finalEvent[i]);
728 for (size_t i = 0; i < MAX_THREADS; ++i) {
732 for (size_t i = 0; i < MAX_THREADS; ++i) {
733 delete controller[i];
734 controller[i] = NULL;
737 int value = sho->GetProperty<0, int>();
738 LogDebug("Final value is " << value << ", expected " <<
739 MAX_THREADS * TEST_AND_SET_REPEATS);
740 RUNNER_ASSERT(value == MAX_THREADS * TEST_AND_SET_REPEATS);
743 //////////////////////////////////////////////
745 class MyModel10 : public DPL::Model
748 explicit MyModel10(const TestSharedObject4Ptr& shared_object) :
749 DPL::Model(), boolValue(this, shared_object) {}
751 SharedProperty<bool, TestSharedObject4::BOOLEAN, TestSharedObject4>
756 * Listening controller
758 class ShmController3 : public ListeningController<TestSharedObject4>
761 explicit ShmController3(DPL::WaitableEvent* event) :
762 ListeningController<TestSharedObject4>(event)
765 virtual void OnEvent(const int event);
767 void OnValueChanged(const DPL::PropertyEvent<bool>& event);
770 typedef std::unique_ptr<MyModel10> MyModelPtr;
772 // model with property bound to shared object
776 void ShmController3::OnEvent(const int event)
778 if (event == INIT_EVENT) {
779 m_model.Reset(new MyModel10(m_so));
780 m_model->boolValue.AddListener(
781 std::bind(&ShmController3::OnValueChanged, this));
782 } else if (event == DESTROY_EVENT) {
783 m_model->boolValue.RemoveListener(
784 std::bind(&ShmController3::OnValueChanged, this));
789 void ShmController3::OnValueChanged(const DPL::PropertyEvent<bool>& event)
793 m_model->boolValue.Set(false);
795 LogError("Expected value = true, got false");
798 m_waitable->Signal();
801 RUNNER_TEST(SharedMemory_050_SharedProperty)
806 DPL::WaitableEvent waitable;
807 // listener controller
808 ShmController3 controller(&waitable);
811 TestSharedObject4Ptr sharedObject = TestSharedObject4::Create();
813 for (size_t i = 0; i < SHARED_PROP_REPEATS; ++i) {
814 sharedObject->SetProperty<TestSharedObject4::BOOLEAN>(true);
816 result = sharedObject->GetProperty<TestSharedObject4::BOOLEAN,
818 RUNNER_ASSERT(result == false);
820 controller.PostEvent(DESTROY_EVENT);
824 //////////////////////////////////////////////
826 class MyModel2 : public DPL::Model
829 explicit MyModel2(const TestSharedObjectPtr& shared_object) :
830 counter(this, shared_object) {}
832 SharedProperty<int, 0, TestSharedObject> counter;
835 class SPController : public ListeningController<TestSharedObject>
838 explicit SPController(DPL::WaitableEvent* event) :
839 ListeningController<TestSharedObject>(event), m_repeats(1) {}
841 virtual void OnEvent(const int event);
843 void OnValueChanged1(const DPL::PropertyEvent<int>& event);
844 void OnValueChanged2(const DPL::PropertyEvent<int>& event);
847 std::unique_ptr<MyModel2> m_model1;
848 std::unique_ptr<MyModel2> m_model2;
851 std::shared_ptr<TestSharedObject> m_so2;
854 void SPController::OnEvent(const int event)
856 if (event == INIT_EVENT) {
857 m_so2 = SharedObjectFactory<TestSharedObject>::Create(SHM_KEY,
860 // create and register 2 models sharing the same property
861 m_model1.Reset(new MyModel2(m_so));
862 m_model2.Reset(new MyModel2(m_so2));
863 m_model1->counter.AddListener(
864 std::bind(&SPController::OnValueChanged1, this));
865 m_model2->counter.AddListener(
866 std::bind(&SPController::OnValueChanged2, this));
867 m_model1->counter.Set(1);
868 } else if (event == DESTROY_EVENT) {
869 m_model1->counter.RemoveListener(
870 std::bind(&SPController::OnValueChanged1, this));
871 m_model2->counter.RemoveListener(
872 std::bind(&SPController::OnValueChanged2, this));
880 void SPController::OnValueChanged1(const DPL::PropertyEvent<int>& event)
882 if (m_repeats >= SINGLE_PROCESS_REPEATS) {
883 PostEvent(DESTROY_EVENT);
887 LogDebug("[1] Value changed to " << event.value);
889 m_model1->counter.Set(event.value + 1);
892 void SPController::OnValueChanged2(const DPL::PropertyEvent<int>& event)
894 if (m_repeats >= SINGLE_PROCESS_REPEATS) {
895 PostEvent(DESTROY_EVENT);
899 LogDebug("[2] Value changed to " << event.value);
901 m_model2->counter.Set(event.value + 1);
904 RUNNER_TEST(SharedMemory_060_SingleProcess)
908 DPL::WaitableEvent waitable;
909 SPController controller(&waitable);
910 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
917 // wait for destruction
920 int value = sho->GetProperty<0, int>();
922 LogDebug("final value: " << value);
925 RUNNER_ASSERT(value == SINGLE_PROCESS_REPEATS);
928 //////////////////////////////////////////////
930 class ListenerTestController : public ListeningController<TestSharedObject>,
931 public ISharedObjectListener<0, int>,
932 public ISharedObjectListener<1, int>,
933 public ISharedObjectListener<2, char>,
934 public ISharedObjectListener<3, int[64]>
937 explicit ListenerTestController(DPL::WaitableEvent* event) :
938 ListeningController<TestSharedObject>(event) {}
940 ~ListenerTestController();
942 virtual void OnEvent(const int event);
944 virtual void ValueChanged(size_t propertyEnum,
946 const void* info = NULL);
947 virtual void ValueChanged(size_t propertyEnum,
949 const void* info = NULL);
950 virtual void ValueChanged(size_t propertyEnum,
951 const int(&value)[64],
952 const void* info = NULL);
955 ListenerTestController::~ListenerTestController()
958 void ListenerTestController::OnEvent(const int event)
960 if (event == INIT_EVENT) {
961 // add self as a listener to shared object
962 m_so->AddListener<0, int>(this);
963 m_so->AddListener<1, int>(this);
964 m_so->AddListener<2, char>(this);
965 m_so->AddListener<3, int[64]>(this);
966 } else if (event == DESTROY_EVENT) {
967 // remove self from listener list
968 m_so->RemoveListener<0, int>(this);
969 m_so->RemoveListener<1, int>(this);
970 m_so->RemoveListener<2, char>(this);
971 m_so->RemoveListener<3, int[64]>(this);
975 void ListenerTestController::ValueChanged(size_t propertyEnum,
977 const void* /*info*/)
979 LogDebug("ValueChanged(int) " << propertyEnum << " " << value);
980 if ((propertyEnum == 0 &&
981 value == 1) || (propertyEnum == 1 && value == 2))
983 g_values[propertyEnum]++;
984 if (g_values[propertyEnum] == 3) {
985 m_waitable->Signal();
990 void ListenerTestController::ValueChanged(size_t propertyEnum,
992 const void* /*info*/)
994 LogDebug("ValueChanged(char) " << propertyEnum << " " << value);
995 if (propertyEnum == 2 && value == 'c') {
996 g_values[propertyEnum]++;
997 if (g_values[propertyEnum] == 3) {
998 m_waitable->Signal();
1003 void ListenerTestController::ValueChanged(size_t propertyEnum,
1004 const int(&value)[64],
1005 const void* /*info*/)
1007 LogDebug("ValueChanged(int[64]) " << propertyEnum << " " << value[5]);
1008 if (propertyEnum == 3 && value[5] == 5) {
1009 g_values[propertyEnum]++;
1010 if (g_values[propertyEnum] == 3) {
1011 m_waitable->Signal();
1016 RUNNER_TEST(SharedMemory_070_SharedObjectListeners)
1020 // setup global flags
1021 for (size_t i = 0; i < TestTypeList::Size; ++i) {
1025 // create shared object
1026 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
1029 // create 1st listener and wait for it
1030 DPL::WaitableEvent waitable;
1031 ListenerTestController c1(&waitable);
1034 // create 2nd listener and wait for it
1035 ListenerTestController c2(&waitable);
1038 // create 3rd listener and wait for it
1039 ListenerTestController c3(&waitable);
1042 // set properties and wait for result
1043 sho->SetProperty<0, int>(1);
1046 RUNNER_ASSERT(g_values[0] == 3);
1048 sho->SetProperty<1, int>(2);
1051 RUNNER_ASSERT(g_values[1] == 3);
1053 sho->SetProperty<2, char>('c');
1056 RUNNER_ASSERT(g_values[2] == 3);
1059 memset(array, 0, 64 * sizeof(array[0]));
1061 sho->SetProperty<3, int[64]>(array);
1064 RUNNER_ASSERT(g_values[3] == 3);
1066 // finalize listeners
1067 c1.PostEvent(DESTROY_EVENT);
1070 c2.PostEvent(DESTROY_EVENT);
1073 c3.PostEvent(DESTROY_EVENT);
1077 //////////////////////////////////////////////
1080 * class simulating DB access
1082 class DAO : public DPL::Noncopyable
1085 DAO() : m_boolValue(false) {}
1087 void SetBoolValue(const bool& value)
1089 m_boolValue = value;
1092 bool GetBoolValue() const
1102 * Model with property having set delegate defined
1104 class MyModel3 : public DPL::Model
1107 typedef SharedPropertyEx<bool,
1108 TestSharedObject4::BOOLEAN,
1109 TestSharedObject4> PropertyType;
1111 MyModel3(const TestSharedObject4Ptr& shared_object, DAO* dao) :
1114 PropertyType::SetDelegate(dao, &DAO::SetBoolValue))
1117 PropertyType boolValue;
1120 RUNNER_TEST(SharedMemory_090_SetPropertyDelegate)
1127 // create shared object
1128 TestSharedObject4Ptr sho = TestSharedObject4::Create();
1130 // set property but call dao delegate within semaphore
1131 sho->SetProperty<TestSharedObject4::BOOLEAN>(
1133 MyModel3::PropertyType::SetDelegate(&dao, &DAO::SetBoolValue));
1136 RUNNER_ASSERT(dao.GetBoolValue() == true);
1138 // check shared object value
1139 bool shoValue = sho->GetProperty<TestSharedObject4::BOOLEAN, bool>();
1140 RUNNER_ASSERT(shoValue == true);
1142 // try the same with shared property
1143 MyModel3 model(sho, &dao);
1146 model.boolValue.Set(false);
1149 RUNNER_ASSERT(dao.GetBoolValue() == false);
1152 shoValue = sho->GetProperty<TestSharedObject4::BOOLEAN, bool>();
1153 RUNNER_ASSERT(shoValue == false);
1155 // check property value
1156 RUNNER_ASSERT(model.boolValue.Get() == false);
1159 //////////////////////////////////////////////
1162 * Lazy initialization test shared object
1164 class LazySharedObject : public SharedObject<TestTypeList>
1167 LazySharedObject() :
1172 explicit LazySharedObject(const std::string& semaphore) :
1173 SharedObject<TestTypeList>(semaphore)
1185 friend class SharedObjectFactory<LazySharedObject>;
1190 void LazySharedObject::Init()
1192 SetPropertyInternal<0>(42);
1196 RUNNER_TEST(SharedMemory_100_LazyInit)
1200 typedef std::shared_ptr<LazySharedObject> LazySharedObjectPtr;
1202 // create shared object
1203 LazySharedObjectPtr sho = SharedObjectFactory<LazySharedObject>::Create(
1206 RUNNER_ASSERT(sho->IsRead() == false);
1208 // get property causing lazy init
1209 int value = sho->GetProperty<0, int>();
1211 RUNNER_ASSERT(sho->IsRead() == true);
1212 RUNNER_ASSERT(value == 42);
1214 // create another object
1215 LazySharedObjectPtr sho2 = SharedObjectFactory<LazySharedObject>::Create(
1218 RUNNER_ASSERT(sho2->IsRead() == false);
1220 // get property NOT causing lazy init
1221 value = sho2->GetProperty<0, int>();
1223 RUNNER_ASSERT(sho2->IsRead() == false);
1224 RUNNER_ASSERT(value == 42);
1226 // destroy both objects
1230 // create shared object
1231 LazySharedObjectPtr sho3 = SharedObjectFactory<LazySharedObject>::Create(
1234 RUNNER_ASSERT(sho3->IsRead() == false);
1236 // set property causing lazy init
1237 sho3->SetProperty<0>(43);
1238 value = sho3->GetProperty<0, int>();
1240 RUNNER_ASSERT(sho3->IsRead() == true);
1241 RUNNER_ASSERT(value == 43);
1244 //////////////////////////////////////////////
1246 bool SetCondition(const int& readValue, int& setValue);
1247 bool SetCondition(const int& readValue, int& setValue)
1249 LogDebug("Condition delegate called with read value = " << readValue <<
1250 " and set value = " << setValue);
1252 if (readValue > 3) {
1253 LogDebug("Condition is false");
1257 LogDebug("Condition is true");
1258 if (4 == setValue) {
1260 LogDebug("Changing set value to " << setValue);
1265 void SetDelegate(const int& readValue);
1266 void SetDelegate(const int& readValue)
1268 LogDebug("Set delegate called " << readValue);
1272 RUNNER_TEST(SharedMemory_120_ConditionalSet)
1276 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
1280 g_delegateCalls = 0;
1282 RUNNER_ASSERT(0 == (sho->GetProperty<0, int>()));
1284 std::function<bool (const int&, int&)> condition = SetCondition;
1285 std::function<void (const int&)> delegate = SetDelegate;
1287 bool succeeded = false;
1289 succeeded = sho->ConditionalSetProperty<0>(-2, condition);
1291 RUNNER_ASSERT(succeeded);
1292 RUNNER_ASSERT(-2 == (sho->GetProperty<0, int>()));
1294 succeeded = sho->ConditionalSetProperty<0>(4, condition, delegate);
1296 RUNNER_ASSERT(succeeded);
1297 RUNNER_ASSERT(10 == (sho->GetProperty<0, int>()));
1298 RUNNER_ASSERT(1 == g_delegateCalls);
1300 succeeded = sho->ConditionalSetProperty<0>(5, condition);
1302 RUNNER_ASSERT(!succeeded);
1303 RUNNER_ASSERT(10 == (sho->GetProperty<0, int>()));
1305 succeeded = sho->ConditionalSetProperty<0>(666, condition, delegate);
1307 RUNNER_ASSERT(!succeeded);
1308 RUNNER_ASSERT(10 == (sho->GetProperty<0, int>()));
1309 RUNNER_ASSERT(1 == g_delegateCalls);
1312 //////////////////////////////////////////////
1315 * Shared object used by multiple threads as a singleton.
1317 class MTSharedObject : public SharedObject<TestTypeList>
1320 explicit MTSharedObject(const std::string& semaphore) :
1321 SharedObject<TestTypeList>(semaphore)
1327 friend class SharedObjectFactory<MTSharedObject>;
1330 typedef std::shared_ptr<MTSharedObject> MTSharedObjectPtr;
1332 void MTSharedObject::Clear()
1337 SetProperty<2>(static_cast<char>(0));
1338 SetProperty<3>(array);
1342 * Shared object singleton
1344 class SharedObjectSingleton
1347 static MTSharedObjectPtr Instance();
1348 static void Destroy();
1351 static MTSharedObjectPtr m_sho;
1352 static DPL::Mutex m_mutex;
1355 MTSharedObjectPtr SharedObjectSingleton::m_sho;
1356 DPL::Mutex SharedObjectSingleton::m_mutex;
1358 MTSharedObjectPtr SharedObjectSingleton::Instance()
1360 DPL::Mutex::ScopedLock lock(&m_mutex);
1362 m_sho = SharedObjectFactory<MTSharedObject>::Create(SHM_KEY, SEM_NAME);
1367 void SharedObjectSingleton::Destroy()
1369 DPL::Mutex::ScopedLock lock(&m_mutex);
1374 * Listening controller
1376 class ShmController4 : public ListeningController<MTSharedObject>,
1377 public ISharedObjectListener<0, int>,
1378 public ISharedObjectListener<1, int>,
1379 public ISharedObjectListener<2, char>,
1380 public ISharedObjectListener<3, int[64]>
1385 REMOVE_LISTENERS = 3,
1386 DESTROY_SINGLETON = 4
1389 explicit ShmController4(DPL::WaitableEvent* event) :
1390 ListeningController<MTSharedObject>(event),
1394 virtual void OnEventReceived(const int& event);
1396 virtual void ValueChanged(size_t propertyEnum,
1398 const void* info = NULL);
1399 virtual void ValueChanged(size_t propertyEnum,
1401 const void* info = NULL);
1402 virtual void ValueChanged(size_t propertyEnum,
1403 const int(&value)[64],
1404 const void* info = NULL);
1406 bool NotRegistered();
1412 static unsigned int seed = time(NULL);
1415 void ShmController4::ValueChanged(size_t propertyEnum,
1417 const void* /*info*/)
1419 LogDebug("ValueChanged(int) " << propertyEnum << " " << value);
1420 if ((propertyEnum == 0 && value == 1) ||
1421 (propertyEnum == 1 && value == 11))
1423 m_waitable->Signal();
1427 void ShmController4::ValueChanged(size_t propertyEnum,
1429 const void* /*info*/)
1431 LogDebug("ValueChanged(char) " << propertyEnum << " " << value);
1432 if (propertyEnum == 2 && value == 'a') {
1433 m_waitable->Signal();
1437 void ShmController4::ValueChanged(size_t propertyEnum,
1438 const int(&value)[64],
1439 const void* /*info*/)
1441 LogDebug("ValueChanged(int[64]) " << propertyEnum << " " << value[5]);
1442 if (propertyEnum == 3 && value[0] == 0 && value[1] == 1 && value[2] == 2) {
1443 m_waitable->Signal();
1447 void ShmController4::Sleep()
1449 DPL::Thread::GetCurrentThread()->MiliSleep(
1450 rand_r(&seed) % MAX_SINGLETON_LISTENER_DELAY);
1453 void ShmController4::OnEventReceived(const int& event)
1457 m_so = SharedObjectSingleton::Instance();
1458 m_waitable->Signal();
1462 LogDebug("Destroying shared object");
1463 // deregister, destroy and notify main thread
1465 m_waitable->Signal();
1469 // add listener and notify
1470 m_so->AddListener<0, int>(this);
1472 m_so->AddListener<1, int>(this);
1474 m_so->AddListener<2, char>(this);
1476 m_so->AddListener<3, int[64]>(this);
1478 m_waitable->Signal();
1481 case REMOVE_LISTENERS:
1482 // remove listener and notify
1483 m_so->RemoveListener<0, int>(this);
1485 m_so->RemoveListener<1, int>(this);
1487 m_so->RemoveListener<2, char>(this);
1489 m_so->RemoveListener<3, int[64]>(this);
1491 m_waitable->Signal();
1494 case DESTROY_SINGLETON:
1495 SharedObjectSingleton::Destroy();
1496 m_waitable->Signal();
1500 LogError("Unsupported event received: " << event);
1504 void MultipleWait(DPL::WaitableEvent(&event)[MAX_THREADS]);
1505 void MultipleWait(DPL::WaitableEvent(&event)[MAX_THREADS])
1507 for (size_t i = 0; i < MAX_THREADS; ++i) {
1513 * Try to remove property listener. If there's no such listener an exception
1516 #define LISTENER_ASSERT(property) \
1518 singleton->RemoveListener<(property)>(controller[i]); \
1519 LogError("Controller " << i << " is still listening for property " \
1521 RUNNER_ASSERT_MSG(false, "No listeners expected"); \
1523 Catch(MTSharedObject::Exception::ListenerNotFound) { \
1524 RUNNER_ASSERT(true); \
1528 RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
1530 RemoveIpcs(); // we need non existing shm
1532 // writer shared object
1533 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
1536 ShmController4* controller[MAX_THREADS];
1537 DPL::WaitableEvent waitable[MAX_THREADS];
1539 const int array[64] = { 0, 1, 2 };
1541 // Create and wait for notification. Make sure that the thread/controller 0
1543 LogDebug("Creating controllers");
1544 for (size_t i = 0; i < MAX_THREADS; ++i) {
1545 controller[i] = new ShmController4(&waitable[i]);
1549 // singleton will be created by thread/controller 0 by now
1550 MTSharedObjectPtr singleton = SharedObjectSingleton::Instance();
1552 for (size_t repeats = 0; repeats < SINGLETON_TEST_REPEATS; ++repeats) {
1553 LogDebug("%%%%%%%%%%%%%%%%%%%%%");
1554 LogDebug("Iteration " << repeats + 1 << " of " << SINGLETON_TEST_REPEATS);
1558 LogDebug("Adding listeners");
1559 for (size_t i = 0; i < MAX_THREADS; ++i) {
1560 controller[i]->PostEvent(ShmController4::ADD_LISTENERS);
1562 // wait for listeners
1563 MultipleWait(waitable);
1565 RUNNER_ASSERT((singleton->GetProperty<0, int>()) == 0);
1566 RUNNER_ASSERT((singleton->GetProperty<1, int>()) == 0);
1567 RUNNER_ASSERT((singleton->GetProperty<2, char>()) == 0);
1569 int checkArray[64] = {};
1570 singleton->GetProperty<3>(checkArray);
1571 RUNNER_ASSERT(checkArray[0] == 0);
1572 RUNNER_ASSERT(checkArray[1] == 0);
1573 RUNNER_ASSERT(checkArray[2] == 0);
1576 LogDebug("Setting property 0");
1577 sho->SetProperty<0>(1);
1578 // wait for confirmations
1579 MultipleWait(waitable);
1582 LogDebug("Setting property 1");
1583 sho->SetProperty<1>(11);
1584 // wait for confirmations
1585 MultipleWait(waitable);
1588 LogDebug("Setting property 2");
1589 sho->SetProperty<2>('a');
1590 // wait for confirmations
1591 MultipleWait(waitable);
1594 LogDebug("Setting property 3");
1595 sho->SetProperty<3>(array);
1596 // wait for confirmations
1597 MultipleWait(waitable);
1600 LogDebug("Removing listeners");
1601 for (size_t i = 0; i < MAX_THREADS; ++i) {
1602 controller[i]->PostEvent(ShmController4::REMOVE_LISTENERS);
1604 // wait for listeners
1605 MultipleWait(waitable);
1607 // check if listeners array is empty
1608 LogDebug("Checking listeners");
1609 for (size_t i = 0; i < MAX_THREADS; ++i) {
1616 RUNNER_ASSERT((singleton->GetProperty<0, int>()) == 1);
1617 RUNNER_ASSERT((singleton->GetProperty<1, int>()) == 11);
1618 RUNNER_ASSERT((singleton->GetProperty<2, char>()) == 'a');
1619 singleton->GetProperty<3>(checkArray);
1620 RUNNER_ASSERT(checkArray[0] == 0);
1621 RUNNER_ASSERT(checkArray[1] == 1);
1622 RUNNER_ASSERT(checkArray[2] == 2);
1627 // Destroy controllers and wait for confirmation. Make sure that
1628 // thread/controller 0 is destroyed in the end
1629 LogDebug("Destroying controllers");
1630 for (int i = MAX_THREADS - 1; i >= 0; --i) {
1631 controller[i]->PostEvent(DESTROY_EVENT);
1635 * Destroy singleton before thread that created it finishes.
1636 * This is to properly close all waitable handles opened by
1637 * SharedObject in thread 0.
1639 LogDebug("Destroying singleton");
1640 controller[i]->PostEvent(ShmController4::DESTROY_SINGLETON);
1643 delete controller[i];
1647 #undef LISTENER_ASSERT
1650 * test preconditions & postconditions:
1651 * - no existing shared memory with given SHM_KEY
1652 * - no existing semaphore of given SEM_NAME
1654 RUNNER_TEST(SharedMemory_001_Preconditions) {
1658 RUNNER_TEST(SharedMemory_999_Postconditions) {