1 // RPC test and pseudo-documentation.
2 // generates print statements on failures, but eventually says "rpctest OK"
10 #include <sys/types.h>
13 #include "lang/verify.h"
17 rpcs *server; // server rpc object
18 rpcc *clients[NUM_CL]; // client rpc object
19 struct sockaddr_in dst; //server's ip address
22 // server-side handlers. they must be methods of some class
23 // to simplify rpcs::reg(). a server process can have handlers
24 // from multiple classes.
27 int handle_22(const std::string a, const std::string b, std::string & r);
28 int handle_fast(const int a, int &r);
29 int handle_slow(const int a, int &r);
30 int handle_bigrep(const int a, std::string &r);
33 // a handler. a and b are arguments, r is the result.
34 // there can be multiple arguments but only one result.
35 // the caller also gets to see the int return value
36 // as the return value from rpcc::call().
37 // rpcs::reg() decides how to unmarshall by looking
38 // at these argument types, so this function definition
39 // does what a .x file does in SunRPC.
41 srv::handle_22(const std::string a, std::string b, std::string &r)
48 srv::handle_fast(const int a, int &r)
55 srv::handle_slow(const int a, int &r)
57 usleep(random() % 5000);
63 srv::handle_bigrep(const int len, std::string &r)
65 r = std::string(len, 'x');
73 server = new rpcs(port);
74 server->reg(22, &service, &srv::handle_22);
75 server->reg(23, &service, &srv::handle_fast);
76 server->reg(24, &service, &srv::handle_slow);
77 server->reg(25, &service, &srv::handle_bigrep);
84 req_header rh(1,2,3,4,5);
85 m.pack_req_header(rh);
86 VERIFY(m.size()==RPC_HEADER_SZ);
88 unsigned long long l = 1223344455L;
89 std::string s = std::string("hallo....");
97 VERIFY(sz == (int)(RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int)));
101 un.unpack_req_header(&rh1);
102 VERIFY(memcmp(&rh,&rh1,sizeof(rh))==0);
104 unsigned long long l1;
110 VERIFY(i1==i && l1==l && s1==s);
117 int which_cl = ((unsigned long) cl ) % NUM_CL;
119 for(int i = 0; i < 100; i++){
120 int arg = (random() % 2000);
122 int ret = clients[which_cl]->call(25, arg, rep);
124 if ((int)rep.size()!=arg) {
125 printf("repsize wrong %d!=%d\n", (int)rep.size(), arg);
127 VERIFY((int)rep.size() == arg);
130 // test rpc replies coming back not in the order of
131 // the original calls -- i.e. does xid reply dispatch work.
132 for(int i = 0; i < 100; i++){
133 int which = (random() % 2);
134 int arg = (random() % 1000);
137 auto start = std::chrono::steady_clock::now();
139 int ret = clients[which_cl]->call(which ? 23 : 24, arg, rep);
140 auto end = std::chrono::steady_clock::now();
141 int diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
143 printf("%d ms have elapsed!!!\n", diff);
145 VERIFY(rep == (which ? arg+1 : arg+2));
152 int which_cl = ((unsigned long) cl ) % NUM_CL;
157 while(time(0) - t1 < 10){
158 int arg = (random() % 2000);
160 int ret = clients[which_cl]->call(25, arg, rep);
161 if ((int)rep.size()!=arg) {
162 printf("ask for %d reply got %d ret %d\n",
163 arg, (int)rep.size(), ret);
165 VERIFY((int)rep.size() == arg);
172 rpcc *c = (rpcc *) xx;
174 for(int i = 0; i < 4; i++){
176 int ret = c->call(24, i, rep, rpcc::to(3000));
177 VERIFY(ret == rpc_const::timeout_failure || rep == i+2);
183 simple_tests(rpcc *c)
185 printf("simple_tests\n");
186 // an RPC call to procedure #22.
187 // rpcc::call() looks at the argument types to decide how
188 // to marshall the RPC call packet, and how to unmarshall
191 int intret = c->call(22, (std::string)"hello", (std::string)" goodbye", rep);
192 VERIFY(intret == 0); // this is what handle_22 returns
193 VERIFY(rep == "hello goodbye");
194 printf(" -- string concat RPC .. ok\n");
196 // small request, big reply (perhaps req via UDP, reply via TCP)
197 intret = c->call(25, 70000, rep, rpcc::to(200000));
199 VERIFY(rep.size() == 70000);
200 printf(" -- small request, big reply .. ok\n");
204 intret = c->call(22, (std::string)"just one", rep);
206 printf(" -- too few arguments .. failed ok\n");
208 // too many arguments; proc #23 expects just one.
209 intret = c->call(23, 1001, 1002, rep);
211 printf(" -- too many arguments .. failed ok\n");
213 // wrong return value size
215 intret = c->call(23, (std::string)"hello", (std::string)" goodbye", wrongrep);
217 printf(" -- wrong ret value size .. failed ok\n");
220 // specify a timeout value to an RPC that should succeed (udp)
222 intret = c->call(23, 77, xx, rpcc::to(3000));
223 VERIFY(intret == 0 && xx == 78);
224 printf(" -- no suprious timeout .. ok\n");
226 // specify a timeout value to an RPC that should succeed (tcp)
228 std::string arg(1000, 'x');
230 c->call(22, arg, (std::string)"x", rep, rpcc::to(3000));
231 VERIFY(rep.size() == 1001);
232 printf(" -- no suprious timeout .. ok\n");
236 std::string big(1000000, 'x');
237 intret = c->call(22, big, (std::string)"z", rep);
238 VERIFY(rep.size() == 1000001);
239 printf(" -- huge 1M rpc request .. ok\n");
241 // specify a timeout value to an RPC that should timeout (udp)
242 struct sockaddr_in non_existent;
243 memset(&non_existent, 0, sizeof(non_existent));
244 non_existent.sin_family = AF_INET;
245 non_existent.sin_addr.s_addr = inet_addr("127.0.0.1");
246 non_existent.sin_port = htons(7661);
247 rpcc *c1 = new rpcc(non_existent);
249 intret = c1->bind(rpcc::to(3000));
251 VERIFY(intret < 0 && (t1 - t0) <= 4);
252 printf(" -- rpc timeout .. ok\n");
253 printf("simple_tests OK\n");
257 concurrent_test(int nt)
259 // create threads that make lots of calls in parallel,
260 // to test thread synchronization for concurrent calls
262 printf("start concurrent_test (%d threads) ...", nt);
264 std::vector<std::thread> th(nt);
265 for(int i = 0; i < nt; i++){
266 th[i] = std::thread(client1, i);
269 for(int i = 0; i < nt; i++){
278 printf("start lossy_test ...");
279 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
286 for (int i = 0; i < NUM_CL; i++) {
288 clients[i] = new rpcc(dst);
289 VERIFY(clients[i]->bind()==0);
293 std::vector<std::thread> th(nt);
294 for(int i = 0; i < nt; i++){
295 th[i] = std::thread(client2, i);
297 for(int i = 0; i < nt; i++){
301 VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
308 rpcc *client = clients[0];
310 printf("failure_test\n");
314 client1 = new rpcc(dst);
315 VERIFY (client1->bind(rpcc::to(3000)) < 0);
316 printf(" -- create new client and try to bind to failed server .. failed ok\n");
323 int intret = client->call(22, (std::string)"hello", (std::string)" goodbye", rep);
324 VERIFY(intret == rpc_const::oldsrv_failure);
325 printf(" -- call recovered server with old client .. failed ok\n");
329 clients[0] = client = new rpcc(dst);
330 VERIFY (client->bind() >= 0);
331 VERIFY (client->bind() < 0);
333 intret = client->call(22, (std::string)"hello", (std::string)" goodbye", rep);
335 VERIFY(rep == "hello goodbye");
337 printf(" -- delete existing rpc client, create replacement rpc client .. ok\n");
341 printf(" -- concurrent test on new rpc client w/ %d threads ..", nt);
343 std::vector<std::thread> th(nt);
344 for(int i = 0; i < nt; i++){
345 th[i] = std::thread(client3, client);
348 for(int i = 0; i < nt; i++){
357 clients[0] = client = new rpcc(dst);
358 VERIFY (client->bind() >= 0);
359 printf(" -- delete existing rpc client and server, create replacements.. ok\n");
361 printf(" -- concurrent test on new client and server w/ %d threads ..", nt);
362 for(int i = 0; i < nt; i++){
363 th[i] = std::thread(client3, client);
366 for(int i = 0; i < nt; i++){
371 printf("failure_test OK\n");
375 main(int argc, char *argv[])
378 setvbuf(stdout, NULL, _IONBF, 0);
379 setvbuf(stderr, NULL, _IONBF, 0);
382 bool isclient = false;
383 bool isserver = false;
386 port = 20000 + (getpid() % 10000);
389 while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
398 debug_level = atoi(optarg);
404 VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
410 if (!isserver && !isclient) {
411 isserver = isclient = true;
414 if (debug_level > 0) {
415 //__loginit.initNow();
416 jsl_set_debug(debug_level);
417 jsl_log(JSL_DBG_1, "DEBUG LEVEL: %d\n", debug_level);
423 printf("starting server on port %d RPC_HEADER_SZ %d\n", port, (int)RPC_HEADER_SZ);
429 memset(&dst, 0, sizeof(dst));
430 dst.sin_family = AF_INET;
431 dst.sin_addr.s_addr = inet_addr("127.0.0.1");
432 dst.sin_port = htons(port);
435 // start the client. bind it to the server.
436 // starts a thread to listen for replies and hand them to
437 // the correct waiting caller thread. there should probably
438 // be only one rpcc per process. you probably need one
440 for (int i = 0; i < NUM_CL; i++) {
441 clients[i] = new rpcc(dst);
442 VERIFY (clients[i]->bind() == 0);
445 simple_tests(clients[0]);
452 printf("rpctest OK\n");