Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - tests/wispers/session - SessionTest.cc (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 84 84 100.0 %
Date: 2012-04-09 Functions: 35 36 97.2 %
Branches: 275 550 50.0 %

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