tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / tests / 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")
41     {
42         Touch();
43     }
44
45   protected:
46     virtual void OnEventReceived(const QuitEvent &)
47     {
48         Quit();
49     }
50 };
51
52 RUNNER_TEST_GROUP_INIT(DPL)
53
54 class CopyThread :
55     public DPL::Thread
56 {
57   private:
58     bool m_success;
59     DPL::AbstractWaitableInput *m_input;
60     DPL::AbstractWaitableOutput *m_output;
61     std::size_t m_dataSize;
62
63   public:
64     CopyThread(DPL::AbstractWaitableInput *input,
65                DPL::AbstractWaitableOutput *output,
66                std::size_t dataSize) :
67         m_success(true),
68         m_input(input),
69         m_output(output),
70         m_dataSize(dataSize)
71     {
72         LogDebug("Thread created");
73     }
74
75   protected:
76     virtual int ThreadEntry()
77     {
78         LogDebug("Entering copy thread");
79
80         Try
81         {
82             DPL::Copy(m_input, m_output, m_dataSize);
83         }
84         Catch(DPL::CopyFailed)
85         {
86             m_success = false;
87
88             LogWarning("Copy failed!");
89             return 0;
90         }
91
92         LogDebug("Copy finished");
93         return 0;
94     }
95 };
96
97 inline std::string BinaryQueueToString(const DPL::BinaryQueue &queue)
98 {
99     char *buffer = new char[queue.Size()];
100     queue.Flatten(buffer, queue.Size());
101     std::string result = std::string(buffer, buffer + queue.Size());
102     delete[] buffer;
103     return result;
104 }
105
106 RUNNER_TEST(MessageQueue_DoubleCopy)
107 {
108     DPL::BinaryQueue dataA;
109     DPL::MessageQueue dataB("/test_mqueue_dataB", true, false, 0660, true);
110     DPL::MessageQueue dataC("/test_mqueue_dataC", true, false, 0660, true);
111     DPL::BinaryQueue dataD;
112
113     DPL::AbstractWaitableInputAdapter dataAdapterA(&dataA);
114     DPL::AbstractWaitableOutputAdapter dataAdapterD(&dataD);
115
116     const std::string testData =
117         "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
118         "Cras elementum venenatis velit, sit amet vehicula odio gravida a."
119         "Curabitur id nibh id ante adipiscing sollicitudin."
120         "Maecenas in tellus vel augue vehicula pharetra hendrerit cursus est."
121         ""
122         "Ut malesuada quam porttitor dui euismod lacinia."
123         "Phasellus quis lectus sed lectus dictum tincidunt et vitae leo."
124         "Fusce id est massa, condimentum bibendum urna."
125         "Donec venenatis quam eget sapien vulputate egestas."
126         "Maecenas scelerisque lorem a neque molestie a varius erat condimentum."
127         "Maecenas varius hendrerit ligula, sed iaculis justo pretium id."
128         "Nunc sit amet nisl vitae justo tristique suscipit id eget tortor."
129         ""
130         "Pellentesque sollicitudin nulla at metus dapibus tincidunt."
131         "Integer consequat justo eget dui imperdiet iaculis."
132         "Sed vestibulum ipsum vitae libero accumsan non molestie metus adipiscing."
133         ""
134         "Vivamus quis dui enim, in blandit urna."
135         "In imperdiet lacus at orci elementum a scelerisque dui blandit."
136         "Donec vulputate enim metus, eget convallis ante."
137         "Etiam mollis enim eget eros pulvinar nec sagittis justo fermentum."
138         ""
139         "Vestibulum sed nunc eu leo lobortis ultrices."
140         "Nullam placerat nulla et est blandit nec interdum nunc pulvinar."
141         "Vivamus a lectus eget dui fermentum hendrerit.";
142
143     QuitController quitter;
144     quitter.PostTimedEvent(QuitEvent(), 1.0);
145
146     CopyThread threadA(&dataAdapterA, &dataB, testData.size());
147     CopyThread threadB(&dataB, &dataC, testData.size());
148     CopyThread threadC(&dataC, &dataAdapterD, testData.size());
149
150     dataA.AppendCopy(testData.c_str(), testData.size());
151
152     threadA.Run();
153     threadB.Run();
154     threadC.Run();
155
156     quitter.Exec();
157
158     threadA.Quit();
159     threadB.Quit();
160     threadC.Quit();
161
162     // Now, test data should be in dataD
163     RUNNER_ASSERT(BinaryQueueToString(dataD) == testData);
164 }