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.
11 static lock_client *lc[nt];
12 static const char * a = "1";
13 static const char * b = "2";
15 // check_grant() and check_release() check that the lock server
16 // doesn't grant the same lock to both clients.
17 // it assumes that lock names are distinct in the first byte.
19 static std::mutex * count_mutex;
21 static void check_grant(lock_protocol::lockid_t lid) {
22 lock ml(*count_mutex);
23 int x = lid[0] & 0x0f;
25 LOG_NONMEMBER << "error: server granted " << lid << " twice";
31 static void check_release(lock_protocol::lockid_t lid) {
32 lock ml(*count_mutex);
33 int x = lid[0] & 0x0f;
35 LOG_NONMEMBER << "error: client released un-held lock " << lid;
41 static void test1(void) {
42 LOG_NONMEMBER << "acquire a release a acquire a release a";
52 LOG_NONMEMBER << "acquire a acquire b release b release a";
63 static void test2(int i) {
64 LOG_NONMEMBER << "test2: client " << i << " acquire a release a";
66 LOG_NONMEMBER << "test2: client " << i << " acquire done";
68 std::this_thread::sleep_for(milliseconds(100));
69 LOG_NONMEMBER << "test2: client " << i << " release";
72 LOG_NONMEMBER << "test2: client " << i << " release done";
75 static void test3(int i) {
76 LOG_NONMEMBER << "test3: client " << i << " acquire a release a concurrent";
77 for (int j = 0; j < 10; j++) {
80 LOG_NONMEMBER << "test3: client " << i << " got lock";
86 static void test4(int i) {
87 LOG_NONMEMBER << "test4: thread " << i << " acquire a release a concurrent; same clnt";
88 for (int j = 0; j < 10; j++) {
91 LOG_NONMEMBER << "test4: thread " << i << " on client 0 got lock";
97 static void test5(int i) {
98 LOG_NONMEMBER << "test5: client " << i << " acquire a release a concurrent; same and diff clnt";
99 for (int j = 0; j < 10; j++) {
100 if (i < 5) lc[0]->acquire(a);
101 else lc[1]->acquire(a);
103 LOG_NONMEMBER << "test5: client " << i << " got lock";
105 if (i < 5) lc[0]->release(a);
106 else lc[1]->release(a);
111 main(int argc, char *argv[])
113 global = new t4_state('c');
114 count_mutex = new std::mutex();
119 setvbuf(stdout, NULL, _IONBF, 0);
120 setvbuf(stderr, NULL, _IONBF, 0);
123 LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
127 string dst = argv[1];
130 test = atoi(argv[2]);
131 if (test < 1 || test > 5) {
132 LOG_NONMEMBER << "Test number must be between 1 and 5";
137 LOG_NONMEMBER << "cache lock client";
138 for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
140 if (!test || test == 1) {
144 if (!test || test == 2) {
146 for (int i = 0; i < nt; i++)
147 th[i] = thread(test2, i);
148 for (int i = 0; i < nt; i++)
152 if (!test || test == 3) {
153 LOG_NONMEMBER << "test 3";
155 for (int i = 0; i < nt; i++)
156 th[i] = thread(test3, i);
157 for (int i = 0; i < nt; i++)
161 if (!test || test == 4) {
162 LOG_NONMEMBER << "test 4";
164 for (int i = 0; i < 2; i++)
165 th[i] = thread(test4, i);
166 for (int i = 0; i < 2; i++)
170 if (!test || test == 5) {
171 LOG_NONMEMBER << "test 5";
173 for (int i = 0; i < nt; i++)
174 th[i] = thread(test5, i);
175 for (int i = 0; i < nt; i++)
179 LOG_NONMEMBER << argv[0] << ": passed all tests successfully";
181 for (int i = 0; i < nt; i++)