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  * Loosely 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 }
XmlRpc::setVerbosity
XMLRPCPP_DECL void setVerbosity(int level)
Sets log message verbosity. This is short for XmlRpcLogHandler::setVerbosity(level)
Definition: XmlRpcUtil.cpp:77
s
XmlRpcServer s
Definition: HelloServer.cpp:11
XmlRpc::XmlRpcClient::executeNonBlock
bool executeNonBlock(const char *method, XmlRpcValue const &params)
Definition: XmlRpcClient.cpp:153
XmlRpc::XmlRpcClient::execute
bool execute(const char *method, XmlRpcValue const &params, XmlRpcValue &result)
Definition: XmlRpcClient.cpp:104
XmlRpc
Definition: XmlRpcClient.h:20
XmlRpc::XmlRpcClient
A class to send XML RPC requests to a server and return the results.
Definition: XmlRpcClient.h:26
main
int main(int argc, char **argv)
Definition: test_dispatch_live.cpp:191
XmlRpc::XmlRpcClient::executeCheckDone
bool executeCheckDone(XmlRpcValue &result)
Definition: XmlRpcClient.cpp:179
XmlRpc::XmlRpcClient::isFault
bool isFault() const
Returns true if the result of the last execute() was a fault response.
Definition: XmlRpcClient.h:65
XmlRpc.h
XmlRpc::XmlRpcSource::getfd
int getfd() const
Return the file descriptor being monitored.
Definition: XmlRpcSource.h:27
test_fixtures.h
Hello
Definition: HelloServer.cpp:14
XmlRpc::XmlRpcDispatch::work
void work(double msTime)
Definition: XmlRpcDispatch.cpp:130
XmlRpc::XmlRpcServer::shutdown
void shutdown()
Close all connections with clients and the socket file descriptor.
Definition: XmlRpcServer.cpp:299
TEST_F
TEST_F(XmlRpcTest, Hello)
Definition: test_dispatch_live.cpp:46
XmlRpcTest
Definition: HelloTest.cpp:91
XmlRpc::XmlRpcClient::_disp
XmlRpcDispatch _disp
Definition: XmlRpcClient.h:131
XmlRpc::XmlRpcValue
RPC method arguments and results are represented by Values.
Definition: XmlRpcValue.h:24


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:32