Cleanups
[invirt/third/libt4.git] / rpc / rpctest.cc
index c381745..dbb10c6 100644 (file)
@@ -14,6 +14,8 @@
 
 #define NUM_CL 2
 
 
 #define NUM_CL 2
 
+char tprintf_thread_prefix = 'r';
+
 rpcs *server;  // server rpc object
 rpcc *clients[NUM_CL];  // client rpc object
 struct sockaddr_in dst; //server's ip address
 rpcs *server;  // server rpc object
 rpcc *clients[NUM_CL];  // client rpc object
 struct sockaddr_in dst; //server's ip address
@@ -27,7 +29,7 @@ class srv {
                int handle_22(std::string & r, const std::string a, const std::string b);
                int handle_fast(int &r, const int a);
                int handle_slow(int &r, const int a);
                int handle_22(std::string & r, const std::string a, const std::string b);
                int handle_fast(int &r, const int a);
                int handle_slow(int &r, const int a);
-               int handle_bigrep(std::string &r, const int a);
+               int handle_bigrep(std::string &r, const size_t a);
 };
 
 // a handler. a and b are arguments, r is the result.
 };
 
 // a handler. a and b are arguments, r is the result.
@@ -60,9 +62,9 @@ srv::handle_slow(int &r, const int a)
 }
 
 int
 }
 
 int
-srv::handle_bigrep(std::string &r, const int len)
+srv::handle_bigrep(std::string &r, const size_t len)
 {
 {
-       r = std::string(len, 'x');
+       r = std::string((size_t)len, 'x');
        return 0;
 }
 
        return 0;
 }
 
@@ -70,7 +72,7 @@ srv service;
 
 void startserver()
 {
 
 void startserver()
 {
-       server = new rpcs(port);
+       server = new rpcs((unsigned int)port);
        server->reg(22, &srv::handle_22, &service);
        server->reg(23, &srv::handle_fast, &service);
        server->reg(24, &srv::handle_slow, &service);
        server->reg(22, &srv::handle_22, &service);
        server->reg(23, &srv::handle_fast, &service);
        server->reg(24, &srv::handle_slow, &service);
@@ -92,9 +94,9 @@ testmarshall()
        m << s;
 
        char *b;
        m << s;
 
        char *b;
-       int sz;
+       size_t sz;
        m.take_buf(&b,&sz);
        m.take_buf(&b,&sz);
-       VERIFY(sz == (int)(RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int)));
+       VERIFY(sz == RPC_HEADER_SZ+sizeof(i)+sizeof(l)+s.size()+sizeof(int));
 
        unmarshall un(b,sz);
        request_header rh1;
 
        unmarshall un(b,sz);
        request_header rh1;
@@ -111,10 +113,10 @@ testmarshall()
 }
 
 void
 }
 
 void
-client1(int cl)
+client1(size_t cl)
 {
        // test concurrency.
 {
        // test concurrency.
-       int which_cl = ((unsigned long) cl ) % NUM_CL;
+       size_t which_cl = cl % NUM_CL;
 
        for(int i = 0; i < 100; i++){
                int arg = (random() % 2000);
 
        for(int i = 0; i < 100; i++){
                int arg = (random() % 2000);
@@ -138,18 +140,18 @@ client1(int cl)
 
                int ret = clients[which_cl]->call(which ? 23 : 24, rep, arg);
                auto end = std::chrono::steady_clock::now();
 
                int ret = clients[which_cl]->call(which ? 23 : 24, rep, arg);
                auto end = std::chrono::steady_clock::now();
-               int diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
+               auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
                if (ret != 0)
                if (ret != 0)
-                       printf("%d ms have elapsed!!!\n", diff);
+                       printf("%d ms have elapsed!!!\n", (int)diff);
                VERIFY(ret == 0);
                VERIFY(rep == (which ? arg+1 : arg+2));
        }
 }
 
 void
                VERIFY(ret == 0);
                VERIFY(rep == (which ? arg+1 : arg+2));
        }
 }
 
 void
-client2(int cl)
+client2(size_t cl)
 {
 {
-       int which_cl = ((unsigned long) cl ) % NUM_CL;
+       size_t which_cl = cl % NUM_CL;
 
        time_t t1;
        time(&t1);
 
        time_t t1;
        time(&t1);
@@ -208,9 +210,9 @@ simple_tests(rpcc *c)
        // specify a timeout value to an RPC that should succeed (tcp)
        {
                std::string arg(1000, 'x');
        // specify a timeout value to an RPC that should succeed (tcp)
        {
                std::string arg(1000, 'x');
-               std::string rep;
-               c->call_timeout(22, rpcc::to(3000), rep, arg, (std::string)"x");
-               VERIFY(rep.size() == 1001);
+               std::string rep2;
+               c->call_timeout(22, rpcc::to(3000), rep2, arg, (std::string)"x");
+               VERIFY(rep2.size() == 1001);
                printf("   -- no spurious timeout .. ok\n");
        }
 
                printf("   -- no spurious timeout .. ok\n");
        }
 
@@ -236,21 +238,21 @@ simple_tests(rpcc *c)
 }
 
 void 
 }
 
 void 
