Skip to content

Commit

Permalink
test: Fix leak in io_queue_test
Browse files Browse the repository at this point in the history
The test allocates IO buffers for plain "new" call and doesn't
release them ever. The fix is in keeping the buffers in unique
ptrs and holding them until corresponding IO's future resolves.

tests: unit(debug)

Signed-off-by: Pavel Emelyanov <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
xemul authored and avikivity committed Apr 5, 2021
1 parent a74b149 commit 9900b2e
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions tests/unit/io_queue_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ template <size_t Len>
struct fake_file {
int data[Len] = {};

static internal::io_request make_write_req(size_t idx, int val) {
int* buf = new int(val);
static internal::io_request make_write_req(size_t idx, int* buf) {
return internal::io_request::make_write(0, idx, buf, 1, false);
}

Expand All @@ -66,7 +65,8 @@ SEASTAR_THREAD_TEST_CASE(test_basic_flow) {
io_queue_for_tests tio;
fake_file<1> file;

auto f = tio.queue.queue_request(default_priority_class(), 0, file.make_write_req(0, 42), nullptr)
auto val = std::make_unique<int>(42);
auto f = tio.queue.queue_request(default_priority_class(), 0, file.make_write_req(0, val.get()), nullptr)
.then([&file] (size_t len) {
BOOST_REQUIRE(file.data[0] == 42);
});
Expand Down Expand Up @@ -145,8 +145,9 @@ SEASTAR_THREAD_TEST_CASE(test_io_cancellation) {
std::vector<future<>> cancelled;

auto queue_legacy_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, val), nullptr)
.then([&file, idx, val] (size_t len) {
auto buf = std::make_unique<int>(val);
auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, buf.get()), nullptr)
.then([&file, idx, val, buf = std::move(buf)] (size_t len) {
BOOST_REQUIRE(file.data[idx] == val);
return make_ready_future<>();
});
Expand All @@ -156,8 +157,9 @@ SEASTAR_THREAD_TEST_CASE(test_io_cancellation) {
};

auto queue_live_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, val), &live)
.then([&file, idx, val] (size_t len) {
auto buf = std::make_unique<int>(val);
auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, buf.get()), &live)
.then([&file, idx, val, buf = std::move(buf)] (size_t len) {
BOOST_REQUIRE(file.data[idx] == val);
return make_ready_future<>();
});
Expand All @@ -167,8 +169,9 @@ SEASTAR_THREAD_TEST_CASE(test_io_cancellation) {
};

auto queue_dead_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, val), &dead)
.then_wrapped([] (auto&& f) {
auto buf = std::make_unique<int>(val);
auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, buf.get()), &dead)
.then_wrapped([buf = std::move(buf)] (auto&& f) {
try {
f.get();
BOOST_REQUIRE(false);
Expand Down

0 comments on commit 9900b2e

Please sign in to comment.