Global destructor clean-ups and python test fixes
[invirt/third/libt4.git] / lock_tester.cc
1 //
2 // Lock server tester
3 //
4
5 #include "lock_client.h"
6 #include <arpa/inet.h>
7 #include <unistd.h>
8 #include "handle.h"
9
10 char log_thread_prefix = 'c';
11
12 // must be >= 2
13 const int nt = 6; //XXX: lab1's rpc handlers are blocking. Since rpcs uses a thread pool of 10 threads, we cannot test more than 10 blocking rpc.
14 static string dst;
15 static lock_client *lc[nt];
16 static lock_protocol::lockid_t a = "1";
17 static lock_protocol::lockid_t b = "2";
18 static lock_protocol::lockid_t c = "3";
19
20 // check_grant() and check_release() check that the lock server
21 // doesn't grant the same lock to both clients.
22 // it assumes that lock names are distinct in the first byte.
23 static int ct[256];
24 static std::mutex count_mutex;
25
26 static void check_grant(lock_protocol::lockid_t lid) {
27     lock ml(count_mutex);
28     int x = lid[0] & 0x0f;
29     if (ct[x] != 0) {
30         LOG_NONMEMBER << "error: server granted " << lid << " twice";
31         exit(1);
32     }
33     ct[x] += 1;
34 }
35
36 static void check_release(lock_protocol::lockid_t lid) {
37     lock ml(count_mutex);
38     int x = lid[0] & 0x0f;
39     if (ct[x] != 1) {
40         LOG_NONMEMBER << "error: client released un-held lock " << lid;
41         exit(1);
42     }
43     ct[x] -= 1;
44 }
45
46 static void test1(void) {
47     LOG_NONMEMBER << "acquire a release a acquire a release a";
48     lc[0]->acquire(a);
49     check_grant(a);
50     lc[0]->release(a);
51     check_release(a);
52     lc[0]->acquire(a);
53     check_grant(a);
54     lc[0]->release(a);
55     check_release(a);
56
57     LOG_NONMEMBER << "acquire a acquire b release b release a";
58     lc[0]->acquire(a);
59     check_grant(a);
60     lc[0]->acquire(b);
61     check_grant(b);
62     lc[0]->release(b);
63     check_release(b);
64     lc[0]->release(a);
65     check_release(a);
66 }
67
68 static void test2(int i) {
69     LOG_NONMEMBER << "test2: client " << i << " acquire a release a";
70     lc[i]->acquire(a);
71     LOG_NONMEMBER << "test2: client " << i << " acquire done";
72     check_grant(a);
73     usleep(100000);
74     LOG_NONMEMBER << "test2: client " << i << " release";
75     check_release(a);
76     lc[i]->release(a);
77     LOG_NONMEMBER << "test2: client " << i << " release done";
78 }
79
80 static void test3(int i) {
81     LOG_NONMEMBER << "test3: client " << i << " acquire a release a concurrent";
82     for (int j = 0; j < 10; j++) {
83         lc[i]->acquire(a);
84         check_grant(a);
85         LOG_NONMEMBER << "test3: client " << i << " got lock";
86         check_release(a);
87         lc[i]->release(a);
88     }
89 }
90
91 static void test4(int i) {
92     LOG_NONMEMBER << "test4: thread " << i << " acquire a release a concurrent; same clnt";
93     for (int j = 0; j < 10; j++) {
94         lc[0]->acquire(a);
95         check_grant(a);
96         LOG_NONMEMBER << "test4: thread " << i << " on client 0 got lock";
97         check_release(a);
98         lc[0]->release(a);
99     }
100 }
101
102 static void test5(int i) {
103     LOG_NONMEMBER << "test5: client " << i << " acquire a release a concurrent; same and diff clnt";
104     for (int j = 0; j < 10; j++) {
105         if (i < 5)  lc[0]->acquire(a);
106         else  lc[1]->acquire(a);
107         check_grant(a);
108         LOG_NONMEMBER << "test5: client " << i << " got lock";
109         check_release(a);
110         if (i < 5) lc[0]->release(a);
111         else lc[1]->release(a);
112     }
113 }
114
115 int
116 main(int argc, char *argv[])
117 {
118     thread th[nt];
119     int test = 0;
120
121     setvbuf(stdout, NULL, _IONBF, 0);
122     setvbuf(stderr, NULL, _IONBF, 0);
123     srandom((uint32_t)getpid());
124
125     if (argc < 2) {
126         LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
127         exit(1);
128     }
129
130     dst = argv[1]; 
131
132     if (argc > 2) {
133         test = atoi(argv[2]);
134         if (test < 1 || test > 5) {
135             LOG_NONMEMBER << "Test number must be between 1 and 5";
136             exit(1);
137         }
138     }
139
140     LOG_NONMEMBER << "cache lock client";
141     for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
142
143     if (!test || test == 1) {
144         test1();
145     }
146
147     if (!test || test == 2) {
148         // test2
149         for (int i = 0; i < nt; i++)
150             th[i] = thread(test2, i);
151         for (int i = 0; i < nt; i++)
152             th[i].join();
153     }
154
155     if (!test || test == 3) {
156         LOG_NONMEMBER << "test 3";
157
158         for (int i = 0; i < nt; i++)
159             th[i] = thread(test3, i);
160         for (int i = 0; i < nt; i++)
161             th[i].join();
162     }
163
164     if (!test || test == 4) {
165         LOG_NONMEMBER << "test 4";
166
167         for (int i = 0; i < 2; i++)
168             th[i] = thread(test4, i);
169         for (int i = 0; i < 2; i++)
170             th[i].join();
171     }
172
173     if (!test || test == 5) {
174         LOG_NONMEMBER << "test 5";
175
176         for (int i = 0; i < nt; i++)
177             th[i] = thread(test5, i);
178         for (int i = 0; i < nt; i++)
179             th[i].join();
180     }
181
182     LOG_NONMEMBER << argv[0] << ": passed all tests successfully";
183
184     for (int i = 0; i < nt; i++)
185         delete lc[i];
186
187     handle::shutdown();
188     poll_mgr::shared_mgr.shutdown();
189
190     LOG_NONMEMBER << argv[0] << ": clean-up complete";
191 }