]> git.proxmox.com Git - ovs.git/blame - tests/test-timeval.c
Global replace of Nicira Networks.
[ovs.git] / tests / test-timeval.c
CommitLineData
e7cfedd6 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011 Nicira, Inc.
e7cfedd6
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18
19#include "timeval.h"
20
21#include <errno.h>
22#include <stdio.h>
93ff0290 23#include <stdlib.h>
e7cfedd6
BP
24#include <sys/time.h>
25#include <sys/types.h>
26#include <unistd.h>
27
40f0707c 28#include "command-line.h"
e7cfedd6
BP
29#include "daemon.h"
30#include "util.h"
31
32#undef NDEBUG
33#include <assert.h>
34
35static long long int
36gettimeofday_in_msec(void)
37{
38 struct timeval tv;
39
279c9e03 40 xgettimeofday(&tv);
e7cfedd6
BP
41 return timeval_to_msec(&tv);
42}
43
44static void
45do_test(void)
46{
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.
50 */
c73814a3 51 long long int start_time_msec, start_time_wall;
e7cfedd6
BP
52 long long int start_gtod;
53
54 start_time_msec = time_msec();
c73814a3 55 start_time_wall = time_wall_msec();
e7cfedd6
BP
56 start_gtod = gettimeofday_in_msec();
57 for (;;) {
58 /* Wait up to 1 second. Using select() to do the timeout avoids
59 * interfering with the interval timer. */
60 struct timeval timeout;
4a8535f3
BP
61 int retval;
62
e7cfedd6
BP
63 timeout.tv_sec = 1;
64 timeout.tv_usec = 0;
4a8535f3
BP
65 retval = select(0, NULL, NULL, NULL, &timeout);
66 if (retval != -1) {
67 ovs_fatal(0, "select returned %d", retval);
68 } else if (errno != EINTR) {
69 ovs_fatal(errno, "select reported unexpected error");
70 }
e7cfedd6
BP
71
72 if (gettimeofday_in_msec() - start_gtod >= TIME_UPDATE_INTERVAL) {
02029a69
JG
73 /* gettimeofday() and time_msec() have different granularities in
74 * their time sources. Depending on the rounding used this could
75 * result in a slight difference, so we allow for 1 ms of slop. */
76 assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL - 1);
77 assert(time_wall_msec() - start_time_wall >=
78 TIME_UPDATE_INTERVAL - 1);
e7cfedd6
BP
79 break;
80 }
81 }
82}
83
84static void
85usage(void)
86{
87 ovs_fatal(0, "usage: %s TEST, where TEST is \"plain\" or \"daemon\"",
88 program_name);
89}
90
91int
92main(int argc, char *argv[])
93{
40f0707c 94 proctitle_init(argc, argv);
e7cfedd6 95 set_program_name(argv[0]);
e7cfedd6
BP
96
97 if (argc != 2) {
98 usage();
99 } else if (!strcmp(argv[1], "plain")) {
100 do_test();
101 } else if (!strcmp(argv[1], "daemon")) {
102 /* Test that time still advances even in a daemon. This is an
103 * interesting test because fork() cancels the interval timer. */
93ff0290 104 char cwd[1024], *pidfile;
e7cfedd6
BP
105 FILE *success;
106
107 assert(getcwd(cwd, sizeof cwd) == cwd);
108
109 unlink("test-timeval.success");
110
111 /* Daemonize, with a pidfile in the current directory. */
112 set_detach();
93ff0290
BP
113 pidfile = xasprintf("%s/test-timeval.pid", cwd);
114 set_pidfile(pidfile);
115 free(pidfile);
e7cfedd6
BP
116 set_no_chdir();
117 daemonize();
118
119 /* Run the test. */
120 do_test();
121
122 /* Report success by writing out a file, since the ultimate invoker of
123 * test-timeval can't wait on the daemonized process. */
124 success = fopen("test-timeval.success", "w");
125 if (!success) {
126 ovs_fatal(errno, "test-timeval.success: create failed");
127 }
128 fprintf(success, "success\n");
129 fclose(success);
130 } else {
131 usage();
132 }
133
134 return 0;
135}