2 * Copyright (c) 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
35 void (*function)(void);
38 static const struct test tests[];
41 run_lock_and_unlock(void)
43 struct lockfile *lockfile;
45 assert(lockfile_lock("file", 0, &lockfile) == 0);
46 lockfile_unlock(lockfile);
50 run_lock_and_unlock_twice(void)
52 struct lockfile *lockfile;
54 assert(lockfile_lock("file", 0, &lockfile) == 0);
55 lockfile_unlock(lockfile);
57 assert(lockfile_lock("file", 0, &lockfile) == 0);
58 lockfile_unlock(lockfile);
62 run_lock_blocks_same_process(void)
64 struct lockfile *lockfile;
66 assert(lockfile_lock("file", 0, &lockfile) == 0);
67 assert(lockfile_lock("file", 0, &lockfile) == EDEADLK);
68 lockfile_unlock(lockfile);
72 run_lock_blocks_same_process_twice(void)
74 struct lockfile *lockfile;
76 assert(lockfile_lock("file", 0, &lockfile) == 0);
77 assert(lockfile_lock("file", 0, &lockfile) == EDEADLK);
78 assert(lockfile_lock("file", 0, &lockfile) == EDEADLK);
79 lockfile_unlock(lockfile);
82 static enum { PARENT, CHILD }
96 ovs_fatal(errno, "fork failed");
101 run_lock_blocks_other_process(void)
103 struct lockfile *lockfile;
105 assert(lockfile_lock("file", 0, &lockfile) == 0);
106 if (do_fork() == CHILD) {
107 assert(lockfile_lock("file", 0, &lockfile) == EAGAIN);
113 run_lock_twice_blocks_other_process(void)
115 struct lockfile *lockfile, *dummy;
117 assert(lockfile_lock("file", 0, &lockfile) == 0);
118 assert(lockfile_lock("file", 0, &dummy) == EDEADLK);
119 if (do_fork() == CHILD) {
120 assert(lockfile_lock("file", 0, &dummy) == EAGAIN);
126 run_lock_and_unlock_allows_other_process(void)
128 struct lockfile *lockfile;
130 assert(lockfile_lock("file", 0, &lockfile) == 0);
131 lockfile_unlock(lockfile);
133 if (do_fork() == CHILD) {
134 assert(lockfile_lock("file", 0, &lockfile) == 0);
140 run_lock_timeout_gets_the_lock(void)
142 struct lockfile *lockfile;
144 assert(lockfile_lock("file", 0, &lockfile) == 0);
146 if (do_fork() == CHILD) {
147 assert(lockfile_lock("file", TIME_UPDATE_INTERVAL * 3,
151 long long int now = time_msec();
152 while (time_msec() < now + TIME_UPDATE_INTERVAL) {
155 lockfile_unlock(lockfile);
160 run_lock_timeout_runs_out(void)
162 struct lockfile *lockfile;
164 assert(lockfile_lock("file", 0, &lockfile) == 0);
166 if (do_fork() == CHILD) {
167 assert(lockfile_lock("file", TIME_UPDATE_INTERVAL,
168 &lockfile) == ETIMEDOUT);
171 long long int now = time_msec();
172 while (time_msec() < now + TIME_UPDATE_INTERVAL * 3) {
175 lockfile_unlock(lockfile);
180 run_lock_multiple(void)
182 struct lockfile *a, *b, *c, *dummy;
184 assert(lockfile_lock("a", 0, &a) == 0);
185 assert(lockfile_lock("b", 0, &b) == 0);
186 assert(lockfile_lock("c", 0, &c) == 0);
189 assert(lockfile_lock("a", 0, &a) == 0);
190 assert(lockfile_lock("a", 0, &dummy) == EDEADLK);
194 assert(lockfile_lock("a", 0, &a) == 0);
205 printf("usage: %s TESTNAME\n"
206 "where TESTNAME is one of the following:\n",
208 for (i = 0; tests[i].name; i++) {
209 fprintf(stderr, "\t%s\n", tests[i].name);
213 static const struct test tests[] = {
214 #define TEST(NAME) { #NAME, run_##NAME }
215 TEST(lock_and_unlock),
216 TEST(lock_and_unlock_twice),
217 TEST(lock_blocks_same_process),
218 TEST(lock_blocks_same_process_twice),
219 TEST(lock_blocks_other_process),
220 TEST(lock_twice_blocks_other_process),
221 TEST(lock_and_unlock_allows_other_process),
222 TEST(lock_timeout_gets_the_lock),
223 TEST(lock_timeout_runs_out),
231 main(int argc, char *argv[])
235 set_program_name(argv[0]);
239 ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
244 for (i = 0; tests[i].name; i++) {
245 if (!strcmp(argv[1], tests[i].name)) {
249 (tests[i].function)();
252 while (wait(&status) > 0) {
253 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
256 ovs_fatal(0, "child exited in unexpected way: %s",
257 process_status_msg(status));
260 if (errno != ECHILD) {
261 ovs_fatal(errno, "wait");
264 printf("%s: success (%d child%s)\n",
265 tests[i].name, n_children, n_children != 1 ? "ren" : "");
269 ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
270 argv[1], program_name);