--- /dev/null
+/*
+ * Copyright (c) 2009 Nicira Networks.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include "timeval.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "daemon.h"
+#include "util.h"
+
+#undef NDEBUG
+#include <assert.h>
+
+static long long int
+gettimeofday_in_msec(void)
+{
+ struct timeval tv;
+
+ assert(!gettimeofday(&tv, NULL));
+ return timeval_to_msec(&tv);
+}
+
+static void
+do_test(void)
+{
+ /* Wait until we are awakened by a signal (typically EINTR due to the
+ * setitimer()). Then ensure that, if time has really advanced by
+ * TIME_UPDATE_INTERVAL, then time_msec() reports that it advanced.
+ */
+ long long int start_time_msec;
+ long long int start_gtod;
+
+ start_time_msec = time_msec();
+ start_gtod = gettimeofday_in_msec();
+ for (;;) {
+ /* Wait up to 1 second. Using select() to do the timeout avoids
+ * interfering with the interval timer. */
+ struct timeval timeout;
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+ assert(select(0, NULL, NULL, NULL, &timeout) == -1 && errno == EINTR);
+
+ if (gettimeofday_in_msec() - start_gtod >= TIME_UPDATE_INTERVAL) {
+ assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL);
+ break;
+ }
+ }
+}
+
+static void
+usage(void)
+{
+ ovs_fatal(0, "usage: %s TEST, where TEST is \"plain\" or \"daemon\"",
+ program_name);
+}
+
+int
+main(int argc, char *argv[])
+{
+ set_program_name(argv[0]);
+ time_init();
+
+ if (argc != 2) {
+ usage();
+ } else if (!strcmp(argv[1], "plain")) {
+ do_test();
+ } else if (!strcmp(argv[1], "daemon")) {
+ /* Test that time still advances even in a daemon. This is an
+ * interesting test because fork() cancels the interval timer. */
+ char cwd[1024];
+ FILE *success;
+
+ assert(getcwd(cwd, sizeof cwd) == cwd);
+
+ unlink("test-timeval.success");
+
+ /* Daemonize, with a pidfile in the current directory. */
+ set_detach();
+ set_pidfile(xasprintf("%s/test-timeval.pid", cwd));
+ set_no_chdir();
+ daemonize();
+
+ /* Run the test. */
+ do_test();
+
+ /* Report success by writing out a file, since the ultimate invoker of
+ * test-timeval can't wait on the daemonized process. */
+ success = fopen("test-timeval.success", "w");
+ if (!success) {
+ ovs_fatal(errno, "test-timeval.success: create failed");
+ }
+ fprintf(success, "success\n");
+ fclose(success);
+ } else {
+ usage();
+ }
+
+ return 0;
+}
--- /dev/null
+AT_BANNER([timeval unit tests])
+
+AT_SETUP([check that time advances])
+AT_KEYWORDS([timeval])
+OVS_CHECK_LCOV([test-timeval plain], [0])
+AT_CLEANUP
+
+AT_SETUP([check that time advances after daemonize()])
+AT_KEYWORDS([timeval])
+OVS_CHECK_LCOV([test-timeval daemon], [0])
+AT_CHECK(
+ [# First try a quick sleep, so that the test completes very quickly
+ # in the normal case. POSIX doesn't require fractional times to
+ # work, so this might not work.
+ sleep 0.1; if test -e test-timeval.success; then echo success; exit 0; fi
+ # Then wait up to 2 seconds.
+ sleep 1; if test -e test-timeval.success; then echo success; exit 0; fi
+ sleep 1; if test -e test-timeval.success; then echo success; exit 0; fi
+ echo failure; exit 1],
+ [0], [success
+], [])
+AT_CLEANUP