5 #include "lock_client.h"
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.
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";
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.
21 static std::mutex count_mutex;
23 static void check_grant(lock_protocol::lockid_t lid) {
25 int x = lid[0] & 0x0f;
27 LOG_NONMEMBER << "error: server granted " << lid << " twice";
33 static void check_release(lock_protocol::lockid_t lid) {
35 int x = lid[0] & 0x0f;
37 LOG_NONMEMBER << "error: client released un-held lock " << lid;
43 static void test1(void) {
44 LOG_NONMEMBER << "acquire a release a acquire a release a";
54 LOG_NONMEMBER << "acquire a acquire b release b release a";
65 static void test2(int i) {
66 LOG_NONMEMBER << "test2: client " << i << " acquire a release a";
68 LOG_NONMEMBER << "test2: client " << i << " acquire done";
71 LOG_NONMEMBER << "test2: client " << i << " release";
74 LOG_NONMEMBER << "test2: client " << i << " release done";
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++) {
82 LOG_NONMEMBER << "test3: client " << i << " got lock";
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++) {
93 LOG_NONMEMBER << "test4: thread " << i << " on client 0 got lock";
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);
105 LOG_NONMEMBER << "test5: client " << i << " got lock";
107 if (i < 5) lc[0]->release(a);
108 else lc[1]->release(a);
113 main(int argc, char *argv[])
115 global = new t4_state('c');
119 setvbuf(stdout, NULL, _IONBF, 0);
120 setvbuf(stderr, NULL, _IONBF, 0);
121 srandom((uint32_t)getpid());
124 LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
131 test = atoi(argv[2]);
132 if (test < 1 || test > 5) {
133 LOG_NONMEMBER << "Test number must be between 1 and 5";
138 LOG_NONMEMBER << "cache lock client";
139 for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
141 if (!test || test == 1) {
145 if (!test || test == 2) {
147 for (int i = 0; i < nt; i++)
148 th[i] = thread(test2, i);
149 for (int i = 0; i < nt; i++)
153 if (!test || test == 3) {
154 LOG_NONMEMBER << "test 3";
156 for (int i = 0; i < nt; i++)
157 th[i] = thread(test3, i);
158 for (int i = 0; i < nt; i++)
162 if (!test || test == 4) {
163 LOG_NONMEMBER << "test 4";
165 for (int i = 0; i < 2; i++)
166 th[i] = thread(test4, i);
167 for (int i = 0; i < 2; i++)
171 if (!test || test == 5) {
172 LOG_NONMEMBER << "test 5";
174 for (int i = 0; i < nt; i++)
175 th[i] = thread(test5, i);
176 for (int i = 0; i < nt; i++)
180 LOG_NONMEMBER << argv[0] << ": passed all tests successfully";
182 for (int i = 0; i < nt; i++)