summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndreas Gampe <agampe@google.com>2018-03-20 09:40:12 -0700
committerAndreas Gampe <agampe@google.com>2018-03-20 18:07:59 +0000
commite4944b10a24c39d9aaba7e1093d91438ddf90d09 (patch)
tree6f7d281652c4d01350647eda3e179883fba6973c /tests
parentacf9c47fa44c858005809ee867ddc5e85be80e76 (diff)
downloadextras-e4944b10a24c39d9aaba7e1093d91438ddf90d09.tar.gz
Timetest: Clean up
Clean up the code a little. The removed TEMP_FAILURE_RETRY is actually wrong. The others are either superfluous or needed. Be more direct with error returns. For EBUSY, loop at most ten times, with sleep. Use unique_fd for descriptor tracking/close. Bug: 76008092 Test: atest time-unit-tests Change-Id: Id7176e0237a416a4bb19d72c47b8e1a2f6b57ca5
Diffstat (limited to 'tests')
-rw-r--r--tests/timetest/Android.bp17
-rw-r--r--tests/timetest/Android.mk23
-rw-r--r--tests/timetest/rtc_test.cpp46
3 files changed, 41 insertions, 45 deletions
diff --git a/tests/timetest/Android.bp b/tests/timetest/Android.bp
new file mode 100644
index 00000000..998faeff
--- /dev/null
+++ b/tests/timetest/Android.bp
@@ -0,0 +1,17 @@
+// Copyright 2006 The Android Open Source Project
+
+cc_test {
+ name: "time-unit-tests",
+ cflags: [
+ "-fstack-protector-all",
+ "-g",
+ "-Wextra",
+ "-fno-builtin",
+ ],
+ srcs: [
+ "rtc_test.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ ],
+}
diff --git a/tests/timetest/Android.mk b/tests/timetest/Android.mk
index af3447fd..be311fa4 100644
--- a/tests/timetest/Android.mk
+++ b/tests/timetest/Android.mk
@@ -10,26 +10,3 @@ LOCAL_MODULE_TAGS := tests
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_STATIC_LIBRARIES := libc
include $(BUILD_NATIVE_TEST)
-
-# -----------------------------------------------------------------------------
-# Unit tests.
-# -----------------------------------------------------------------------------
-
-test_c_flags := \
- -fstack-protector-all \
- -g \
- -Wall -Wextra \
- -Werror \
- -fno-builtin \
-
-test_src_files := \
- rtc_test.cpp
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := time-unit-tests
-LOCAL_COMPATIBILITY_SUITE := device-tests
-LOCAL_MODULE_TAGS := tests
-LOCAL_CFLAGS += $(test_c_flags)
-LOCAL_SRC_FILES := $(test_src_files)
-include $(BUILD_NATIVE_TEST)
-
diff --git a/tests/timetest/rtc_test.cpp b/tests/timetest/rtc_test.cpp
index 26ca13ad..e7874b1d 100644
--- a/tests/timetest/rtc_test.cpp
+++ b/tests/timetest/rtc_test.cpp
@@ -24,49 +24,51 @@
#include <gtest/gtest.h>
+#include <android-base/unique_fd.h>
+
static int hwtime(int flag, int request, struct rtc_time *tm) {
static const char rtc[] = "/dev/rtc0";
- int ret = TEMP_FAILURE_RETRY(access(rtc, flag & O_WRONLY) ? W_OK : R_OK);
+ int ret = access(rtc, flag & O_WRONLY);
if (ret < 0) {
- ret = -errno;
- }
- if (ret == -EACCES) {
- return ret;
+ return -errno;
}
if (flag & O_WRONLY) {
struct stat st;
ret = TEMP_FAILURE_RETRY(stat(rtc, &st));
if (ret < 0) {
- ret = -errno;
+ return -errno;
} else if (!(st.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))) {
- ret = -EACCES;
+ return -EACCES;
}
}
- if (ret == -EACCES) {
- return ret;
- }
- do {
+ for (int count = 0; count < 10; count++) {
ret = TEMP_FAILURE_RETRY(open(rtc, flag));
if (ret < 0) {
- ret = -errno;
+ if (errno == EBUSY) {
+ sleep(1);
+ continue;
+ }
+ return -errno;
}
- } while (ret == -EBUSY);
- if (ret < 0) {
- return ret;
+ break;
}
+ android::base::unique_fd fd(ret);
- int fd = ret;
- do {
- ret = TEMP_FAILURE_RETRY(ioctl(fd, request, tm));
+ for (int count = 0; count < 10; count++) {
+ ret = TEMP_FAILURE_RETRY(ioctl(fd.get(), request, tm));
if (ret < 0) {
- ret = -errno;
+ if (errno == EBUSY) {
+ sleep(1);
+ continue;
+ }
+ return -errno;
}
- } while (ret == -EBUSY);
- close(fd);
- return ret;
+ return ret;
+ };
+ return -EBUSY;
}
static int rd_hwtime(struct rtc_time *tm) {