1 // RPC test and pseudo-documentation.
2 // generates print statements on failures, but eventually says "rpctest OK"
13 char log_thread_prefix = 'r';
15 static rpcs *server; // server rpc object
16 static rpcc *clients[NUM_CL]; // client rpc object
17 static string dst; //server's ip address
18 static in_port_t port;
20 // server-side handlers. they must be methods of some class
21 // to simplify rpcs::reg(). a server process can have handlers
22 // from multiple classes.
25 int handle_22(string & r, const string a, const string b);
26 int handle_fast(int & r, const int a);
27 int handle_slow(int & r, const int a);
28 int handle_bigrep(string & r, const size_t a);
31 namespace srv_protocol {
32 using status = rpc_protocol::status;
33 REMOTE_PROCEDURE_BASE(0);
34 REMOTE_PROCEDURE(22, _22, (string &, string, string));
35 REMOTE_PROCEDURE(23, fast, (int &, int));
36 REMOTE_PROCEDURE(24, slow, (int &, int));
37 REMOTE_PROCEDURE(25, bigrep, (string &, size_t));
40 // a handler. a and b are arguments, r is the result.
41 // there can be multiple arguments but only one result.
42 // the caller also gets to see the int return value
43 // as the return value from rpcc::call().
44 // rpcs::reg() decides how to unmarshall by looking
45 // at these argument types, so this function definition
46 // does what a .x file does in SunRPC.
47 int srv::handle_22(string & r, const string a, string b) {
52 int srv::handle_fast(int & r, const int a) {
57 int srv::handle_slow(int & r, const int a) {
58 usleep(random() % 500);
63 int srv::handle_bigrep(string & r, const size_t len) {
71 server = new rpcs(port);
72 server->reg(srv_protocol::_22, &srv::handle_22, &service);
73 server->reg(srv_protocol::fast, &srv::handle_fast, &service);
74 server->reg(srv_protocol::slow, &srv::handle_slow, &service);
75 server->reg(srv_protocol::bigrep, &srv::handle_bigrep, &service);
81 rpc_protocol::request_header rh{1,2,3,4,5};
83 VERIFY(((string)m).size()==rpc_protocol::RPC_HEADER_SZ);
85 unsigned long long l = 1223344455L;
86 size_t sz = 101010101;
87 string s = "hallo....";
94 VERIFY(b.size() == rpc_protocol::RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int)+sizeof(uint32_t));
96 unmarshall un(b, true);
97 rpc_protocol::request_header rh1;
98 un.unpack_header(rh1);
99 VERIFY(memcmp(&rh,&rh1,sizeof(rh))==0);
101 unsigned long long l1;
109 VERIFY(i1==i && l1==l && s1==s && sz1==sz);
112 void client1(size_t cl) {
114 size_t which_cl = cl % NUM_CL;
116 for(int i = 0; i < 100; i++){
117 unsigned long arg = (random() % 2000);
119 int ret = clients[which_cl]->call(srv_protocol::bigrep, rep, arg);
121 if ((unsigned long)rep.size()!=arg)
122 cout << "repsize wrong " << rep.size() << "!=" << arg << endl;
123 VERIFY((unsigned long)rep.size() == arg);
126 // test rpc replies coming back not in the order of
127 // the original calls -- i.e. does xid reply dispatch work.
128 for(int i = 0; i < 100; i++){
129 int which = (random() % 2);
130 int arg = (random() % 1000);
133 auto start = steady_clock::now();
135 int ret = clients[which_cl]->call(which ? srv_protocol::fast : srv_protocol::slow, rep, arg);
136 auto end = steady_clock::now();
137 auto diff = duration_cast<milliseconds>(end - start).count();
139 cout << diff << " ms have elapsed!!!" << endl;
141 VERIFY(rep == (which ? arg+1 : arg+2));
145 void client2(size_t cl) {
146 size_t which_cl = cl % NUM_CL;
151 while(time(0) - t1 < 10){
152 unsigned long arg = (random() % 2000);
154 int ret = clients[which_cl]->call(srv_protocol::bigrep, rep, arg);
155 if ((unsigned long)rep.size()!=arg)
156 cout << "ask for " << arg << " reply got " << rep.size() << " ret " << ret << endl;
157 VERIFY((unsigned long)rep.size() == arg);
161 void client3(void *xx) {
162 rpcc *c = (rpcc *) xx;
164 for(int i = 0; i < 4; i++){
166 int ret = c->call_timeout(srv_protocol::slow, milliseconds(300), rep, i);
167 VERIFY(ret == rpc_protocol::timeout_failure || rep == i+2);
171 void simple_tests(rpcc *c) {
172 cout << "simple_tests" << endl;
173 // an RPC call to procedure #22.
174 // rpcc::call() looks at the argument types to decide how
175 // to marshall the RPC call packet, and how to unmarshall
178 int intret = c->call(srv_protocol::_22, rep, (string)"hello", (string)" goodbye");
179 VERIFY(intret == 0); // this is what handle_22 returns
180 VERIFY(rep == "hello goodbye");
181 cout << " -- string concat RPC .. ok" << endl;
183 // small request, big reply (perhaps req via UDP, reply via TCP)
184 intret = c->call_timeout(srv_protocol::bigrep, milliseconds(20000), rep, 70000ul);
186 VERIFY(rep.size() == 70000);
187 cout << " -- small request, big reply .. ok" << endl;
189 // specify a timeout value to an RPC that should succeed (udp)
191 intret = c->call_timeout(srv_protocol::fast, milliseconds(300), xx, 77);
192 VERIFY(intret == 0 && xx == 78);
193 cout << " -- no spurious timeout .. ok" << endl;
195 // specify a timeout value to an RPC that should succeed (tcp)
197 string arg(1000, 'x');
199 c->call_timeout(srv_protocol::_22, milliseconds(300), rep2, arg, (string)"x");
200 VERIFY(rep2.size() == 1001);
201 cout << " -- no spurious timeout .. ok" << endl;
205 string big(1000000, 'x');
206 intret = c->call(srv_protocol::_22, rep, big, (string)"z");
208 VERIFY(rep.size() == 1000001);
209 cout << " -- huge 1M rpc request .. ok" << endl;
211 // specify a timeout value to an RPC that should timeout (udp)
212 string non_existent = "127.0.0.1:7661";
213 rpcc *c1 = new rpcc(non_existent);
215 intret = c1->bind(milliseconds(300));
217 VERIFY(intret < 0 && (t1 - t0) <= 4);
218 cout << " -- rpc timeout .. ok" << endl;
219 cout << "simple_tests OK" << endl;
222 void concurrent_test(size_t nt) {
223 // create threads that make lots of calls in parallel,
224 // to test thread synchronization for concurrent calls
226 cout << "start concurrent_test (" << nt << " threads) ...";
228 vector<thread> th(nt);
230 for(size_t i = 0; i < nt; i++)
231 th[i] = thread(client1, i);
233 for(size_t i = 0; i < nt; i++)
236 cout << " OK" << endl;
240 cout << "start lossy_test ...";
241 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
248 for (int i = 0; i < NUM_CL; i++) {
250 clients[i] = new rpcc(dst);
251 VERIFY(clients[i]->bind()==0);
256 vector<thread> th(nt);
258 for(size_t i = 0; i < nt; i++)
259 th[i] = thread(client2, i);
261 for(size_t i = 0; i < nt; i++)
264 cout << ".. OK" << endl;
265 VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
268 void failure_test() {
270 rpcc *client = clients[0];
272 cout << "failure_test" << endl;
276 client1 = new rpcc(dst);
277 VERIFY (client1->bind(milliseconds(3000)) < 0);
278 cout << " -- create new client and try to bind to failed server .. failed ok" << endl;
285 int intret = client->call(srv_protocol::_22, rep, (string)"hello", (string)" goodbye");
286 VERIFY(intret == rpc_protocol::oldsrv_failure);
287 cout << " -- call recovered server with old client .. failed ok" << endl;
291 clients[0] = client = new rpcc(dst);
292 VERIFY (client->bind() >= 0);
293 VERIFY (client->bind() < 0);
295 intret = client->call(srv_protocol::_22, rep, (string)"hello", (string)" goodbye");
297 VERIFY(rep == "hello goodbye");
299 cout << " -- delete existing rpc client, create replacement rpc client .. ok" << endl;
303 cout << " -- concurrent test on new rpc client w/ " << nt << " threads ..";
305 vector<thread> th(nt);
307 for(size_t i = 0; i < nt; i++)
308 th[i] = thread(client3, client);
310 for(size_t i = 0; i < nt; i++)
313 cout << "ok" << endl;
319 clients[0] = client = new rpcc(dst);
320 VERIFY (client->bind() >= 0);
321 cout << " -- delete existing rpc client and server, create replacements.. ok" << endl;
323 cout << " -- concurrent test on new client and server w/ " << nt << " threads ..";
325 for(size_t i = 0; i < nt; i++)
326 th[i] = thread(client3, client);
328 for(size_t i = 0; i < nt; i++)
331 cout << "ok" << endl;
333 cout << "failure_test OK" << endl;
336 int main(int argc, char *argv[]) {
338 setvbuf(stdout, NULL, _IONBF, 0);
339 setvbuf(stderr, NULL, _IONBF, 0);
342 bool isclient = false;
343 bool isserver = false;
345 srandom((uint32_t)getpid());
346 port = 20000 + (getpid() % 10000);
349 while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
358 debug_level = atoi(optarg);
361 port = (in_port_t)atoi(optarg);
364 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
371 if (!isserver && !isclient) {
372 isserver = isclient = true;
375 if (debug_level > 0) {
376 DEBUG_LEVEL = debug_level;
377 IF_LEVEL(1) LOG_NONMEMBER("DEBUG LEVEL: " << debug_level);
383 cout << "starting server on port " << port << " RPC_HEADER_SZ " << (int)rpc_protocol::RPC_HEADER_SZ << endl;
389 dst = "127.0.0.1:" + to_string(port);
392 // start the client. bind it to the server.
393 // starts a thread to listen for replies and hand them to
394 // the correct waiting caller thread. there should probably
395 // be only one rpcc per process. you probably need one
397 for (int i = 0; i < NUM_CL; i++) {
398 clients[i] = new rpcc(dst);
399 VERIFY (clients[i]->bind() == 0);
402 simple_tests(clients[0]);
409 cout << "rpctest OK" << endl;