5 #include "lock_protocol.h"
6 #include "lock_client.h"
12 #include "lang/verify.h"
13 #include "threaded_log.h"
14 #include <sys/types.h>
18 char log_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;
34 void check_grant(lock_protocol::lockid_t lid) {
36 int x = lid[0] & 0x0f;
38 fprintf(stderr, "error: server granted %s twice\n", lid.c_str());
39 fprintf(stdout, "error: server granted %s twice\n", lid.c_str());
45 void check_release(lock_protocol::lockid_t lid) {
47 int x = lid[0] & 0x0f;
49 fprintf(stderr, "error: client released un-held lock %s\n", lid.c_str());
56 LOG_NONMEMBER("acquire a release a acquire a release a");
66 LOG_NONMEMBER("acquire a acquire b release b release a");
78 LOG_NONMEMBER("test2: client " << i << " acquire a release a");
80 LOG_NONMEMBER("test2: client " << i << " acquire done");
83 LOG_NONMEMBER("test2: client " << i << " release");
86 LOG_NONMEMBER("test2: client " << i << " release done");
90 LOG_NONMEMBER("test3: client " << i << " acquire a release a concurrent");
91 for (int j = 0; j < 10; j++) {
94 LOG_NONMEMBER("test3: client " << i << " got lock");
101 LOG_NONMEMBER("test4: thread " << i << " acquire a release a concurrent; same clnt");
102 for (int j = 0; j < 10; j++) {
105 LOG_NONMEMBER("test4: thread " << i << " on client 0 got lock");
112 LOG_NONMEMBER("test5: client " << i << " acquire a release a concurrent; same and diff clnt");
113 for (int j = 0; j < 10; j++) {
114 if (i < 5) lc[0]->acquire(a);
115 else lc[1]->acquire(a);
117 LOG_NONMEMBER("test5: client " << i << " got lock");
119 if (i < 5) lc[0]->release(a);
120 else lc[1]->release(a);
125 main(int argc, char *argv[])
130 setvbuf(stdout, NULL, _IONBF, 0);
131 setvbuf(stderr, NULL, _IONBF, 0);
132 srandom((uint32_t)getpid());
135 fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
142 test = atoi(argv[2]);
143 if (test < 1 || test > 5) {
144 LOG_NONMEMBER("Test number must be between 1 and 5");
149 LOG_NONMEMBER("cache lock client");
150 for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
152 if (!test || test == 1) {
156 if (!test || test == 2) {
158 for (int i = 0; i < nt; i++)
159 th[i] = std::thread(test2, i);
160 for (int i = 0; i < nt; i++)
164 if (!test || test == 3) {
165 LOG_NONMEMBER("test 3");
167 for (int i = 0; i < nt; i++)
168 th[i] = std::thread(test3, i);
169 for (int i = 0; i < nt; i++)
173 if (!test || test == 4) {
174 LOG_NONMEMBER("test 4");
176 for (int i = 0; i < 2; i++)
177 th[i] = std::thread(test4, i);
178 for (int i = 0; i < 2; i++)
182 if (!test || test == 5) {
183 LOG_NONMEMBER("test 5");
185 for (int i = 0; i < nt; i++)
186 th[i] = std::thread(test5, i);
187 for (int i = 0; i < nt; i++)
191 LOG_NONMEMBER(argv[0] << ": passed all tests successfully");