test_dispatch_live.cpp
Go to the documentation of this file.
1 /*
2  * Unit tests for XmlRpc++
3  *
4  * Copyright (C) 2017, Zoox Inc
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Author: Austin Hendrix <austin@zoox.com>
21  * Loosley based on HelloServer.cpp and HelloClient.cpp by Chris Morley
22  *
23  */
24 
25 #include "xmlrpcpp/XmlRpc.h"
26 
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #ifndef _WIN32
32 # include <sys/socket.h>
33 #else
34 # include <winsock2.h>
35 #endif
36 
37 #include <iostream>
38 #include <functional>
39 
40 #include <gtest/gtest.h>
41 
42 #include "test_fixtures.h"
43 
44 using namespace XmlRpc;
45 
47 {
48  XmlRpcClient c("localhost", port);
49  XmlRpcValue noArgs, result;
50 
51  // Call the Hello method
52  ASSERT_TRUE(c.execute("Hello", noArgs, result));
53 
54  EXPECT_FALSE(c.isFault());
55  XmlRpcValue hello("Hello");
56  EXPECT_EQ(result, hello);
57 }
58 
59 TEST_F(XmlRpcTest, HelloNonBlock)
60 {
61  XmlRpcClient c("localhost", port);
62  XmlRpcValue noArgs, result;
63 
64  // Call the Hello method, non-blocking
65  ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
66 
67  bool done = false;
68  for (int i = 0; i < 30; i++)
69  {
70  done = c.executeCheckDone(result);
71  if (done)
72  break;
73  // run the client's dispatch loop to service the respond when it comes back
74  c._disp.work(0.1);
75  }
76 
77  ASSERT_TRUE(done);
78 
79  XmlRpcValue hello("Hello");
80  EXPECT_EQ(result, hello);
81 }
82 
83 TEST_F(XmlRpcTest, HelloNonBlock2)
84 {
85  XmlRpcClient c("localhost", port);
86  XmlRpcValue noArgs, result;
87 
88  // Lock the hello mutex so that the service call cannot return immediately
89  hello.hello_mutex.lock();
90 
91  // Call the Hello method, non-blocking
92  ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
93 
94  bool done = false;
95  for (int i = 0; i < 100; i++)
96  {
97  done = c.executeCheckDone(result);
98  if (done)
99  break;
100  // run the client's dispatch loop to service the respond when it comes back
101  c._disp.work(0.1);
102 
103  // unlock the hello mutex after 10 cycles
104  if (i == 10)
105  hello.hello_mutex.unlock();
106  }
107 
108  ASSERT_TRUE(done);
109 
110  XmlRpcValue hello("Hello");
111  EXPECT_EQ(result, hello);
112 }
113 
114 TEST_F(XmlRpcTest, ClientDisconnect)
115 {
116  XmlRpcClient* c = new XmlRpcClient("localhost", port);
117  XmlRpcValue noArgs, result;
118 
119  // Lock the hello mutex so that the service call cannot return immediately
120  hello.hello_mutex.lock();
121 
122  // Call the Hello method, non-blocking
123  ASSERT_TRUE(c->executeNonBlock("Hello", noArgs));
124 
125  // Destroy the client before the server can answer
126  delete c;
127 
128  // Unlock the mutex so the server can finish
129  hello.hello_mutex.unlock();
130 }
131 
132 TEST_F(XmlRpcTest, ServerDisconnect)
133 {
134  XmlRpcClient c("localhost", port);
135  XmlRpcValue noArgs, result;
136 
138 
139  // Stop calling the work method on the server
140  server_done = true;
141  server_thread.join();
142 
143  // Call the Hello method, non-blocking
144  ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
145 
146  // Destroy the server before it can answer
147  s.shutdown();
148 
149  // Run the client to completion
150  bool done = false;
151  for (int i = 0; i < 100; i++)
152  {
153  done = c.executeCheckDone(result);
154  if (done)
155  break;
156  // run the client's dispatch loop to service the respond when it comes back
157  c._disp.work(0.1);
158  }
159 
160  // The client should return true because the request is done, even though it
161  // timed out and wasn't able to complete.
162  EXPECT_TRUE(done);
163  EXPECT_EQ(-1, c.getfd());
164 
165  EXPECT_EQ(result, XmlRpcValue()); // Expect empty result
166 }
167 
168 TEST_F(XmlRpcTest, ServerDisconnect2)
169 {
170  XmlRpcClient c("localhost", port);
171  XmlRpcValue noArgs, result;
172 
173  // Stop calling the work method on the server
174  server_done = true;
175  server_thread.join();
176  // Close the server socket to reads (ie incoming connections)
177  #ifndef _WIN32
178  shutdown(s.getfd(), SHUT_RD);
179  #else
180  shutdown(s.getfd(), SD_RECEIVE);
181  #endif
182 
183  // Call the Hello method. Expect failure since the server socket is not
184  // accepting new connections.
185  ASSERT_FALSE(c.execute("Hello", noArgs, result));
186 
187  XmlRpcValue hello; // Expect empty result
188  EXPECT_EQ(result, hello);
189 }
190 
191 int main(int argc, char **argv)
192 {
193  ::testing::InitGoogleTest(&argc, argv);
194  return RUN_ALL_TESTS();
195 }
int getfd() const
Return the file descriptor being monitored.
Definition: XmlRpcSource.h:27
TEST_F(XmlRpcTest, Hello)
RPC method arguments and results are represented by Values.
Definition: XmlRpcValue.h:24
void shutdown()
Close all connections with clients and the socket file descriptor.
XmlRpcServer s
Definition: HelloServer.cpp:11
XmlRpcDispatch _disp
Definition: XmlRpcClient.h:131
void work(double msTime)
XMLRPCPP_DECL void setVerbosity(int level)
Sets log message verbosity. This is short for XmlRpcLogHandler::setVerbosity(level) ...
Definition: XmlRpcUtil.cpp:76
A class to send XML RPC requests to a server and return the results.
Definition: XmlRpcClient.h:26
int main(int argc, char **argv)
bool execute(const char *method, XmlRpcValue const &params, XmlRpcValue &result)
bool executeNonBlock(const char *method, XmlRpcValue const &params)
bool isFault() const
Returns true if the result of the last execute() was a fault response.
Definition: XmlRpcClient.h:65
bool executeCheckDone(XmlRpcValue &result)


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix
autogenerated on Wed Oct 16 2019 03:27:40