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");
88 tprintf ("test2: client %d acquire a release a\n", i);
90 tprintf ("test2: client %d acquire done\n", i);
93 tprintf ("test2: client %d release\n", i);
96 tprintf ("test2: client %d release done\n", i);
105 tprintf ("test3: client %d acquire a release a concurrent\n", i);
106 for (int j = 0; j < 10; j++) {
109 tprintf ("test3: client %d got lock\n", i);
121 tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i);
122 for (int j = 0; j < 10; j++) {
125 tprintf ("test4: thread %d on client 0 got lock\n", i);
137 tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i);
138 for (int j = 0; j < 10; j++) {
139 if (i < 5) lc[0]->acquire(a);
140 else lc[1]->acquire(a);
142 tprintf ("test5: client %d got lock\n", i);
144 if (i < 5) lc[0]->release(a);
145 else lc[1]->release(a);
151 main(int argc, char *argv[])
156 setvbuf(stdout, NULL, _IONBF, 0);
157 setvbuf(stderr, NULL, _IONBF, 0);
161 fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
168 test = atoi(argv[2]);
169 if(test < 1 || test > 5){
170 tprintf("Test number must be between 1 and 5\n");
175 tprintf("cache lock client\n");
176 for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
178 if(!test || test == 1){
182 if(!test || test == 2){
184 for (int i = 0; i < nt; i++) {
185 int *a = new int (i);
186 th[i] = std::thread(test2, a);
188 for (int i = 0; i < nt; i++) {
193 if(!test || test == 3){
197 for (int i = 0; i < nt; i++) {
198 int *a = new int (i);
199 th[i] = std::thread(test3, a);
201 for (int i = 0; i < nt; i++) {
206 if(!test || test == 4){
210 for (int i = 0; i < 2; i++) {
211 int *a = new int (i);
212 th[i] = std::thread(test4, a);
214 for (int i = 0; i < 2; i++) {
219 if(!test || test == 5){
224 for (int i = 0; i < nt; i++) {
225 int *a = new int (i);
226 th[i] = std::thread(test5, a);
228 for (int i = 0; i < nt; i++) {
233 tprintf ("%s: passed all tests successfully\n", argv[0]);