Commit 8adbabc6 authored by zyearn's avatar zyearn

make test compiled in macos

parent 14acce29
cmake_minimum_required(VERSION 2.8.10) cmake_minimum_required(VERSION 2.8.10)
project(brpc C CXX) project(brpc C CXX)
# Enable MACOSX_RPATH. Run "cmake --help-policy CMP0042" for policy details.
cmake_policy(SET CMP0042 NEW)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# require at least gcc 4.8 # require at least gcc 4.8
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8) if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
...@@ -123,6 +126,7 @@ set(DYNAMIC_LIB ...@@ -123,6 +126,7 @@ set(DYNAMIC_LIB
${LEVELDB_LIB} ${LEVELDB_LIB}
${PROTOC_LIB} ${PROTOC_LIB}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
pthread
rt rt
ssl ssl
crypto crypto
......
...@@ -385,7 +385,17 @@ int pthread_fd_wait(int fd, unsigned epoll_events, ...@@ -385,7 +385,17 @@ int pthread_fd_wait(int fd, unsigned epoll_events,
int diff_ms = -1; int diff_ms = -1;
if (abstime) { if (abstime) {
timespec now; timespec now;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
now.tv_sec = mts.tv_sec;
now.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, &now); clock_gettime(CLOCK_REALTIME, &now);
#endif
int64_t now_us = butil::timespec_to_microseconds(now); int64_t now_us = butil::timespec_to_microseconds(now);
int64_t abstime_us = butil::timespec_to_microseconds(*abstime); int64_t abstime_us = butil::timespec_to_microseconds(*abstime);
if (abstime_us <= now_us) { if (abstime_us <= now_us) {
......
...@@ -127,6 +127,10 @@ public: ...@@ -127,6 +127,10 @@ public:
// Routine of the main task which should be called from a dedicated pthread. // Routine of the main task which should be called from a dedicated pthread.
void run_main_task(); void run_main_task();
// current_task is a function in macOS 10.0+
#ifdef current_task
#undef current_task
#endif
// Meta/Identifier of current task in this group. // Meta/Identifier of current task in this group.
TaskMeta* current_task() const { return _cur_meta; } TaskMeta* current_task() const { return _cur_meta; }
bthread_t current_tid() const { return _cur_meta->tid; } bthread_t current_tid() const { return _cur_meta->tid; }
......
...@@ -53,7 +53,6 @@ SET(TEST_BUTIL_SOURCES ...@@ -53,7 +53,6 @@ SET(TEST_BUTIL_SOURCES
${CMAKE_SOURCE_DIR}/test/cpu_unittest.cc ${CMAKE_SOURCE_DIR}/test/cpu_unittest.cc
${CMAKE_SOURCE_DIR}/test/crash_logging_unittest.cc ${CMAKE_SOURCE_DIR}/test/crash_logging_unittest.cc
${CMAKE_SOURCE_DIR}/test/leak_tracker_unittest.cc ${CMAKE_SOURCE_DIR}/test/leak_tracker_unittest.cc
${CMAKE_SOURCE_DIR}/test/proc_maps_linux_unittest.cc
${CMAKE_SOURCE_DIR}/test/stack_trace_unittest.cc ${CMAKE_SOURCE_DIR}/test/stack_trace_unittest.cc
${CMAKE_SOURCE_DIR}/test/environment_unittest.cc ${CMAKE_SOURCE_DIR}/test/environment_unittest.cc
${CMAKE_SOURCE_DIR}/test/file_util_unittest.cc ${CMAKE_SOURCE_DIR}/test/file_util_unittest.cc
...@@ -134,11 +133,16 @@ SET(TEST_BUTIL_SOURCES ...@@ -134,11 +133,16 @@ SET(TEST_BUTIL_SOURCES
${CMAKE_SOURCE_DIR}/test/iobuf_unittest.cpp ${CMAKE_SOURCE_DIR}/test/iobuf_unittest.cpp
${CMAKE_SOURCE_DIR}/test/test_switches.cc ${CMAKE_SOURCE_DIR}/test/test_switches.cc
${CMAKE_SOURCE_DIR}/test/scoped_locale.cc ${CMAKE_SOURCE_DIR}/test/scoped_locale.cc
${CMAKE_SOURCE_DIR}/test/test_file_util_linux.cc
${CMAKE_SOURCE_DIR}/test/butil_unittest_main.cpp ${CMAKE_SOURCE_DIR}/test/butil_unittest_main.cpp
${CMAKE_SOURCE_DIR}/test/butil_unittest_main.cpp ${CMAKE_SOURCE_DIR}/test/butil_unittest_main.cpp
) )
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
SET(TEST_BUTIL_SOURCES ${TEST_BUTIL_SOURCES}
${CMAKE_SOURCE_DIR}/test/proc_maps_linux_unittest.cc
${CMAKE_SOURCE_DIR}/test/test_file_util_linux.cc)
endif()
# -DBVAR_NOT_LINK_DEFAULT_VARIABLES not work for gcc >= 5.0, just remove the file to prevent linking into unit tests # -DBVAR_NOT_LINK_DEFAULT_VARIABLES not work for gcc >= 5.0, just remove the file to prevent linking into unit tests
list(REMOVE_ITEM BVAR_SOURCES ${CMAKE_SOURCE_DIR}/src/bvar/default_variables.cpp) list(REMOVE_ITEM BVAR_SOURCES ${CMAKE_SOURCE_DIR}/src/bvar/default_variables.cpp)
......
...@@ -19,7 +19,17 @@ namespace { ...@@ -19,7 +19,17 @@ namespace {
TEST(BaiduTimeTest, diff_between_gettimeofday_and_REALTIME) { TEST(BaiduTimeTest, diff_between_gettimeofday_and_REALTIME) {
long t1 = butil::gettimeofday_us(); long t1 = butil::gettimeofday_us();
timespec time; timespec time;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, &time); clock_gettime(CLOCK_REALTIME, &time);
#endif
long t2 = butil::timespec_to_microseconds(time); long t2 = butil::timespec_to_microseconds(time);
LOG(INFO) << "t1=" << t1 << " t2=" << t2; LOG(INFO) << "t1=" << t1 << " t2=" << t2;
} }
...@@ -100,6 +110,7 @@ TEST(BaiduTimeTest, cost_of_timer) { ...@@ -100,6 +110,7 @@ TEST(BaiduTimeTest, cost_of_timer) {
clock_desc[i], t1.n_elapsed() / N); clock_desc[i], t1.n_elapsed() / N);
} }
#endif #endif
#if !defined(OS_MACOSX)
if (0 == clock_gettime((clockid_t)i, &ts)) { if (0 == clock_gettime((clockid_t)i, &ts)) {
t1.start(); t1.start();
for (size_t j = 0; j < N; ++j) { for (size_t j = 0; j < N; ++j) {
...@@ -109,6 +120,7 @@ TEST(BaiduTimeTest, cost_of_timer) { ...@@ -109,6 +120,7 @@ TEST(BaiduTimeTest, cost_of_timer) {
printf("glibc clock_gettime(%s) takes %" PRId64 "ns\n", printf("glibc clock_gettime(%s) takes %" PRId64 "ns\n",
clock_desc[i], t1.n_elapsed() / N); clock_desc[i], t1.n_elapsed() / N);
} }
#endif
} }
} }
......
...@@ -25,7 +25,17 @@ TEST(ButexTest, wait_on_already_timedout_butex) { ...@@ -25,7 +25,17 @@ TEST(ButexTest, wait_on_already_timedout_butex) {
uint32_t* butex = bthread::butex_create_checked<uint32_t>(); uint32_t* butex = bthread::butex_create_checked<uint32_t>();
ASSERT_TRUE(butex); ASSERT_TRUE(butex);
timespec now; timespec now;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
ASSERT_EQ(0, clock_get_time(cclock, &mts));
mach_port_deallocate(mach_task_self(), cclock);
now.tv_sec = mts.tv_sec;
now.tv_nsec = mts.tv_nsec;
#else
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now)); ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
#endif
*butex = 1; *butex = 1;
ASSERT_EQ(-1, bthread::butex_wait(butex, 1, &now)); ASSERT_EQ(-1, bthread::butex_wait(butex, 1, &now));
ASSERT_EQ(ETIMEDOUT, errno); ASSERT_EQ(ETIMEDOUT, errno);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment