Skip to content

Commit

Permalink
Get gd os tests running on host
Browse files Browse the repository at this point in the history
* Run os tests on host
* Fix flaky hander clear test
* Move to linux_generic
* Remove dependency on libchrome
* Allow alarms to run on linux host where we can't get CAP_WAKE_ALARM

Test: atest bluetooth_gd_test_os
Change-Id: Ia95ce39169ac8c963052e3252e7b39ef32bd5bb1
  • Loading branch information
Zach Johnson committed Mar 28, 2019
1 parent 8b3df83 commit 3a77003
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 81 deletions.
3 changes: 0 additions & 3 deletions TEST_MAPPING
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"postsubmit" : [
{
"name" : "bluetooth_gd_test_os"
},
{
"name" : "bluetooth_test_common"
},
Expand Down
112 changes: 90 additions & 22 deletions gd/Android.bp
Original file line number Diff line number Diff line change
@@ -1,36 +1,104 @@
cc_library_static {
name: "libbt-gd-os",
cc_defaults {
name: "gd_defaults",
target: {
android: {
test_config_template: "AndroidTestTemplate.xml",
cflags: [
"-DOS_ANDROID",
"-DOS_LINUX_GENERIC",
],
shared_libs: [
"liblog"
]
},
host: {
cflags: [
"-DOS_LINUX",
"-DOS_LINUX_GENERIC",
]
}
},
cpp_std: "c++17",
cflags: [
"-DEXPORT_SYMBOL=__attribute__((visibility(\"default\")))",
"-fvisibility=hidden",
"-DLOG_NDEBUG=1",
"-DGOOGLE_PROTOBUF_NO_RTTI",
],
conlyflags: [
"-std=c99",
],
sanitize: {
misc_undefined: ["bounds"],
},
}

// Enables code coverage for a set of source files. Must be combined with
// "clang_coverage_bin" in order to work. See //test/gen_coverage.py for more information
// on generating code coverage.
cc_defaults {
name: "gd_clang_file_coverage",
target: {
host: {
clang_cflags: [
"-fprofile-instr-generate",
"-fcoverage-mapping",
],
},
},
}

// Enabled code coverage on a binary. These flags allow libraries that were
// compiled with "clang_file_coverage" to be properly linked together in
// order to create a binary that will create a profraw file when ran. Note
// these flags themselves don't enable code coverage for the source files
// compiled in the binary. See //test/gen_coverage.py for more information
// on generating code coverage.
cc_defaults {
name: "gd_clang_coverage_bin",
target: {
host: {
ldflags: [
"-fprofile-instr-generate",
"-fcoverage-mapping",
],
},
},
}

cc_library {
name: "libbluetooth_gd",
defaults: [
"fluoride_defaults",
"clang_file_coverage",
"gd_defaults",
"gd_clang_file_coverage",
],
host_supported: true,
srcs: [
"os/alarm.cc",
"os/handler.cc",
"os/reactor.cc",
"os/repeating_alarm.cc",
"os/thread.cc",
],
target: {
linux: {
srcs: [
":BluetoothOsSources_linux_generic",
]
}
},
}

cc_test {
name: "bluetooth_gd_test_os",
name: "bluetooth_test_gd",
test_suites: ["device-tests"],
defaults: [
"fluoride_defaults",
"clang_coverage_bin",
"gd_defaults",
"gd_clang_coverage_bin",
],
host_supported: true,
srcs : [
"os/alarm_unittest.cc",
"os/handler_unittest.cc",
"os/reactor_unittest.cc",
"os/repeating_alarm_unittest.cc",
"os/thread_unittest.cc",
],
target: {
linux: {
srcs: [
":BluetoothOsTestSources_linux_generic",
]
}
},
static_libs : [
"libbt-gd-os",
"libbluetooth_gd",
],
sanitize: {
cfi: false,
Expand Down
36 changes: 36 additions & 0 deletions gd/AndroidTestTemplate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 The Android Open Source Project
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.
-->
<configuration description="Runs {MODULE}.">
<option name="test-suite-tag" value="apct" />
<option name="test-suite-tag" value="apct-native" />
<target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer" />
<target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
<option name="cleanup" value="true" />
<option name="push" value="{MODULE}->/data/local/tmp/{MODULE}" />
</target_preparer>
<target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
<option name="run-command" value="settings put global ble_scan_always_enabled 0" />
<option name="run-command" value="svc bluetooth disable" />
</target_preparer>
<target_preparer class="com.android.tradefed.targetprep.FolderSaver">
<option name="device-path" value="/data/vendor/ssrdump" />
</target_preparer>
<test class="com.android.tradefed.testtype.GTest" >
<option name="native-test-device-path" value="/data/local/tmp" />
<option name="module-name" value="{MODULE}" />
<option name="run-test-as" value="0" />
</test>
</configuration>
8 changes: 8 additions & 0 deletions gd/TEST_MAPPING
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presubmit" : [
{
"name" : "bluetooth_test_gd",
"host" : true
}
]
}
21 changes: 21 additions & 0 deletions gd/os/Android.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
filegroup {
name: "BluetoothOsSources_linux_generic",
srcs: [
"linux_generic/alarm.cc",
"linux_generic/handler.cc",
"linux_generic/reactor.cc",
"linux_generic/repeating_alarm.cc",
"linux_generic/thread.cc",
]
}

filegroup {
name: "BluetoothOsTestSources_linux_generic",
srcs: [
"linux_generic/alarm_unittest.cc",
"linux_generic/handler_unittest.cc",
"linux_generic/reactor_unittest.cc",
"linux_generic/repeating_alarm_unittest.cc",
"linux_generic/thread_unittest.cc",
]
}
23 changes: 15 additions & 8 deletions gd/os/alarm.cc → gd/os/linux_generic/alarm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@

#include <sys/timerfd.h>
#include <cstring>
#include <unistd.h>

#include "base/logging.h"
#include "os/log.h"
#include "os/utils.h"

#ifdef OS_ANDROID
#define ALARM_CLOCK CLOCK_BOOTTIME_ALARM
#else
#define ALARM_CLOCK CLOCK_BOOTTIME
#endif

namespace bluetooth {
namespace os {

Alarm::Alarm(Thread* thread)
: thread_(thread),
fd_(timerfd_create(CLOCK_BOOTTIME_ALARM, 0)) {
CHECK_NE(fd_, -1) << __func__ << ": cannot create timerfd: " << strerror(errno);
fd_(timerfd_create(ALARM_CLOCK, 0)) {
LOG_FATAL_WHEN(fd_ != -1, "cannot create timerfd: %s", strerror(errno));

token_ = thread_->GetReactor()->Register(fd_, [this] { on_fire(); }, nullptr);
}
Expand All @@ -38,7 +45,7 @@ Alarm::~Alarm() {

int close_status;
RUN_NO_INTR(close_status = close(fd_));
CHECK_NE(close_status, -1) << __func__ << ": cannot close timerfd: " << strerror(errno);
FATAL_WHEN(close_status != -1);
}

void Alarm::Schedule(Closure task, std::chrono::milliseconds delay) {
Expand All @@ -49,7 +56,7 @@ void Alarm::Schedule(Closure task, std::chrono::milliseconds delay) {
{delay_ms / 1000, delay_ms % 1000 * 1000000}
};
int result = timerfd_settime(fd_, 0, &timer_itimerspec, nullptr);
CHECK_EQ(result, 0) << __func__ << ": failed, error=" << strerror(errno);
FATAL_WHEN(result == 0);

task_ = std::move(task);
}
Expand All @@ -58,7 +65,7 @@ void Alarm::Cancel() {
std::lock_guard<std::mutex> lock(mutex_);
itimerspec disarm_itimerspec{/* disarm timer */};
int result = timerfd_settime(fd_, 0, &disarm_itimerspec, nullptr);
CHECK_EQ(result, 0) << __func__ << ": failed, error=" << strerror(errno);
FATAL_WHEN(result == 0);
}

void Alarm::on_fire() {
Expand All @@ -68,8 +75,8 @@ void Alarm::on_fire() {
auto bytes_read = read(fd_, &times_invoked, sizeof(uint64_t));
lock.unlock();
task();
CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(uint64_t))) << __func__ << ": failed, error=" << strerror(errno);
CHECK_EQ(times_invoked, static_cast<uint64_t>(1));
FATAL_WHEN(bytes_read == static_cast<ssize_t>(sizeof(uint64_t)))
FATAL_WHEN(times_invoked == static_cast<uint64_t>(1));
}

} // namespace os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <future>

#include "base/logging.h"
#include "gtest/gtest.h"

namespace bluetooth {
Expand Down
18 changes: 12 additions & 6 deletions gd/os/handler.cc → gd/os/linux_generic/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

#include <sys/eventfd.h>
#include <cstring>
#include <unistd.h>

#include "base/logging.h"

#include "os/log.h"
#include "os/reactor.h"
#include "os/utils.h"

Expand All @@ -34,7 +34,7 @@ namespace os {
Handler::Handler(Thread* thread)
: thread_(thread),
fd_(eventfd(0, EFD_SEMAPHORE | EFD_NONBLOCK)) {
CHECK_NE(fd_, -1) << __func__ << ": cannot create eventfd: " << strerror(errno);
FATAL_WHEN(fd_ != -1);

reactable_ = thread_->GetReactor()->Register(fd_, [this] { this->handle_next_event(); }, nullptr);
}
Expand All @@ -45,7 +45,7 @@ Handler::~Handler() {

int close_status;
RUN_NO_INTR(close_status = close(fd_));
CHECK_NE(close_status, -1) << __func__ << ": cannot close eventfd: " << strerror(errno);
FATAL_WHEN(close_status != -1);
}

void Handler::Post(Closure closure) {
Expand All @@ -55,7 +55,7 @@ void Handler::Post(Closure closure) {
}
uint64_t val = 1;
auto write_result = eventfd_write(fd_, val);
CHECK_NE(write_result, -1) << __func__ << ": failed to write: " << strerror(errno);
FATAL_WHEN(write_result != -1);
}

void Handler::Clear() {
Expand All @@ -73,7 +73,13 @@ void Handler::handle_next_event() {
Closure closure;
uint64_t val = 0;
auto read_result = eventfd_read(fd_, &val);
CHECK_NE(read_result, -1) << __func__ << ": failed to read fd: " << strerror(errno);
if (read_result == -1 && errno == EAGAIN) {
// We were told there was an item, but it was removed before we got there
// (aka the queue was cleared). Not a fatal error, so just bail.
return;
}

FATAL_WHEN(read_result != -1);

{
std::lock_guard<std::mutex> lock(mutex_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <thread>

#include <gtest/gtest.h>
#include "base/logging.h"

namespace bluetooth {
namespace os {
Expand Down Expand Up @@ -58,7 +57,9 @@ TEST_F(HandlerTest, post_task_cleared) {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
};
handler_->Post(std::move(closure));
closure = []() { LOG(FATAL) << "Should not happen"; };
closure = []() {
ASSERT_TRUE(false);
};
std::this_thread::sleep_for(std::chrono::milliseconds(5));
handler_->Post(std::move(closure));
handler_->Clear();
Expand Down
Loading

0 comments on commit 3a77003

Please sign in to comment.