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) {}
563 virtual void PropertyChanged(size_t propertyEnum);
566 friend class SharedObjectFactory<EnumTestSO1>;
568 DPL::WaitableEvent* m_waitable;
571 void EnumTestSO1::PropertyChanged(size_t propertyEnum)
573 if (propertyEnum == 1) {
574 LogDebug("Property enum " << propertyEnum << " correctly set");
575 g_enumTestCorrect = true;
577 if (propertyEnum == 4) {
578 // This is bad. We only have 4 types
579 LogError("Property enum " << propertyEnum << " should be skipped");
580 g_enumTestIncorrect = true;
582 // confirm property change notification
583 m_waitable->Signal();
586 class EnumController : public ListeningController<EnumTestSO1>
589 explicit EnumController(DPL::WaitableEvent* waitable) :
590 ListeningController<EnumTestSO1>(waitable) {}
592 virtual void OnEvent(const int event);
595 void EnumController::OnEvent(const int event)
597 if (event == INIT_EVENT) {
598 m_so->SetWaitable(m_waitable);
603 * Writing shared object with correct size but different number of types
605 class EnumTestSO2 : public TestSharedObject3
608 explicit EnumTestSO2(const std::string& semaphore) :
609 TestSharedObject3(semaphore) {}
612 friend class SharedObjectFactory<EnumTestSO2>;
615 RUNNER_TEST(SharedMemory_025_InvalidEnumTest)
617 RemoveIpcs(); // we need non existing shm
619 g_enumTestCorrect = false;
620 g_enumTestIncorrect = false;
622 DPL::WaitableEvent waitable;
624 // create listening controller and wait until it registers
625 EnumController controller(&waitable);
627 LogDebug("Listening controller created");
629 // create writing shared object
630 std::shared_ptr<EnumTestSO2> sho2 =
631 SharedObjectFactory<EnumTestSO2>::Create(SHM_KEY, SEM_NAME);
632 DPL::WaitableHandleIndexList list;
634 // write property and wait for confirmation
635 sho2->SetProperty<1>(2);
638 // write incorrect property and wait for confirmation
640 sho2->SetProperty<4>(2);
641 Wait(waitable, true);
643 // schedule listener deregistration and wait for confirmation
644 controller.PostEvent(DESTROY_EVENT);
648 RUNNER_ASSERT(g_enumTestCorrect == true);
649 RUNNER_ASSERT(g_enumTestIncorrect == false);
652 //////////////////////////////////////////////
654 class MultiThreadSO : public TestSharedObject
657 void TestAndSetProperty();
660 explicit MultiThreadSO(const std::string& semaphore) :
661 TestSharedObject(semaphore) {}
664 friend class SharedObjectFactory<MultiThreadSO>;
667 void MultiThreadSO::TestAndSetProperty()
669 ScopedFlaggedLock lock(*this);
671 int value = PropertyRef<0, int>();
672 DPL::Thread::MicroSleep(100);
673 SetPropertyInternal<0>(value + 1);
676 class ShmController : public ListeningController<MultiThreadSO>
679 explicit ShmController(DPL::WaitableEvent* event) :
680 ListeningController<MultiThreadSO>(event), m_counter(0)
683 virtual void OnEventReceived(const int& event);
689 void ShmController::OnEventReceived(const int& event)
691 if (event == INIT_EVENT) {
692 m_so = SharedObjectFactory<MultiThreadSO>::Create(SHM_KEY, SEM_NAME);
694 } else if (event == DESTROY_EVENT) {
695 LogDebug("Destroying shared object");
696 // deregister, destroy ad notify main thread
698 m_waitable->Signal();
699 } else if (event == 2) {
700 m_so->TestAndSetProperty();
702 if (m_counter >= TEST_AND_SET_REPEATS) {
703 LogDebug("Max tests reached. Finishing thread");
704 PostEvent(DESTROY_EVENT);
711 RUNNER_TEST(SharedMemory_030_MultithreadTest)
713 RemoveIpcs(); // we need non existing shm
715 typedef SharedObject<TestTypeList> SHO;
716 SHO::Ptr sho = SharedObjectFactory<SHO>::Create(SHM_KEY, SEM_NAME);
718 ShmController* controller[MAX_THREADS];
719 DPL::WaitableEvent finalEvent[MAX_THREADS];
721 for (size_t i = 0; i < MAX_THREADS; ++i) {
722 controller[i] = new ShmController(&finalEvent[i]);
725 for (size_t i = 0; i < MAX_THREADS; ++i) {
729 for (size_t i = 0; i < MAX_THREADS; ++i) {
730 delete controller[i];
731 controller[i] = NULL;
734 int value = sho->GetProperty<0, int>();
735 LogDebug("Final value is " << value << ", expected " <<
736 MAX_THREADS * TEST_AND_SET_REPEATS);
737 RUNNER_ASSERT(value == MAX_THREADS * TEST_AND_SET_REPEATS);
740 //////////////////////////////////////////////
742 class MyModel10 : public DPL::Model
745 explicit MyModel10(const TestSharedObject4Ptr& shared_object) :
746 DPL::Model(), boolValue(this, shared_object) {}
748 SharedProperty<bool, TestSharedObject4::BOOLEAN, TestSharedObject4>
753 * Listening controller
755 class ShmController3 : public ListeningController<TestSharedObject4>
758 explicit ShmController3(DPL::WaitableEvent* event) :
759 ListeningController<TestSharedObject4>(event)
762 virtual void OnEvent(const int event);
764 void OnValueChanged(const DPL::PropertyEvent<bool>& event);
767 typedef std::unique_ptr<MyModel10> MyModelPtr;
769 // model with property bound to shared object
773 void ShmController3::OnEvent(const int event)
775 if (event == INIT_EVENT) {
776 m_model.Reset(new MyModel10(m_so));
777 m_model->boolValue.AddListener(
778 DPL::FastDelegate<void (const DPL::PropertyEvent<bool>&)>(
780 &ShmController3::OnValueChanged));
781 } else if (event == DESTROY_EVENT) {
782 m_model->boolValue.RemoveListener(
783 DPL::FastDelegate<void (const DPL::PropertyEvent<bool>&)>(
785 &ShmController3::OnValueChanged));
790 void ShmController3::OnValueChanged(const DPL::PropertyEvent<bool>& event)
794 m_model->boolValue.Set(false);
796 LogError("Expected value = true, got false");
799 m_waitable->Signal();
802 RUNNER_TEST(SharedMemory_050_SharedProperty)
807 DPL::WaitableEvent waitable;
808 // listener controller
809 ShmController3 controller(&waitable);
812 TestSharedObject4Ptr sharedObject = TestSharedObject4::Create();
814 for (size_t i = 0; i < SHARED_PROP_REPEATS; ++i) {
815 sharedObject->SetProperty<TestSharedObject4::BOOLEAN>(true);
817 result = sharedObject->GetProperty<TestSharedObject4::BOOLEAN,
819 RUNNER_ASSERT(result == false);
821 controller.PostEvent(DESTROY_EVENT);
825 //////////////////////////////////////////////
827 class MyModel2 : public DPL::Model
830 explicit MyModel2(const TestSharedObjectPtr& shared_object) :
831 counter(this, shared_object) {}
833 SharedProperty<int, 0, TestSharedObject> counter;
836 class SPController : public ListeningController<TestSharedObject>
839 explicit SPController(DPL::WaitableEvent* event) :
840 ListeningController<TestSharedObject>(event), m_repeats(1) {}
842 virtual void OnEvent(const int event);
844 void OnValueChanged1(const DPL::PropertyEvent<int>& event);
845 void OnValueChanged2(const DPL::PropertyEvent<int>& event);
848 std::unique_ptr<MyModel2> m_model1;
849 std::unique_ptr<MyModel2> m_model2;
852 std::shared_ptr<TestSharedObject> m_so2;
855 void SPController::OnEvent(const int event)
857 if (event == INIT_EVENT) {
858 m_so2 = SharedObjectFactory<TestSharedObject>::Create(SHM_KEY,
861 // create and register 2 models sharing the same property
862 m_model1.Reset(new MyModel2(m_so));
863 m_model2.Reset(new MyModel2(m_so2));
864 m_model1->counter.AddListener(
865 DPL::FastDelegate<void (const DPL::PropertyEvent<int>&)>(
867 &SPController::OnValueChanged1));
868 m_model2->counter.AddListener(
869 DPL::FastDelegate<void (const DPL::PropertyEvent<int>&)>(
871 &SPController::OnValueChanged2));
872 m_model1->counter.Set(1);
873 } else if (event == DESTROY_EVENT) {
874 m_model1->counter.RemoveListener(
875 DPL::FastDelegate<void (const DPL::PropertyEvent<int>&)>(
877 &SPController::OnValueChanged1));
878 m_model2->counter.RemoveListener(
879 DPL::FastDelegate<void (const DPL::PropertyEvent<int>&)>(
881 &SPController::OnValueChanged2));
889 void SPController::OnValueChanged1(const DPL::PropertyEvent<int>& event)
891 if (m_repeats >= SINGLE_PROCESS_REPEATS) {
892 PostEvent(DESTROY_EVENT);
896 LogDebug("[1] Value changed to " << event.value);
898 m_model1->counter.Set(event.value + 1);
901 void SPController::OnValueChanged2(const DPL::PropertyEvent<int>& event)
903 if (m_repeats >= SINGLE_PROCESS_REPEATS) {
904 PostEvent(DESTROY_EVENT);
908 LogDebug("[2] Value changed to " << event.value);
910 m_model2->counter.Set(event.value + 1);
913 RUNNER_TEST(SharedMemory_060_SingleProcess)
917 DPL::WaitableEvent waitable;
918 SPController controller(&waitable);
919 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
926 // wait for destruction
929 int value = sho->GetProperty<0, int>();
931 LogDebug("final value: " << value);
934 RUNNER_ASSERT(value == SINGLE_PROCESS_REPEATS);
937 //////////////////////////////////////////////
939 class ListenerTestController : public ListeningController<TestSharedObject>,
940 public ISharedObjectListener<0, int>,
941 public ISharedObjectListener<1, int>,
942 public ISharedObjectListener<2, char>,
943 public ISharedObjectListener<3, int[64]>
946 explicit ListenerTestController(DPL::WaitableEvent* event) :
947 ListeningController<TestSharedObject>(event) {}
949 ~ListenerTestController();
951 virtual void OnEvent(const int event);
953 virtual void ValueChanged(size_t propertyEnum,
955 const void* info = NULL);
956 virtual void ValueChanged(size_t propertyEnum,
958 const void* info = NULL);
959 virtual void ValueChanged(size_t propertyEnum,
960 const int(&value)[64],
961 const void* info = NULL);
964 ListenerTestController::~ListenerTestController()
967 void ListenerTestController::OnEvent(const int event)
969 if (event == INIT_EVENT) {
970 // add self as a listener to shared object
971 m_so->AddListener<0, int>(this);
972 m_so->AddListener<1, int>(this);
973 m_so->AddListener<2, char>(this);
974 m_so->AddListener<3, int[64]>(this);
975 } else if (event == DESTROY_EVENT) {
976 // remove self from listener list
977 m_so->RemoveListener<0, int>(this);
978 m_so->RemoveListener<1, int>(this);
979 m_so->RemoveListener<2, char>(this);
980 m_so->RemoveListener<3, int[64]>(this);
984 void ListenerTestController::ValueChanged(size_t propertyEnum,
986 const void* /*info*/)
988 LogDebug("ValueChanged(int) " << propertyEnum << " " << value);
989 if ((propertyEnum == 0 &&
990 value == 1) || (propertyEnum == 1 && value == 2))
992 g_values[propertyEnum]++;
993 if (g_values[propertyEnum] == 3) {
994 m_waitable->Signal();
999 void ListenerTestController::ValueChanged(size_t propertyEnum,
1001 const void* /*info*/)
1003 LogDebug("ValueChanged(char) " << propertyEnum << " " << value);
1004 if (propertyEnum == 2 && value == 'c') {
1005 g_values[propertyEnum]++;
1006 if (g_values[propertyEnum] == 3) {
1007 m_waitable->Signal();
1012 void ListenerTestController::ValueChanged(size_t propertyEnum,
1013 const int(&value)[64],
1014 const void* /*info*/)
1016 LogDebug("ValueChanged(int[64]) " << propertyEnum << " " << value[5]);
1017 if (propertyEnum == 3 && value[5] == 5) {
1018 g_values[propertyEnum]++;
1019 if (g_values[propertyEnum] == 3) {
1020 m_waitable->Signal();
1025 RUNNER_TEST(SharedMemory_070_SharedObjectListeners)
1029 // setup global flags
1030 for (size_t i = 0; i < TestTypeList::Size; ++i) {
1034 // create shared object
1035 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
1038 // create 1st listener and wait for it
1039 DPL::WaitableEvent waitable;
1040 ListenerTestController c1(&waitable);
1043 // create 2nd listener and wait for it
1044 ListenerTestController c2(&waitable);
1047 // create 3rd listener and wait for it
1048 ListenerTestController c3(&waitable);
1051 // set properties and wait for result
1052 sho->SetProperty<0, int>(1);
1055 RUNNER_ASSERT(g_values[0] == 3);
1057 sho->SetProperty<1, int>(2);
1060 RUNNER_ASSERT(g_values[1] == 3);
1062 sho->SetProperty<2, char>('c');
1065 RUNNER_ASSERT(g_values[2] == 3);
1068 memset(array, 64 * sizeof(array[0]), 0);
1070 sho->SetProperty<3, int[64]>(array);
1073 RUNNER_ASSERT(g_values[3] == 3);
1075 // finalize listeners
1076 c1.PostEvent(DESTROY_EVENT);
1079 c2.PostEvent(DESTROY_EVENT);
1082 c3.PostEvent(DESTROY_EVENT);
1086 //////////////////////////////////////////////
1089 * class simulating DB access
1091 class DAO : public DPL::Noncopyable
1094 DAO() : m_boolValue(false) {}
1096 void SetBoolValue(const bool& value)
1098 m_boolValue = value;
1101 bool GetBoolValue() const
1111 * Model with property having set delegate defined
1113 class MyModel3 : public DPL::Model
1116 typedef SharedPropertyEx<bool,
1117 TestSharedObject4::BOOLEAN,
1118 TestSharedObject4> PropertyType;
1120 MyModel3(const TestSharedObject4Ptr& shared_object, DAO* dao) :
1123 PropertyType::SetDelegate(dao, &DAO::SetBoolValue))
1126 PropertyType boolValue;
1129 RUNNER_TEST(SharedMemory_090_SetPropertyDelegate)
1136 // create shared object
1137 TestSharedObject4Ptr sho = TestSharedObject4::Create();
1139 // set property but call dao delegate within semaphore
1140 sho->SetProperty<TestSharedObject4::BOOLEAN>(
1142 MyModel3::PropertyType::SetDelegate(&dao, &DAO::SetBoolValue));
1145 RUNNER_ASSERT(dao.GetBoolValue() == true);
1147 // check shared object value
1148 bool shoValue = sho->GetProperty<TestSharedObject4::BOOLEAN, bool>();
1149 RUNNER_ASSERT(shoValue == true);
1151 // try the same with shared property
1152 MyModel3 model(sho, &dao);
1155 model.boolValue.Set(false);
1158 RUNNER_ASSERT(dao.GetBoolValue() == false);
1161 shoValue = sho->GetProperty<TestSharedObject4::BOOLEAN, bool>();
1162 RUNNER_ASSERT(shoValue == false);
1164 // check property value
1165 RUNNER_ASSERT(model.boolValue.Get() == false);
1168 //////////////////////////////////////////////
1171 * Lazy initialization test shared object
1173 class LazySharedObject : public SharedObject<TestTypeList>
1176 explicit LazySharedObject(const std::string& semaphore) :
1177 SharedObject<TestTypeList>(semaphore)
1189 friend class SharedObjectFactory<LazySharedObject>;
1194 void LazySharedObject::Init()
1196 SetPropertyInternal<0>(42);
1200 RUNNER_TEST(SharedMemory_100_LazyInit)
1204 typedef std::shared_ptr<LazySharedObject> LazySharedObjectPtr;
1206 // create shared object
1207 LazySharedObjectPtr sho = SharedObjectFactory<LazySharedObject>::Create(
1210 RUNNER_ASSERT(sho->IsRead() == false);
1212 // get property causing lazy init
1213 int value = sho->GetProperty<0, int>();
1215 RUNNER_ASSERT(sho->IsRead() == true);
1216 RUNNER_ASSERT(value == 42);
1218 // create another object
1219 LazySharedObjectPtr sho2 = SharedObjectFactory<LazySharedObject>::Create(
1222 RUNNER_ASSERT(sho2->IsRead() == false);
1224 // get property NOT causing lazy init
1225 value = sho2->GetProperty<0, int>();
1227 RUNNER_ASSERT(sho2->IsRead() == false);
1228 RUNNER_ASSERT(value == 42);
1230 // destroy both objects
1234 // create shared object
1235 LazySharedObjectPtr sho3 = SharedObjectFactory<LazySharedObject>::Create(
1238 RUNNER_ASSERT(sho3->IsRead() == false);
1240 // set property causing lazy init
1241 sho3->SetProperty<0>(43);
1242 value = sho3->GetProperty<0, int>();
1244 RUNNER_ASSERT(sho3->IsRead() == true);
1245 RUNNER_ASSERT(value == 43);
1248 //////////////////////////////////////////////
1250 bool SetCondition(const int& readValue, int& setValue);
1251 bool SetCondition(const int& readValue, int& setValue)
1253 LogDebug("Condition delegate called with read value = " << readValue <<
1254 " and set value = " << setValue);
1256 if (readValue > 3) {
1257 LogDebug("Condition is false");
1261 LogDebug("Condition is true");
1262 if (4 == setValue) {
1264 LogDebug("Changing set value to " << setValue);
1269 void SetDelegate(const int& readValue);
1270 void SetDelegate(const int& readValue)
1272 LogDebug("Set delegate called " << readValue);
1276 RUNNER_TEST(SharedMemory_120_ConditionalSet)
1280 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
1284 g_delegateCalls = 0;
1286 RUNNER_ASSERT(0 == (sho->GetProperty<0, int>()));
1288 DPL::FastDelegate<bool (const int&, int&)> condition(&SetCondition);
1289 DPL::FastDelegate<void (const int&)> delegate(&SetDelegate);
1291 bool succeeded = false;
1293 succeeded = sho->ConditionalSetProperty<0>(-2, condition);
1295 RUNNER_ASSERT(succeeded);
1296 RUNNER_ASSERT(-2 == (sho->GetProperty<0, int>()));
1298 succeeded = sho->ConditionalSetProperty<0>(4, condition, delegate);
1300 RUNNER_ASSERT(succeeded);
1301 RUNNER_ASSERT(10 == (sho->GetProperty<0, int>()));
1302 RUNNER_ASSERT(1 == g_delegateCalls);
1304 succeeded = sho->ConditionalSetProperty<0>(5, condition);
1306 RUNNER_ASSERT(!succeeded);
1307 RUNNER_ASSERT(10 == (sho->GetProperty<0, int>()));
1309 succeeded = sho->ConditionalSetProperty<0>(666, condition, delegate);
1311 RUNNER_ASSERT(!succeeded);
1312 RUNNER_ASSERT(10 == (sho->GetProperty<0, int>()));
1313 RUNNER_ASSERT(1 == g_delegateCalls);
1316 //////////////////////////////////////////////
1319 * Shared object used by multiple threads as a singleton.
1321 class MTSharedObject : public SharedObject<TestTypeList>
1324 explicit MTSharedObject(const std::string& semaphore) :
1325 SharedObject<TestTypeList>(semaphore)
1331 friend class SharedObjectFactory<MTSharedObject>;
1334 typedef std::shared_ptr<MTSharedObject> MTSharedObjectPtr;
1336 void MTSharedObject::Clear()
1341 SetProperty<2>(static_cast<char>(0));
1342 SetProperty<3>(array);
1346 * Shared object singleton
1348 class SharedObjectSingleton
1351 static MTSharedObjectPtr Instance();
1352 static void Destroy();
1355 static MTSharedObjectPtr m_sho;
1356 static DPL::Mutex m_mutex;
1359 MTSharedObjectPtr SharedObjectSingleton::m_sho;
1360 DPL::Mutex SharedObjectSingleton::m_mutex;
1362 MTSharedObjectPtr SharedObjectSingleton::Instance()
1364 DPL::Mutex::ScopedLock lock(&m_mutex);
1366 m_sho = SharedObjectFactory<MTSharedObject>::Create(SHM_KEY, SEM_NAME);
1371 void SharedObjectSingleton::Destroy()
1373 DPL::Mutex::ScopedLock lock(&m_mutex);
1378 * Listening controller
1380 class ShmController4 : public ListeningController<MTSharedObject>,
1381 public ISharedObjectListener<0, int>,
1382 public ISharedObjectListener<1, int>,
1383 public ISharedObjectListener<2, char>,
1384 public ISharedObjectListener<3, int[64]>
1389 REMOVE_LISTENERS = 3,
1390 DESTROY_SINGLETON = 4
1393 explicit ShmController4(DPL::WaitableEvent* event) :
1394 ListeningController<MTSharedObject>(event)
1397 virtual void OnEventReceived(const int& event);
1399 virtual void ValueChanged(size_t propertyEnum,
1401 const void* info = NULL);
1402 virtual void ValueChanged(size_t propertyEnum,
1404 const void* info = NULL);
1405 virtual void ValueChanged(size_t propertyEnum,
1406 const int(&value)[64],
1407 const void* info = NULL);
1409 bool NotRegistered();
1417 void ShmController4::ValueChanged(size_t propertyEnum,
1419 const void* /*info*/)
1421 LogDebug("ValueChanged(int) " << propertyEnum << " " << value);
1422 if ((propertyEnum == 0 && value == 1) ||
1423 (propertyEnum == 1 && value == 11))
1425 m_waitable->Signal();
1429 void ShmController4::ValueChanged(size_t propertyEnum,
1431 const void* /*info*/)
1433 LogDebug("ValueChanged(char) " << propertyEnum << " " << value);
1434 if (propertyEnum == 2 && value == 'a') {
1435 m_waitable->Signal();
1439 void ShmController4::ValueChanged(size_t propertyEnum,
1440 const int(&value)[64],
1441 const void* /*info*/)
1443 LogDebug("ValueChanged(int[64]) " << propertyEnum << " " << value[5]);
1444 if (propertyEnum == 3 && value[0] == 0 && value[1] == 1 && value[2] == 2) {
1445 m_waitable->Signal();
1449 void ShmController4::Sleep()
1451 DPL::Thread::GetCurrentThread()->MiliSleep(
1452 rand() % MAX_SINGLETON_LISTENER_DELAY);
1455 void ShmController4::OnEventReceived(const int& event)
1459 m_so = SharedObjectSingleton::Instance();
1460 m_waitable->Signal();
1464 LogDebug("Destroying shared object");
1465 // deregister, destroy and notify main thread
1467 m_waitable->Signal();
1471 // add listener and notify
1472 m_so->AddListener<0, int>(this);
1474 m_so->AddListener<1, int>(this);
1476 m_so->AddListener<2, char>(this);
1478 m_so->AddListener<3, int[64]>(this);
1480 m_waitable->Signal();
1483 case REMOVE_LISTENERS:
1484 // remove listener and notify
1485 m_so->RemoveListener<0, int>(this);
1487 m_so->RemoveListener<1, int>(this);
1489 m_so->RemoveListener<2, char>(this);
1491 m_so->RemoveListener<3, int[64]>(this);
1493 m_waitable->Signal();
1496 case DESTROY_SINGLETON:
1497 SharedObjectSingleton::Destroy();
1498 m_waitable->Signal();
1502 LogError("Unsupported event received: " << event);
1506 void MultipleWait(DPL::WaitableEvent(&event)[MAX_THREADS]);
1507 void MultipleWait(DPL::WaitableEvent(&event)[MAX_THREADS])
1509 for (size_t i = 0; i < MAX_THREADS; ++i) {
1515 * Try to remove property listener. If there's no such listener an exception
1518 #define LISTENER_ASSERT(property) \
1520 singleton->RemoveListener<(property)>(controller[i]); \
1521 LogError("Controller " << i << " is still listening for property " \
1523 RUNNER_ASSERT_MSG(false, "No listeners expected"); \
1525 Catch(MTSharedObject::Exception::ListenerNotFound) { \
1526 RUNNER_ASSERT(true); \
1530 RUNNER_TEST(SharedMemory_130_SharedObjectSingleton)
1532 RemoveIpcs(); // we need non existing shm
1536 // writer shared object
1537 TestSharedObjectPtr sho = SharedObjectFactory<TestSharedObject>::Create(
1540 ShmController4* controller[MAX_THREADS];
1541 DPL::WaitableEvent waitable[MAX_THREADS];
1543 const int array[64] = { 0, 1, 2 };
1545 // Create and wait for notification. Make sure that the thread/controller 0
1547 LogInfo("Creating controllers");
1548 for (size_t i = 0; i < MAX_THREADS; ++i) {
1549 controller[i] = new ShmController4(&waitable[i]);
1553 // singleton will be created by thread/controller 0 by now
1554 MTSharedObjectPtr singleton = SharedObjectSingleton::Instance();
1556 for (size_t repeats = 0; repeats < SINGLETON_TEST_REPEATS; ++repeats) {
1557 LogInfo("%%%%%%%%%%%%%%%%%%%%%");
1558 LogInfo("Iteration " << repeats + 1 << " of " << SINGLETON_TEST_REPEATS);
1562 LogInfo("Adding listeners");
1563 for (size_t i = 0; i < MAX_THREADS; ++i) {
1564 controller[i]->PostEvent(ShmController4::ADD_LISTENERS);
1566 // wait for listeners
1567 MultipleWait(waitable);
1569 RUNNER_ASSERT((singleton->GetProperty<0, int>()) == 0);
1570 RUNNER_ASSERT((singleton->GetProperty<1, int>()) == 0);
1571 RUNNER_ASSERT((singleton->GetProperty<2, char>()) == 0);
1573 int checkArray[64] = {};
1574 singleton->GetProperty<3>(checkArray);
1575 RUNNER_ASSERT(checkArray[0] == 0);
1576 RUNNER_ASSERT(checkArray[1] == 0);
1577 RUNNER_ASSERT(checkArray[2] == 0);
1580 LogInfo("Setting property 0");
1581 sho->SetProperty<0>(1);
1582 // wait for confirmations
1583 MultipleWait(waitable);
1586 LogInfo("Setting property 1");
1587 sho->SetProperty<1>(11);
1588 // wait for confirmations
1589 MultipleWait(waitable);
1592 LogInfo("Setting property 2");
1593 sho->SetProperty<2>('a');
1594 // wait for confirmations
1595 MultipleWait(waitable);
1598 LogInfo("Setting property 3");
1599 sho->SetProperty<3>(array);
1600 // wait for confirmations
1601 MultipleWait(waitable);
1604 LogInfo("Removing listeners");
1605 for (size_t i = 0; i < MAX_THREADS; ++i) {
1606 controller[i]->PostEvent(ShmController4::REMOVE_LISTENERS);
1608 // wait for listeners
1609 MultipleWait(waitable);
1611 // check if listeners array is empty
1612 LogInfo("Checking listeners");
1613 for (size_t i = 0; i < MAX_THREADS; ++i) {
1620 RUNNER_ASSERT((singleton->GetProperty<0, int>()) == 1);
1621 RUNNER_ASSERT((singleton->GetProperty<1, int>()) == 11);
1622 RUNNER_ASSERT((singleton->GetProperty<2, char>()) == 'a');
1623 singleton->GetProperty<3>(checkArray);
1624 RUNNER_ASSERT(checkArray[0] == 0);
1625 RUNNER_ASSERT(checkArray[1] == 1);
1626 RUNNER_ASSERT(checkArray[2] == 2);
1631 // Destroy controllers and wait for confirmation. Make sure that
1632 // thread/controller 0 is destroyed in the end
1633 LogInfo("Destroying controllers");
1634 for (int i = MAX_THREADS - 1; i >= 0; --i) {
1635 controller[i]->PostEvent(DESTROY_EVENT);
1639 * Destroy singleton before thread that created it finishes.
1640 * This is to properly close all waitable handles opened by
1641 * SharedObject in thread 0.
1643 LogInfo("Destroying singleton");
1644 controller[i]->PostEvent(ShmController4::DESTROY_SINGLETON);
1647 delete controller[i];
1651 #undef LISTENER_ASSERT
1654 * test preconditions & postconditions:
1655 * - no existing shared memory with given SHM_KEY
1656 * - no existing semaphore of given SEM_NAME
1658 RUNNER_TEST(SharedMemory_001_Preconditions) {
1662 RUNNER_TEST(SharedMemory_999_Postconditions) {