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