Branch data Line data Source code
1 : : /* Copyright 2006-2013 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 <condition_variable>
20 : :
21 : : #define BOOST_TEST_MODULE ContinuationCallerTest
22 : : #include <boost/test/unit_test.hpp>
23 : :
24 : : using namespace NS_SSRC_WISP_TEST;
25 : : using NS_SSRC_WISP_PROTOCOL::message_info_ptr;
26 : :
27 : : WISP_IMPORT(EchoProtocol, CallEchoRequest);
28 : : WISP_IMPORT(EchoProtocol, MessageEchoRequest);
29 : : WISP_IMPORT(EchoProtocol, MessageEchoReply);
30 : :
31 : : // We cannot subclass EchoService to expose the service's caller to issue
32 : : // a future_call and take advantage of the service event loop to execute
33 : : // the asynchronous receive. Even with synchronization inside the tests
34 : : // to coordinate waiting for the continuation to execute, there remains
35 : : // a race condition making it possible to try to send at the same time
36 : : // the event loop is waiting in receive(), throwing an exception.
37 : : // Therefore, we use the service_thread to execute the send and receive,
38 : : // using the service only to reply. No synchronization required because
39 : : // we use only blocking I/O calls.
40 : :
41 : 2 : class ReplyFixture :
42 : : public ServiceTestFixture<EchoService, EchoProtocol::CallStop>
43 : : {
44 : : protected:
45 : : std::string _reply;
46 : :
47 : : public:
48 : 2 : ReplyFixture() {
49 : 2 : _reply.clear();
50 : 2 : }
51 : :
52 : 2 : void continue_echo_request(const MessageEchoReply & reply) {
53 : 2 : _reply = reply.payload;
54 : 2 : }
55 : : };
56 : :
57 [ + - + - : 5 : BOOST_FIXTURE_TEST_CASE(test_future_call, ReplyFixture) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
58 [ + - + - ]: 2 : MessageEchoRequest request("Test for Echo");
59 [ + - ]: 2 : service_thread st(*serv);
60 : :
61 [ + - + - : 1 : BOOST_REQUIRE(_reply.empty());
+ - + - +
- + - -
+ ]
62 : :
63 [ + - + - : 1 : st.future_call<CallEchoRequest>(std::bind(&ReplyFixture::continue_echo_request, this, std::placeholders::_1), serv->name(), request, Message::Safe);
+ - ]
64 : :
65 [ + - + - : 1 : BOOST_REQUIRE(st.continuations_map_size() == 1);
+ - + - +
- + - -
+ ]
66 : :
67 : 2 : message_info_ptr info;
68 [ + - ]: 1 : st.receive(info);
69 : :
70 [ + - + - : 1 : BOOST_REQUIRE(st.resume(*info));
+ - + - +
- + - + -
- + ]
71 : :
72 [ + - + - : 1 : BOOST_REQUIRE(_reply == request.payload);
+ - + - +
- + - -
+ ]
73 : 1 : }
74 : :
75 [ + - + - : 5 : BOOST_FIXTURE_TEST_CASE(test_future_callp, ReplyFixture) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
76 [ + - ]: 2 : const std::string echo_str("Test for Echo");
77 [ + - ]: 2 : service_thread st(*serv);
78 : :
79 [ + - + - : 1 : BOOST_REQUIRE(_reply.empty());
+ - + - +
- + - -
+ ]
80 : :
81 : 1 : auto continuation = [this](const CallEchoRequest::return_type & reply)->void {
82 : 1 : this->continue_echo_request(reply);
83 : 2 : };
84 : :
85 : 1 : st.future_call<CallEchoRequest>(Message::Safe, std::move(continuation),
86 [ + - + - ]: 2 : serv->name(), echo_str);
87 : :
88 [ + - + - : 1 : BOOST_REQUIRE(st.continuations_map_size() == 1);
+ - + - +
- + - -
+ ]
89 : :
90 : 2 : message_info_ptr info;
91 [ + - ]: 1 : st.receive(info);
92 : :
93 [ + - + - : 1 : BOOST_REQUIRE(st.resume(*info));
+ - + - +
- + - + -
- + ]
94 : :
95 [ + - + - : 1 : BOOST_REQUIRE(_reply == echo_str);
+ - + - +
- + - -
+ ]
96 [ + - + - : 6 : }
+ - + - ]
|