tests: Fix ovsdb tests.
[sliver-openvswitch.git] / tests / test-timeval.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
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>
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include "daemon.h"
28 #include "util.h"
29
30 #undef NDEBUG
31 #include <assert.h>
32
33 static long long int
34 gettimeofday_in_msec(void)
35 {
36     struct timeval tv;
37
38     assert(!gettimeofday(&tv, NULL));
39     return timeval_to_msec(&tv);
40 }
41
42 static void
43 do_test(void)
44 {
45     /* Wait until we are awakened by a signal (typically EINTR due to the
46      * setitimer()).  Then ensure that, if time has really advanced by
47      * TIME_UPDATE_INTERVAL, then time_msec() reports that it advanced.
48      */
49     long long int start_time_msec;
50     long long int start_gtod;
51
52     start_time_msec = time_msec();
53     start_gtod = gettimeofday_in_msec();
54     for (;;) {
55         /* Wait up to 1 second.  Using select() to do the timeout avoids
56          * interfering with the interval timer. */
57         struct timeval timeout;
58         int retval;
59
60         timeout.tv_sec = 1;
61         timeout.tv_usec = 0;
62         retval = select(0, NULL, NULL, NULL, &timeout);
63         if (retval != -1) {
64             ovs_fatal(0, "select returned %d", retval);
65         } else if (errno != EINTR) {
66             ovs_fatal(errno, "select reported unexpected error");
67         }
68
69         if (gettimeofday_in_msec() - start_gtod >= TIME_UPDATE_INTERVAL) {
70             assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL);
71             break;
72         }
73     }
74 }
75
76 static void
77 usage(void)
78 {
79     ovs_fatal(0, "usage: %s TEST, where TEST is \"plain\" or \"daemon\"",
80               program_name);
81 }
82
83 int
84 main(int argc, char *argv[])
85 {
86     set_program_name(argv[0]);
87     time_init();
88
89     if (argc != 2) {
90         usage();
91     } else if (!strcmp(argv[1], "plain")) {
92         do_test();
93     } else if (!strcmp(argv[1], "daemon")) {
94         /* Test that time still advances even in a daemon.  This is an
95          * interesting test because fork() cancels the interval timer. */
96         char cwd[1024];
97         FILE *success;
98
99         assert(getcwd(cwd, sizeof cwd) == cwd);
100
101         unlink("test-timeval.success");
102
103         /* Daemonize, with a pidfile in the current directory. */
104         set_detach();
105         set_pidfile(xasprintf("%s/test-timeval.pid", cwd));
106         set_no_chdir();
107         daemonize();
108
109         /* Run the test. */
110         do_test();
111
112         /* Report success by writing out a file, since the ultimate invoker of
113          * test-timeval can't wait on the daemonized process. */
114         success = fopen("test-timeval.success", "w");
115         if (!success) {
116             ovs_fatal(errno, "test-timeval.success: create failed");
117         }
118         fprintf(success, "success\n");
119         fclose(success);
120     } else {
121         usage();
122     }
123
124     return 0;
125 }