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 : :
17 : : #include <unistd.h>
18 : :
19 : : #include <tests/TestCommon.h>
20 : : #include <ssrc/wispers/session/service.h>
21 : :
22 : : using namespace NS_SSRC_WSPR_SESSION;
23 : : using namespace NS_SSRC_WSPR_FCGI;
24 : : using NS_SSRC_WSPR_SERVICE::ServiceProtocol;
25 : :
26 [ + - ]: 6 : struct SessionTestOptions : public TestOptions {
27 : : SessionInitializer initializer;
28 : :
29 [ + - ]: 6 : SessionTestOptions() {
30 [ + - ]: 6 : initializer.checkpoint_path = CHECKPOINT_FILE;
31 : 6 : }
32 : : };
33 : :
34 [ - + ]: 18 : class SessionTest : public ServiceTestCase<Session, ServiceEventHandler,
35 : : SessionTestOptions, true>
36 : : {
37 : : WISP_IMPORT(SessionProtocol, MessageInsert);
38 : : WISP_IMPORT(SessionProtocol, MessageErase);
39 : : WISP_IMPORT(SessionProtocol, MessageQueryAll);
40 : : WISP_IMPORT(SessionProtocol, MessageQueryResult);
41 : : WISP_IMPORT(SessionProtocol, MessageSingleQueryResult);
42 : : WISP_IMPORT(SessionProtocol, MessageCreateSession);
43 : : WISP_IMPORT(SessionProtocol, MessageSetAttributes);
44 : : WISP_IMPORT(SessionProtocol, CallInsert);
45 : : WISP_IMPORT(SessionProtocol, CallErase);
46 : : WISP_IMPORT(SessionProtocol, CallQueryAll);
47 : : WISP_IMPORT(SessionProtocol, CallCreateSession);
48 : : WISP_IMPORT(SessionProtocol, CallGetSession);
49 : : WISP_IMPORT(SessionProtocol, CallSetAttributes);
50 : : WISP_IMPORT(ServiceProtocol, MessageEchoRequest);
51 : : WISP_IMPORT(ServiceProtocol, MessageEchoReply);
52 : : WISP_IMPORT(ServiceProtocol, CallEchoRequest);
53 : :
54 : : public:
55 : :
56 : 1 : void test_event_group_expire() {
57 [ + - + - : 1 : CPPUNIT_ASSERT(SessionProtocol::event_group_expire() == "wspr.evt.session.expire");
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
58 : 1 : }
59 : :
60 : 1 : void test_call_insert() {
61 [ + - ]: 2 : const string foo("foo");
62 [ + - + - ]: 2 : const string bar("bar");
63 [ + - + - ]: 2 : sid_type sid(foo);
64 : :
65 [ + - + - : 2 : service_thread call(*serv);
+ - ]
66 : :
67 [ + - + - ]: 2 : MessageInsert msg;
68 [ + - + - ]: 2 : MessageQueryResult result;
69 : :
70 [ + - + - : 2 : SessionData entry(sid, bar);
+ - + - ]
71 [ + - + - : 1 : entry.attributes->set(sid, "value");
+ - ]
72 [ + - ]: 1 : msg.values.push_back(entry);
73 : :
74 [ + - + - : 1 : call(CallInsert(), serv->name(), msg);
+ - ]
75 [ + - + - ]: 1 : call(CallQueryAll(), serv->name(), &result);
76 : :
77 [ + - ]: 1 : session_map::iterator it = result.result.get<BySID>().find(sid);
78 : :
79 [ + - + - : 1 : CPPUNIT_ASSERT(it == result.result.get<BySID>().end());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
80 : :
81 : : std::pair<session_map::iterator, bool> pair =
82 [ + - + - : 1 : serv->protocol().create_session();
+ - + - ]
83 : :
84 [ + - + - : 1 : msg.values[0].sid = sid = pair.first->sid;
+ - + - ]
85 : :
86 [ + - + - : 1 : call(CallInsert(), serv->name(), msg);
+ - ]
87 [ + - + - ]: 1 : call(CallQueryAll(), serv->name(), &result);
88 : :
89 [ + - ]: 1 : it = result.result.get<BySID>().find(sid);
90 : :
91 [ + - + - : 1 : CPPUNIT_ASSERT(it != result.result.get<BySID>().end());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
92 [ + - + - : 1 : CPPUNIT_ASSERT(sid == it->sid);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
93 [ + - + - : 1 : CPPUNIT_ASSERT(foo == it->attributes->get<string>("", "value"));
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
94 : 1 : }
95 : :
96 : 1 : void test_call_create_session() {
97 : 2 : service_thread call(*serv);
98 [ + - + - ]: 2 : MessageSingleQueryResult result;
99 : :
100 [ + - + - ]: 1 : call(CallCreateSession(), serv->name(), &result);
101 : :
102 [ + - + - : 1 : CPPUNIT_ASSERT(result.found);
+ - + - +
- + - + -
+ - + - +
- + - ]
103 [ + - + - : 1 : CPPUNIT_ASSERT(!result.result.sid.empty());
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
104 [ + - + - : 1 : CPPUNIT_ASSERT(result.result.attributes);
+ - + - +
- + - + -
+ - + - +
- + - ]
105 : 1 : }
106 : :
107 : 1 : void test_call_erase_session() {
108 : : std::pair<session_map::iterator, bool> pair =
109 : 1 : serv->protocol().create_session();
110 : :
111 [ + - + - : 1 : CPPUNIT_ASSERT(pair.first != serv->protocol().end<BySID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
112 : :
113 : 2 : service_thread call(*serv);
114 [ + - + - ]: 2 : MessageErase msg;
115 [ + - + - ]: 2 : MessageQueryResult result;
116 [ + - + - : 2 : const string key(pair.first->sid);
+ - ]
117 : :
118 [ + - ]: 1 : msg.keys.push_back(key);
119 : :
120 [ + - + - : 1 : call(CallErase(), serv->name(), msg);
+ - ]
121 : : // Issue call only so we know erase has been processed.
122 : : MessageEchoReply echo_reply;
123 [ + - + - ]: 1 : call(CallEchoRequest(), serv->name(), &echo_reply);
124 : :
125 [ + - + - : 1 : CPPUNIT_ASSERT(0u == serv->protocol().size<BySID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
126 [ + - + - : 1 : CPPUNIT_ASSERT(0u == serv->protocol().count<BySID>(key));
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
127 : :
128 [ + - + - : 1 : session_map::iterator it = serv->protocol().find<BySID>(key);
+ - ]
129 [ + - + - : 1 : CPPUNIT_ASSERT(it == serv->protocol().end<BySID>());
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
130 : 1 : }
131 : :
132 : 1 : void test_call_set_attribute() {
133 : 2 : service_thread call(*serv);
134 [ + - + - ]: 2 : MessageSingleQueryResult result;
135 : :
136 [ + - + - ]: 1 : call(CallCreateSession(), serv->name(), &result);
137 : :
138 [ + - + - ]: 2 : MessageSetAttributes msg(result.result.sid);
139 [ + - + - : 1 : msg.attributes.set(string("foo.value"), "foo", "key");
+ - + - +
- ]
140 [ + - + - ]: 1 : call.send<CallSetAttributes>(serv->name(), msg);
141 : :
142 [ + - + - ]: 1 : call.callp<CallGetSession>(serv->name(), &result, result.result.sid);
143 : :
144 [ + - + - : 1 : CPPUNIT_ASSERT(result.result.attributes->get<string>("", "foo", "key") == "foo.value");
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
145 : 1 : }
146 : :
147 : 1 : void test_expire_sessions() {
148 : 2 : service_thread call(*serv);
149 [ + - + - ]: 2 : MessageSingleQueryResult result;
150 [ + - + - : 2 : sid_type sid1, sid2;
+ - + - ]
151 : :
152 [ + - + - ]: 1 : call(CallCreateSession(), serv->name(), &result);
153 [ + - + - : 1 : CPPUNIT_ASSERT(result.found);
+ - + - +
- + - + -
+ - + - +
- + - ]
154 [ + - ]: 1 : sid1 = result.result.sid;
155 [ + - + - ]: 1 : call(CallCreateSession(), serv->name(), &result);
156 [ + - + - : 1 : CPPUNIT_ASSERT(result.found);
+ - + - +
- + - + -
+ - + - +
- + - ]
157 [ + - ]: 1 : sid2 = result.result.sid;
158 : :
159 [ + - + - : 1 : serv->protocol().expire_sessions(100);
+ - ]
160 : :
161 [ + - + - ]: 1 : call.callp<CallGetSession>(serv->name(), &result, sid1);
162 [ + - + - : 1 : CPPUNIT_ASSERT(!result.found);
+ - + - +
- + - + -
+ - + - +
- + - ]
163 [ + - + - ]: 1 : call.callp<CallGetSession>(serv->name(), &result, sid2);
164 [ + - + - : 1 : CPPUNIT_ASSERT(!result.found);
+ - + - +
- + - + -
+ - + - +
- + - ]
165 : 1 : }
166 : :
167 [ + - + - : 4 : CPPUNIT_TEST_SUITE(SessionTest);
+ - # # ]
168 [ + - + - : 1 : CPPUNIT_TEST(test_event_group_expire);
+ - + - +
- + - + -
+ - ]
169 [ + - + - : 1 : CPPUNIT_TEST(test_call_insert);
+ - + - +
- + - + -
+ - ]
170 [ + - + - : 1 : CPPUNIT_TEST(test_call_create_session);
+ - + - +
- + - + -
+ - ]
171 [ + - + - : 1 : CPPUNIT_TEST(test_call_erase_session);
+ - + - +
- + - + -
+ - ]
172 [ + - + - : 1 : CPPUNIT_TEST(test_call_set_attribute);
+ - + - +
- + - + -
+ - ]
173 [ + - + - : 1 : CPPUNIT_TEST(test_expire_sessions);
+ - + - +
- + - + -
+ - ]
174 [ + - + - : 2 : CPPUNIT_TEST_SUITE_END();
+ - + - +
- + - ]
175 : : };
176 : :
177 : 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SessionTest);
178 [ + - + - : 18 : WISP_TEST_MAIN()
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
|