sleep/usleep: switch to trivial implementations.
Upstream keeps rearranging the deckchairs for these, so let's just
switch to the [roughly] one-liners rather than track that...
Test: treehugger
Change-Id: If655cf7a7f316657de44d41fadd43a8c55ee6f23
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
index 1ae174a..6db7751 100644
--- a/tests/signal_test.cpp
+++ b/tests/signal_test.cpp
@@ -20,6 +20,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include <chrono>
#include <thread>
#include <android-base/macros.h>
@@ -27,6 +28,8 @@
#include "SignalUtils.h"
+using namespace std::chrono_literals;
+
static int SIGNAL_MIN() {
return 1; // Signals start at 1 (SIGHUP), not 0.
}
@@ -753,12 +756,6 @@
ASSERT_EQ(0, errno);
}
-static int64_t NanoTime() {
- timespec t;
- clock_gettime(CLOCK_MONOTONIC, &t);
- return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
-}
-
TEST(signal, sigtimedwait_timeout) {
// Block SIGALRM.
sigset_t just_SIGALRM;
@@ -768,13 +765,14 @@
ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
// Wait timeout.
- int64_t start_time = NanoTime();
+ auto t0 = std::chrono::steady_clock::now();
siginfo_t info;
timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
errno = 0;
ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
ASSERT_EQ(EAGAIN, errno);
- ASSERT_GE(NanoTime() - start_time, 1000000);
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 1000000ns);
ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr));
}
diff --git a/tests/system_properties_test2.cpp b/tests/system_properties_test2.cpp
index c061faa..b9936dd 100644
--- a/tests/system_properties_test2.cpp
+++ b/tests/system_properties_test2.cpp
@@ -19,16 +19,16 @@
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
+
+#include <chrono>
#include <sstream>
#include <string>
#if defined(__BIONIC__)
#include <sys/system_properties.h>
-
-static uint64_t NanoTime() {
- timespec now;
- clock_gettime(CLOCK_MONOTONIC, &now);
- return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
+int64_t NanoTime() {
+ auto t = std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now());
+ return t.time_since_epoch().count();
}
#endif
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index 4e17242..378b4ac 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -24,13 +24,17 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
+
#include <atomic>
+#include <chrono>
#include "SignalUtils.h"
#include "utils.h"
#include "private/bionic_constants.h"
+using namespace std::chrono_literals;
+
TEST(time, time) {
// Acquire time
time_t p1, t1 = time(&p1);
@@ -797,7 +801,7 @@
ASSERT_EQ(EINVAL, errno);
}
-TEST(time, clock_nanosleep) {
+TEST(time, clock_nanosleep_EINVAL) {
timespec in;
timespec out;
ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
@@ -810,6 +814,29 @@
ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
}
+TEST(time, clock_nanosleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ const timespec ts = {.tv_nsec = 5000000};
+ ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 5000000ns);
+}
+
+TEST(time, nanosleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ const timespec ts = {.tv_nsec = 5000000};
+ ASSERT_EQ(0, nanosleep(&ts, nullptr));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 5000000ns);
+}
+
+TEST(time, nanosleep_EINVAL) {
+ timespec ts = {.tv_sec = -1};
+ errno = 0;
+ ASSERT_EQ(-1, nanosleep(&ts, nullptr));
+ ASSERT_EQ(EINVAL, errno);
+}
+
TEST(time, bug_31938693) {
// User-visible symptoms in N:
// http://b/31938693
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 10c1710..99d92e6 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -33,6 +33,8 @@
#include <sys/wait.h>
#include <unistd.h>
+#include <chrono>
+
#include <android-base/file.h>
#include <android-base/strings.h>
@@ -46,6 +48,8 @@
#define UNISTD_DEATHTEST unistd_DeathTest
#endif
+using namespace std::chrono_literals;
+
static void* get_brk() {
return sbrk(0);
}
@@ -1516,3 +1520,17 @@
swab("hello", buf, -1);
ASSERT_EQ('x', buf[0]);
}
+
+TEST(UNISTD_TEST, usleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ ASSERT_EQ(0, usleep(5000));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 5000us);
+}
+
+TEST(UNISTD_TEST, sleep) {
+ auto t0 = std::chrono::steady_clock::now();
+ ASSERT_EQ(0U, sleep(1));
+ auto t1 = std::chrono::steady_clock::now();
+ ASSERT_GE(t1-t0, 1s);
+}
diff --git a/tests/utils.h b/tests/utils.h
index fe41019..1f4cece 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef __TEST_UTILS_H
-#define __TEST_UTILS_H
+#pragma once
#include <dlfcn.h>
#include <fcntl.h>
@@ -254,5 +253,3 @@
std::string output_;
};
#endif
-
-#endif