2 * Copyright (c) 2009, 2010, 2011 Nicira, Inc.
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.
33 void (*function)(void);
36 static const struct test tests[];
38 #define CHECK(A, B) check(A, B, #A, #B, __FILE__, __LINE__)
41 const char *a_string, const char *b_string, const char *file, int line)
44 fprintf(stderr, "%s:%d: expected %s == %s but %d != %d\n",
45 file, line, a_string, b_string, a, b);
52 run_lock_and_unlock(void)
54 struct lockfile *lockfile;
56 CHECK(lockfile_lock("file", 0, &lockfile), 0);
57 lockfile_unlock(lockfile);
61 run_lock_and_unlock_twice(void)
63 struct lockfile *lockfile;
65 CHECK(lockfile_lock("file", 0, &lockfile), 0);
66 lockfile_unlock(lockfile);
68 CHECK(lockfile_lock("file", 0, &lockfile), 0);
69 lockfile_unlock(lockfile);
73 run_lock_blocks_same_process(void)
75 struct lockfile *lockfile;
77 CHECK(lockfile_lock("file", 0, &lockfile), 0);
78 CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
79 lockfile_unlock(lockfile);
83 run_lock_blocks_same_process_twice(void)
85 struct lockfile *lockfile;
87 CHECK(lockfile_lock("file", 0, &lockfile), 0);
88 CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
89 CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
90 lockfile_unlock(lockfile);
93 static enum { PARENT, CHILD }
107 ovs_fatal(errno, "fork failed");
112 run_lock_blocks_other_process(void)
114 /* Making this static prevents a memory leak warning from valgrind for the
115 * parent process, which cannot easily unlock (and free) 'lockfile' because
116 * it can only do so after the child has exited, and it's the caller of
117 * this function that does the wait() call. */
118 static struct lockfile *lockfile;
120 CHECK(lockfile_lock("file", 0, &lockfile), 0);
121 if (do_fork() == CHILD) {
122 lockfile_unlock(lockfile);
123 CHECK(lockfile_lock("file", 0, &lockfile), EAGAIN);
129 run_lock_twice_blocks_other_process(void)
131 struct lockfile *lockfile, *dummy;
133 CHECK(lockfile_lock("file", 0, &lockfile), 0);
134 CHECK(lockfile_lock("file", 0, &dummy), EDEADLK);
135 if (do_fork() == CHILD) {
136 CHECK(lockfile_lock("file", 0, &dummy), EAGAIN);
142 run_lock_and_unlock_allows_other_process(void)
144 struct lockfile *lockfile;
146 CHECK(lockfile_lock("file", 0, &lockfile), 0);
147 lockfile_unlock(lockfile);
149 if (do_fork() == CHILD) {
150 CHECK(lockfile_lock("file", 0, &lockfile), 0);
156 run_lock_timeout_gets_the_lock(void)
158 struct lockfile *lockfile;
160 CHECK(lockfile_lock("file", 0, &lockfile), 0);
162 if (do_fork() == CHILD) {
163 lockfile_unlock(lockfile);
164 CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL * 3, &lockfile), 0);
167 long long int now = time_msec();
168 while (time_msec() < now + TIME_UPDATE_INTERVAL) {
171 lockfile_unlock(lockfile);
176 run_lock_timeout_runs_out(void)
178 struct lockfile *lockfile;
180 CHECK(lockfile_lock("file", 0, &lockfile), 0);
182 if (do_fork() == CHILD) {
183 lockfile_unlock(lockfile);
184 CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL, &lockfile),
188 long long int now = time_msec();
189 while (time_msec() < now + TIME_UPDATE_INTERVAL * 3) {
192 lockfile_unlock(lockfile);
197 run_lock_multiple(void)
199 struct lockfile *a, *b, *c, *dummy;
201 CHECK(lockfile_lock("a", 0, &a), 0);
202 CHECK(lockfile_lock("b", 0, &b), 0);
203 CHECK(lockfile_lock("c", 0, &c), 0);
206 CHECK(lockfile_lock("a", 0, &a), 0);
207 CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
211 CHECK(lockfile_lock("a", 0, &a), 0);
222 printf("usage: %s TESTNAME\n"
223 "where TESTNAME is one of the following:\n",
225 for (i = 0; tests[i].name; i++) {
226 fprintf(stderr, "\t%s\n", tests[i].name);
230 static const struct test tests[] = {
231 #define TEST(NAME) { #NAME, run_##NAME }
232 TEST(lock_and_unlock),
233 TEST(lock_and_unlock_twice),
234 TEST(lock_blocks_same_process),
235 TEST(lock_blocks_same_process_twice),
236 TEST(lock_blocks_other_process),
237 TEST(lock_twice_blocks_other_process),
238 TEST(lock_and_unlock_allows_other_process),
239 TEST(lock_timeout_gets_the_lock),
240 TEST(lock_timeout_runs_out),
248 main(int argc, char *argv[])
250 extern struct vlog_module VLM_lockfile;
253 set_program_name(argv[0]);
254 vlog_set_levels(&VLM_lockfile, VLF_ANY_FACILITY, VLL_ERR);
257 ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
262 for (i = 0; tests[i].name; i++) {
263 if (!strcmp(argv[1], tests[i].name)) {
267 (tests[i].function)();
270 while (wait(&status) > 0) {
271 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
274 ovs_fatal(0, "child exited in unexpected way: %s",
275 process_status_msg(status));
278 if (errno != ECHILD) {
279 ovs_fatal(errno, "wait");
282 printf("%s: success (%d child%s)\n",
283 tests[i].name, n_children, n_children != 1 ? "ren" : "");
287 ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
288 argv[1], program_name);