2 * Copyright (c) 2009, 2010 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 /* Making this static prevents a memory leak warning from valgrind for the
104 * parent process, which cannot easily unlock (and free) 'lockfile' because
105 * it can only do so after the child has exited, and it's the caller of
106 * this function that does the wait() call. */
107 static struct lockfile
*lockfile
;
109 assert(lockfile_lock("file", 0, &lockfile
) == 0);
110 if (do_fork() == CHILD
) {
111 lockfile_unlock(lockfile
);
112 assert(lockfile_lock("file", 0, &lockfile
) == EAGAIN
);
118 run_lock_twice_blocks_other_process(void)
120 struct lockfile
*lockfile
, *dummy
;
122 assert(lockfile_lock("file", 0, &lockfile
) == 0);
123 assert(lockfile_lock("file", 0, &dummy
) == EDEADLK
);
124 if (do_fork() == CHILD
) {
125 assert(lockfile_lock("file", 0, &dummy
) == EAGAIN
);
131 run_lock_and_unlock_allows_other_process(void)
133 struct lockfile
*lockfile
;
135 assert(lockfile_lock("file", 0, &lockfile
) == 0);
136 lockfile_unlock(lockfile
);
138 if (do_fork() == CHILD
) {
139 assert(lockfile_lock("file", 0, &lockfile
) == 0);
145 run_lock_timeout_gets_the_lock(void)
147 struct lockfile
*lockfile
;
149 assert(lockfile_lock("file", 0, &lockfile
) == 0);
151 if (do_fork() == CHILD
) {
152 lockfile_unlock(lockfile
);
153 assert(lockfile_lock("file", TIME_UPDATE_INTERVAL
* 3,
157 long long int now
= time_msec();
158 while (time_msec() < now
+ TIME_UPDATE_INTERVAL
) {
161 lockfile_unlock(lockfile
);
166 run_lock_timeout_runs_out(void)
168 struct lockfile
*lockfile
;
170 assert(lockfile_lock("file", 0, &lockfile
) == 0);
172 if (do_fork() == CHILD
) {
173 lockfile_unlock(lockfile
);
174 assert(lockfile_lock("file", TIME_UPDATE_INTERVAL
,
175 &lockfile
) == ETIMEDOUT
);
178 long long int now
= time_msec();
179 while (time_msec() < now
+ TIME_UPDATE_INTERVAL
* 3) {
182 lockfile_unlock(lockfile
);
187 run_lock_multiple(void)
189 struct lockfile
*a
, *b
, *c
, *dummy
;
191 assert(lockfile_lock("a", 0, &a
) == 0);
192 assert(lockfile_lock("b", 0, &b
) == 0);
193 assert(lockfile_lock("c", 0, &c
) == 0);
196 assert(lockfile_lock("a", 0, &a
) == 0);
197 assert(lockfile_lock("a", 0, &dummy
) == EDEADLK
);
201 assert(lockfile_lock("a", 0, &a
) == 0);
212 printf("usage: %s TESTNAME\n"
213 "where TESTNAME is one of the following:\n",
215 for (i
= 0; tests
[i
].name
; i
++) {
216 fprintf(stderr
, "\t%s\n", tests
[i
].name
);
220 static const struct test tests
[] = {
221 #define TEST(NAME) { #NAME, run_##NAME }
222 TEST(lock_and_unlock
),
223 TEST(lock_and_unlock_twice
),
224 TEST(lock_blocks_same_process
),
225 TEST(lock_blocks_same_process_twice
),
226 TEST(lock_blocks_other_process
),
227 TEST(lock_twice_blocks_other_process
),
228 TEST(lock_and_unlock_allows_other_process
),
229 TEST(lock_timeout_gets_the_lock
),
230 TEST(lock_timeout_runs_out
),
238 main(int argc
, char *argv
[])
242 set_program_name(argv
[0]);
246 ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
251 for (i
= 0; tests
[i
].name
; i
++) {
252 if (!strcmp(argv
[1], tests
[i
].name
)) {
256 (tests
[i
].function
)();
259 while (wait(&status
) > 0) {
260 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 11) {
263 ovs_fatal(0, "child exited in unexpected way: %s",
264 process_status_msg(status
));
267 if (errno
!= ECHILD
) {
268 ovs_fatal(errno
, "wait");
271 printf("%s: success (%d child%s)\n",
272 tests
[i
].name
, n_children
, n_children
!= 1 ? "ren" : "");
276 ovs_fatal(0, "unknown test \"%s\"; use \"%s help\" for help",
277 argv
[1], program_name
);