Web Wispers 1.2.0 C++ Unit Test Coverage
Current view: top level - tests/wispers/registry - RegistryTest.cc (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.0 C++ Unit Tests Lines: 132 132 100.0 %
Date: 2010-05-27 Functions: 18 19 94.7 %
Branches: 706 1412 50.0 %

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