-concurrent_test(int nt)
+concurrent_test(size_t nt)
 {
        // create threads that make lots of calls in parallel,
        // to test thread synchronization for concurrent calls
        // and dispatches.
 {
        // create threads that make lots of calls in parallel,
        // to test thread synchronization for concurrent calls
        // and dispatches.
-       printf("start concurrent_test (%d threads) ...", nt);
+       printf("start concurrent_test (%lu threads) ...", nt);
 
     std::vector<std::thread> th(nt);
 
     std::vector<std::thread> th(nt);
-       for(int i = 0; i < nt; i++){
+
+       for(size_t i = 0; i < nt; i++)
         th[i] = std::thread(client1, i);
         th[i] = std::thread(client1, i);
-       }
 
 
-       for(int i = 0; i < nt; i++){
+       for(size_t i = 0; i < nt; i++)
         th[i].join();
         th[i].join();
-       }
+
        printf(" OK\n");
 }
 
        printf(" OK\n");
 }
 
@@ -271,14 +273,16 @@ lossy_test()
                VERIFY(clients[i]->bind()==0);
        }
 
                VERIFY(clients[i]->bind()==0);
        }
 
-       int nt = 1;
+       size_t nt = 1;
+
     std::vector<std::thread> th(nt);
     std::vector<std::thread> th(nt);
-       for(int i = 0; i < nt; i++){
+
+       for(size_t i = 0; i < nt; i++)
         th[i] = std::thread(client2, i);
         th[i] = std::thread(client2, i);
-       }
-       for(int i = 0; i < nt; i++){
+
+       for(size_t i = 0; i < nt; i++)
         th[i].join();
         th[i].join();
-       }
+
        printf(".. OK\n");
        VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
 }
        printf(".. OK\n");
        VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
 }
@@ -319,17 +323,17 @@ failure_test()
        printf("   -- delete existing rpc client, create replacement rpc client .. ok\n");
 
 
        printf("   -- delete existing rpc client, create replacement rpc client .. ok\n");
 
 
-       int nt = 10;
-       printf("   -- concurrent test on new rpc client w/ %d threads ..", nt);
+       size_t nt = 10;
+       printf("   -- concurrent test on new rpc client w/ %lu threads ..", nt);
 
     std::vector<std::thread> th(nt);
 
     std::vector<std::thread> th(nt);
-       for(int i = 0; i < nt; i++){
+
+       for(size_t i = 0; i < nt; i++)
         th[i] = std::thread(client3, client);
         th[i] = std::thread(client3, client);
-       }
 
 
-       for(int i = 0; i < nt; i++){
+       for(size_t i = 0; i < nt; i++)
         th[i].join();
         th[i].join();
-       }
+
        printf("ok\n");
 
        delete server;
        printf("ok\n");
 
        delete server;
@@ -340,14 +344,14 @@ failure_test()
        VERIFY (client->bind() >= 0);
        printf("   -- delete existing rpc client and server, create replacements.. ok\n");
 
        VERIFY (client->bind() >= 0);
        printf("   -- delete existing rpc client and server, create replacements.. ok\n");
 
-       printf("   -- concurrent test on new client and server w/ %d threads ..", nt);
-       for(int i = 0; i < nt; i++){
+       printf("   -- concurrent test on new client and server w/ %lu threads ..", nt);
+
+       for(size_t i = 0; i < nt; i++)
         th[i] = std::thread(client3, client);
         th[i] = std::thread(client3, client);
-       }
 
 
-       for(int i = 0; i < nt; i++){
+       for(size_t i = 0; i < nt; i++)
         th[i].join();
         th[i].join();
-       }
+
        printf("ok\n");
 
        printf("failure_test OK\n");
        printf("ok\n");
 
        printf("failure_test OK\n");
@@ -364,10 +368,10 @@ main(int argc, char *argv[])
        bool isclient = false;
        bool isserver = false;
 
        bool isclient = false;
        bool isserver = false;
 
-       srandom(getpid());
+       srandom((uint32_t)getpid());
        port = 20000 + (getpid() % 10000);
 
        port = 20000 + (getpid() % 10000);
 
-       char ch = 0;
+       int ch = 0;
        while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
                switch (ch) {
                        case 'c':
        while ((ch = getopt(argc, argv, "csd:p:l"))!=-1) {
                switch (ch) {
                        case 'c':
@@ -384,6 +388,7 @@ main(int argc, char *argv[])
                                break;
                        case 'l':
                                VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
                                break;
                        case 'l':
                                VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
+                break;
                        default:
                                break;
                }
                        default:
                                break;
                }