1 // RPC test and pseudo-documentation.
2 // generates print statements on failures, but eventually says "rpctest OK"
13 char log_thread_prefix = 'r';
15 rpcs *server; // server rpc object
16 rpcc *clients[NUM_CL]; // client rpc object
17 string dst; //server's ip address
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 // a handler. a and b are arguments, r is the result.
32 // there can be multiple arguments but only one result.
33 // the caller also gets to see the int return value
34 // as the return value from rpcc::call().
35 // rpcs::reg() decides how to unmarshall by looking
36 // at these argument types, so this function definition
37 // does what a .x file does in SunRPC.
39 srv::handle_22(string &r, const string a, string b)
46 srv::handle_fast(int &r, const int a)
53 srv::handle_slow(int &r, const int a)
55 usleep(random() % 500);
61 srv::handle_bigrep(string &r, const size_t len)
63 r = string((size_t)len, 'x');
71 server = new rpcs(port);
72 server->reg(22, &srv::handle_22, &service);
73 server->reg(23, &srv::handle_fast, &service);
74 server->reg(24, &srv::handle_slow, &service);
75 server->reg(25, &srv::handle_bigrep, &service);
82 request_header rh{1,2,3,4,5};
84 VERIFY(((string)m).size()==RPC_HEADER_SZ);
86 unsigned long long l = 1223344455L;
87 string s = "hallo....";
93 VERIFY(b.size() == RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int));
95 unmarshall un(b, true);
97 un.unpack_header(rh1);
98 VERIFY(memcmp(&rh,&rh1,sizeof(rh))==0);
100 unsigned long long l1;
106 VERIFY(i1==i && l1==l && s1==s);
113 size_t which_cl = cl % NUM_CL;
115 for(int i = 0; i < 100; i++){
116 int arg = (random() % 2000);
118 int ret = clients[which_cl]->call(25, rep, arg);
120 if ((int)rep.size()!=arg)
121 cout << "repsize wrong " << rep.size() << "!=" << arg << endl;
122 VERIFY((int)rep.size() == arg);
125 // test rpc replies coming back not in the order of
126 // the original calls -- i.e. does xid reply dispatch work.
127 for(int i = 0; i < 100; i++){
128 int which = (random() % 2);
129 int arg = (random() % 1000);
132 auto start = steady_clock::now();
134 int ret = clients[which_cl]->call(which ? 23 : 24, rep, arg);
135 auto end = steady_clock::now();
136 auto diff = duration_cast<milliseconds>(end - start).count();
138 cout << diff << " ms have elapsed!!!" << endl;
140 VERIFY(rep == (which ? arg+1 : arg+2));
147 size_t which_cl = cl % NUM_CL;
152 while(time(0) - t1 < 10){
153 int arg = (random() % 2000);
155 int ret = clients[which_cl]->call(25, rep, arg);
156 if ((int)rep.size()!=arg)
157 cout << "ask for " << arg << " reply got " << rep.size() << " ret " << ret << endl;
158 VERIFY((int)rep.size() == arg);
165 rpcc *c = (rpcc *) xx;
167 for(int i = 0; i < 4; i++){
169 int ret = c->call_timeout(24, milliseconds(300), rep, i);
170 VERIFY(ret == rpc_const::timeout_failure || rep == i+2);
176 simple_tests(rpcc *c)
178 cout << "simple_tests" << endl;
179 // an RPC call to procedure #22.
180 // rpcc::call() looks at the argument types to decide how
181 // to marshall the RPC call packet, and how to unmarshall
184 int intret = c->call(22, rep, (string)"hello", (string)" goodbye");
185 VERIFY(intret == 0); // this is what handle_22 returns
186 VERIFY(rep == "hello goodbye");
187 cout << " -- string concat RPC .. ok" << endl;
189 // small request, big reply (perhaps req via UDP, reply via TCP)
190 intret = c->call_timeout(25, milliseconds(20000), rep, 70000);
192 VERIFY(rep.size() == 70000);
193 cout << " -- small request, big reply .. ok" << endl;
195 // specify a timeout value to an RPC that should succeed (udp)
197 intret = c->call_timeout(23, milliseconds(300), xx, 77);
198 VERIFY(intret == 0 && xx == 78);
199 cout << " -- no spurious timeout .. ok" << endl;
201 // specify a timeout value to an RPC that should succeed (tcp)
203 string arg(1000, 'x');
205 c->call_timeout(22, milliseconds(300), rep2, arg, (string)"x");
206 VERIFY(rep2.size() == 1001);
207 cout << " -- no spurious timeout .. ok" << endl;
211 string big(1000000, 'x');
212 intret = c->call(22, rep, big, (string)"z");
213 VERIFY(rep.size() == 1000001);
214 cout << " -- huge 1M rpc request .. ok" << endl;
216 // specify a timeout value to an RPC that should timeout (udp)
217 string non_existent = "127.0.0.1:7661";
218 rpcc *c1 = new rpcc(non_existent);
220 intret = c1->bind(milliseconds(300));
222 VERIFY(intret < 0 && (t1 - t0) <= 4);
223 cout << " -- rpc timeout .. ok" << endl;
224 cout << "simple_tests OK" << endl;
228 concurrent_test(size_t nt)
230 // create threads that make lots of calls in parallel,
231 // to test thread synchronization for concurrent calls
233 cout << "start concurrent_test (" << nt << " threads) ...";
235 vector<thread> th(nt);
237 for(size_t i = 0; i < nt; i++)
238 th[i] = thread(client1, i);
240 for(size_t i = 0; i < nt; i++)
243 cout << " OK" << endl;
249 cout << "start lossy_test ...";
250 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
257 for (int i = 0; i < NUM_CL; i++) {
259 clients[i] = new rpcc(dst);
260 VERIFY(clients[i]->bind()==0);
265 vector<thread> th(nt);
267 for(size_t i = 0; i < nt; i++)
268 th[i] = thread(client2, i);
270 for(size_t i = 0; i < nt; i++)
273 cout << ".. OK" << endl;
274 VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
281 rpcc *client = clients[0];
283 cout << "failure_test" << endl;
287 client1 = new rpcc(dst);
288 VERIFY (client1->bind(milliseconds(3000)) < 0);
289 cout << " -- create new client and try to bind to failed server .. failed ok" << endl;
296 int intret = client->call(22, rep, (string)"hello", (string)" goodbye");
297 VERIFY(intret == rpc_const::oldsrv_failure);
298 cout << " -- call recovered server with old client .. failed ok" << endl;
302 clients[0] = client = new rpcc(dst);
303 VERIFY (client->bind() >= 0);
304 VERIFY (client->bind() < 0);
306 intret = client->call(22, rep, (string)"hello", (string)" goodbye");
308 VERIFY(rep == "hello goodbye");
310 cout << " -- delete existing rpc client, create replacement rpc client .. ok" << endl;
314 cout << " -- concurrent test on new rpc client w/ " << nt << " threads ..";
316 vector<thread> th(nt);
318 for(size_t i = 0; i < nt; i++)
319 th[i] = thread(client3, client);
321 for(size_t i = 0; i < nt; i++)
324 cout << "ok" << endl;
330 clients[0] = client = new rpcc(dst);
331 VERIFY (client->bind() >= 0);
332 cout << " -- delete existing rpc client and server, create replacements.. ok" << endl;
334 cout << " -- concurrent test on new client and server w/ " << nt << " threads ..";
336 for(size_t i = 0; i < nt; i++)
337 th[i] = thread(client3, client);
339 for(size_t i = 0; i < nt; i++)
342 cout << "ok" << endl;
344 cout << "failure_test OK" << endl;
348 main(int argc, char *argv[])
351 setvbuf(stdout, NULL, _IONBF, 0);
352 setvbuf(stderr, NULL, _IONBF, 0);
355 bool isclient = false;
356 bool isserver = false;
358 srandom((uint32_t)getpid());
359 port = 20000 + (getpid() % 10000);
362 while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
371 debug_level = atoi(optarg);
374 port = (in_port_t)atoi(optarg);
377 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
384 if (!isserver && !isclient) {
385 isserver = isclient = true;
388 if (debug_level > 0) {
389 DEBUG_LEVEL = debug_level;
390 IF_LEVEL(1) LOG_NONMEMBER("DEBUG LEVEL: " << debug_level);
396 cout << "starting server on port " << port << " RPC_HEADER_SZ " << (int)RPC_HEADER_SZ << endl;
402 dst = "127.0.0.1:" + to_string(port);
405 // start the client. bind it to the server.
406 // starts a thread to listen for replies and hand them to
407 // the correct waiting caller thread. there should probably
408 // be only one rpcc per process. you probably need one
410 for (int i = 0; i < NUM_CL; i++) {
411 clients[i] = new rpcc(dst);
412 VERIFY (clients[i]->bind() == 0);
415 simple_tests(clients[0]);
422 cout << "rpctest OK" << endl;