5 #include "lock_client.h"
10 char log_thread_prefix = 'c';
13 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.
15 static lock_client *lc[nt];
16 static lock_protocol::lockid_t a = "1";
17 static lock_protocol::lockid_t b = "2";
18 static lock_protocol::lockid_t c = "3";
20 // check_grant() and check_release() check that the lock server
21 // doesn't grant the same lock to both clients.
22 // it assumes that lock names are distinct in the first byte.
24 static std::mutex count_mutex;
26 static void check_grant(lock_protocol::lockid_t lid) {
28 int x = lid[0] & 0x0f;
30 LOG_NONMEMBER << "error: server granted " << lid << " twice";
36 static void check_release(lock_protocol::lockid_t lid) {
38 int x = lid[0] & 0x0f;
40 LOG_NONMEMBER << "error: client released un-held lock " << lid;
46 static void test1(void) {
47 LOG_NONMEMBER << "acquire a release a acquire a release a";
57 LOG_NONMEMBER << "acquire a acquire b release b release a";
68 static void test2(int i) {
69 LOG_NONMEMBER << "test2: client " << i << " acquire a release a";
71 LOG_NONMEMBER << "test2: client " << i << " acquire done";
74 LOG_NONMEMBER << "test2: client " << i << " release";
77 LOG_NONMEMBER << "test2: client " << i << " release done";
80 static void test3(int i) {
81 LOG_NONMEMBER << "test3: client " << i << " acquire a release a concurrent";
82 for (int j = 0; j < 10; j++) {
85 LOG_NONMEMBER << "test3: client " << i << " got lock";
91 static void test4(int i) {
92 LOG_NONMEMBER << "test4: thread " << i << " acquire a release a concurrent; same clnt";
93 for (int j = 0; j < 10; j++) {
96 LOG_NONMEMBER << "test4: thread " << i << " on client 0 got lock";
102 static void test5(int i) {
103 LOG_NONMEMBER << "test5: client " << i << " acquire a release a concurrent; same and diff clnt";
104 for (int j = 0; j < 10; j++) {
105 if (i < 5) lc[0]->acquire(a);
106 else lc[1]->acquire(a);
108 LOG_NONMEMBER << "test5: client " << i << " got lock";
110 if (i < 5) lc[0]->release(a);
111 else lc[1]->release(a);
116 main(int argc, char *argv[])
121 setvbuf(stdout, NULL, _IONBF, 0);
122 setvbuf(stderr, NULL, _IONBF, 0);
123 srandom((uint32_t)getpid());
126 LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
133 test = atoi(argv[2]);
134 if (test < 1 || test > 5) {
135 LOG_NONMEMBER << "Test number must be between 1 and 5";
140 LOG_NONMEMBER << "cache lock client";
141 for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
143 if (!test || test == 1) {
147 if (!test || test == 2) {
149 for (int i = 0; i < nt; i++)
150 th[i] = thread(test2, i);
151 for (int i = 0; i < nt; i++)
155 if (!test || test == 3) {
156 LOG_NONMEMBER << "test 3";
158 for (int i = 0; i < nt; i++)
159 th[i] = thread(test3, i);
160 for (int i = 0; i < nt; i++)
164 if (!test || test == 4) {
165 LOG_NONMEMBER << "test 4";
167 for (int i = 0; i < 2; i++)
168 th[i] = thread(test4, i);
169 for (int i = 0; i < 2; i++)
173 if (!test || test == 5) {
174 LOG_NONMEMBER << "test 5";
176 for (int i = 0; i < nt; i++)
177 th[i] = thread(test5, i);
178 for (int i = 0; i < nt; i++)
182 LOG_NONMEMBER << argv[0] << ": passed all tests successfully";
184 for (int i = 0; i < nt; i++)
188 poll_mgr::shared_mgr.shutdown();
190 LOG_NONMEMBER << argv[0] << ": clean-up complete";