From 91194788302305ffe4c32547f74c658c26b8aaed Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 24 May 2019 12:57:34 -0700 Subject: Extract SleepSafe from test_util Allows socket tests that rely on test_util to compile on Fuchsia. PiperOrigin-RevId: 249884084 Change-Id: I17617e3f1baaba4c85c689f40db4a42a8de1597e --- test/util/BUILD | 10 ++++++++++ test/util/test_util.cc | 16 ---------------- test/util/test_util.h | 3 --- test/util/time_util.cc | 41 +++++++++++++++++++++++++++++++++++++++++ test/util/time_util.h | 29 +++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 test/util/time_util.cc create mode 100644 test/util/time_util.h (limited to 'test/util') diff --git a/test/util/BUILD b/test/util/BUILD index a4b47de56..a1b9ff526 100644 --- a/test/util/BUILD +++ b/test/util/BUILD @@ -237,6 +237,16 @@ cc_library( deps = [":logging"], ) +cc_library( + name = "time_util", + testonly = 1, + srcs = ["time_util.cc"], + hdrs = ["time_util.h"], + deps = [ + "@com_google_absl//absl/time", + ], +) + cc_library( name = "timer_util", testonly = 1, diff --git a/test/util/test_util.cc b/test/util/test_util.cc index c52fd9a4a..bf0029951 100644 --- a/test/util/test_util.cc +++ b/test/util/test_util.cc @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -211,21 +210,6 @@ std::vector> GenerateIovecs(uint64_t total_size, return result; } -void SleepSafe(absl::Duration duration) { - if (duration == absl::ZeroDuration()) { - return; - } - - struct timespec ts = absl::ToTimespec(duration); - int ret; - while (1) { - ret = syscall(__NR_nanosleep, &ts, &ts); - if (ret == 0 || (ret <= 0 && errno != EINTR)) { - break; - } - } -} - uint64_t Megabytes(uint64_t n) { // Overflow check, upper 20 bits in n shouldn't be set. TEST_CHECK(!(0xfffff00000000000 & n)); diff --git a/test/util/test_util.h b/test/util/test_util.h index 8f5eb5089..cdbe8bfd1 100644 --- a/test/util/test_util.h +++ b/test/util/test_util.h @@ -738,9 +738,6 @@ inline PosixErrorOr Atof(absl::string_view str) { std::vector> GenerateIovecs(uint64_t total_size, void* buf, size_t buflen); -// Sleep for at least the specified duration. Avoids glibc. -void SleepSafe(absl::Duration duration); - // Returns bytes in 'n' megabytes. Used for readability. uint64_t Megabytes(uint64_t n); diff --git a/test/util/time_util.cc b/test/util/time_util.cc new file mode 100644 index 000000000..1ddfbfc9c --- /dev/null +++ b/test/util/time_util.cc @@ -0,0 +1,41 @@ +// Copyright 2019 The gVisor Authors. +// +// 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 "test/util/time_util.h" + +#include +#include + +#include "absl/time/time.h" + +namespace gvisor { +namespace testing { + +void SleepSafe(absl::Duration duration) { + if (duration == absl::ZeroDuration()) { + return; + } + + struct timespec ts = absl::ToTimespec(duration); + int ret; + while (1) { + ret = syscall(__NR_nanosleep, &ts, &ts); + if (ret == 0 || (ret <= 0 && errno != EINTR)) { + break; + } + } +} + +} // namespace testing +} // namespace gvisor diff --git a/test/util/time_util.h b/test/util/time_util.h new file mode 100644 index 000000000..f3ddc9fde --- /dev/null +++ b/test/util/time_util.h @@ -0,0 +1,29 @@ +// Copyright 2019 The gVisor Authors. +// +// 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. + +#ifndef GVISOR_TEST_UTIL_TIME_UTIL_H_ +#define GVISOR_TEST_UTIL_TIME_UTIL_H_ + +#include "absl/time/time.h" + +namespace gvisor { +namespace testing { + +// Sleep for at least the specified duration. Avoids glibc. +void SleepSafe(absl::Duration duration); + +} // namespace testing +} // namespace gvisor + +#endif // GVISOR_TEST_UTIL_TIME_UTIL_H_ -- cgit v1.2.3