Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2009 Savarese Software Research Corporation
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.savarese.com/software/ApacheLicense-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 : : #include <tests/TestCommon.h>
17 : : #include <ssrc/wispers/index/DictionaryProtocol.h>
18 : : #include <ssrc/wispers/index/DictionaryService.h>
19 : :
20 : : using namespace NS_SSRC_WSPR_INDEX;
21 : :
22 [ + - + - : 50 : struct Entry {
+ - ]
23 : : string id;
24 : : string data;
25 : :
26 [ + - ]: 5 : Entry() : id(), data() { }
27 : :
28 : 10 : Entry(const string & _id, const string & _data) :
29 [ + - ]: 10 : id(_id), data(_data)
30 : 10 : { }
31 : :
32 : : template<class Archive>
33 : 10 : void serialize(Archive & ar, const unsigned int) {
34 : 10 : ar & id & data;
35 : 10 : }
36 : : };
37 : :
38 : : typedef boost::multi_index_container<
39 : : Entry,
40 : : boost::multi_index::indexed_by<
41 : : boost::multi_index::hashed_unique<
42 : : boost::multi_index::member<Entry, string, &Entry::id>
43 : : >
44 : : > > entry_map;
45 : :
46 : : enum IndexScheme { ByID = 0 };
47 : :
48 : : enum ProtocolNumber { DictionaryTest = protocol::MaxReserved + 1 };
49 : :
50 : 21 : WSPR_DEFINE_CUSTOM_PROTOCOL(DictionaryTest,dictionary_test);
51 : :
52 : : typedef
53 : : DictionaryProtocol<MessageDictionaryTest, entry_map, IndexScheme>
54 : : ProtocolTraits;
55 : :
56 : : struct TestDictionaryService : public DictionaryService<ProtocolTraits> {
57 : : typedef DictionaryService<ProtocolTraits> super;
58 : :
59 : 8 : explicit TestDictionaryService(super::caller_type & caller) :
60 : 8 : super(caller)
61 : 8 : { }
62 : :
63 [ - + ]: 8 : virtual ~TestDictionaryService() { }
64 : :
65 : 10 : virtual void transition(super::State state) {
66 [ + + ]: 10 : if(state == super::Starting)
67 : 5 : state = super::Started;
68 [ + - ]: 5 : else if(state == super::Stopping)
69 : 5 : state = super::Stopped;
70 : 10 : super::transition(state);
71 : 10 : }
72 : : };
73 : :
74 [ - + ]: 24 : class DictionaryServiceTest : public ServiceTestCase<TestDictionaryService> {
75 : : WISP_IMPORT(protocol_processor, caller_type);
76 : : typedef NS_SSRC_WSPR_SERVICE::ServiceProtocol service_protocol;
77 : : WISP_IMPORT(service_protocol, MessageEchoRequest);
78 : : WISP_IMPORT(service_protocol, MessageEchoReply);
79 : : WISP_IMPORT(ProtocolTraits, packing_traits);
80 : : WISP_IMPORT(ProtocolTraits, MessageInsert);
81 : : WISP_IMPORT(ProtocolTraits, MessageErase);
82 : : WISP_IMPORT(ProtocolTraits, MessageQueryResult);
83 : : WISP_IMPORT(ProtocolTraits, MessageQueryAll);
84 : : WISP_IMPORT(ProtocolTraits, MessageQueryByDefault);
85 : : WISP_IMPORT(ProtocolTraits, MessageSingleQueryResult);
86 : : WISP_IMPORT(ProtocolTraits, MessageSingleQueryByDefault);
87 : : WISP_IMPORT(ProtocolTraits, CallInsert);
88 : : WISP_IMPORT(ProtocolTraits, CallErase);
89 : : WISP_IMPORT(ProtocolTraits, CallQueryAll);
90 : : WISP_IMPORT(ProtocolTraits, CallQueryByDefault);
91 : : WISP_IMPORT(ProtocolTraits, CallSingleQueryByDefault);
92 : : WISP_IMPORT(service_protocol, CallEchoRequest);
93 : :
94 : : public:
95 : :
96 : 1 : void test_insert() {
97 : 1 : protocol_processor & protocol = serv->protocol();
98 [ + - ]: 2 : const string key("foo");
99 [ + - + - ]: 2 : const string value("bar");
100 : :
101 : : std::pair<entry_map::iterator, bool> pair =
102 [ + - + - : 1 : protocol.insert<ByID>(Entry(key, value));
+ - + - ]
103 : :
104 [ + - + - : 1 : CPPUNIT_ASSERT_EQUAL(true, pair.second);
+ - + - +
- + - +
- ]
105 [ + - + - : 1 : CPPUNIT_ASSERT(1u == protocol.size<ByID>());
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
106 [ + - + - : 1 : CPPUNIT_ASSERT(1u == protocol.count<ByID>(key));
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
107 : :
108 [ + - + - : 1 : pair = protocol.insert<ByID>(Entry(key, value));
+ - + - ]
109 : :
110 [ + - + - : 1 : CPPUNIT_ASSERT_EQUAL(false, pair.second);
+ - + - +
- + - +
- ]
111 : :
112 [ + - ]: 1 : entry_map::iterator it = protocol.find<ByID>(key);
113 : :
114 [ + - + - : 1 : CPPUNIT_ASSERT(it != protocol.end<ByID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
115 [ + - + - : 1 : CPPUNIT_ASSERT(key == it->id);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
116 [ + - + - : 1 : CPPUNIT_ASSERT(value == it->data);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
117 : 1 : }
118 : :
119 : 1 : void test_erase() {
120 : 1 : protocol_processor & protocol = serv->protocol();
121 [ + - ]: 2 : const string key("foo");
122 [ + - + - ]: 2 : const string value("bar");
123 : :
124 [ + - + - : 1 : protocol.insert<ByID>(Entry(key, value));
+ - ]
125 : :
126 [ + - + - : 1 : CPPUNIT_ASSERT(1u == protocol.erase<ByID>(key));
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
127 [ + - + - : 1 : CPPUNIT_ASSERT(0u == protocol.size<ByID>());
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
128 [ + - + - : 1 : CPPUNIT_ASSERT(0u == protocol.count<ByID>(key));
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
129 : :
130 [ + - ]: 1 : entry_map::iterator it = protocol.find<ByID>(key);
131 [ + - + - : 1 : CPPUNIT_ASSERT(it == protocol.end<ByID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
132 : 1 : }
133 : :
134 : 1 : void test_replace() {
135 : 1 : protocol_processor & protocol = serv->protocol();
136 [ + - ]: 2 : const string key("foo");
137 [ + - + - ]: 2 : const string value("bar");
138 [ + - + - ]: 2 : const string value2("baz");
139 : :
140 [ + - + - : 1 : protocol.insert<ByID>(Entry(key, value));
+ - ]
141 : :
142 [ + - ]: 1 : entry_map::iterator it = protocol.find<ByID>(key);
143 : :
144 [ + - + - : 1 : CPPUNIT_ASSERT(it != protocol.end<ByID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
145 [ + - + - : 1 : CPPUNIT_ASSERT(key == it->id);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
146 [ + - + - : 1 : CPPUNIT_ASSERT(value == it->data);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
147 : :
148 [ + - + - : 2 : CPPUNIT_ASSERT_EQUAL(true,
+ - + - +
- + - + -
+ - + - ]
149 [ + - ]: 1 : protocol.replace<ByID>(it, Entry(key, value2)));
150 : :
151 [ + - + - : 1 : CPPUNIT_ASSERT(key == it->id);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
152 [ + - + - : 1 : CPPUNIT_ASSERT(value2 == it->data);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
153 : 1 : }
154 : :
155 : 1 : void test_call_insert() {
156 [ + - ]: 2 : const string key("foo");
157 [ + - + - ]: 2 : const string value("bar");
158 : :
159 [ + - + - : 2 : service_thread call(*serv);
+ - ]
160 [ + - + - ]: 2 : MessageInsert msg;
161 [ + - + - ]: 2 : MessageQueryResult result;
162 : :
163 [ + - + - : 1 : msg.values.push_back(Entry(key, value));
+ - ]
164 [ + - + - : 1 : call(CallInsert(), serv->name(), msg);
+ - ]
165 [ + - + - ]: 1 : call(CallQueryAll(), serv->name(), &result);
166 : :
167 [ + - ]: 1 : entry_map::iterator it = result.result.get<ByID>().find(key);
168 : :
169 [ + - + - : 1 : CPPUNIT_ASSERT(it != result.result.get<ByID>().end());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
170 [ + - + - : 1 : CPPUNIT_ASSERT(it->id == key);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
171 [ + - + - : 1 : CPPUNIT_ASSERT(it->data == value);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
172 : 1 : }
173 : :
174 : 1 : void test_call_single_query_by_default() {
175 [ + - ]: 2 : const string key("foo");
176 [ + - + - ]: 2 : const string value("bar");
177 : :
178 [ + - + - : 1 : serv->protocol().insert<ByID>(Entry(key, value));
+ - + - +
- ]
179 : :
180 [ + - + - : 2 : service_thread call(*serv);
+ - ]
181 [ + - + - ]: 2 : string service_type = "Test";
182 [ + - + - ]: 2 : string service_name = call.name();
183 [ + - ]: 1 : service_type += service_name;
184 : :
185 [ + - + - ]: 2 : MessageSingleQueryResult result;
186 : :
187 [ + - + - ]: 1 : call.callp<CallSingleQueryByDefault>(serv->name(), &result, key);
188 : :
189 [ + - + - : 1 : CPPUNIT_ASSERT(result.found);
+ - + - +
- + - + -
+ - + - +
- + - ]
190 [ + - + - : 1 : CPPUNIT_ASSERT(result.result.id == key);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
191 [ + - + - : 1 : CPPUNIT_ASSERT(result.result.data == value);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
192 : 1 : }
193 : :
194 : 1 : void test_call_query_all() {
195 [ + - ]: 2 : const string key("foo");
196 [ + - + - ]: 2 : const string value("bar");
197 : :
198 [ + - + - : 1 : serv->protocol().insert<ByID>(Entry(key, value));
+ - + - +
- ]
199 : :
200 [ + - + - : 2 : service_thread call(*serv);
+ - ]
201 [ + - + - ]: 2 : string service_type = "Test";
202 [ + - + - ]: 2 : string service_name = call.name();
203 [ + - ]: 1 : service_type += service_name;
204 : :
205 [ + - + - ]: 2 : MessageQueryResult result;
206 : :
207 [ + - + - ]: 1 : call(CallQueryAll(), serv->name(), &result);
208 : :
209 [ + - ]: 1 : entry_map::iterator it = result.result.get<ByID>().find(key);
210 : :
211 [ + - + - : 1 : CPPUNIT_ASSERT(it != result.result.get<ByID>().end());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
212 [ + - + - : 1 : CPPUNIT_ASSERT(it->id == key);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
213 [ + - + - : 1 : CPPUNIT_ASSERT(it->data == value);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
214 : 1 : }
215 : :
216 : 1 : void test_call_query_by_default() {
217 [ + - ]: 2 : const string key("foo");
218 [ + - + - ]: 2 : const string value("bar");
219 : :
220 [ + - + - : 1 : serv->protocol().insert<ByID>(Entry(key, value));
+ - + - +
- ]
221 : :
222 [ + - + - : 2 : service_thread call(*serv);
+ - ]
223 [ + - + - ]: 2 : string service_type = "Test";
224 [ + - + - ]: 2 : string service_name = call.name();
225 [ + - ]: 1 : service_type += service_name;
226 : :
227 [ + - + - ]: 2 : MessageQueryByDefault query;
228 [ + - + - ]: 2 : MessageQueryResult result;
229 : :
230 [ + - ]: 1 : query.keys.push_back(key);
231 [ + - + - ]: 1 : call(CallQueryByDefault(), serv->name(), &result, query);
232 : :
233 [ + - ]: 1 : entry_map::iterator it = result.result.get<ByID>().find(key);
234 : :
235 [ + - + - : 1 : CPPUNIT_ASSERT(it != result.result.get<ByID>().end());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
236 [ + - + - : 1 : CPPUNIT_ASSERT(it->id == key);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
237 [ + - + - : 1 : CPPUNIT_ASSERT(it->data == value);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
238 : 1 : }
239 : :
240 : 1 : void test_call_erase() {
241 [ + - ]: 2 : const string key("foo");
242 [ + - + - ]: 2 : const string value("bar");
243 : :
244 [ + - + - : 1 : serv->protocol().insert<ByID>(Entry(key, value));
+ - + - +
- ]
245 : :
246 [ + - + - : 2 : service_thread call(*serv);
+ - ]
247 [ + - + - ]: 2 : MessageErase msg;
248 [ + - + - ]: 2 : MessageQueryResult result;
249 : :
250 [ + - ]: 1 : msg.keys.push_back(key);
251 : :
252 [ + - + - : 1 : call(CallErase(), serv->name(), msg);
+ - ]
253 : : // Issue call only so we know erase has been processed.
254 : : MessageEchoReply echo_reply;
255 [ + - + - ]: 1 : call(CallEchoRequest(), serv->name(), &echo_reply);
256 : :
257 [ + - + - : 1 : CPPUNIT_ASSERT(0u == serv->protocol().size<ByID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
258 [ + - + - : 1 : CPPUNIT_ASSERT(0u == serv->protocol().count<ByID>(key));
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
259 : :
260 [ + - + - : 1 : entry_map::iterator it = serv->protocol().find<ByID>(key);
+ - ]
261 [ + - + - : 1 : CPPUNIT_ASSERT(it == serv->protocol().end<ByID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
262 : 1 : }
263 : :
264 [ + - + - : 4 : CPPUNIT_TEST_SUITE(DictionaryServiceTest);
+ - # # ]
265 [ + - + - : 1 : CPPUNIT_TEST(test_insert);
+ - + - +
- + - + -
+ - ]
266 [ + - + - : 1 : CPPUNIT_TEST(test_erase);
+ - + - +
- + - + -
+ - ]
267 [ + - + - : 1 : CPPUNIT_TEST(test_replace);
+ - + - +
- + - + -
+ - ]
268 [ + - + - : 1 : CPPUNIT_TEST(test_call_insert);
+ - + - +
- + - + -
+ - ]
269 [ + - + - : 1 : CPPUNIT_TEST(test_call_single_query_by_default);
+ - + - +
- + - + -
+ - ]
270 [ + - + - : 1 : CPPUNIT_TEST(test_call_query_all);
+ - + - +
- + - + -
+ - ]
271 [ + - + - : 1 : CPPUNIT_TEST(test_call_query_by_default);
+ - + - +
- + - + -
+ - ]
272 [ + - + - : 1 : CPPUNIT_TEST(test_call_erase);
+ - + - +
- + - + -
+ - ]
273 [ + - + - : 2 : CPPUNIT_TEST_SUITE_END();
+ - + - +
- + - ]
274 : : };
275 : :
276 : 1 : CPPUNIT_TEST_SUITE_REGISTRATION(DictionaryServiceTest);
277 [ + - + - : 8 : WISP_TEST_MAIN()
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
|