5 #include "lock_protocol.h"
6 #include "lock_client.h"
12 #include "lang/verify.h"
14 #include <sys/types.h>
18 char tprintf_thread_prefix = 'c';
21 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.
23 lock_client **lc = new lock_client * [nt];
24 lock_protocol::lockid_t a = "1";
25 lock_protocol::lockid_t b = "2";
26 lock_protocol::lockid_t c = "3";
28 // check_grant() and check_release() check that the lock server
29 // doesn't grant the same lock to both clients.
30 // it assumes that lock names are distinct in the first byte.
32 std::mutex count_mutex;
35 check_grant(lock_protocol::lockid_t lid)
38 int x = lid[0] & 0x0f;
40 fprintf(stderr, "error: server granted %s twice\n", lid.c_str());
41 fprintf(stdout, "error: server granted %s twice\n", lid.c_str());
48 check_release(lock_protocol::lockid_t lid)
51 int x = lid[0] & 0x0f;
53 fprintf(stderr, "error: client released un-held lock %s\n", lid.c_str());
62 tprintf ("acquire a release a acquire a release a\n");
72 tprintf ("acquire a acquire b release b release a\n");
86 tprintf ("test2: client %d acquire a release a\n", i);
88 tprintf ("test2: client %d acquire done\n", i);
91 tprintf ("test2: client %d release\n", i);
94 tprintf ("test2: client %d release done\n", i);
101 tprintf ("test3: client %d acquire a release a concurrent\n", i);
102 for (int j = 0; j < 10; j++) {
105 tprintf ("test3: client %d got lock\n", i);
115 tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i);
116 for (int j = 0; j < 10; j++) {
119 tprintf ("test4: thread %d on client 0 got lock\n", i);
129 tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i);
130 for (int j = 0; j < 10; j++) {
131 if (i < 5) lc[0]->acquire(a);
132 else lc[1]->acquire(a);
134 tprintf ("test5: client %d got lock\n", i);
136 if (i < 5) lc[0]->release(a);
137 else lc[1]->release(a);
143 main(int argc, char *argv[])
148 setvbuf(stdout, NULL, _IONBF, 0);
149 setvbuf(stderr, NULL, _IONBF, 0);
150 srandom((uint32_t)getpid());
153 fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
160 test = atoi(argv[2]);
161 if(test < 1 || test > 5){
162 tprintf("Test number must be between 1 and 5\n");
167 tprintf("cache lock client\n");
168 for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
170 if(!test || test == 1){
174 if(!test || test == 2){
176 for (int i = 0; i < nt; i++)
177 th[i] = std::thread(test2, i);
178 for (int i = 0; i < nt; i++)
182 if(!test || test == 3){
186 for (int i = 0; i < nt; i++)
187 th[i] = std::thread(test3, i);
188 for (int i = 0; i < nt; i++)
192 if(!test || test == 4){
196 for (int i = 0; i < 2; i++)
197 th[i] = std::thread(test4, i);
198 for (int i = 0; i < 2; i++)
202 if(!test || test == 5){
206 for (int i = 0; i < nt; i++)
207 th[i] = std::thread(test5, i);
208 for (int i = 0; i < nt; i++)
212 tprintf ("%s: passed all tests successfully\n", argv[0]);