Wisp 1.2.1 C++ Unit Test Coverage
Current view: top level - tests/wisp/protocol - ContinuationCallerTest.cc (source / functions) Hit Total Coverage
Test: Wisp 1.2.1 C++ Unit Tests Lines: 37 37 100.0 %
Date: 2011-05-11 Functions: 18 20 90.0 %
Branches: 93 186 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* Copyright 2006-2011 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                 :            :  *     http://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/wisp/test/ServiceTestFixture.h>
      17                 :            : #include <examples/echo/echo_service.h>
      18                 :            : 
      19                 :            : #include <boost/thread/condition_variable.hpp>
      20                 :            : 
      21                 :            : #define BOOST_TEST_MODULE ContinuationCallerTest
      22                 :            : #include <boost/test/unit_test.hpp>
      23                 :            : 
      24                 :            : using namespace NS_SSRC_WISP_TEST;
      25                 :            : 
      26                 :            : WISP_IMPORT(EchoProtocol, CallEchoRequest);
      27                 :            : WISP_IMPORT(EchoProtocol, MessageEchoRequest);
      28                 :            : WISP_IMPORT(EchoProtocol, MessageEchoReply);
      29                 :            : 
      30                 :            : // This class allows us to use the service's caller to issue a future_call,
      31                 :            : // thereby taking advantage of the service event loop to execute the
      32                 :            : // asynchronous receive.
      33                 :            : struct TestEchoService : public EchoService {
      34                 :            :   typedef EchoService super;
      35                 :            : 
      36                 :          2 :   TestEchoService(super::caller_type & caller) : super(caller) { }
      37                 :            : 
      38         [ -  + ]:          2 :   virtual ~TestEchoService() { }
      39                 :            :   
      40                 :          3 :   super::caller_type & caller() { return _caller; }
      41                 :            : };
      42                 :            : 
      43 [ +  - ][ +  - ]:          2 : class ReplyFixture :
                 [ +  - ]
      44                 :            :   public ServiceTestFixture<TestEchoService, EchoProtocol::CallStop>
      45                 :            : {
      46                 :            : protected:
      47                 :            :   string _reply;
      48                 :            :   boost::condition_variable _cond;
      49                 :            :   boost::mutex _mutex;
      50                 :            : 
      51                 :            : public:
      52 [ +  - ][ +  - ]:          2 :   ReplyFixture() {
                 [ +  - ]
      53         [ +  - ]:          2 :     _reply.clear();
      54                 :          2 :   }
      55                 :            : 
      56                 :          2 :   void continue_echo_request(const MessageEchoReply & reply) {
      57                 :            :     {
      58                 :          4 :       boost::lock_guard<boost::mutex> lock(_mutex);
      59         [ +  - ]:          2 :       _reply = reply.payload;
      60                 :            :     }
      61                 :          2 :     _cond.notify_one();
      62                 :          2 :   }
      63                 :            : 
      64                 :            : };
      65                 :            : 
      66 [ +  - ][ +  - ]:          4 : BOOST_FIXTURE_TEST_CASE(test_future_call, ReplyFixture) {
                 [ +  - ]
      67 [ +  - ][ +  - ]:          2 :   MessageEchoRequest request("Test for Echo");
                 [ +  - ]
      68 [ +  - ][ +  - ]:          2 :   service_thread st(*serv);
                 [ +  - ]
      69                 :            : 
      70 [ +  - ][ +  - ]:          1 :   BOOST_REQUIRE(_reply.empty());
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ -  + ]
      71                 :            : 
      72                 :            :   {
      73 [ +  - ][ +  - ]:          2 :     boost::unique_lock<boost::mutex> lock(_mutex);
      74                 :            : 
      75 [ +  - ][ +  - ]:          1 :     serv->protocol().caller().future_call<CallEchoRequest>(boost::bind(&ReplyFixture::continue_echo_request, this, _1), serv->name(), request, Message::Safe);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
      76                 :            : 
      77 [ +  - ][ +  - ]:          1 :     BOOST_REQUIRE(serv->protocol().caller().continuations_map_size() == 1);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ -  + ]
      78                 :            : 
      79         [ +  - ]:          1 :     _cond.wait(lock);
      80                 :            :   }
      81                 :            : 
      82 [ +  - ][ +  - ]:          1 :   BOOST_REQUIRE(_reply == request.payload);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ -  + ]
      83                 :          1 : }
      84                 :            : 
      85 [ +  - ][ +  - ]:          4 : BOOST_FIXTURE_TEST_CASE(test_future_callp, ReplyFixture) {
                 [ +  - ]
      86         [ +  - ]:          2 :   const string echo_str("Test for Echo");
      87 [ +  - ][ +  - ]:          2 :   service_thread st(*serv);
                 [ +  - ]
      88                 :            : 
      89 [ +  - ][ +  - ]:          1 :   BOOST_REQUIRE(_reply.empty());
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ -  + ]
      90                 :            : 
      91 [ +  - ][ +  - ]:          2 :   boost::unique_lock<boost::mutex> lock(_mutex);
      92                 :            : 
      93                 :          1 :   auto continuation = [this](const CallEchoRequest::return_type & reply)->void {
      94                 :          1 :     this->continue_echo_request(reply);
      95                 :          1 :   };
      96                 :            : 
      97 [ +  - ][ +  - ]:          1 :   serv->protocol().caller().future_call<CallEchoRequest>(Message::Safe,
      98                 :          1 :                                                          std::move(continuation),
      99 [ +  - ][ +  - ]:          1 :                                                          serv->name(),
     100         [ +  - ]:          1 :                                                          echo_str);
     101                 :            : 
     102         [ +  - ]:          1 :   _cond.wait(lock);
     103                 :            : 
     104 [ +  - ][ +  - ]:          1 :   BOOST_REQUIRE(_reply == echo_str);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ -  + ]
     105 [ +  - ][ +  - ]:          6 : }
         [ +  - ][ +  - ]