Weave port [4/n]: Introduce weave write request and single packet write request

PiperOrigin-RevId: 530439848
This commit is contained in:
Anay Wadhera
2023-05-08 16:33:42 -07:00
committed by Copybara-Service
parent 072d24238b
commit 332da370a4
4 changed files with 119 additions and 0 deletions
+1
View File
@@ -573,6 +573,7 @@ let package = Package(
"internal/test/fake_timer_test.cc",
"internal/test/fake_device_info_test.cc",
"internal/test/fake_task_runner_test.cc",
"internal/weave/control_packet_write_request_test.cc",
"internal/weave/packet_test.cc",
"internal/weave/packet_sequence_number_generator_test.cc",
"internal/weave/packetizer_test.cc",
+15
View File
@@ -6,6 +6,7 @@ cc_library(
"packetizer.cc",
],
hdrs = [
"control_packet_write_request.h",
"packet.h",
"packet_sequence_number_generator.h",
"packetizer.h",
@@ -62,3 +63,17 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "control_packet_write_request_test",
srcs = [
"control_packet_write_request_test.cc",
],
deps = [
":weave",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/status",
"@com_google_googletest//:gtest_main",
],
)
@@ -0,0 +1,49 @@
// Copyright 2023 Google LLC
//
// 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
//
// https://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.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_WEAVE_SINGLE_PACKET_WRITE_REQUEST_H_
#define THIRD_PARTY_NEARBY_INTERNAL_WEAVE_SINGLE_PACKET_WRITE_REQUEST_H_
#include <utility>
#include "absl/status/status.h"
#include "internal/weave/packet.h"
namespace nearby {
namespace weave {
// This class encapsulates a Weave control packet to be sent over the wire.
// Note that this means it is only responsible for writing one packet.
class ControlPacketWriteRequest {
public:
explicit ControlPacketWriteRequest(Packet packet)
: control_packet_(std::move(packet)) {}
absl::StatusOr<Packet> NextPacket(int) {
if (is_started_) {
return absl::OutOfRangeError("Packet was already sent");
}
is_started_ = true;
return std::move(control_packet_);
}
private:
Packet control_packet_;
bool is_started_ = false;
};
} // namespace weave
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_WEAVE_SINGLE_PACKET_WRITE_REQUEST_H_
@@ -0,0 +1,54 @@
// Copyright 2023 Google LLC
//
// 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
//
// https://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.
#include "internal/weave/control_packet_write_request.h"
#include <string>
#include <type_traits>
#include <utility>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "internal/weave/packet.h"
namespace nearby {
namespace weave {
namespace {
TEST(ControlPacketWriteRequestTest, WriteRequestIsNotTriviallyConstructible) {
EXPECT_FALSE(
std::is_trivially_constructible<ControlPacketWriteRequest>::value);
}
TEST(ControlPacketWriteRequestTest, WriteRequestWorks) {
Packet packet = Packet::CreateConnectionRequestPacket(1, 1, 15, "").value();
std::string packet_bytes = packet.GetBytes();
auto request = ControlPacketWriteRequest(std::move(packet));
EXPECT_EQ(packet_bytes, request.NextPacket(15).value().GetBytes());
}
TEST(ControlPacketWriteRequestTest, TestOutOfRange) {
Packet packet = Packet::CreateConnectionRequestPacket(1, 1, 15, "").value();
std::string packet_bytes = packet.GetBytes();
auto request = ControlPacketWriteRequest(std::move(packet));
EXPECT_EQ(packet_bytes, request.NextPacket(15).value().GetBytes());
EXPECT_THAT(request.NextPacket(15),
testing::status::StatusIs(absl::StatusCode::kOutOfRange));
}
} // namespace
} // namespace weave
} // namespace nearby