1 // RPC test and pseudo-documentation.
2 // generates print statements on failures, but eventually says "rpctest OK"
11 #include <sys/types.h>
14 #include "lang/verify.h"
18 char log_thread_prefix = 'r';
26 rpcs *server; // server rpc object
27 rpcc *clients[NUM_CL]; // client rpc object
28 string dst; //server's ip address
31 // server-side handlers. they must be methods of some class
32 // to simplify rpcs::reg(). a server process can have handlers
33 // from multiple classes.
36 int handle_22(string & r, const string a, const string b);
37 int handle_fast(int &r, const int a);
38 int handle_slow(int &r, const int a);
39 int handle_bigrep(string &r, const size_t a);
42 // a handler. a and b are arguments, r is the result.
43 // there can be multiple arguments but only one result.
44 // the caller also gets to see the int return value
45 // as the return value from rpcc::call().
46 // rpcs::reg() decides how to unmarshall by looking
47 // at these argument types, so this function definition
48 // does what a .x file does in SunRPC.
50 srv::handle_22(string &r, const string a, string b)
57 srv::handle_fast(int &r, const int a)
64 srv::handle_slow(int &r, const int a)
66 usleep(random() % 5000);
72 srv::handle_bigrep(string &r, const size_t len)
74 r = string((size_t)len, 'x');
82 server = new rpcs((unsigned int)port);
83 server->reg(22, &srv::handle_22, &service);
84 server->reg(23, &srv::handle_fast, &service);
85 server->reg(24, &srv::handle_slow, &service);
86 server->reg(25, &srv::handle_bigrep, &service);
93 request_header rh{1,2,3,4,5};
94 m.pack_req_header(rh);
95 VERIFY(m.size()==RPC_HEADER_SZ);
97 unsigned long long l = 1223344455L;
98 string s = "hallo....";
106 VERIFY(sz == RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int));
110 un.unpack_req_header(&rh1);
111 VERIFY(memcmp(&rh,&rh1,sizeof(rh))==0);
113 unsigned long long l1;
119 VERIFY(i1==i && l1==l && s1==s);
126 size_t which_cl = cl % NUM_CL;
128 for(int i = 0; i < 100; i++){
129 int arg = (random() % 2000);
131 int ret = clients[which_cl]->call(25, rep, arg);
133 if ((int)rep.size()!=arg)
134 cout << "repsize wrong " << rep.size() << "!=" << arg << endl;
135 VERIFY((int)rep.size() == arg);
138 // test rpc replies coming back not in the order of
139 // the original calls -- i.e. does xid reply dispatch work.
140 for(int i = 0; i < 100; i++){
141 int which = (random() % 2);
142 int arg = (random() % 1000);
145 auto start = std::chrono::steady_clock::now();
147 int ret = clients[which_cl]->call(which ? 23 : 24, rep, arg);
148 auto end = std::chrono::steady_clock::now();
149 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
151 cout << diff << " ms have elapsed!!!" << endl;
153 VERIFY(rep == (which ? arg+1 : arg+2));
160 size_t which_cl = cl % NUM_CL;
165 while(time(0) - t1 < 10){
166 int arg = (random() % 2000);
168 int ret = clients[which_cl]->call(25, rep, arg);
169 if ((int)rep.size()!=arg)
170 cout << "ask for " << arg << " reply got " << rep.size() << " ret " << ret << endl;
171 VERIFY((int)rep.size() == arg);
178 rpcc *c = (rpcc *) xx;
180 for(int i = 0; i < 4; i++){
182 int ret = c->call_timeout(24, rpcc::to(3000), rep, i);
183 VERIFY(ret == rpc_const::timeout_failure || rep == i+2);
189 simple_tests(rpcc *c)
191 cout << "simple_tests" << endl;
192 // an RPC call to procedure #22.
193 // rpcc::call() looks at the argument types to decide how
194 // to marshall the RPC call packet, and how to unmarshall
197 int intret = c->call(22, rep, (string)"hello", (string)" goodbye");
198 VERIFY(intret == 0); // this is what handle_22 returns
199 VERIFY(rep == "hello goodbye");
200 cout << " -- string concat RPC .. ok" << endl;
202 // small request, big reply (perhaps req via UDP, reply via TCP)
203 intret = c->call_timeout(25, rpcc::to(200000), rep, 70000);
205 VERIFY(rep.size() == 70000);
206 cout << " -- small request, big reply .. ok" << endl;
208 // specify a timeout value to an RPC that should succeed (udp)
210 intret = c->call_timeout(23, rpcc::to(3000), xx, 77);
211 VERIFY(intret == 0 && xx == 78);
212 cout << " -- no spurious timeout .. ok" << endl;
214 // specify a timeout value to an RPC that should succeed (tcp)
216 string arg(1000, 'x');
218 c->call_timeout(22, rpcc::to(3000), rep2, arg, (string)"x");
219 VERIFY(rep2.size() == 1001);
220 cout << " -- no spurious timeout .. ok" << endl;
224 string big(1000000, 'x');
225 intret = c->call(22, rep, big, (string)"z");
226 VERIFY(rep.size() == 1000001);
227 cout << " -- huge 1M rpc request .. ok" << endl;
229 // specify a timeout value to an RPC that should timeout (udp)
230 string non_existent = "127.0.0.1:7661";
231 rpcc *c1 = new rpcc(non_existent);
233 intret = c1->bind(rpcc::to(3000));
235 VERIFY(intret < 0 && (t1 - t0) <= 4);
236 cout << " -- rpc timeout .. ok" << endl;
237 cout << "simple_tests OK" << endl;
241 concurrent_test(size_t nt)
243 // create threads that make lots of calls in parallel,
244 // to test thread synchronization for concurrent calls
246 cout << "start concurrent_test (" << nt << " threads) ...";
248 vector<thread> th(nt);
250 for(size_t i = 0; i < nt; i++)
251 th[i] = thread(client1, i);
253 for(size_t i = 0; i < nt; i++)
256 cout << " OK" << endl;
262 cout << "start lossy_test ...";
263 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
270 for (int i = 0; i < NUM_CL; i++) {
272 clients[i] = new rpcc(dst);
273 VERIFY(clients[i]->bind()==0);
278 vector<thread> th(nt);
280 for(size_t i = 0; i < nt; i++)
281 th[i] = thread(client2, i);
283 for(size_t i = 0; i < nt; i++)
286 cout << ".. OK" << endl;
287 VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
294 rpcc *client = clients[0];
296 cout << "failure_test" << endl;
300 client1 = new rpcc(dst);
301 VERIFY (client1->bind(rpcc::to(3000)) < 0);
302 cout << " -- create new client and try to bind to failed server .. failed ok" << endl;
309 int intret = client->call(22, rep, (string)"hello", (string)" goodbye");
310 VERIFY(intret == rpc_const::oldsrv_failure);
311 cout << " -- call recovered server with old client .. failed ok" << endl;
315 clients[0] = client = new rpcc(dst);
316 VERIFY (client->bind() >= 0);
317 VERIFY (client->bind() < 0);
319 intret = client->call(22, rep, (string)"hello", (string)" goodbye");
321 VERIFY(rep == "hello goodbye");
323 cout << " -- delete existing rpc client, create replacement rpc client .. ok" << endl;
327 cout << " -- concurrent test on new rpc client w/ " << nt << " threads ..";
329 vector<thread> th(nt);
331 for(size_t i = 0; i < nt; i++)
332 th[i] = thread(client3, client);
334 for(size_t i = 0; i < nt; i++)
337 cout << "ok" << endl;
343 clients[0] = client = new rpcc(dst);
344 VERIFY (client->bind() >= 0);
345 cout << " -- delete existing rpc client and server, create replacements.. ok" << endl;
347 cout << " -- concurrent test on new client and server w/ " << nt << " threads ..";
349 for(size_t i = 0; i < nt; i++)
350 th[i] = thread(client3, client);
352 for(size_t i = 0; i < nt; i++)
355 cout << "ok" << endl;
357 cout << "failure_test OK" << endl;
361 main(int argc, char *argv[])
364 setvbuf(stdout, NULL, _IONBF, 0);
365 setvbuf(stderr, NULL, _IONBF, 0);
368 bool isclient = false;
369 bool isserver = false;
371 srandom((uint32_t)getpid());
372 port = 20000 + (getpid() % 10000);
375 while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
384 debug_level = atoi(optarg);
390 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
397 if (!isserver && !isclient) {
398 isserver = isclient = true;
401 if (debug_level > 0) {
402 JSL_DEBUG_LEVEL = debug_level;
403 jsl_log(JSL_DBG_1, "DEBUG LEVEL: %d\n", debug_level);
409 cout << "starting server on port " << port << " RPC_HEADER_SZ " << (int)RPC_HEADER_SZ << endl;
415 dst = "127.0.0.1:" + std::to_string(port);
418 // start the client. bind it to the server.
419 // starts a thread to listen for replies and hand them to
420 // the correct waiting caller thread. there should probably
421 // be only one rpcc per process. you probably need one
423 for (int i = 0; i < NUM_CL; i++) {
424 clients[i] = new rpcc(dst);
425 VERIFY (clients[i]->bind() == 0);
428 simple_tests(clients[0]);
435 cout << "rpctest OK" << endl;