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.
25 #include <sys/types.h>
28 #include "command-line.h"
36 gettimeofday_in_msec(void)
40 assert(!gettimeofday(&tv, NULL));
41 return timeval_to_msec(&tv);
47 /* Wait until we are awakened by a signal (typically EINTR due to the
48 * setitimer()). Then ensure that, if time has really advanced by
49 * TIME_UPDATE_INTERVAL, then time_msec() reports that it advanced.
51 long long int start_time_msec, start_time_wall;
52 long long int start_gtod;
54 start_time_msec = time_msec();
55 start_time_wall = time_wall_msec();
56 start_gtod = gettimeofday_in_msec();
58 /* Wait up to 1 second. Using select() to do the timeout avoids
59 * interfering with the interval timer. */
60 struct timeval timeout;
65 retval = select(0, NULL, NULL, NULL, &timeout);
67 ovs_fatal(0, "select returned %d", retval);
68 } else if (errno != EINTR) {
69 ovs_fatal(errno, "select reported unexpected error");
72 if (gettimeofday_in_msec() - start_gtod >= TIME_UPDATE_INTERVAL) {
73 assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL);
74 assert(time_wall_msec() - start_time_wall >= TIME_UPDATE_INTERVAL);
83 ovs_fatal(0, "usage: %s TEST, where TEST is \"plain\" or \"daemon\"",
88 main(int argc, char *argv[])
90 proctitle_init(argc, argv);
91 set_program_name(argv[0]);
96 } else if (!strcmp(argv[1], "plain")) {
98 } else if (!strcmp(argv[1], "daemon")) {
99 /* Test that time still advances even in a daemon. This is an
100 * interesting test because fork() cancels the interval timer. */
101 char cwd[1024], *pidfile;
104 assert(getcwd(cwd, sizeof cwd) == cwd);
106 unlink("test-timeval.success");
108 /* Daemonize, with a pidfile in the current directory. */
110 pidfile = xasprintf("%s/test-timeval.pid", cwd);
111 set_pidfile(pidfile);
119 /* Report success by writing out a file, since the ultimate invoker of
120 * test-timeval can't wait on the daemonized process. */
121 success = fopen("test-timeval.success", "w");
123 ovs_fatal(errno, "test-timeval.success: create failed");
125 fprintf(success, "success\n");