mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Refactor WifiLanBwuHandler test to improve test coverage.
PiperOrigin-RevId: 806350972
This commit is contained in:
@@ -126,6 +126,7 @@ cc_library(
|
||||
"@com_google_absl//absl/strings:str_format",
|
||||
"@com_google_absl//absl/synchronization",
|
||||
"@com_google_absl//absl/time",
|
||||
"@com_google_absl//absl/types:optional",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -14,14 +14,15 @@
|
||||
|
||||
#include "internal/platform/implementation/g3/wifi_lan.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/log/check.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/cancellation_flag_listener.h"
|
||||
@@ -34,6 +35,13 @@
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
namespace {
|
||||
constexpr absl::string_view kServiceInfoName{"DEFAULT_SERVICE_INFO_NAME"};
|
||||
constexpr absl::string_view kServiceType{"_default._tcp.local"};
|
||||
constexpr absl::string_view kEndpointName{"DEFAULT_ENDPOINT_NAME"};
|
||||
constexpr absl::string_view kEndpointInfoKey{"n"};
|
||||
} // namespace
|
||||
|
||||
std::string WifiLanServerSocket::GetName(const std::string& ip_address,
|
||||
int port) {
|
||||
std::string dot_delimited_string;
|
||||
@@ -120,6 +128,10 @@ Exception WifiLanServerSocket::DoClose() {
|
||||
WifiLanMedium::WifiLanMedium() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.RegisterWifiLanMedium(*this);
|
||||
default_nsd_service_info_.SetServiceName(std::string(kServiceInfoName));
|
||||
default_nsd_service_info_.SetServiceType(std::string(kServiceType));
|
||||
default_nsd_service_info_.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
std::string(kEndpointName));
|
||||
}
|
||||
|
||||
WifiLanMedium::~WifiLanMedium() {
|
||||
@@ -144,6 +156,11 @@ bool WifiLanMedium::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
}
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
// Delete the default_nsd_service_info_ that added in ListenForService stage
|
||||
// as we will have a real service info to advertise.
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, default_nsd_service_info_,
|
||||
/*enabled=*/false);
|
||||
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, nsd_service_info,
|
||||
/*enabled=*/true);
|
||||
{
|
||||
@@ -230,12 +247,21 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
|
||||
std::string socket_name = WifiLanServerSocket::GetName(ip_address, port);
|
||||
LOG(INFO) << "G3 WifiLan ConnectToService [self]: medium=" << this
|
||||
<< ", ip address + port=" << socket_name;
|
||||
|
||||
// First, find an instance of remote medium, that exposed this service.
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
auto* remote_medium =
|
||||
static_cast<WifiLanMedium*>(env.GetWifiLanMedium(ip_address, port));
|
||||
if (!remote_medium) {
|
||||
return {};
|
||||
// In case of WLAN BWU, here's no discovery phase, so we need to
|
||||
// update the discovery state with default_nsd_service_info_.
|
||||
env.UpdateWifiLanMediumForDiscovery(
|
||||
*this, {}, default_nsd_service_info_.GetServiceType(), true);
|
||||
remote_medium =
|
||||
static_cast<WifiLanMedium*>(env.GetWifiLanMedium(ip_address, port));
|
||||
if (!remote_medium) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
WifiLanServerSocket* server_socket = nullptr;
|
||||
@@ -285,8 +311,10 @@ std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(
|
||||
int port) {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
auto server_socket = std::make_unique<WifiLanServerSocket>();
|
||||
server_socket->SetIPAddress(env.GetFakeIPAddress());
|
||||
server_socket->SetPort(port == 0 ? env.GetFakePort() : port);
|
||||
std::string ip_address = env.GetFakeIPAddress();
|
||||
int fake_port = port == 0 ? env.GetFakePort() : port;
|
||||
server_socket->SetIPAddress(ip_address);
|
||||
server_socket->SetPort(fake_port);
|
||||
std::string socket_name = WifiLanServerSocket::GetName(
|
||||
server_socket->GetIPAddress(), server_socket->GetPort());
|
||||
server_socket->SetCloseNotifier([this, socket_name]() {
|
||||
@@ -295,6 +323,13 @@ std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(
|
||||
});
|
||||
LOG(INFO) << "G3 WifiLan Adding server socket: medium=" << this
|
||||
<< ", socket_name=" << socket_name;
|
||||
default_nsd_service_info_.SetIPAddress(ip_address);
|
||||
default_nsd_service_info_.SetPort(fake_port);
|
||||
|
||||
// In case of WLAN BWU, here's no advertisement phase, so we need to update
|
||||
// the advertising state with default_nsd_service_info_ in advance.
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, default_nsd_service_info_,
|
||||
/*enabled=*/true);
|
||||
absl::MutexLock lock(mutex_);
|
||||
server_sockets_.insert({socket_name, server_socket.get()});
|
||||
return server_socket;
|
||||
|
||||
@@ -15,15 +15,19 @@
|
||||
#ifndef PLATFORM_IMPL_G3_WIFI_LAN_H_
|
||||
#define PLATFORM_IMPL_G3_WIFI_LAN_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/g3/multi_thread_executor.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/implementation/g3/socket_base.h"
|
||||
#include "internal/platform/implementation/wifi_lan.h"
|
||||
#include "internal/platform/input_stream.h"
|
||||
@@ -241,6 +245,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
|
||||
NsdServiceInfo default_nsd_service_info_;
|
||||
absl::Mutex mutex_;
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveringInfo discovering_info_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
Reference in New Issue
Block a user