-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
264 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2024 gRPC authors. | ||
// | ||
// 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 | ||
// | ||
// http://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 "test/core/transport/chaotic_good/mock_frame_transport.h" | ||
|
||
#include "gtest/gtest.h" | ||
#include "src/core/lib/promise/loop.h" | ||
#include "src/core/lib/promise/try_seq.h" | ||
|
||
namespace grpc_core { | ||
namespace chaotic_good { | ||
namespace testing { | ||
|
||
MockFrameTransport::~MockFrameTransport() { | ||
while (!expected_writes_.empty()) { | ||
auto& w = expected_writes_.front(); | ||
ADD_FAILURE_AT(w.whence.file(), w.whence.line()) | ||
<< "Expected write of " | ||
<< absl::ConvertVariantTo<FrameInterface&>(w.frame).ToString(); | ||
expected_writes_.pop(); | ||
} | ||
if (on_read_done_ != nullptr) { | ||
on_read_done_(absl::OkStatus()); | ||
} | ||
} | ||
|
||
void MockFrameTransport::StartReading( | ||
Party*, MpscSender<IncomingFrame> frames, | ||
absl::AnyInvocable<void(absl::Status)> on_done) { | ||
reader_ = std::move(frames); | ||
on_read_done_ = std::move(on_done); | ||
} | ||
|
||
void MockFrameTransport::StartWriting( | ||
Party* party, MpscReceiver<Frame> frames, | ||
absl::AnyInvocable<void(absl::Status)> on_done) { | ||
party->Spawn( | ||
"MockFrameTransport_Writer", | ||
[this, frames = std::move(frames)]() mutable { | ||
return Loop([this, frames = std::move(frames)]() mutable { | ||
return TrySeq( | ||
frames.Next(), [this](Frame frame) -> LoopCtl<absl::Status> { | ||
if (expected_writes_.empty()) { | ||
ADD_FAILURE() | ||
<< "Unexpected write of " | ||
<< absl::ConvertVariantTo<FrameInterface&>(frame) | ||
.ToString(); | ||
return Continue{}; | ||
} | ||
auto expected = std::move(expected_writes_.front()); | ||
expected_writes_.pop(); | ||
EXPECT_EQ(expected.frame, frame) | ||
<< " from " << expected.whence.file() << ":" | ||
<< expected.whence.line(); | ||
return Continue{}; | ||
}); | ||
}); | ||
}, | ||
std::move(on_done)); | ||
} | ||
|
||
void MockFrameTransport::Read(Frame frame) { | ||
SliceBuffer buffer; | ||
auto& frame_interface = absl::ConvertVariantTo<FrameInterface&>(frame); | ||
frame_interface.SerializePayload(buffer); | ||
reader_.UnbufferedImmediateSend( | ||
IncomingFrame(frame_interface.MakeHeader(), std::move(buffer))); | ||
} | ||
|
||
} // namespace testing | ||
} // namespace chaotic_good | ||
} // namespace grpc_core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2024 gRPC authors. | ||
// | ||
// 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 | ||
// | ||
// http://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 GRPC_TEST_CORE_TRANSPORT_CHAOTIC_GOOD_MOCK_FRAME_TRANSPORT_H | ||
#define GRPC_TEST_CORE_TRANSPORT_CHAOTIC_GOOD_MOCK_FRAME_TRANSPORT_H | ||
|
||
#include <google/protobuf/text_format.h> | ||
|
||
#include <queue> | ||
|
||
#include "src/core/ext/transport/chaotic_good/frame_transport.h" | ||
|
||
namespace grpc_core { | ||
namespace chaotic_good { | ||
namespace testing { | ||
|
||
class MockFrameTransport final : public FrameTransport { | ||
public: | ||
~MockFrameTransport() override; | ||
|
||
void StartReading(Party* party, MpscSender<IncomingFrame> frames, | ||
absl::AnyInvocable<void(absl::Status)> on_done) final; | ||
void StartWriting(Party* party, MpscReceiver<Frame> frames, | ||
absl::AnyInvocable<void(absl::Status)> on_done) final; | ||
|
||
void ExpectWrite(Frame frame, SourceLocation whence = {}) { | ||
expected_writes_.emplace(std::move(frame), whence); | ||
} | ||
void Read(Frame frame); | ||
|
||
private: | ||
struct ExpectedWrite { | ||
ExpectedWrite(Frame frame, SourceLocation whence) | ||
: frame(std::move(frame)), whence(whence) {} | ||
Frame frame; | ||
SourceLocation whence; | ||
}; | ||
std::queue<ExpectedWrite> expected_writes_; | ||
MpscSender<IncomingFrame> reader_; | ||
absl::AnyInvocable<void(absl::Status)> on_read_done_; | ||
}; | ||
|
||
template <typename T> | ||
Frame MakeProtoFrame(std::string body) { | ||
T frame; | ||
CHECK(google::protobuf::TextFormat::ParseFromString(body, &frame.body)); | ||
return frame; | ||
} | ||
|
||
template <typename T> | ||
Frame MakeProtoFrame(uint32_t stream_id, std::string body) { | ||
T frame; | ||
CHECK(google::protobuf::TextFormat::ParseFromString(body, &frame.body)); | ||
frame.stream_id = stream_id; | ||
return frame; | ||
} | ||
|
||
inline Frame MakeMessageFrame(uint32_t stream_id, absl::string_view payload) { | ||
MessageFrame frame; | ||
frame.message = Arena::MakePooled<Message>( | ||
SliceBuffer(Slice::FromCopiedString(payload)), 0); | ||
frame.stream_id = stream_id; | ||
return frame; | ||
} | ||
|
||
} // namespace testing | ||
} // namespace chaotic_good | ||
} // namespace grpc_core | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.