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() % 5000);
61 srv::handle_bigrep(string &r, const size_t len)
63 r = string((size_t)len, 'x');
71 server = new rpcs((unsigned int)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};
83 m.pack_req_header(rh);
84 VERIFY(m.size()==RPC_HEADER_SZ);
86 unsigned long long l = 1223344455L;
87 string s = "hallo....";
95 VERIFY(sz == RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int));
99 un.unpack_req_header(&rh1);
100 VERIFY(memcmp(&rh,&rh1,sizeof(rh))==0);
102 unsigned long long l1;
108 VERIFY(i1==i && l1==l && s1==s);
115 size_t which_cl = cl % NUM_CL;
117 for(int i = 0; i < 100; i++){
118 int arg = (random() % 2000);
120 int ret = clients[which_cl]->call(25, rep, arg);
122 if ((int)rep.size()!=arg)
123 cout << "repsize wrong " << rep.size() << "!=" << arg << endl;
124 VERIFY((int)rep.size() == arg);
127 // test rpc replies coming back not in the order of
128 // the original calls -- i.e. does xid reply dispatch work.
129 for(int i = 0; i < 100; i++){
130 int which = (random() % 2);
131 int arg = (random() % 1000);
134 auto start = std::chrono::steady_clock::now();
136 int ret = clients[which_cl]->call(which ? 23 : 24, rep, arg);
137 auto end = std::chrono::steady_clock::now();
138 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
140 cout << diff << " ms have elapsed!!!" << endl;
142 VERIFY(rep == (which ? arg+1 : arg+2));
149 size_t which_cl = cl % NUM_CL;
154 while(time(0) - t1 < 10){
155 int arg = (random() % 2000);
157 int ret = clients[which_cl]->call(25, rep, arg);
158 if ((int)rep.size()!=arg)
159 cout << "ask for " << arg << " reply got " << rep.size() << " ret " << ret << endl;
160 VERIFY((int)rep.size() == arg);
167 rpcc *c = (rpcc *) xx;
169 for(int i = 0; i < 4; i++){
171 int ret = c->call_timeout(24, rpcc::to(3000), rep, i);
172 VERIFY(ret == rpc_const::timeout_failure || rep == i+2);
178 simple_tests(rpcc *c)
180 cout << "simple_tests" << endl;
181 // an RPC call to procedure #22.
182 // rpcc::call() looks at the argument types to decide how
183 // to marshall the RPC call packet, and how to unmarshall
186 int intret = c->call(22, rep, (string)"hello", (string)" goodbye");
187 VERIFY(intret == 0); // this is what handle_22 returns
188 VERIFY(rep == "hello goodbye");
189 cout << " -- string concat RPC .. ok" << endl;
191 // small request, big reply (perhaps req via UDP, reply via TCP)
192 intret = c->call_timeout(25, rpcc::to(200000), rep, 70000);
194 VERIFY(rep.size() == 70000);
195 cout << " -- small request, big reply .. ok" << endl;
197 // specify a timeout value to an RPC that should succeed (udp)
199 intret = c->call_timeout(23, rpcc::to(3000), xx, 77);
200 VERIFY(intret == 0 && xx == 78);
201 cout << " -- no spurious timeout .. ok" << endl;
203 // specify a timeout value to an RPC that should succeed (tcp)
205 string arg(1000, 'x');
207 c->call_timeout(22, rpcc::to(3000), rep2, arg, (string)"x");
208 VERIFY(rep2.size() == 1001);
209 cout << " -- no spurious timeout .. ok" << endl;
213 string big(1000000, 'x');
214 intret = c->call(22, rep, big, (string)"z");
215 VERIFY(rep.size() == 1000001);
216 cout << " -- huge 1M rpc request .. ok" << endl;
218 // specify a timeout value to an RPC that should timeout (udp)
219 string non_existent = "127.0.0.1:7661";
220 rpcc *c1 = new rpcc(non_existent);
222 intret = c1->bind(rpcc::to(3000));
224 VERIFY(intret < 0 && (t1 - t0) <= 4);
225 cout << " -- rpc timeout .. ok" << endl;
226 cout << "simple_tests OK" << endl;
230 concurrent_test(size_t nt)
232 // create threads that make lots of calls in parallel,
233 // to test thread synchronization for concurrent calls
235 cout << "start concurrent_test (" << nt << " threads) ...";
237 vector<thread> th(nt);
239 for(size_t i = 0; i < nt; i++)
240 th[i] = thread(client1, i);
242 for(size_t i = 0; i < nt; i++)
245 cout << " OK" << endl;
251 cout << "start lossy_test ...";
252 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
259 for (int i = 0; i < NUM_CL; i++) {
261 clients[i] = new rpcc(dst);
262 VERIFY(clients[i]->bind()==0);
267 vector<thread> th(nt);
269 for(size_t i = 0; i < nt; i++)
270 th[i] = thread(client2, i);
272 for(size_t i = 0; i < nt; i++)
275 cout << ".. OK" << endl;
276 VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
283 rpcc *client = clients[0];
285 cout << "failure_test" << endl;
289 client1 = new rpcc(dst);
290 VERIFY (client1->bind(rpcc::to(3000)) < 0);
291 cout << " -- create new client and try to bind to failed server .. failed ok" << endl;
298 int intret = client->call(22, rep, (string)"hello", (string)" goodbye");
299 VERIFY(intret == rpc_const::oldsrv_failure);
300 cout << " -- call recovered server with old client .. failed ok" << endl;
304 clients[0] = client = new rpcc(dst);
305 VERIFY (client->bind() >= 0);
306 VERIFY (client->bind() < 0);
308 intret = client->call(22, rep, (string)"hello", (string)" goodbye");
310 VERIFY(rep == "hello goodbye");
312 cout << " -- delete existing rpc client, create replacement rpc client .. ok" << endl;
316 cout << " -- concurrent test on new rpc client w/ " << nt << " threads ..";
318 vector<thread> th(nt);
320 for(size_t i = 0; i < nt; i++)
321 th[i] = thread(client3, client);
323 for(size_t i = 0; i < nt; i++)
326 cout << "ok" << endl;
332 clients[0] = client = new rpcc(dst);
333 VERIFY (client->bind() >= 0);
334 cout << " -- delete existing rpc client and server, create replacements.. ok" << endl;
336 cout << " -- concurrent test on new client and server w/ " << nt << " threads ..";
338 for(size_t i = 0; i < nt; i++)
339 th[i] = thread(client3, client);
341 for(size_t i = 0; i < nt; i++)
344 cout << "ok" << endl;
346 cout << "failure_test OK" << endl;
350 main(int argc, char *argv[])
353 setvbuf(stdout, NULL, _IONBF, 0);
354 setvbuf(stderr, NULL, _IONBF, 0);
357 bool isclient = false;
358 bool isserver = false;
360 srandom((uint32_t)getpid());
361 port = 20000 + (getpid() % 10000);
364 while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
373 debug_level = atoi(optarg);
379 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
386 if (!isserver && !isclient) {
387 isserver = isclient = true;
390 if (debug_level > 0) {
391 DEBUG_LEVEL = debug_level;
392 IF_LEVEL(1) LOG_NONMEMBER("DEBUG LEVEL: " << debug_level);
398 cout << "starting server on port " << port << " RPC_HEADER_SZ " << (int)RPC_HEADER_SZ << endl;
404 dst = "127.0.0.1:" + std::to_string(port);
407 // start the client. bind it to the server.
408 // starts a thread to listen for replies and hand them to
409 // the correct waiting caller thread. there should probably
410 // be only one rpcc per process. you probably need one
412 for (int i = 0; i < NUM_CL; i++) {
413 clients[i] = new rpcc(dst);
414 VERIFY (clients[i]->bind() == 0);
417 simple_tests(clients[0]);
424 cout << "rpctest OK" << endl;