cleanup tests

PiperOrigin-RevId: 661384334
This commit is contained in:
Francis Tsui
2024-08-09 13:33:50 -07:00
committed by Copybara-Service
parent c3f3beb349
commit c9c05b73fc
2 changed files with 50 additions and 25 deletions
+19 -3
View File
@@ -222,10 +222,24 @@ cc_library(
],
)
cc_library(
name = "nearby_connection_impl",
srcs = ["nearby_connection_impl.cc"],
hdrs = ["nearby_connection_impl.h"],
deps = [
":connection_types",
":types",
"//internal/platform:types",
"//sharing/internal/public:logging",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
],
)
cc_library(
name = "nearby_sharing_service",
srcs = [
"nearby_connection_impl.cc",
"nearby_connections_manager.cc",
"nearby_connections_manager_factory.cc",
"nearby_connections_manager_impl.cc",
@@ -245,7 +259,6 @@ cc_library(
hdrs = [
"connection_lifecycle_listener.h",
"endpoint_discovery_listener.h",
"nearby_connection_impl.h",
"nearby_connections_manager_factory.h",
"nearby_connections_manager_impl.h",
"nearby_connections_service.h",
@@ -277,6 +290,7 @@ cc_library(
":attachments",
":connection_types",
":incoming_frame_reader",
":nearby_connection_impl",
":nearby_sharing_decoder_impl",
":paired_key_verification_runner",
":share_session",
@@ -464,10 +478,12 @@ cc_test(
srcs = ["incoming_frames_reader_test.cc"],
deps = [
":incoming_frame_reader",
":nearby_connection_impl",
":nearby_sharing_decoder_impl",
":test_support",
"//internal/platform/implementation/g3", # fixdeps: keep
"//internal/test",
"//sharing/internal/public:logging",
"//sharing/proto:wire_format_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/synchronization",
@@ -481,8 +497,8 @@ cc_test(
srcs = ["nearby_connection_impl_test.cc"],
deps = [
":incoming_frame_reader",
":nearby_connection_impl",
":nearby_sharing_decoder_impl",
":nearby_sharing_service",
":test_support",
"//internal/platform/implementation/g3", # fixdeps: keep
"//internal/test",
+31 -22
View File
@@ -24,8 +24,11 @@
#include "absl/synchronization/notification.h"
#include "absl/time/time.h"
#include "internal/test/fake_clock.h"
#include "internal/test/fake_device_info.h"
#include "internal/test/fake_task_runner.h"
#include "sharing/fake_nearby_connection.h"
#include "sharing/fake_nearby_connections_manager.h"
#include "sharing/internal/public/logging.h"
#include "sharing/nearby_connection_impl.h"
#include "sharing/nearby_sharing_decoder_impl.h"
#include "sharing/proto/wire_format.pb.h"
@@ -80,16 +83,22 @@ std::optional<std::vector<uint8_t>> GetInvalidFrame() {
class IncomingFramesReaderTest : public testing::Test {
public:
IncomingFramesReaderTest() = default;
IncomingFramesReaderTest() {
nearby_connection_ = std::make_unique<NearbyConnectionImpl>(
fake_device_info_, &fake_nearby_connections_manager_, "endpoint_id");
}
~IncomingFramesReaderTest() override = default;
void SetUp() override {
FakeTaskRunner::ResetPendingTasksCount();
frames_reader_ = std::make_shared<IncomingFramesReader>(
fake_task_runner_, nearby_sharing_decoder_, &fake_nearby_connection_);
fake_task_runner_, nearby_sharing_decoder_, nearby_connection_.get());
}
FakeNearbyConnection& connection() { return fake_nearby_connection_; }
NearbyConnectionImpl& connection() {
NL_CHECK(nearby_connection_);
return *nearby_connection_;
}
IncomingFramesReader* frames_reader() { return frames_reader_.get(); }
@@ -102,11 +111,16 @@ class IncomingFramesReaderTest : public testing::Test {
}
void ReleaseFrameReader() { frames_reader_.reset(); }
void CloseConnection() {
nearby_connection_ = nullptr;
}
private:
FakeClock fake_clock_;
FakeTaskRunner fake_task_runner_ {&fake_clock_, 1};
FakeNearbyConnection fake_nearby_connection_;
FakeDeviceInfo fake_device_info_;
FakeNearbyConnectionsManager fake_nearby_connections_manager_;
std::unique_ptr<NearbyConnectionImpl> nearby_connection_;
NearbySharingDecoderImpl nearby_sharing_decoder_;
std::shared_ptr<IncomingFramesReader> frames_reader_ = nullptr;
};
@@ -123,18 +137,13 @@ TEST_F(IncomingFramesReaderTest, ReadTimedOut) {
Sync();
FastForward(kTimeout);
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout));
// Ensure that the OnDataReadFromConnection callback is not run since the
// read timed out.
EXPECT_FALSE(connection().has_read_callback_been_run());
// Ensure that the IncomingFramesReader does not close the connection.
EXPECT_FALSE(connection().IsClosed());
}
TEST_F(IncomingFramesReaderTest, ReadAnyFrameSuccessful) {
std::optional<std::vector<uint8_t>> introduction_frame =
GetIntroductionFrame();
ASSERT_TRUE(introduction_frame.has_value());
connection().AppendReadableData(*introduction_frame);
connection().WriteMessage(*introduction_frame);
absl::Notification notification;
frames_reader()->ReadFrame([&](std::optional<V1Frame> frame) {
@@ -148,7 +157,7 @@ TEST_F(IncomingFramesReaderTest, ReadSuccessful) {
std::optional<std::vector<uint8_t>> introduction_frame =
GetIntroductionFrame();
ASSERT_TRUE(introduction_frame.has_value());
connection().AppendReadableData(*introduction_frame);
connection().WriteMessage(*introduction_frame);
absl::Notification notification;
frames_reader()->ReadFrame(
@@ -164,12 +173,12 @@ TEST_F(IncomingFramesReaderTest, ReadSuccessful) {
TEST_F(IncomingFramesReaderTest, ReadSuccessful_JumbledFramesOrdering) {
std::optional<std::vector<uint8_t>> cancel_frame = GetCancelFrame();
ASSERT_TRUE(cancel_frame.has_value());
connection().AppendReadableData(*cancel_frame);
connection().WriteMessage(*cancel_frame);
std::optional<std::vector<uint8_t>> introduction_frame =
GetIntroductionFrame();
ASSERT_TRUE(introduction_frame.has_value());
connection().AppendReadableData(*introduction_frame);
connection().WriteMessage(*introduction_frame);
absl::Notification notification;
frames_reader()->ReadFrame(
@@ -185,12 +194,12 @@ TEST_F(IncomingFramesReaderTest, ReadSuccessful_JumbledFramesOrdering) {
TEST_F(IncomingFramesReaderTest, JumbledFramesOrdering_ReadFromCache) {
std::optional<std::vector<uint8_t>> cancel_frame = GetCancelFrame();
ASSERT_TRUE(cancel_frame.has_value());
connection().AppendReadableData(*cancel_frame);
connection().WriteMessage(*cancel_frame);
std::optional<std::vector<uint8_t>> introduction_frame =
GetIntroductionFrame();
ASSERT_TRUE(introduction_frame.has_value());
connection().AppendReadableData(*introduction_frame);
connection().WriteMessage(*introduction_frame);
absl::Notification notification;
frames_reader()->ReadFrame(
@@ -222,7 +231,7 @@ TEST_F(IncomingFramesReaderTest, ReadAfterConnectionClosed) {
},
kTimeout);
Sync();
connection().Close();
CloseConnection();
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout));
}
@@ -244,12 +253,12 @@ TEST_F(IncomingFramesReaderTest, ReadTwoFramesWithTimeoutSuccessfully) {
std::optional<std::vector<uint8_t>> cancel_frame = GetCancelFrame();
ASSERT_TRUE(cancel_frame.has_value());
connection().AppendReadableData(*cancel_frame);
connection().WriteMessage(*cancel_frame);
std::optional<std::vector<uint8_t>> introduction_frame =
GetIntroductionFrame();
ASSERT_TRUE(introduction_frame.has_value());
connection().AppendReadableData(*introduction_frame);
connection().WriteMessage(*introduction_frame);
Sync();
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout));
@@ -268,11 +277,11 @@ TEST_F(IncomingFramesReaderTest, ReadTwoFramesWithoutTimeoutSuccessfully) {
std::optional<std::vector<uint8_t>> introduction_frame =
GetIntroductionFrame();
ASSERT_TRUE(introduction_frame.has_value());
connection().AppendReadableData(*introduction_frame);
connection().WriteMessage(*introduction_frame);
std::optional<std::vector<uint8_t>> cancel_frame = GetCancelFrame();
ASSERT_TRUE(cancel_frame.has_value());
connection().AppendReadableData(*cancel_frame);
connection().WriteMessage(*cancel_frame);
Sync();
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout));
@@ -300,7 +309,7 @@ TEST_F(IncomingFramesReaderTest, ReadInvalidFrame) {
std::optional<std::vector<uint8_t>> invalid_frame = GetInvalidFrame();
ASSERT_TRUE(invalid_frame.has_value());
connection().AppendReadableData(*invalid_frame);
connection().WriteMessage(*invalid_frame);
Sync();
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kTimeout));