Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - tests/wispers/registry - RegistryTest.cc (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 118 118 100.0 %
Date: 2012-04-09 Functions: 28 29 96.6 %
Branches: 531 1060 50.1 %

           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 <ssrc/wispers/registry/service.h>
      17                 :            : #include <ssrc/wispers/test/ServiceTestFixture.h>
      18                 :            : 
      19                 :            : #define BOOST_TEST_MODULE RegistryTest
      20                 :            : #include <boost/test/unit_test.hpp>
      21                 :            : 
      22                 :            : using namespace ssrc::wispers::test;
      23                 :            : using namespace NS_SSRC_WSPR;
      24                 :            : using namespace NS_SSRC_WSPR_REGISTRY;
      25                 :            : 
      26                 :            : typedef ServiceTestFixture<Registry> RegistryTestFixture;
      27                 :            : 
      28                 :          1 : BOOST_FIXTURE_TEST_SUITE(all, RegistryTestFixture)
      29                 :            : 
      30   [ +  -  +  -  :          5 : BOOST_AUTO_TEST_CASE(test_empty) {
                   +  - ]
      31                 :          1 :   protocol_processor & protocol = serv->protocol();
      32   [ +  -  +  -  :          1 :   BOOST_CHECK(protocol.empty<ByName>());
          +  -  +  -  -  
                      + ]
      33   [ +  -  +  -  :          1 :   BOOST_CHECK(protocol.empty<ByType>());
          +  -  +  -  -  
                      + ]
      34   [ +  -  +  -  :          1 :   BOOST_CHECK(protocol.empty<ByComposite>());
          +  -  +  -  -  
                      + ]
      35                 :          1 : }
      36                 :            : 
      37   [ +  -  +  -  :          5 : BOOST_AUTO_TEST_CASE(test_join) {
                   +  - ]
      38                 :          1 :   protocol_processor & protocol = serv->protocol();
      39         [ +  - ]:          2 :   const string foo("foo");
      40   [ +  -  +  - ]:          2 :   const string unknown(protocol::service_type_unknown());
      41                 :            : 
      42         [ +  - ]:          1 :   protocol.service_join(foo);
      43                 :            :   // Join twice on purpose.
      44         [ +  - ]:          1 :   protocol.service_join(foo);
      45   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == protocol.count<ByName>(foo));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
      46   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == protocol.count<ByType>(unknown));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
      47   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == protocol.count<ByComposite>(
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  -  + ]
      48                 :            :                                                    boost::make_tuple(foo, unknown)));
      49                 :          1 : }
      50                 :            : 
      51   [ +  -  +  -  :          5 : BOOST_AUTO_TEST_CASE(test_leave) {
                   +  - ]
      52                 :          1 :   protocol_processor & protocol = serv->protocol();
      53         [ +  - ]:          2 :   const string foo("foo");
      54         [ +  - ]:          1 :   protocol.service_join(foo);
      55         [ +  - ]:          1 :   protocol.service_leave(foo);
      56   [ +  -  +  -  :          1 :   BOOST_CHECK(0 == protocol.count<ByName>(foo));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
      57                 :          1 : }
      58                 :            : 
      59   [ +  -  +  -  :          5 : BOOST_AUTO_TEST_CASE(test_register_service) {
                   +  - ]
      60                 :          1 :   protocol_processor & protocol = serv->protocol();
      61         [ +  - ]:          2 :   const string foo("foo");
      62   [ +  -  +  - ]:          2 :   std::vector<string> service_types;
      63   [ +  -  +  -  :          1 :   service_types.push_back("A");
                   +  - ]
      64   [ +  -  +  -  :          1 :   service_types.push_back("B");
                   +  - ]
      65   [ +  -  +  -  :          1 :   service_types.push_back("C");
                   +  - ]
      66   [ +  -  +  -  :          1 :   service_types.push_back("D");
                   +  - ]
      67                 :            : 
      68   [ +  -  +  -  :          1 :   protocol.register_service(foo, service_types.begin(), service_types.end());
                   +  - ]
      69                 :            : 
      70   [ +  -  +  -  :          1 :   BOOST_CHECK_EQUAL(service_types.size(), protocol.size<ByName>());
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
      71   [ +  -  +  -  :          1 :   BOOST_CHECK_EQUAL(service_types.size(), protocol.size<ByType>());
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
      72   [ +  -  +  -  :          3 :   BOOST_CHECK_EQUAL(service_types.size(),
             +  -  -  + ]
      73   [ +  -  +  -  :          2 :                        protocol.size<ByComposite>());
             +  -  +  - ]
      74                 :            : 
      75   [ +  -  +  -  :          1 :   BOOST_CHECK_EQUAL(service_types.size(), protocol.count<ByName>(foo));
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
      76                 :            : 
      77   [ +  -  +  + ]:          5 :   for(unsigned int i = 0; i < service_types.size(); ++i) {
      78   [ +  -  +  -  :          4 :     BOOST_CHECK(1u == protocol.count<ByType>(service_types[i]));
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
      79   [ +  -  +  -  :          4 :     BOOST_CHECK(1u == protocol.count<ByComposite>(
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  -  
                      + ]
      80                 :            :                                                      boost::make_tuple(foo, service_types[i])));
      81                 :            :   }
      82                 :          1 : }
      83                 :            : 
      84   [ +  -  +  -  :          5 : BOOST_AUTO_TEST_CASE(test_unregister_service) {
                   +  - ]
      85                 :          1 :   protocol_processor & protocol = serv->protocol();
      86         [ +  - ]:          2 :   const string foo("foo");
      87   [ +  -  +  - ]:          2 :   const string unknown(protocol::service_type_unknown());
      88                 :            : 
      89   [ +  -  +  - ]:          2 :   std::set<string> service_types;
      90   [ +  -  +  -  :          1 :   service_types.insert("A");
                   +  - ]
      91   [ +  -  +  -  :          1 :   service_types.insert("B");
                   +  - ]
      92   [ +  -  +  -  :          1 :   service_types.insert("C");
                   +  - ]
      93   [ +  -  +  -  :          1 :   service_types.insert("D");
                   +  - ]
      94                 :            : 
      95   [ +  -  +  -  :          1 :   protocol.register_service(foo, service_types.begin(), service_types.end());
                   +  - ]
      96                 :            : 
      97   [ +  -  +  -  :          1 :   service_types.erase("A");
                   +  - ]
      98   [ +  -  +  -  :          1 :   service_types.erase("D");
                   +  - ]
      99                 :            : 
     100                 :            :   protocol.unregister_service(foo, service_types.begin(),
     101   [ +  -  +  -  :          1 :                               service_types.end());
                   +  - ]
     102                 :            : 
     103   [ +  -  +  -  :          1 :   BOOST_CHECK(2u == protocol.size<ByName>());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     104   [ +  -  +  -  :          1 :   BOOST_CHECK(2u == protocol.size<ByType>());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     105   [ +  -  +  -  :          1 :   BOOST_CHECK(2u == protocol.size<ByComposite>());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     106                 :            : 
     107   [ +  -  +  -  :          1 :   BOOST_CHECK(2u == protocol.count<ByName>(foo));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     108                 :            : 
     109   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == protocol.count<ByType>("A"));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     110   [ +  -  +  -  :          1 :   BOOST_CHECK(0u == protocol.count<ByType>("B"));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     111   [ +  -  +  -  :          1 :   BOOST_CHECK(0u == protocol.count<ByType>("C"));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     112   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == protocol.count<ByType>("D"));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     113   [ +  -  +  -  :          1 :   BOOST_CHECK(0u == protocol.count<ByType>(unknown));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     114                 :          1 : }
     115                 :            : 
     116   [ +  -  +  -  :          5 : BOOST_AUTO_TEST_CASE(test_process_requests) {
                   +  - ]
     117                 :            :   using NS_SSRC_WSPR_SERVICE::ServiceProtocol;
     118                 :          2 :   service_thread call(*serv);
     119   [ +  -  +  - ]:          2 :   string service_type = "Test";
     120   [ +  -  +  - ]:          2 :   string service_name = call.name();
     121         [ +  - ]:          1 :   service_type += service_name;
     122                 :            : 
     123                 :            :   // Join test
     124                 :            : 
     125   [ +  -  +  -  :          1 :   call.join(ServiceProtocol::service_group());
                   +  - ]
     126                 :            : 
     127   [ +  -  +  - ]:          2 :   MessageQueryByName query;
     128   [ +  -  +  - ]:          2 :   MessageQueryResult result;
     129         [ +  - ]:          1 :   query.keys.push_back(service_name);
     130   [ +  -  +  - ]:          1 :   call(CallQueryByName(), serv->name(), &result, query);
     131                 :            : 
     132   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == result.result.size());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     133                 :            : 
     134                 :            :   index_by_name::iterator it_byname =
     135         [ +  - ]:          1 :     result.result.get<ByName>().find(service_name);
     136                 :            : 
     137   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname != result.result.get<ByName>().end());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     138   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_name == service_name);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     139   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_type ==
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  -  
                      + ]
     140                 :            :                  protocol::service_type_unknown());
     141                 :            : 
     142   [ +  -  +  -  :          1 :   BOOST_CHECK(serv->protocol().is_leader());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     143                 :            : 
     144                 :            :   // Register test
     145                 :            : 
     146   [ +  -  +  - ]:          2 :   MessageRegister mregister;
     147         [ +  - ]:          1 :   mregister.service_name = service_name;
     148         [ +  - ]:          1 :   mregister.service_types.push_back(service_type);
     149   [ +  -  +  -  :          1 :   call(CallRegister(), serv->name(), mregister);
                   +  - ]
     150                 :            : 
     151   [ +  -  +  - ]:          2 :   MessageQueryByType query2;
     152         [ +  - ]:          1 :   query2.keys.push_back(service_type);
     153   [ +  -  +  - ]:          1 :   call(CallQueryByType(), serv->name(), &result, query2);
     154                 :            : 
     155   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == result.result.get<ByType>().size());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     156                 :            : 
     157                 :            :   index_by_type::iterator
     158         [ +  - ]:          1 :     it_bytype = result.result.get<ByType>().find(service_type);
     159                 :            : 
     160   [ +  -  +  -  :          1 :   BOOST_CHECK(it_bytype != result.result.get<ByType>().end());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     161   [ +  -  +  -  :          1 :   BOOST_CHECK(it_bytype->service_name == service_name);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     162   [ +  -  +  -  :          1 :   BOOST_CHECK(it_bytype->service_type == service_type);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     163                 :            : 
     164                 :            :   // Query All test
     165   [ +  -  +  - ]:          1 :   call(CallQueryAll(), serv->name(), &result);
     166                 :            : 
     167   [ +  -  +  -  :          1 :   BOOST_CHECK(2u == result.result.size());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     168                 :            : 
     169         [ +  - ]:          1 :   it_byname = result.result.get<ByName>().find(service_name);
     170                 :            : 
     171   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname != result.result.get<ByName>().end());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     172   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_name == service_name);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     173   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_type == service_type);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     174                 :            : 
     175   [ +  -  +  - ]:          1 :   it_byname = result.result.get<ByName>().find(serv->name());
     176                 :            : 
     177   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname != result.result.get<ByName>().end());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     178   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_name == serv->name());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  -  + ]
     179   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_type ==
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  -  
                      + ]
     180                 :            :                  protocol::service_type_registry());
     181                 :            : 
     182                 :            :   // Unregister test
     183                 :            : 
     184   [ +  -  +  - ]:          2 :   MessageUnregister unregister;
     185         [ +  - ]:          1 :   unregister.service_name = service_name;
     186         [ +  - ]:          1 :   unregister.service_types.push_back(service_type);
     187   [ +  -  +  -  :          1 :   call(CallUnregister(), serv->name(), unregister);
                   +  - ]
     188                 :            : 
     189   [ +  -  +  - ]:          1 :   call(CallQueryByName(), serv->name(), &result, query);
     190                 :            : 
     191   [ +  -  +  -  :          1 :   BOOST_CHECK(1u == result.result.size());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     192                 :            : 
     193         [ +  - ]:          1 :   it_byname = result.result.get<ByName>().find(service_name);
     194                 :            : 
     195   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname != result.result.get<ByName>().end());
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     196   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_name == service_name);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
                      + ]
     197   [ +  -  +  -  :          1 :   BOOST_CHECK(it_byname->service_type ==
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  -  
                      + ]
     198                 :            :                  protocol::service_type_unknown());
     199                 :            : 
     200                 :            :   // Leave test
     201                 :            : 
     202   [ +  -  +  -  :          1 :   call.leave(ServiceProtocol::service_group());
                   +  - ]
     203                 :            : 
     204   [ +  -  +  - ]:          1 :   call(CallQueryByName(), serv->name(), &result, query);
     205                 :            : 
     206   [ +  -  +  -  :          1 :   BOOST_CHECK(0u == result.result.size());
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
     207                 :          1 : }
     208                 :            : 
     209   [ +  -  +  -  :          8 : BOOST_AUTO_TEST_SUITE_END()
          +  -  +  -  +  
                -  +  - ]