So many changes. Broken.
[invirt/third/libt4.git] / lock_tester.cc
1 //
2 // Lock server tester
3 //
4
5 #include "include/lock_client.h"
6 #include <arpa/inet.h>
7 #include <unistd.h>
8
9 using namespace std::chrono;
10
11 // must be >= 2
12 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.
13 static lock_client *lc[nt];
14 static const char * a = "1";
15 static const char * b = "2";
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     std::this_thread::sleep_for(100ms);
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     count_mutex = new std::mutex();
117
118     thread th[nt];
119     int test = 0;
120
121     setvbuf(stdout, NULL, _IONBF, 0);
122     setvbuf(stderr, NULL, _IONBF, 0);
123
124     if (argc < 2) {
125         LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
126         exit(1);
127     }
128
129     string dst = argv[1]; 
130
131     if (argc > 2) {
132         test = atoi(argv[2]);
133         if (test < 1 || test > 5) {
134             LOG_NONMEMBER << "Test number must be between 1 and 5";
135             exit(1);
136         }
137     }
138
139     LOG_NONMEMBER << "cache lock client";
140     for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
141
142     if (!test || test == 1) {
143         test1();
144     }
145
146     if (!test || test == 2) {
147         // test2
148         for (int i = 0; i < nt; i++)
149             th[i] = thread(test2, i);
150         for (int i = 0; i < nt; i++)
151             th[i].join();
152     }
153
154     if (!test || test == 3) {
155         LOG_NONMEMBER << "test 3";
156
157         for (int i = 0; i < nt; i++)
158             th[i] = thread(test3, i);
159         for (int i = 0; i < nt; i++)
160             th[i].join();
161     }
162
163     if (!test || test == 4) {
164         LOG_NONMEMBER << "test 4";
165
166         for (int i = 0; i < 2; i++)
167             th[i] = thread(test4, i);
168         for (int i = 0; i < 2; i++)
169             th[i].join();
170     }
171
172     if (!test || test == 5) {
173         LOG_NONMEMBER << "test 5";
174
175         for (int i = 0; i < nt; i++)
176             th[i] = thread(test5, i);
177         for (int i = 0; i < nt; i++)
178             th[i].join();
179     }
180
181     LOG_NONMEMBER << argv[0] << ": passed all tests successfully";
182
183     for (int i = 0; i < nt; i++)
184         delete lc[i];
185 }