Extract SleepSafe from test_util

Allows socket tests that rely on test_util to compile on Fuchsia.

PiperOrigin-RevId: 249884084
Change-Id: I17617e3f1baaba4c85c689f40db4a42a8de1597e
This commit is contained in:
Tamir Duberstein
2019-05-24 12:58:46 -07:00
committed by Shentubot
parent e4b395db49
commit 9119478830
13 changed files with 94 additions and 19 deletions
+7
View File
@@ -638,6 +638,7 @@ cc_binary(
"//test/util:file_descriptor",
"//test/util:test_main",
"//test/util:test_util",
"//test/util:time_util",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
],
@@ -815,6 +816,7 @@ cc_binary(
"//test/util:test_main",
"//test/util:test_util",
"//test/util:thread_util",
"//test/util:time_util",
"//test/util:timer_util",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/time",
@@ -1410,6 +1412,7 @@ cc_binary(
"//test/util:temp_path",
"//test/util:test_util",
"//test/util:thread_util",
"//test/util:time_util",
"//test/util:timer_util",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
@@ -1470,6 +1473,7 @@ cc_binary(
"//test/util:save_util",
"//test/util:test_main",
"//test/util:test_util",
"//test/util:time_util",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
],
@@ -1501,6 +1505,7 @@ cc_binary(
"//test/util:signal_util",
"//test/util:test_util",
"//test/util:thread_util",
"//test/util:time_util",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
],
@@ -3161,6 +3166,7 @@ cc_binary(
"//test/util:logging",
"//test/util:multiprocess_util",
"//test/util:test_util",
"//test/util:time_util",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
],
@@ -3180,6 +3186,7 @@ cc_binary(
"//test/util:test_main",
"//test/util:test_util",
"//test/util:thread_util",
"//test/util:time_util",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
+1
View File
@@ -19,6 +19,7 @@
#include "absl/time/time.h"
#include "test/util/file_descriptor.h"
#include "test/util/test_util.h"
#include "test/util/time_util.h"
namespace gvisor {
namespace testing {
+1
View File
@@ -36,6 +36,7 @@
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
#include "test/util/time_util.h"
#include "test/util/timer_util.h"
namespace gvisor {
+1
View File
@@ -62,6 +62,7 @@
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
#include "test/util/time_util.h"
#include "test/util/timer_util.h"
// NOTE(magi): No, this isn't really a syscall but this is a really simple
@@ -37,6 +37,7 @@
#include "test/util/posix_error.h"
#include "test/util/save_util.h"
#include "test/util/test_util.h"
#include "test/util/time_util.h"
namespace gvisor {
namespace testing {
+1
View File
@@ -34,6 +34,7 @@
#include "test/util/signal_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
#include "test/util/time_util.h"
DEFINE_bool(ptrace_test_execve_child, false,
"If true, run the "
+1
View File
@@ -26,6 +26,7 @@
#include "test/util/logging.h"
#include "test/util/multiprocess_util.h"
#include "test/util/test_util.h"
#include "test/util/time_util.h"
DEFINE_bool(vfork_test_child, false,
"If true, run the VforkTest child workload.");
+1
View File
@@ -37,6 +37,7 @@
#include "test/util/signal_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
#include "test/util/time_util.h"
using ::testing::UnorderedElementsAre;
+10
View File
@@ -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,
-16
View File
@@ -18,7 +18,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/utsname.h>
@@ -211,21 +210,6 @@ std::vector<std::vector<struct iovec>> 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));
-3
View File
@@ -738,9 +738,6 @@ inline PosixErrorOr<float> Atof(absl::string_view str) {
std::vector<std::vector<struct iovec>> 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);
+41
View File
@@ -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 <sys/syscall.h>
#include <unistd.h>
#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
+29
View File
@@ -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_