Merge branch 'master' into release

Change-Id: I8a6cfe28093bf3d60dc91bcbc4e98e641764c4c0
This commit is contained in:
Alexey Polyudov
2020-07-07 12:54:19 -07:00
29 changed files with 1034 additions and 234 deletions
+1
View File
@@ -107,6 +107,7 @@ cc_test(
"atomic_reference_test.cc",
"bluetooth_adapter_test.cc",
"bluetooth_classic_test.cc",
"cancelable_alarm_test.cc",
"condition_variable_test.cc",
"count_down_latch_test.cc",
"crypto_test.cc",
+3 -1
View File
@@ -38,7 +38,9 @@ class Cancelable final {
explicit Cancelable(std::shared_ptr<api::Cancelable> impl)
: impl_(std::move(impl)) {}
bool Cancel() { return impl_->Cancel(); }
bool Cancel() { return impl_ ? impl_->Cancel() : false; }
bool IsValid() { return impl_ != nullptr; }
private:
std::shared_ptr<api::Cancelable> impl_;
@@ -35,6 +35,7 @@ namespace nearby {
*/
class CancelableAlarm {
public:
CancelableAlarm() = default;
CancelableAlarm(absl::string_view name, std::function<void()>&& runnable,
absl::Duration delay, ScheduledExecutor* scheduled_executor)
: name_(name),
@@ -58,6 +59,10 @@ class CancelableAlarm {
return cancelable_.Cancel();
}
bool IsValid() {
return cancelable_.IsValid();
}
private:
Mutex mutex_;
std::string name_;
@@ -0,0 +1,54 @@
#include "platform_v2/public/cancelable_alarm.h"
#include "platform_v2/public/atomic_boolean.h"
#include "platform_v2/public/scheduled_executor.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/time/time.h"
namespace location {
namespace nearby {
namespace {
TEST(CancelableAlarmTest, CanCreateDefault) { CancelableAlarm alarm; }
TEST(CancelableAlarmTest, CancelDefaultFails) {
CancelableAlarm alarm;
EXPECT_FALSE(alarm.Cancel());
}
TEST(CancelableAlarmTest, CanCreateAndFireAlarm) {
ScheduledExecutor alarm_executor;
AtomicBoolean done{false};
CancelableAlarm alarm(
"test_alarm", [&done]() { done.Set(true); }, absl::Milliseconds(100),
&alarm_executor);
SystemClock::Sleep(absl::Milliseconds(1000));
EXPECT_TRUE(done.Get());
}
TEST(CancelableAlarmTest, CanCreateAndCancelAlarm) {
ScheduledExecutor alarm_executor;
AtomicBoolean done{false};
CancelableAlarm alarm(
"test_alarm", [&done]() { done.Set(true); }, absl::Milliseconds(100),
&alarm_executor);
EXPECT_TRUE(alarm.Cancel());
SystemClock::Sleep(absl::Milliseconds(1000));
EXPECT_FALSE(done.Get());
}
TEST(CancelableAlarmTest, CancelExpiredAlarmFails) {
ScheduledExecutor alarm_executor;
AtomicBoolean done{false};
CancelableAlarm alarm(
"test_alarm", [&done]() { done.Set(true); }, absl::Milliseconds(100),
&alarm_executor);
SystemClock::Sleep(absl::Milliseconds(1000));
EXPECT_TRUE(done.Get());
EXPECT_FALSE(alarm.Cancel());
}
} // namespace
} // namespace nearby
} // namespace location
+3 -3
View File
@@ -62,6 +62,7 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_id,
[this](api::WifiLanService& service,
const std::string& service_id) {
MutexLock lock(&mutex_);
if (services_.empty()) return;
auto item = services_.extract(&service);
auto& context = *item.mapped();
NEARBY_LOG(INFO, "Removing service=%p, impl=%p",
@@ -101,9 +102,8 @@ bool WifiLanMedium::StartAcceptingConnections(
if (!pair.second) {
NEARBY_LOG(INFO, "Adding (again) socket=%p, impl=%p",
&context.socket, &socket);
return;
context.socket = WifiLanSocket(&socket);
}
context.socket = WifiLanSocket(&socket);
NEARBY_LOG(INFO, "Adding socket=%p, impl=%p", &context.socket,
&socket);
accepted_connection_callback_.accepted_cb(context.socket,
@@ -120,7 +120,7 @@ bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
NEARBY_LOG(INFO, "WifiLan accepted connection disabled: impl=%p",
&GetImpl());
}
return impl_->StopDiscovery(service_id);
return impl_->StopAcceptingConnections(service_id);
}
WifiLanSocket WifiLanMedium::Connect(WifiLanService& service,
+2 -2
View File
@@ -116,8 +116,8 @@ class WifiLanMedium final {
};
struct AcceptedConnectionCallback {
std::function<void(WifiLanSocket& socket, const std::string& service_id)>
accepted_cb = DefaultCallback<WifiLanSocket&, const std::string&>();
std::function<void(WifiLanSocket socket, const std::string& service_id)>
accepted_cb = DefaultCallback<WifiLanSocket, const std::string&>();
};
struct AcceptedConnectionInfo {
WifiLanSocket socket;
+100 -24
View File
@@ -27,10 +27,12 @@ namespace nearby {
namespace {
constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"};
constexpr absl::string_view kServiceName{"service name"};
class WifiLanMediumTest : public ::testing::Test {
protected:
using DiscoveredServiceCallback = WifiLanMedium::DiscoveredServiceCallback;
using AcceptedConnectionCallback = WifiLanMedium::AcceptedConnectionCallback;
WifiLanMediumTest() { env_.Stop(); }
@@ -39,75 +41,149 @@ class WifiLanMediumTest : public ::testing::Test {
TEST_F(WifiLanMediumTest, ConstructorDestructorWorks) {
env_.Start();
WifiLanMedium medium_a;
WifiLanMedium medium_b;
WifiLanMedium wifi_a;
WifiLanMedium wifi_b;
// Make sure we can create functional mediums.
ASSERT_TRUE(medium_a.IsValid());
ASSERT_TRUE(medium_b.IsValid());
ASSERT_TRUE(wifi_a.IsValid());
ASSERT_TRUE(wifi_b.IsValid());
// Make sure we can create 2 distinct mediums.
EXPECT_NE(&medium_a.GetImpl(), &medium_b.GetImpl());
EXPECT_NE(&wifi_a.GetImpl(), &wifi_b.GetImpl());
env_.Stop();
}
TEST_F(WifiLanMediumTest, CanStartDiscoveryAndServiceIndeedDiscovered) {
TEST_F(WifiLanMediumTest, CanStartAdvertising) {
env_.Start();
WifiLanMedium medium;
WifiLanMedium wifi_a;
WifiLanMedium wifi_b;
std::string service_id(kServiceID);
std::string service_name{kServiceName};
CountDownLatch found_latch(1);
wifi_a.StartAdvertising(service_id, service_name);
EXPECT_TRUE(wifi_b.StartDiscovery(
service_id, DiscoveredServiceCallback{
.service_discovered_cb =
[&found_latch](WifiLanService& service,
const std::string& service_id) {
found_latch.CountDown();
},
}));
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(wifi_a.StopAdvertising(service_id));
EXPECT_TRUE(wifi_b.StopDiscovery(service_id));
env_.Stop();
}
TEST_F(WifiLanMediumTest, CanStartDiscovery) {
env_.Start();
WifiLanMedium wifi_a;
WifiLanMedium wifi_b;
std::string service_id(kServiceID);
std::string service_name{kServiceName};
CountDownLatch found_latch(1);
CountDownLatch lost_latch(1);
medium.StartDiscovery(std::string(kServiceID),
wifi_a.StartDiscovery(service_id,
DiscoveredServiceCallback{
.service_discovered_cb =
[&found_latch](WifiLanService& service,
const std::string& service_id) {
NEARBY_LOG(INFO, "Service discovered: %s",
service.GetName().c_str());
EXPECT_EQ(kServiceID, service_id);
found_latch.CountDown();
},
.service_lost_cb =
[&lost_latch](WifiLanService& service,
const std::string& service_id) {
NEARBY_LOG(INFO, "Service lost: %s",
service.GetName().c_str());
EXPECT_EQ(kServiceID, service_id);
lost_latch.CountDown();
},
});
EXPECT_TRUE(wifi_b.StartAdvertising(service_id, service_name));
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(wifi_b.StopAdvertising(service_id));
EXPECT_TRUE(lost_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(wifi_a.StopDiscovery(service_id));
env_.Stop();
}
TEST_F(WifiLanMediumTest, CanStopDiscovery) {
env_.Start();
WifiLanMedium medium;
WifiLanMedium wifi_a;
WifiLanMedium wifi_b;
std::string service_id(kServiceID);
std::string service_name{kServiceName};
CountDownLatch found_latch(1);
CountDownLatch lost_latch(1);
medium.StartDiscovery(std::string(kServiceID),
wifi_a.StartDiscovery(service_id,
DiscoveredServiceCallback{
.service_discovered_cb =
[&found_latch](WifiLanService& service,
const std::string& service_id) {
NEARBY_LOG(INFO, "Service discovered: %s",
service.GetName().c_str());
EXPECT_EQ(kServiceID, service_id);
found_latch.CountDown();
},
.service_lost_cb =
[&lost_latch](WifiLanService& service,
const std::string& service_id) {
NEARBY_LOG(INFO, "Service lost: %s",
service.GetName().c_str());
EXPECT_EQ(kServiceID, service_id);
lost_latch.CountDown();
},
});
EXPECT_TRUE(wifi_b.StartAdvertising(service_id, service_name));
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
bool stop = medium.StopDiscovery(std::string(kServiceID));
EXPECT_TRUE(stop);
EXPECT_TRUE(wifi_a.StopDiscovery(service_id));
EXPECT_TRUE(wifi_b.StopAdvertising(service_id));
EXPECT_FALSE(lost_latch.Await(absl::Milliseconds(1000)).result());
env_.Stop();
}
TEST_F(WifiLanMediumTest, CanStartAcceptingConnectionsAndConnect) {
env_.Start();
WifiLanMedium wifi_a;
WifiLanMedium wifi_b;
std::string service_id(kServiceID);
std::string service_name{kServiceName};
CountDownLatch found_latch(1);
CountDownLatch accepted_latch(1);
WifiLanService* discovered_service = nullptr;
wifi_a.StartDiscovery(
service_id,
DiscoveredServiceCallback{
.service_discovered_cb =
[&found_latch, &discovered_service](
WifiLanService& service, const std::string& service_id) {
NEARBY_LOG(INFO, "Service discovered: %s, %p",
service.GetName().c_str(), &service);
discovered_service = &service;
found_latch.CountDown();
},
});
wifi_b.StartAdvertising(service_id, service_name);
wifi_b.StartAcceptingConnections(
service_id,
AcceptedConnectionCallback{
.accepted_cb = [&accepted_latch](WifiLanSocket socket,
const std::string& service_id) {
NEARBY_LOG(INFO, "Connection accepted: socket=%p, service_id=%s",
&socket, service_id.c_str());
accepted_latch.CountDown();
}});
EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result());
WifiLanSocket socket_a;
EXPECT_FALSE(socket_a.IsValid());
{
SingleThreadExecutor client_executor;
client_executor.Execute(
[&wifi_a, &socket_a, discovered_service, &service_id]() {
socket_a = wifi_a.Connect(*discovered_service, service_id);
});
}
EXPECT_TRUE(accepted_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(socket_a.IsValid());
wifi_b.StopAdvertising(service_id);
wifi_a.StopDiscovery(service_id);
env_.Stop();
}