Skip to content

Commit

Permalink
add is_full() to serializer stream
Browse files Browse the repository at this point in the history
  • Loading branch information
cmazakas committed Apr 15, 2024
1 parent 417cfb1 commit b2b46e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
8 changes: 6 additions & 2 deletions include/boost/http_proto/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,15 @@ struct serializer::stream

BOOST_HTTP_PROTO_DECL
std::size_t
capacity() const;
capacity() const noexcept;

BOOST_HTTP_PROTO_DECL
std::size_t
size() const;
size() const noexcept;

BOOST_HTTP_PROTO_DECL
bool
is_full() const noexcept;

BOOST_HTTP_PROTO_DECL
buffers_type
Expand Down
15 changes: 13 additions & 2 deletions src/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,19 +540,30 @@ start_stream(
std::size_t
serializer::
stream::
capacity() const
capacity() const noexcept
{
return sr_->tmp0_.capacity();
}

std::size_t
serializer::
stream::
size() const
size() const noexcept
{
return sr_->tmp0_.size();
}

bool
serializer::
stream::
is_full() const noexcept
{
if( sr_->is_chunked_ )
return capacity() < chunked_overhead_ + 1;

return capacity() == 0;
}

auto
serializer::
stream::
Expand Down
20 changes: 19 additions & 1 deletion test/unit/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,18 @@ struct serializer_test
BOOST_TEST_GT(stream.capacity(), 0);
BOOST_TEST_LE(stream.capacity(), sr_capacity);

auto const N = stream.size() + stream.capacity();
auto check_N = [&]
{
BOOST_TEST_EQ(
stream.capacity() + stream.size(), N);
};

std::vector<char> s; // stores complete output

auto prepare_chunk = [&]
{
BOOST_TEST(!stream.is_full());
auto mbs = stream.prepare();

auto bs = buffers::buffer_size(mbs);
Expand All @@ -294,12 +302,19 @@ struct serializer_test
mbs, buffers::const_buffer(body.data(), bs));

stream.commit(bs);
if( bs < body.size() )
BOOST_TEST(stream.is_full());
else
BOOST_TEST(!stream.is_full());

body.remove_prefix(bs);
if(! res.chunked() )
BOOST_TEST_EQ(stream.size(), bs);
else
// chunk overhead: header + \r\n
BOOST_TEST_EQ(stream.size(), bs + 18 + 2);

check_N();
};

auto consume_body_buffer = [&](
Expand Down Expand Up @@ -348,6 +363,7 @@ struct serializer_test
for(auto pos = cbs.begin(); pos != end; ++pos)
consume_body_buffer(*pos);
BOOST_TEST_EQ(stream.size(), 0);
check_N();
}

BOOST_TEST_THROWS(stream.close(), std::logic_error);
Expand Down Expand Up @@ -695,6 +711,7 @@ struct serializer_test
auto mbs = stream.prepare();
BOOST_TEST_GT(
buffers::buffer_size(mbs), 0);
BOOST_TEST(!stream.is_full());
BOOST_TEST_THROWS(
stream.commit(0), std::logic_error);

Expand Down Expand Up @@ -752,14 +769,15 @@ struct serializer_test
buffers::const_buffer(
chunk.data(),
chunk.size()));

BOOST_TEST(!stream.is_full());
BOOST_TEST_GT(n, 0);
stream.commit(n);
num_written += n;
}
};

BOOST_TEST_THROWS(push(), std::length_error);
BOOST_TEST(stream.is_full());
}
}

Expand Down

0 comments on commit b2b46e8

Please sign in to comment.