Branch data Line data Source code
1 : : /* Copyright 2006,2007,2011 Savarese Software Research Corporation
2 : : *
3 : : * Licensed under the Apache License, Version 2.0 (the "License");
4 : : * you may not use this file except in compliance with the License.
5 : : * You may obtain a copy of the License at
6 : : *
7 : : * http://www.savarese.com/software/ApacheLicense-2.0
8 : : *
9 : : * Unless required by applicable law or agreed to in writing, software
10 : : * distributed under the License is distributed on an "AS IS" BASIS,
11 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : * See the License for the specific language governing permissions and
13 : : * limitations under the License.
14 : : */
15 : :
16 : : #include <ssrc/spread.h>
17 : :
18 : : #include <utility>
19 : : #include <cstdlib>
20 : :
21 : : #define BOOST_TEST_MODULE MailboxTest
22 : : #include <boost/test/unit_test.hpp>
23 : :
24 : : using namespace NS_SSRC_SPREAD;
25 : : using namespace std::rel_ops;
26 : :
27 : : const char *TestGroupName = "MailboxTestGroup";
28 : : const char *TestGroupName2 = "MailboxTestGroup2";
29 : :
30 : : class MailboxFixture {
31 : : public:
32 : : Mailbox *mbox;
33 : : Mailbox *rmbox;
34 : : Message *message;
35 : :
36 : 10 : MailboxFixture() {
37 : : try {
38 [ + - + - : 10 : mbox = new Mailbox(LIBSSRCSPREAD_TEST_DAEMON);
+ - + - ]
39 [ + - + - : 10 : rmbox = new Mailbox(LIBSSRCSPREAD_TEST_DAEMON, "", false);
+ - + - #
# ]
40 : 0 : } catch(const Error & e) {
41 [ # # ]: 0 : e.print();
42 : 0 : throw e;
43 : : }
44 [ + - ]: 10 : message = new Message();
45 : 10 : }
46 : :
47 : 20 : ~MailboxFixture() {
48 [ + - ]: 10 : delete mbox;
49 [ + - ]: 10 : delete rmbox;
50 [ + - ]: 10 : delete message;
51 : 10 : mbox = 0;
52 : 10 : message = 0;
53 : 10 : }
54 : :
55 : 6 : void join() {
56 [ + - + - ]: 6 : mbox->join(TestGroupName);
57 [ + - + - ]: 6 : mbox->join(TestGroupName2);
58 : 6 : }
59 : :
60 : 6 : void leave() {
61 [ + - + - ]: 6 : mbox->leave(TestGroupName);
62 [ + - + - ]: 6 : mbox->leave(TestGroupName2);
63 : 6 : }
64 : : };
65 : :
66 [ + - + - ]: 1 : BOOST_FIXTURE_TEST_SUITE(all, MailboxFixture)
67 : :
68 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_join_and_leave) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
69 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(this->join());
+ - + - +
- + - + -
- + - + #
# # # # #
# # # # #
# # # ]
70 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(this->leave());
+ - + - +
- + - + -
- + - + #
# # # # #
# # # # #
# # # ]
71 : 1 : }
72 : :
73 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_send) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
74 : 1 : this->join();
75 : 1 : message->write("test_send", 9);
76 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, TestGroupName));
+ - + - +
- + - + -
+ - - + -
+ # # # #
# # # # #
# # # #
# ]
77 : 1 : message->clear();
78 : 1 : message->write("test_send2", 10);
79 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, TestGroupName));
+ - + - +
- + - + -
+ - - + -
+ # # # #
# # # # #
# # # #
# ]
80 : 1 : this->leave();
81 : 1 : }
82 : :
83 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_scatter_send) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
84 [ + - ]: 2 : ScatterMessage sm;
85 [ + - ]: 2 : Message m;
86 : :
87 [ + - ]: 1 : this->join();
88 [ + - ]: 1 : message->write("test_scatter_send", 17);
89 [ + - ]: 1 : m.write("test_scatter_send2", 18);
90 [ + - + - : 1 : BOOST_REQUIRE(sm.add(*message));
+ - + - +
- + - + -
- + ]
91 [ + - + - : 1 : BOOST_REQUIRE(sm.add(m));
+ - + - +
- + - + -
- + ]
92 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send(sm, TestGroupName));
+ - + - +
- + - + -
+ - - + -
+ # # # #
# # # # #
# # # # #
# # ]
93 : :
94 [ + - ]: 1 : mbox->clear_groups();
95 [ + - + - ]: 1 : mbox->add_group(TestGroupName);
96 : 1 : mbox->clear_message_parts();
97 [ + - + - : 1 : BOOST_REQUIRE(mbox->add_message_part("test_scatter_send3", 18));
+ - + - +
- + - + -
- + ]
98 [ + - + - : 1 : BOOST_REQUIRE(mbox->add_message_part("test_scatter_send4", 18));
+ - + - +
- + - + -
- + ]
99 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send());
+ - + - +
- + - + -
- + - + #
# # # # #
# # # # #
# # # #
# ]
100 [ + - ]: 1 : this->leave();
101 : 1 : }
102 : :
103 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_group_send) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
104 [ + - ]: 2 : GroupList groups;
105 : :
106 [ + - + - ]: 1 : groups.add(TestGroupName);
107 [ + - + - ]: 1 : groups.add(TestGroupName2);
108 : :
109 [ + - ]: 1 : this->join();
110 [ + - ]: 1 : message->write("test_group_send", 15);
111 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, groups));
+ - + - +
- + - + -
- + - + #
# # # # #
# # # # #
# # # #
# ]
112 [ + - ]: 1 : message->clear();
113 [ + - ]: 1 : message->write("test_group_send2", 16);
114 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, groups));
+ - + - +
- + - + -
- + - + #
# # # # #
# # # # #
# # # #
# ]
115 [ + - ]: 1 : this->leave();
116 : 1 : }
117 : :
118 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_scatter_group_send) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
119 [ + - ]: 2 : GroupList groups;
120 [ + - ]: 2 : ScatterMessage sm;
121 [ + - ]: 2 : Message m;
122 : :
123 [ + - + - ]: 1 : groups.add(TestGroupName);
124 [ + - + - ]: 1 : groups.add(TestGroupName2);
125 : :
126 [ + - ]: 1 : this->join();
127 [ + - ]: 1 : message->write("test_scatter_group_send", 23);
128 [ + - ]: 1 : m.write("test_scatter_group_send2", 24);
129 [ + - ]: 1 : sm.add(*message);
130 [ + - ]: 1 : sm.add(m);
131 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send(sm, groups));
+ - + - +
- + - + -
- + - + #
# # # # #
# # # # #
# # # #
# ]
132 [ + - ]: 1 : this->leave();
133 : 1 : }
134 : :
135 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_receive_message) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
136 : 1 : const char *data = "foobar";
137 : 1 : unsigned int data_len = std::strlen(data) + 1;
138 : : unsigned int bytes;
139 [ + - ]: 2 : GroupList groups;
140 [ + - ]: 2 : Message padding;
141 : :
142 [ + - ]: 1 : mbox->clear_groups();
143 : 1 : mbox->clear_message_parts();
144 [ + - ]: 1 : mbox->add_group(rmbox->private_group());
145 [ + - ]: 1 : mbox->add_message_part(data, data_len);
146 [ + - ]: 1 : mbox->send();
147 : :
148 [ + - ]: 1 : rmbox->clear_groups();
149 : 1 : rmbox->clear_message_parts();
150 [ + - ]: 1 : rmbox->add_message_part(*message);
151 [ + - ]: 1 : rmbox->add_message_part(padding);
152 : :
153 [ + - ]: 1 : padding.resize(padding.capacity());
154 [ + - + - : 1 : BOOST_REQUIRE(padding.size() > 0);
+ - + - +
- + - + -
- + ]
155 : :
156 [ + - ]: 1 : bytes = rmbox->receive();
157 [ + - ]: 1 : rmbox->copy_groups(groups);
158 : :
159 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(padding.size(), 0u);
+ - + - +
- + - -
+ ]
160 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(bytes, data_len);
+ - + - +
- - + ]
161 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(message->size(), data_len);
+ - + - +
- + - -
+ ]
162 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(std::memcmp(data, &((*message)[0]), data_len), 0);
+ - + - +
- - + ]
163 [ + - + - : 1 : BOOST_REQUIRE(rmbox->private_group() == groups[0]);
+ - + - +
- + - + -
- + ]
164 [ + - + - : 1 : BOOST_REQUIRE(mbox->private_group() == message->sender());
+ - + - +
- + - -
+ ]
165 : 1 : }
166 : :
167 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_receive_scatter_message) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
168 : 1 : const char *data1 = "foobar";
169 : 1 : const char *data2 = "baz";
170 : 1 : unsigned int data_len1 = std::strlen(data1) + 1;
171 : 1 : unsigned int data_len2 = std::strlen(data2) + 1;
172 : : unsigned int bytes;
173 [ + - ]: 2 : GroupList groups;
174 [ + - + - ]: 2 : Message msg1(data_len1), msg2(data_len2);
175 : :
176 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg1.size(), 0);
+ - + - +
- + - -
+ ]
177 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg2.size(), 0);
+ - + - +
- + - -
+ ]
178 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg1.capacity(), data_len1);
+ - + - +
- - + ]
179 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg2.capacity(), data_len2);
+ - + - +
- - + ]
180 : :
181 [ + - ]: 1 : mbox->clear_groups();
182 : 1 : mbox->clear_message_parts();
183 [ + - ]: 1 : mbox->add_group(rmbox->private_group());
184 [ + - ]: 1 : mbox->add_message_part(data1, data_len1);
185 [ + - ]: 1 : mbox->add_message_part(data2, data_len2);
186 [ + - ]: 1 : mbox->send();
187 : :
188 [ + - ]: 1 : rmbox->clear_groups();
189 : 1 : rmbox->clear_message_parts();
190 [ + - ]: 1 : rmbox->add_message_part(msg1);
191 [ + - ]: 1 : rmbox->add_message_part(msg2);
192 : :
193 [ + - ]: 1 : bytes = rmbox->receive();
194 : :
195 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(bytes, data_len1 + data_len2);
+ - + - +
- - + ]
196 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg1.size(), data_len1);
+ - + - +
- + - -
+ ]
197 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg2.size(), data_len2);
+ - + - +
- + - -
+ ]
198 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg1.capacity(), data_len1);
+ - + - +
- - + ]
199 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg2.capacity(), data_len2);
+ - + - +
- - + ]
200 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(std::memcmp(data1, &msg1[0], data_len1), 0);
+ - + - +
- - + ]
201 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(std::memcmp(data2, &msg2[0], data_len2), 0);
+ - + - +
- - + ]
202 : :
203 [ + - ]: 1 : rmbox->copy_groups(groups);
204 : :
205 [ + - + - : 1 : BOOST_REQUIRE(rmbox->private_group() == groups[0]);
+ - + - +
- + - + -
- + ]
206 [ + - + - : 1 : BOOST_REQUIRE(mbox->private_group() == msg1.sender());
+ - + - +
- + - -
+ ]
207 : :
208 [ + - ]: 2 : ScatterMessage msg;
209 : :
210 [ + - ]: 1 : msg1.clear();
211 [ + - ]: 1 : msg2.clear();
212 : :
213 [ + - ]: 1 : msg.add(msg1);
214 [ + - ]: 1 : msg.add(msg2);
215 : :
216 [ + - ]: 1 : groups.clear();
217 : :
218 [ + - ]: 1 : mbox->send();
219 [ + - ]: 1 : bytes = rmbox->receive(msg, groups);
220 : :
221 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(bytes, data_len1 + data_len2);
+ - + - +
- - + ]
222 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg1.size(), data_len1);
+ - + - +
- + - -
+ ]
223 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg2.size(), data_len2);
+ - + - +
- + - -
+ ]
224 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg1.capacity(), data_len1);
+ - + - +
- - + ]
225 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(msg2.capacity(), data_len2);
+ - + - +
- - + ]
226 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(std::memcmp(data1, &msg1[0], data_len1), 0);
+ - + - +
- - + ]
227 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(std::memcmp(data2, &msg2[0], data_len2), 0);
+ - + - +
- - + ]
228 [ + - + - : 1 : BOOST_REQUIRE(rmbox->private_group() == groups[0]);
+ - + - +
- + - + -
- + ]
229 [ + - + - : 1 : BOOST_REQUIRE(mbox->private_group() == msg1.sender());
+ - + - +
- + - -
+ ]
230 : 1 : }
231 : :
232 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_receive_message_buffer_too_short) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
233 : 1 : const unsigned int capacity = 131072;
234 [ + - ]: 2 : Message short_buffer(0);
235 : :
236 [ + - ]: 1 : message->resize(capacity);
237 : :
238 [ + + ]: 32769 : for(unsigned int i = 0; i < capacity; i+=sizeof(int)) {
239 : 32768 : const int j = std::rand();
240 [ + - ]: 32768 : message->write(&j, sizeof(j));
241 : : }
242 : :
243 : 1 : mbox->clear_message_parts();
244 [ + - ]: 1 : mbox->add_message_part(*message);
245 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(mbox->send(rmbox->private_group()));
+ - + - +
- + - + -
- + - + #
# # # # #
# # # # #
# # # #
# ]
246 : :
247 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(short_buffer.size(), 0u);
+ - + - +
- + - -
+ ]
248 : :
249 [ + - ]: 1 : const unsigned int bytes = rmbox->receive(short_buffer);
250 : :
251 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(bytes, capacity);
+ - + - +
- - + ]
252 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(short_buffer.size(), capacity);
+ - + - +
- + - -
+ ]
253 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(std::memcmp(&((*message)[0]), &short_buffer[0],
+ - + - +
- - + ]
254 : : capacity), 0);
255 [ + - + - : 1 : BOOST_REQUIRE(rmbox->private_group() == rmbox->group(0));
+ - + - +
- + - + -
- + ]
256 [ + - + - : 1 : BOOST_REQUIRE(mbox->private_group() == short_buffer.sender());
+ - + - +
- + - -
+ ]
257 : 1 : }
258 : :
259 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_receive_membership_message) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
260 [ + - ]: 1 : this->join();
261 : :
262 : : // mbox join message
263 [ + - ]: 2 : GroupList groups;
264 [ + - ]: 1 : mbox->receive(*message, groups);
265 : :
266 [ + - + - : 1 : BOOST_REQUIRE(message->is_membership());
+ - + - +
- + - -
+ ]
267 [ + - + - : 1 : BOOST_REQUIRE(TestGroupName == message->sender());
+ - + - +
- + - + -
- + ]
268 : :
269 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
270 [ + - ]: 2 : MembershipInfo info;
271 : :
272 [ + - ]: 1 : message->get_membership_info(info);
273 : :
274 [ + - + - : 1 : BOOST_REQUIRE(info.is_regular_membership());
+ - + - +
- + - -
+ ]
275 [ + - + - : 1 : BOOST_REQUIRE(info.caused_by_join());
+ - + - +
- + - -
+ ]
276 [ + - + - : 1 : BOOST_REQUIRE(mbox->private_group() == info.changed_member());
+ - + - +
- + - + -
- + ]
277 : :
278 [ + - + - : 2 : GroupList mbox_group, local_members, non_local_members, all_members;
+ - + - ]
279 : :
280 [ + - ]: 1 : mbox_group.add(mbox->private_group());
281 [ + - ]: 1 : info.get_local_members(local_members);
282 [ + - ]: 1 : info.get_non_local_members(non_local_members);
283 [ + - ]: 1 : info.get_all_members(all_members);
284 : :
285 [ + - + - : 1 : BOOST_REQUIRE(mbox_group == local_members);
+ - + - +
- + - + -
- + ]
286 [ + - + - : 1 : BOOST_REQUIRE(mbox_group != non_local_members);
+ - + - +
- + - + -
- + ]
287 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(non_local_members.size(), 0u);
+ - + - +
- + - -
+ ]
288 [ + - + - : 1 : BOOST_REQUIRE(mbox_group == all_members);
+ - + - +
- + - + -
- + ]
289 [ + - + - : 1 : BOOST_REQUIRE(groups == all_members);
+ - + - +
- + - + -
- + ]
290 : : #endif
291 : :
292 : : // mbox join message
293 [ + - ]: 1 : mbox->receive(*message);
294 [ + - + - : 1 : BOOST_REQUIRE(message->is_membership());
+ - + - +
- + - -
+ ]
295 [ + - + - : 1 : BOOST_REQUIRE(TestGroupName2 == message->sender());
+ - + - +
- + - + -
- + ]
296 : :
297 : : // rmbox join message
298 [ + - + - ]: 1 : rmbox->join(TestGroupName2);
299 : :
300 [ + - ]: 1 : mbox->receive(*message);
301 [ + - + - : 1 : BOOST_REQUIRE(message->is_membership());
+ - + - +
- + - -
+ ]
302 [ + - + - : 1 : BOOST_REQUIRE(TestGroupName2 == message->sender());
+ - + - +
- + - + -
- + ]
303 : :
304 : : // rmbox leave message
305 [ + - + - ]: 1 : rmbox->leave(TestGroupName2);
306 : :
307 [ + - ]: 1 : mbox->receive(*message);
308 [ + - + - : 1 : BOOST_REQUIRE(message->is_membership());
+ - + - +
- + - -
+ ]
309 : :
310 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
311 [ + - ]: 1 : message->get_membership_info(info);
312 : :
313 [ + - + - : 1 : BOOST_REQUIRE(info.caused_by_leave());
+ - + - +
- + - -
+ ]
314 : : #endif
315 : :
316 [ + - + - : 1 : BOOST_REQUIRE(TestGroupName2 == message->sender());
+ - + - +
- + - + -
- + ]
317 : :
318 [ + - ]: 1 : this->leave();
319 : 1 : }
320 : :
321 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_self_leave_message) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
322 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
323 [ + - + - ]: 1 : mbox->join(TestGroupName);
324 [ + - ]: 1 : mbox->receive(*message);
325 [ + - + - ]: 1 : mbox->leave(TestGroupName);
326 [ + - ]: 1 : mbox->receive(*message);
327 : :
328 [ + - ]: 2 : MembershipInfo info;
329 : :
330 [ + - ]: 1 : message->get_membership_info(info);
331 : :
332 [ + - + - : 1 : BOOST_REQUIRE(info.caused_by_leave());
+ - + - +
- + - -
+ ]
333 [ + - + - : 1 : BOOST_REQUIRE(info.is_self_leave());
+ - + - +
- + - -
+ ]
334 : : #endif
335 : 1 : }
336 : :
337 [ + - + - ]: 3 : BOOST_AUTO_TEST_SUITE_END()
|