2 * Copyright (c) 2000 - 2015 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
17 * @file test_comm-manager.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
22 #include <boost/test/unit_test.hpp>
23 #include <boost/test/results_reporter.hpp>
24 #include <communication-manager.h>
31 MessageA(int ai) : i(ai) {}
36 MessageB(char ac) : c(ac) {}
41 MessageC(const std::string& astr) : str(astr) {}
48 void Handle(const MessageA& msg) {
52 void Handle(const MessageC& msg) {
60 } // namespace anonymous
62 BOOST_AUTO_TEST_SUITE(MESSAGE_MANAGER_TEST)
64 BOOST_AUTO_TEST_CASE(TMM_0010_NoListener) {
65 CKM::CommunicationManager<MessageA> mgr;
67 mgr.SendMessage(MessageA(22));
70 BOOST_AUTO_TEST_CASE(TMM_0020_Basic) {
71 CKM::CommunicationManager<MessageA> mgr;
73 mgr.Register<MessageA>([&](const MessageA& msg){ received = msg.i; });
74 mgr.SendMessage(MessageA(4));
75 BOOST_REQUIRE_MESSAGE(received != 0, "Message not received");
76 BOOST_REQUIRE_MESSAGE(received == 4, "Wrong message received i=" << received);
79 BOOST_AUTO_TEST_CASE(TMM_0030_MultipleMessages) {
80 CKM::CommunicationManager<MessageA, MessageB> mgr;
83 mgr.Register<MessageA>([&](const MessageA& msg){ reci = msg.i; });
84 mgr.Register<MessageB>([&](const MessageB& msg){ recc = msg.c; });
85 mgr.SendMessage(MessageB('c'));
86 BOOST_REQUIRE_MESSAGE(reci == 0, "Unexpected message received");
87 BOOST_REQUIRE_MESSAGE(recc != 0, "Message not received");
88 BOOST_REQUIRE_MESSAGE(recc == 'c', "Wrong message received c=" << recc);
90 mgr.SendMessage(MessageA(42));
91 BOOST_REQUIRE_MESSAGE(reci!= 0, "Message not received");
92 BOOST_REQUIRE_MESSAGE(reci == 42, "Wrong message received i=" << reci);
93 BOOST_REQUIRE_MESSAGE(recc == 'c', "Previous message overwritten c=" << recc);
96 BOOST_AUTO_TEST_CASE(TMM_0040_Listener) {
97 CKM::CommunicationManager<MessageA, MessageB, MessageC> mgr;
99 mgr.Register<MessageC>([&](const MessageC& msg){ l.Handle(msg); });
100 mgr.Register<MessageA>([&](const MessageA& msg){ l.Handle(msg); });
102 mgr.SendMessage(MessageC("lorem ipsum"));
103 BOOST_REQUIRE_MESSAGE(l.i == 0, "Unexpected message received");
104 BOOST_REQUIRE_MESSAGE(!l.str.empty(), "Message not received");
105 BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum", "Wrong message received c=" << l.str);
107 mgr.SendMessage(MessageA(3));
108 BOOST_REQUIRE_MESSAGE(l.i!= 0, "Message not received");
109 BOOST_REQUIRE_MESSAGE(l.i == 3, "Wrong message received i=" << l.i);
110 BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum", "Previous message overwritten str=" << l.str);
113 BOOST_AUTO_TEST_CASE(TMM_0050_2Listeners) {
114 CKM::CommunicationManager<MessageA> mgr;
118 mgr.Register<MessageA>([&](const MessageA& msg){
119 BOOST_REQUIRE_MESSAGE(msg.i == 5, "Unexpected message received i=" << msg.i);
122 mgr.Register<MessageA>([&](const MessageA& msg){
123 BOOST_REQUIRE_MESSAGE(msg.i == 5, "Unexpected message received i=" << msg.i);
127 mgr.SendMessage(MessageA(5));
128 BOOST_REQUIRE_MESSAGE(called[0], "First listener not called");
129 BOOST_REQUIRE_MESSAGE(called[1], "Second listener not called");
132 BOOST_AUTO_TEST_CASE(TMM_0060_Stress) {
133 CKM::CommunicationManager<MessageA, MessageB, MessageC> mgr;
135 std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
136 std::uniform_int_distribution<size_t> message_dist(0,2);
137 std::uniform_int_distribution<size_t> count_dist(1,10);
142 mgr.Register<MessageA>([&](const MessageA& msg) {
143 BOOST_REQUIRE_MESSAGE(msg.i == 42, "Wrong message: " << msg.i);
146 mgr.Register<MessageB>([&](const MessageB& msg) {
147 BOOST_REQUIRE_MESSAGE(msg.c == 'c', "Wrong message: " << msg.c);
150 mgr.Register<MessageC>([&](const MessageC& msg) {
151 BOOST_REQUIRE_MESSAGE(msg.str == "lorem ipsum", "Wrong message: " << msg.str);
155 for (size_t i=0; i < 1000; i++)
157 size_t cnt = count_dist(generator);
158 for (size_t s = 0; s < cnt; s++) {
159 switch(message_dist(generator))
162 mgr.SendMessage(MessageA(42));
166 mgr.SendMessage(MessageB('c'));
170 mgr.SendMessage(MessageC("lorem ipsum"));
174 BOOST_FAIL("Unexpected message type");
178 BOOST_REQUIRE_MESSAGE(a == 0, "Unexpected number of MessageA: " << a);
179 BOOST_REQUIRE_MESSAGE(b == 0, "Unexpected number of MessageB: " << b);
180 BOOST_REQUIRE_MESSAGE(c == 0, "Unexpected number of MessageC: " << c);
183 BOOST_AUTO_TEST_SUITE_END()