Merge "master" branch into "db".
[sliver-openvswitch.git] / tests / test-timeval.c
1 /*
2  * Copyright (c) 2009 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         timeout.tv_sec = 1;
59         timeout.tv_usec = 0;
60         assert(select(0, NULL, NULL, NULL, &timeout) == -1 && errno == EINTR);
61
62         if (gettimeofday_in_msec() - start_gtod >= TIME_UPDATE_INTERVAL) {
63             assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL);
64             break;
65         }
66     }
67 }
68
69 static void
70 usage(void)
71 {
72     ovs_fatal(0, "usage: %s TEST, where TEST is \"plain\" or \"daemon\"",
73               program_name);
74 }
75
76 int
77 main(int argc, char *argv[])
78 {
79     set_program_name(argv[0]);
80     time_init();
81
82     if (argc != 2) {
83         usage();
84     } else if (!strcmp(argv[1], "plain")) {
85         do_test();
86     } else if (!strcmp(argv[1], "daemon")) {
87         /* Test that time still advances even in a daemon.  This is an
88          * interesting test because fork() cancels the interval timer. */
89         char cwd[1024];
90         FILE *success;
91
92         assert(getcwd(cwd, sizeof cwd) == cwd);
93
94         unlink("test-timeval.success");
95
96         /* Daemonize, with a pidfile in the current directory. */
97         set_detach();
98         set_pidfile(xasprintf("%s/test-timeval.pid", cwd));
99         set_no_chdir();
100         daemonize();
101
102         /* Run the test. */
103         do_test();
104
105         /* Report success by writing out a file, since the ultimate invoker of
106          * test-timeval can't wait on the daemonized process. */
107         success = fopen("test-timeval.success", "w");
108         if (!success) {
109             ovs_fatal(errno, "test-timeval.success: create failed");
110         }
111         fprintf(success, "success\n");
112         fclose(success);
113     } else {
114         usage();
115     }
116
117     return 0;
118 }