5 #include "lock_client.h"
9 char log_thread_prefix = 'c';
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.
14 static lock_client **lc = new lock_client * [nt];
15 static lock_protocol::lockid_t a = "1";
16 static lock_protocol::lockid_t b = "2";
17 static lock_protocol::lockid_t c = "3";
19 // check_grant() and check_release() check that the lock server
20 // doesn't grant the same lock to both clients.
21 // it assumes that lock names are distinct in the first byte.
23 static mutex count_mutex;
25 static void check_grant(lock_protocol::lockid_t lid) {
27 int x = lid[0] & 0x0f;
29 LOG_NONMEMBER << "error: server granted " << lid << " twice";
35 static void check_release(lock_protocol::lockid_t lid) {
37 int x = lid[0] & 0x0f;
39 LOG_NONMEMBER << "error: client released un-held lock " << lid;
45 static void test1(void) {
46 LOG_NONMEMBER << "acquire a release a acquire a release a";
56 LOG_NONMEMBER << "acquire a acquire b release b release a";
67 static void test2(int i) {
68 LOG_NONMEMBER << "test2: client " << i << " acquire a release a";
70 LOG_NONMEMBER << "test2: client " << i << " acquire done";
73 LOG_NONMEMBER << "test2: client " << i << " release";
76 LOG_NONMEMBER << "test2: client " << i << " release done";
79 static void test3(int i) {
80 LOG_NONMEMBER << "test3: client " << i << " acquire a release a concurrent";
81 for (int j = 0; j < 10; j++) {
84 LOG_NONMEMBER << "test3: client " << i << " got lock";
90 static void test4(int i) {
91 LOG_NONMEMBER << "test4: thread " << i << " acquire a release a concurrent; same clnt";
92 for (int j = 0; j < 10; j++) {
95 LOG_NONMEMBER << "test4: thread " << i << " on client 0 got lock";
101 static void test5(int i) {
102 LOG_NONMEMBER << "test5: client " << i << " acquire a release a concurrent; same and diff clnt";
103 for (int j = 0; j < 10; j++) {
104 if (i < 5) lc[0]->acquire(a);
105 else lc[1]->acquire(a);
107 LOG_NONMEMBER << "test5: client " << i << " got lock";
109 if (i < 5) lc[0]->release(a);
110 else lc[1]->release(a);
115 main(int argc, char *argv[])
120 setvbuf(stdout, NULL, _IONBF, 0);
121 setvbuf(stderr, NULL, _IONBF, 0);
122 srandom((uint32_t)getpid());
125 LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
132 test = atoi(argv[2]);
133 if (test < 1 || test > 5) {
134 LOG_NONMEMBER << "Test number must be between 1 and 5";
139 LOG_NONMEMBER << "cache lock client";
140 for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
142 if (!test || test == 1) {
146 if (!test || test == 2) {
148 for (int i = 0; i < nt; i++)
149 th[i] = thread(test2, i);
150 for (int i = 0; i < nt; i++)
154 if (!test || test == 3) {
155 LOG_NONMEMBER << "test 3";
157 for (int i = 0; i < nt; i++)
158 th[i] = thread(test3, i);
159 for (int i = 0; i < nt; i++)
163 if (!test || test == 4) {
164 LOG_NONMEMBER << "test 4";
166 for (int i = 0; i < 2; i++)
167 th[i] = thread(test4, i);
168 for (int i = 0; i < 2; i++)
172 if (!test || test == 5) {
173 LOG_NONMEMBER << "test 5";
175 for (int i = 0; i < nt; i++)
176 th[i] = thread(test5, i);
177 for (int i = 0; i < nt; i++)
181 LOG_NONMEMBER << argv[0] << ": passed all tests successfully";