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);
83 request_header rh{1,2,3,4,5};
85 VERIFY(((string)m).size()==RPC_HEADER_SZ);
87 unsigned long long l = 1223344455L;
88 string s = "hallo....";
94 VERIFY(b.size() == RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int));
96 unmarshall un(b, true);
98 un.unpack_header(rh1);
99 VERIFY(memcmp(&rh,&rh1,sizeof(rh))==0);
101 unsigned long long l1;
107 VERIFY(i1==i && l1==l && s1==s);
114 size_t which_cl = cl % NUM_CL;
116 for(int i = 0; i < 100; i++){
117 int arg = (random() % 2000);
119 int ret = clients[which_cl]->call(25, rep, arg);
121 if ((int)rep.size()!=arg)
122 cout << "repsize wrong " << rep.size() << "!=" << arg << endl;
123 VERIFY((int)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 ? 23 : 24, 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));
148 size_t which_cl = cl % NUM_CL;
153 while(time(0) - t1 < 10){
154 int arg = (random() % 2000);
156 int ret = clients[which_cl]->call(25, rep, arg);
157 if ((int)rep.size()!=arg)
158 cout << "ask for " << arg << " reply got " << rep.size() << " ret " << ret << endl;
159 VERIFY((int)rep.size() == arg);
166 rpcc *c = (rpcc *) xx;
168 for(int i = 0; i < 4; i++){
170 int ret = c->call_timeout(24, milliseconds(300), rep, i);
171 VERIFY(ret == rpc_const::timeout_failure || rep == i+2);
177 simple_tests(rpcc *c)
179 cout << "simple_tests" << endl;
180 // an RPC call to procedure #22.
181 // rpcc::call() looks at the argument types to decide how
182 // to marshall the RPC call packet, and how to unmarshall
185 int intret = c->call(22, rep, (string)"hello", (string)" goodbye");
186 VERIFY(intret == 0); // this is what handle_22 returns
187 VERIFY(rep == "hello goodbye");
188 cout << " -- string concat RPC .. ok" << endl;
190 // small request, big reply (perhaps req via UDP, reply via TCP)
191 intret = c->call_timeout(25, milliseconds(20000), rep, 70000);
193 VERIFY(rep.size() == 70000);
194 cout << " -- small request, big reply .. ok" << endl;
196 // specify a timeout value to an RPC that should succeed (udp)
198 intret = c->call_timeout(23, milliseconds(300), xx, 77);
199 VERIFY(intret == 0 && xx == 78);
200 cout << " -- no spurious timeout .. ok" << endl;
202 // specify a timeout value to an RPC that should succeed (tcp)
204 string arg(1000, 'x');
206 c->call_timeout(22, milliseconds(300), rep2, arg, (string)"x");
207 VERIFY(rep2.size() == 1001);
208 cout << " -- no spurious timeout .. ok" << endl;
212 string big(1000000, 'x');
213 intret = c->call(22, rep, big, (string)"z");
214 VERIFY(rep.size() == 1000001);
215 cout << " -- huge 1M rpc request .. ok" << endl;
217 // specify a timeout value to an RPC that should timeout (udp)
218 string non_existent = "127.0.0.1:7661";
219 rpcc *c1 = new rpcc(non_existent);
221 intret = c1->bind(milliseconds(300));
223 VERIFY(intret < 0 && (t1 - t0) <= 4);
224 cout << " -- rpc timeout .. ok" << endl;
225 cout << "simple_tests OK" << endl;
229 concurrent_test(size_t nt)
231 // create threads that make lots of calls in parallel,
232 // to test thread synchronization for concurrent calls
234 cout << "start concurrent_test (" << nt << " threads) ...";
236 vector<thread> th(nt);
238 for(size_t i = 0; i < nt; i++)
239 th[i] = thread(client1, i);
241 for(size_t i = 0; i < nt; i++)
244 cout << " OK" << endl;
250 cout << "start lossy_test ...";
251 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
258 for (int i = 0; i < NUM_CL; i++) {
260 clients[i] = new rpcc(dst);
261 VERIFY(clients[i]->bind()==0);
266 vector<thread> th(nt);
268 for(size_t i = 0; i < nt; i++)
269 th[i] = thread(client2, i);
271 for(size_t i = 0; i < nt; i++)
274 cout << ".. OK" << endl;
275 VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
282 rpcc *client = clients[0];
284 cout << "failure_test" << endl;
288 client1 = new rpcc(dst);
289 VERIFY (client1->bind(milliseconds(3000)) < 0);
290 cout << " -- create new client and try to bind to failed server .. failed ok" << endl;
297 int intret = client->call(22, rep, (string)"hello", (string)" goodbye");
298 VERIFY(intret == rpc_const::oldsrv_failure);
299 cout << " -- call recovered server with old client .. failed ok" << endl;
303 clients[0] = client = new rpcc(dst);
304 VERIFY (client->bind() >= 0);
305 VERIFY (client->bind() < 0);
307 intret = client->call(22, rep, (string)"hello", (string)" goodbye");
309 VERIFY(rep == "hello goodbye");
311 cout << " -- delete existing rpc client, create replacement rpc client .. ok" << endl;
315 cout << " -- concurrent test on new rpc client w/ " << nt << " threads ..";
317 vector<thread> th(nt);
319 for(size_t i = 0; i < nt; i++)
320 th[i] = thread(client3, client);
322 for(size_t i = 0; i < nt; i++)
325 cout << "ok" << endl;
331 clients[0] = client = new rpcc(dst);
332 VERIFY (client->bind() >= 0);
333 cout << " -- delete existing rpc client and server, create replacements.. ok" << endl;
335 cout << " -- concurrent test on new client and server w/ " << nt << " threads ..";
337 for(size_t i = 0; i < nt; i++)
338 th[i] = thread(client3, client);
340 for(size_t i = 0; i < nt; i++)
343 cout << "ok" << endl;
345 cout << "failure_test OK" << endl;
349 main(int argc, char *argv[])
352 setvbuf(stdout, NULL, _IONBF, 0);
353 setvbuf(stderr, NULL, _IONBF, 0);
356 bool isclient = false;
357 bool isserver = false;
359 srandom((uint32_t)getpid());
360 port = 20000 + (getpid() % 10000);
363 while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
372 debug_level = atoi(optarg);
375 port = (in_port_t)atoi(optarg);
378 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
385 if (!isserver && !isclient) {
386 isserver = isclient = true;
389 if (debug_level > 0) {
390 DEBUG_LEVEL = debug_level;
391 IF_LEVEL(1) LOG_NONMEMBER("DEBUG LEVEL: " << debug_level);
397 cout << "starting server on port " << port << " RPC_HEADER_SZ " << (int)RPC_HEADER_SZ << endl;
403 dst = "127.0.0.1:" + to_string(port);
406 // start the client. bind it to the server.
407 // starts a thread to listen for replies and hand them to
408 // the correct waiting caller thread. there should probably
409 // be only one rpcc per process. you probably need one
411 for (int i = 0; i < NUM_CL; i++) {
412 clients[i] = new rpcc(dst);
413 VERIFY (clients[i]->bind() == 0);
416 simple_tests(clients[0]);
423 cout << "rpctest OK" << endl;