Fix wifi test flakiness

PiperOrigin-RevId: 549683201
This commit is contained in:
Janusz Sobczak
2023-07-20 11:06:44 -07:00
committed by Copybara-Service
parent 0e44ed626d
commit 2e1ed2b465
7 changed files with 49 additions and 16 deletions
@@ -23,6 +23,7 @@
#include "connections/implementation/offline_frames.h"
#include "connections/implementation/wifi_lan_endpoint_channel.h"
#include "internal/platform/wifi_lan.h"
#include "internal/platform/wifi_utils.h"
namespace nearby {
namespace connections {
@@ -60,7 +61,8 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel(
if (!socket.IsValid()) {
NEARBY_LOGS(ERROR)
<< "WifiLanBwuHandler failed to connect to the WifiLan service ("
<< ip_address << ":" << port << ") for endpoint " << endpoint_id;
<< WifiUtils::GetHumanReadableIpAddress(ip_address) << ":" << port
<< ") for endpoint " << endpoint_id;
return nullptr;
}
@@ -70,7 +72,7 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel(
<< endpoint_id;
// Create a new WifiLanEndpointChannel.
auto channel = absl::make_unique<WifiLanEndpointChannel>(
auto channel = std::make_unique<WifiLanEndpointChannel>(
service_id, /*channel_name=*/service_id, socket);
if (channel == nullptr) {
NEARBY_LOGS(ERROR) << "WifiLanBwuHandler failed to create WifiLan endpoint "
+1
View File
@@ -439,6 +439,7 @@ cc_library(
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
],
)
+18 -10
View File
@@ -921,18 +921,26 @@ void MediumEnvironment::UnregisterWifiLanMedium(api::WifiLanMedium& medium) {
api::WifiLanMedium* MediumEnvironment::GetWifiLanMedium(
const std::string& ip_address, int port) {
for (auto& medium_info : wifi_lan_mediums_) {
auto* medium_found = medium_info.first;
auto& info = medium_info.second;
for (auto& advertising_service : info.advertising_services) {
auto& service_info = advertising_service.second;
if (ip_address == service_info.GetIPAddress() &&
port == service_info.GetPort()) {
return medium_found;
api::WifiLanMedium* result = nullptr;
CountDownLatch latch(1);
RunOnMediumEnvironmentThread([&]() {
for (auto& medium_info : wifi_lan_mediums_) {
auto* medium_found = medium_info.first;
auto& info = medium_info.second;
for (auto& advertising_service : info.advertising_services) {
auto& service_info = advertising_service.second;
if (ip_address == service_info.GetIPAddress() &&
port == service_info.GetPort()) {
result = medium_found;
latch.CountDown();
return;
}
}
latch.CountDown();
}
}
return nullptr;
});
latch.Await();
return result;
}
void MediumEnvironment::RegisterWifiDirectMedium(
+3 -1
View File
@@ -18,6 +18,7 @@
#include <utility>
#include "internal/platform/mutex_lock.h"
#include "internal/platform/wifi_utils.h"
namespace nearby {
@@ -167,7 +168,8 @@ WifiLanSocket WifiLanMedium::ConnectToService(
const std::string& ip_address, int port,
CancellationFlag* cancellation_flag) {
NEARBY_LOGS(INFO) << "WifiLanMedium::ConnectToService: ip address="
<< ip_address << ", port=" << port;
<< WifiUtils::GetHumanReadableIpAddress(ip_address)
<< ", port=" << port;
return WifiLanSocket(
impl_->ConnectToService(ip_address, port, cancellation_flag));
}
+11
View File
@@ -18,6 +18,8 @@
#include <vector>
#include "absl/strings/numbers.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
namespace nearby {
@@ -115,4 +117,13 @@ bool WifiUtils::ValidateIPV4(std::string ipv4) {
return true;
}
std::string WifiUtils::GetHumanReadableIpAddress(
absl::string_view binary_address) {
std::vector<std::string> parts;
for (unsigned int b : binary_address) {
parts.push_back(absl::StrFormat("%d", b));
}
return absl::StrJoin(parts, ".");
}
} // namespace nearby
+3
View File
@@ -50,6 +50,9 @@ class WifiUtils {
static int ConvertFrequencyMhzToChannel(int freq_mhz);
static bool ValidateIPV4(std::string ipv4);
// Converts an IP address from binary format for human readable.
static std::string GetHumanReadableIpAddress(
absl::string_view binary_address);
};
} // namespace nearby
+9 -3
View File
@@ -17,6 +17,7 @@
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/escaping.h"
namespace nearby {
namespace {
@@ -40,8 +41,6 @@ constexpr int kChan30Num_60G_NotExist = 30;
constexpr int kFreqNotExist = 1002;
TEST(WifiUtilsTest, ConvertChannelToFrequency) {
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan6Num_2G,
WifiBandType::kUnknown),
@@ -69,7 +68,8 @@ TEST(WifiUtilsTest, ConvertChannelToFrequency) {
WifiUtils::kUnspecified);
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan30Num_60G_NotExist,
WifiBandType::kBand60Ghz),
WifiUtils::kUnspecified);}
WifiUtils::kUnspecified);
}
TEST(WifiUtilsTest, ConvertFrequencyToChannel) {
EXPECT_EQ(WifiUtils::ConvertFrequencyMhzToChannel(kChan6NumFreq_2G),
@@ -92,5 +92,11 @@ TEST(WifiUtilsTest, Ipv4Validation) {
EXPECT_TRUE(WifiUtils::ValidateIPV4("192.168.1.46"));
}
TEST(WifiUtilsTest, GetHumanReadableIpAddress) {
EXPECT_EQ(
WifiUtils::GetHumanReadableIpAddress(absl::HexStringToBytes("000AFEFF")),
"0.10.254.255");
}
} // namespace
} // namespace nearby