5 #include "include/lock_client.h"
9 using namespace std::chrono;
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.
13 static lock_client *lc[nt];
14 static const char * a = "1";
15 static const char * b = "2";
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) {
24 lock ml(*count_mutex);
25 int x = lid[0] & 0x0f;
27 LOG_NONMEMBER << "error: server granted " << lid << " twice";
33 static void check_release(lock_protocol::lockid_t lid) {
34 lock ml(*count_mutex);
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";
70 std::this_thread::sleep_for(100ms);
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');
116 count_mutex = new std::mutex();
121 setvbuf(stdout, NULL, _IONBF, 0);
122 setvbuf(stderr, NULL, _IONBF, 0);
125 LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
129 string dst = argv[1];
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";
183 for (int i = 0; i < nt; i++)