1
0
mirror of https://github.com/facebookincubator/mvfst.git synced 2025-11-07 22:46:22 +03:00
Files
mvfst/quic/api/test/IoBufQuicBatchTest.cpp
Yang Chi efa466158e QUIC IoBufQuicBatch no need to have a ref to connection state
Summary:
all it wants is just a pointer to the statsCallback member

(Note: this ignores all push blocking failures!)

Reviewed By: mjoras

Differential Revision: D27786211

fbshipit-source-id: 3364681348ee01684d6cb8d2837bee9549f64e3a
2021-04-28 08:06:27 -07:00

98 lines
2.4 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
#include <quic/api/IoBufQuicBatch.h>
#include <gtest/gtest.h>
#include <quic/client/state/ClientStateMachine.h>
#include <quic/fizz/client/handshake/FizzClientQuicHandshakeContext.h>
#include <quic/state/StateData.h>
constexpr const auto kNumLoops = 64;
constexpr const auto kMaxBufs = 10;
namespace quic {
namespace testing {
class TestPacketBatchWriter : public IOBufBatchWriter {
public:
explicit TestPacketBatchWriter(int maxBufs) : maxBufs_(maxBufs) {}
~TestPacketBatchWriter() override {
CHECK_EQ(bufNum_, 0);
CHECK_EQ(bufSize_, 0);
}
void reset() override {
bufNum_ = 0;
bufSize_ = 0;
}
bool append(
std::unique_ptr<folly::IOBuf>&& /*unused*/,
size_t size,
const folly::SocketAddress& /*unused*/,
folly::AsyncUDPSocket* /*unused*/) override {
bufNum_++;
bufSize_ += size;
return ((maxBufs_ < 0) || (bufNum_ >= maxBufs_));
}
ssize_t write(
folly::AsyncUDPSocket& /*unused*/,
const folly::SocketAddress& /*unused*/) override {
return bufSize_;
}
private:
int maxBufs_{0};
int bufNum_{0};
size_t bufSize_{0};
};
void RunTest(int numBatch) {
folly::EventBase evb;
folly::AsyncUDPSocket sock(&evb);
auto batchWriter = BatchWriterPtr(new TestPacketBatchWriter(numBatch));
folly::SocketAddress peerAddress{"127.0.0.1", 1234};
QuicClientConnectionState conn(
FizzClientQuicHandshakeContext::Builder().build());
QuicConnectionStateBase::HappyEyeballsState happyEyeballsState;
IOBufQuicBatch ioBufBatch(
std::move(batchWriter),
false,
sock,
peerAddress,
conn.statsCallback,
happyEyeballsState);
std::string strTest("Test");
for (size_t i = 0; i < kNumLoops; i++) {
auto buf = folly::IOBuf::copyBuffer(strTest.c_str(), strTest.length());
CHECK(ioBufBatch.write(std::move(buf), strTest.length()));
}
// check flush is successful
CHECK(ioBufBatch.flush());
// check we sent all the packets
CHECK_EQ(ioBufBatch.getPktSent(), kNumLoops);
}
TEST(QuicBatch, TestBatchingNone) {
RunTest(1);
}
TEST(QuicBatch, TestBatchingNoFlush) {
RunTest(-1);
}
TEST(QuicBatch, TestBatching) {
RunTest(kMaxBufs);
}
} // namespace testing
} // namespace quic