6340b5728d0936b30a8c32588b3f85cf46f1fb01
[platform/framework/web/wrt-commons.git] / tests / dpl / unused / test_message_queue.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        test_message_queue.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of message queue tests
21  */
22 #include <dpl/test_runner.h>
23 #include <dpl/abstract_waitable_input_adapter.h>
24 #include <dpl/abstract_waitable_output_adapter.h>
25 #include <dpl/message_queue.h>
26 #include <dpl/binary_queue.h>
27 #include <dpl/copy.h>
28 #include <dpl/log.h>
29 #include <dpl/application.h>
30 #include <dpl/controller.h>
31 #include <dpl/generic_event.h>
32
33 DECLARE_GENERIC_EVENT_0(QuitEvent)
34
35 class QuitController
36     : public DPL::Controller<DPL::TypeListDecl<QuitEvent>::Type>,
37       public DPL::ApplicationExt
38 {
39 public:
40     QuitController() : DPL::ApplicationExt(1, NULL, "test-app") { Touch(); }
41
42 protected:
43     virtual void OnEventReceived(const QuitEvent &) { Quit(); }
44 };
45
46 RUNNER_TEST_GROUP_INIT(DPL)
47
48 class CopyThread
49     : public DPL::Thread
50 {
51 private:
52     bool m_success;
53     DPL::AbstractWaitableInput *m_input;
54     DPL::AbstractWaitableOutput *m_output;
55     std::size_t m_dataSize;
56
57 public:
58     CopyThread(DPL::AbstractWaitableInput *input,
59                DPL::AbstractWaitableOutput *output,
60                std::size_t dataSize)
61         : m_success(true),
62           m_input(input),
63           m_output(output),
64           m_dataSize(dataSize)
65     {
66         LogInfo("Thread created");
67     }
68
69 protected:
70     virtual int ThreadEntry()
71     {
72         LogInfo("Entering copy thread");
73
74         Try
75         {
76             DPL::Copy(m_input, m_output, m_dataSize);
77         }
78         Catch (DPL::CopyFailed)
79         {
80             m_success = false;
81
82             LogWarning("Copy failed!");
83             return 0;
84         }
85
86         LogInfo("Copy finished");
87         return 0;
88     }
89 };
90
91 inline std::string BinaryQueueToString(const DPL::BinaryQueue &queue)
92 {
93     char *buffer = new char[queue.Size()];
94     queue.Flatten(buffer, queue.Size());
95     std::string result = std::string(buffer, buffer + queue.Size());
96     delete [] buffer;
97     return result;
98 }
99
100 RUNNER_TEST(MessageQueue_DoubleCopy)
101 {
102     DPL::BinaryQueue dataA;
103     DPL::MessageQueue dataB("/test_mqueue_dataB", true, false, 0660, true);
104     DPL::MessageQueue dataC("/test_mqueue_dataC", true, false, 0660, true);
105     DPL::BinaryQueue dataD;
106
107     DPL::AbstractWaitableInputAdapter dataAdapterA(&dataA);
108     DPL::AbstractWaitableOutputAdapter dataAdapterD(&dataD);
109
110     const std::string testData =
111     "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
112     "Cras elementum venenatis velit, sit amet vehicula odio gravida a."
113     "Curabitur id nibh id ante adipiscing sollicitudin."
114     "Maecenas in tellus vel augue vehicula pharetra hendrerit cursus est."
115     ""
116     "Ut malesuada quam porttitor dui euismod lacinia."
117     "Phasellus quis lectus sed lectus dictum tincidunt et vitae leo."
118     "Fusce id est massa, condimentum bibendum urna."
119     "Donec venenatis quam eget sapien vulputate egestas."
120     "Maecenas scelerisque lorem a neque molestie a varius erat condimentum."
121     "Maecenas varius hendrerit ligula, sed iaculis justo pretium id."
122     "Nunc sit amet nisl vitae justo tristique suscipit id eget tortor."
123     ""
124     "Pellentesque sollicitudin nulla at metus dapibus tincidunt."
125     "Integer consequat justo eget dui imperdiet iaculis."
126     "Sed vestibulum ipsum vitae libero accumsan non molestie metus adipiscing."
127     ""
128     "Vivamus quis dui enim, in blandit urna."
129     "In imperdiet lacus at orci elementum a scelerisque dui blandit."
130     "Donec vulputate enim metus, eget convallis ante."
131     "Etiam mollis enim eget eros pulvinar nec sagittis justo fermentum."
132     ""
133     "Vestibulum sed nunc eu leo lobortis ultrices."
134     "Nullam placerat nulla et est blandit nec interdum nunc pulvinar."
135     "Vivamus a lectus eget dui fermentum hendrerit.";
136
137     QuitController quitter;
138     quitter.PostTimedEvent(QuitEvent(), 1.0);
139
140     CopyThread threadA(&dataAdapterA, &dataB, testData.size());
141     CopyThread threadB(&dataB, &dataC, testData.size());
142     CopyThread threadC(&dataC, &dataAdapterD, testData.size());
143
144     dataA.AppendCopy(testData.c_str(), testData.size());
145
146     threadA.Run();
147     threadB.Run();
148     threadC.Run();
149
150     quitter.Exec();
151
152     threadA.Quit();
153     threadB.Quit();
154     threadC.Quit();
155
156     // Now, test data should be in dataD
157     RUNNER_ASSERT(BinaryQueueToString(dataD) == testData);
158 }