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 <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 
35 #include <iostream>
36 #include <functional>
37 
38 #include <gtest/gtest.h>
39 
40 #include "test_fixtures.h"
41 
42 using namespace XmlRpc;
43 
45 {
46  XmlRpcClient c("localhost", port);
47  XmlRpcValue noArgs, result;
48 
49  // Call the Hello method
50  ASSERT_TRUE(c.execute("Hello", noArgs, result));
51 
52  EXPECT_FALSE(c.isFault());
53  XmlRpcValue hello("Hello");
54  EXPECT_EQ(result, hello);
55 }
56 
57 TEST_F(XmlRpcTest, HelloNonBlock)
58 {
59  XmlRpcClient c("localhost", port);
60  XmlRpcValue noArgs, result;
61 
62  // Call the Hello method, non-blocking
63  ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
64 
65  bool done = false;
66  for (int i = 0; i < 30; i++)
67  {
68  done = c.executeCheckDone(result);
69  if (done)
70  break;
71  // run the client's dispatch loop to service the respond when it comes back
72  c._disp.work(0.1);
73  }
74 
75  ASSERT_TRUE(done);
76 
77  XmlRpcValue hello("Hello");
78  EXPECT_EQ(result, hello);
79 }
80 
81 TEST_F(XmlRpcTest, HelloNonBlock2)
82 {
83  XmlRpcClient c("localhost", port);
84  XmlRpcValue noArgs, result;
85 
86  // Lock the hello mutex so that the service call cannot return immediately
87  hello.hello_mutex.lock();
88 
89  // Call the Hello method, non-blocking
90  ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
91 
92  bool done = false;
93  for (int i = 0; i < 100; i++)
94  {
95  done = c.executeCheckDone(result);
96  if (done)
97  break;
98  // run the client's dispatch loop to service the respond when it comes back
99  c._disp.work(0.1);
100 
101  // unlock the hello mutex after 10 cycles
102  if (i == 10)
103  hello.hello_mutex.unlock();
104  }
105 
106  ASSERT_TRUE(done);
107 
108  XmlRpcValue hello("Hello");
109  EXPECT_EQ(result, hello);
110 }
111 
112 TEST_F(XmlRpcTest, ClientDisconnect)
113 {
114  XmlRpcClient* c = new XmlRpcClient("localhost", port);
115  XmlRpcValue noArgs, result;
116 
117  // Lock the hello mutex so that the service call cannot return immediately
118  hello.hello_mutex.lock();
119 
120  // Call the Hello method, non-blocking
121  ASSERT_TRUE(c->executeNonBlock("Hello", noArgs));
122 
123  // Destroy the client before the server can answer
124  delete c;
125 
126  // Unlock the mutex so the server can finish
127  hello.hello_mutex.unlock();
128 }
129 
130 TEST_F(XmlRpcTest, ServerDisconnect)
131 {
132  XmlRpcClient c("localhost", port);
133  XmlRpcValue noArgs, result;
134 
136 
137  // Stop calling the work method on the server
138  server_done = true;
139  server_thread.join();
140 
141  // Call the Hello method, non-blocking
142  ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
143 
144  // Destroy the server before it can answer
145  s.shutdown();
146 
147  // Run the client to completion
148  bool done = false;
149  for (int i = 0; i < 100; i++)
150  {
151  done = c.executeCheckDone(result);
152  if (done)
153  break;
154  // run the client's dispatch loop to service the respond when it comes back
155  c._disp.work(0.1);
156  }
157 
158  // The client should return true because the request is done, even though it
159  // timed out and wasn't able to complete.
160  EXPECT_TRUE(done);
161  EXPECT_EQ(-1, c.getfd());
162 
163  EXPECT_EQ(result, XmlRpcValue()); // Expect empty result
164 }
165 
166 TEST_F(XmlRpcTest, ServerDisconnect2)
167 {
168  XmlRpcClient c("localhost", port);
169  XmlRpcValue noArgs, result;
170 
171  // Stop calling the work method on the server
172  server_done = true;
173  server_thread.join();
174  // Close the server socket to reads (ie incoming connections)
175  shutdown(s.getfd(), SHUT_RD);
176 
177  // Call the Hello method. Expect failure since the server socket is not
178  // accepting new connections.
179  ASSERT_FALSE(c.execute("Hello", noArgs, result));
180 
181  XmlRpcValue hello; // Expect empty result
182  EXPECT_EQ(result, hello);
183 }
184 
185 int main(int argc, char **argv)
186 {
187  ::testing::InitGoogleTest(&argc, argv);
188  return RUN_ALL_TESTS();
189 }
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
bool isFault() const
Returns true if the result of the last execute() was a fault response.
Definition: XmlRpcClient.h:65
XmlRpcDispatch _disp
Definition: XmlRpcClient.h:131
int getfd() const
Return the file descriptor being monitored.
Definition: XmlRpcSource.h:27
void work(double msTime)
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 executeCheckDone(XmlRpcValue &result)


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix
autogenerated on Sun Feb 3 2019 03:29:51