Skip to content

Commit

Permalink
Improve BufferedWriterTest to use parametrized tests and do the same …
Browse files Browse the repository at this point in the history
…random logic from D35053135 (#140)

Summary:
Pull Request resolved: #140

See diff summary of previous diff, this does the same thing where we iterate over many chunk sizes

Plus, see D35053135 (3eb2484) where we fixed flaky tests. we do the same here

Reviewed By: elliottlawrence

Differential Revision: D35058622

fbshipit-source-id: f61abee097ee6ac63a1469ff0af64d78e47794be
  • Loading branch information
adshastri authored and facebook-github-bot committed Mar 24, 2022
1 parent bd7e70c commit a82dd51
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions fbpcf/io/api/test/BufferedWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <gtest/gtest.h>
#include <stdio.h>
#include <filesystem>
#include <random>
#include <string>
#include "folly/logging/xlog.h"

Expand All @@ -17,12 +18,16 @@

namespace fbpcf::io {

TEST(BufferedWriterTest, testWritingToFile) {
std::string base_dir = IOTestHelper::getBaseDirFromPath(__FILE__);
std::string file_to_write_to =
base_dir + "data/buffered_writer_writer_test_file.txt";
auto writer = fbpcf::io::LocalFileWriter(file_to_write_to);
auto bufferedWriter = std::make_unique<BufferedWriter>(writer, 40);
inline void runBufferedWriterTest(size_t chunkSize) {
std::string baseDir = IOTestHelper::getBaseDirFromPath(__FILE__);
std::random_device rd;
std::default_random_engine defEngine(rd());
std::uniform_int_distribution<int> intDistro(1, 25000);
auto randint = intDistro(defEngine);
std::string fileToWriteTo = baseDir + "data/local_file_writer_test_file" +
std::to_string(randint) + ".txt";
auto writer = fbpcf::io::LocalFileWriter(fileToWriteTo);
auto bufferedWriter = std::make_unique<BufferedWriter>(writer, chunkSize);

std::string to_write = "this file tests the buffered writer\n";
auto buf =
Expand Down Expand Up @@ -61,10 +66,34 @@ TEST(BufferedWriterTest, testWritingToFile) {
Verify that file contents match the expected
*/
IOTestHelper::expectFileContentsMatch(
file_to_write_to,
base_dir + "data/expected_buffered_writer_test_file.txt");
fileToWriteTo, baseDir + "data/expected_buffered_writer_test_file.txt");

IOTestHelper::cleanup(file_to_write_to);
IOTestHelper::cleanup(fileToWriteTo);
}

class BufferedWriterTest
: public ::testing::TestWithParam<size_t> { // the only parameter is the
// chunk size
protected:
void SetUp() override {}

void TearDown() override {}
};

TEST_P(BufferedWriterTest, testWritingToFile) {
auto chunkSize = GetParam();

runBufferedWriterTest(chunkSize);
}

INSTANTIATE_TEST_SUITE_P(
BufferedWriterTest,
BufferedWriterTest,
::testing::Values(1, 10, 40, 100, 200, 500, 1000, 4096),
[](const testing::TestParamInfo<BufferedWriterTest::ParamType>& info) {
auto chunkSize = std::to_string(info.param);
std::string name = "Chunk_size_" + chunkSize;
return name;
});

} // namespace fbpcf::io

0 comments on commit a82dd51

Please sign in to comment.