diff --git a/dbms/src/IO/tests/read_buffer_aio.cpp b/dbms/src/IO/tests/read_buffer_aio.cpp index 18b4d4ac879..aca8c26cda5 100644 --- a/dbms/src/IO/tests/read_buffer_aio.cpp +++ b/dbms/src/IO/tests/read_buffer_aio.cpp @@ -14,12 +14,13 @@ namespace { void run(); -void prepare(size_t s, std::string & directory, std::string & filename, std::string & buf); -void prepare2(std::string & directory, std::string & filename, std::string & buf); -void prepare3(std::string & directory, std::string & filename, std::string & buf); -void prepare4(std::string & directory, std::string & filename, std::string & buf); +void prepare(size_t s, std::string & filename, std::string & buf); +void prepare2(std::string & filename, std::string & buf); +void prepare3(std::string & filename, std::string & buf); +void prepare4(std::string & filename, std::string & buf); +std::string createTmpFile(); void die(const std::string & msg); -void run_test(unsigned int num, const std::function func); +void runTest(unsigned int num, const std::function func); bool test1(const std::string & filename); bool test2(const std::string & filename, const std::string & buf); @@ -46,30 +47,25 @@ void run() { namespace fs = boost::filesystem; - std::string directory; std::string filename; std::string buf; - prepare(10 * DEFAULT_AIO_FILE_BLOCK_SIZE, directory, filename, buf); + prepare(10 * DEFAULT_AIO_FILE_BLOCK_SIZE, filename, buf); - std::string directory2; std::string filename2; std::string buf2; - prepare(2 * DEFAULT_AIO_FILE_BLOCK_SIZE - 3, directory2, filename2, buf2); + prepare(2 * DEFAULT_AIO_FILE_BLOCK_SIZE - 3, filename2, buf2); - std::string directory3; std::string filename3; std::string buf3; - prepare2(directory3, filename3, buf3); + prepare2(filename3, buf3); - std::string directory4; std::string filename4; std::string buf4; - prepare3(directory4, filename4, buf4); + prepare3(filename4, buf4); - std::string directory5; std::string filename5; std::string buf5; - prepare4(directory5, filename5, buf5); + prepare4(filename5, buf5); const std::vector > tests = { @@ -99,27 +95,21 @@ void run() for (const auto & test : tests) { ++num; - run_test(num, test); + runTest(num, test); } - fs::remove_all(directory); - fs::remove_all(directory2); - fs::remove_all(directory3); - fs::remove_all(directory4); - fs::remove_all(directory5); + fs::remove_all(fs::path(filename).parent_path().string()); + fs::remove_all(fs::path(filename2).parent_path().string()); + fs::remove_all(fs::path(filename3).parent_path().string()); + fs::remove_all(fs::path(filename4).parent_path().string()); + fs::remove_all(fs::path(filename5).parent_path().string()); } -void prepare(size_t s, std::string & directory, std::string & filename, std::string & buf) +void prepare(size_t s, std::string & filename, std::string & buf) { static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - directory = std::string(dir); - filename = directory + "/foo"; + filename = createTmpFile(); size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; buf.reserve(n); @@ -134,15 +124,9 @@ void prepare(size_t s, std::string & directory, std::string & filename, std::st out << buf; } -void prepare2(std::string & directory, std::string & filename, std::string & buf) +void prepare2(std::string & filename, std::string & buf) { - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - directory = std::string(dir); - filename = directory + "/foo"; + filename = createTmpFile(); buf = "122333444455555666666777777788888888999999999"; @@ -153,15 +137,9 @@ void prepare2(std::string & directory, std::string & filename, std::string & buf out << buf; } -void prepare3(std::string & directory, std::string & filename, std::string & buf) +void prepare3(std::string & filename, std::string & buf) { - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - directory = std::string(dir); - filename = directory + "/foo"; + filename = createTmpFile(); buf = "122333444455555666666777777788888888999999999"; @@ -173,17 +151,11 @@ void prepare3(std::string & directory, std::string & filename, std::string & buf out << buf; } -void prepare4(std::string & directory, std::string & filename, std::string & buf) +void prepare4(std::string & filename, std::string & buf) { static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - directory = std::string(dir); - filename = directory + "/foo"; + filename = createTmpFile(); std::ofstream out(filename.c_str()); if (!out.is_open()) @@ -196,13 +168,23 @@ void prepare4(std::string & directory, std::string & filename, std::string & buf out << buf; } +std::string createTmpFile() +{ + char pattern[] = "/tmp/fileXXXXXX"; + char * dir = ::mkdtemp(pattern); + if (dir == nullptr) + die("Could not create directory"); + + return std::string(dir) + "/foo"; +} + void die(const std::string & msg) { std::cout << msg << "\n"; ::exit(EXIT_FAILURE); } -void run_test(unsigned int num, const std::function func) +void runTest(unsigned int num, const std::function func) { bool ok; diff --git a/dbms/src/IO/tests/write_buffer_aio.cpp b/dbms/src/IO/tests/write_buffer_aio.cpp index 53ff48d4aec..8dd34dc6864 100644 --- a/dbms/src/IO/tests/write_buffer_aio.cpp +++ b/dbms/src/IO/tests/write_buffer_aio.cpp @@ -11,9 +11,13 @@ namespace { +namespace fs = boost::filesystem; + void run(); void die(const std::string & msg); -void run_test(unsigned int num, const std::function func); +void runTest(unsigned int num, const std::function func); +std::string createTmpFile(); +std::string generateString(size_t n); bool test1(); bool test2(); @@ -46,7 +50,7 @@ void run() for (const auto & test : tests) { ++num; - run_test(num, test); + runTest(num, test); } } @@ -56,7 +60,7 @@ void die(const std::string & msg) ::exit(EXIT_FAILURE); } -void run_test(unsigned int num, const std::function func) +void runTest(unsigned int num, const std::function func) { bool ok; @@ -81,21 +85,19 @@ void run_test(unsigned int num, const std::function func) std::cout << "Test " << num << " failed\n"; } -bool test1() +std::string createTmpFile() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - char pattern[] = "/tmp/fileXXXXXX"; char * dir = ::mkdtemp(pattern); if (dir == nullptr) die("Could not create directory"); - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + return std::string(dir) + "/foo"; +} - size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; +std::string generateString(size_t n) +{ + static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; std::string buf; buf.reserve(n); @@ -103,6 +105,17 @@ bool test1() for (size_t i = 0; i < n; ++i) buf += symbols[i % symbols.length()]; + return buf; +} + +bool test1() +{ + std::string filename = createTmpFile(); + + size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; + + std::string buf = generateString(n); + { DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE); @@ -121,32 +134,18 @@ bool test1() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); return (received == buf); } bool test2() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; - std::string buf; - buf.reserve(n); - - for (size_t i = 0; i < n; ++i) - buf += symbols[i % symbols.length()]; + std::string buf = generateString(n); { DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE); @@ -168,7 +167,7 @@ bool test2() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); if (received.substr(0, buf.length() / 2) != buf.substr(0, buf.length() / 2)) return false; @@ -182,25 +181,11 @@ bool test2() bool test3() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; - std::string buf; - buf.reserve(n); - - for (size_t i = 0; i < n; ++i) - buf += symbols[i % symbols.length()]; + std::string buf = generateString(n); { DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE); @@ -229,32 +214,18 @@ bool test3() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); return (received == buf.substr(0, buf.length() / 2)); } bool test4() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; - std::string buf; - buf.reserve(n); - - for (size_t i = 0; i < n; ++i) - buf += symbols[i % symbols.length()]; + std::string buf = generateString(n); { DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE); @@ -283,7 +254,7 @@ bool test4() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); if (received.substr(0, buf.length()) != buf) return false; @@ -296,25 +267,11 @@ bool test4() bool test5() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; - std::string buf; - buf.reserve(n); - - for (size_t i = 0; i < n; ++i) - buf += symbols[i % symbols.length()]; + std::string buf = generateString(n); { DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE); @@ -335,32 +292,18 @@ bool test5() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); return received.substr(1) == buf; } bool test6() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; - std::string buf; - buf.reserve(n); - - for (size_t i = 0; i < n; ++i) - buf += symbols[i % symbols.length()]; + std::string buf = generateString(n); std::string buf2 = "1111111111"; @@ -385,7 +328,7 @@ bool test6() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); if (received.substr(3, 8 * DEFAULT_AIO_FILE_BLOCK_SIZE) != buf.substr(0, 8 * DEFAULT_AIO_FILE_BLOCK_SIZE)) return false; @@ -401,17 +344,7 @@ bool test6() bool test7() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); std::string buf2 = "11111111112222222222"; @@ -441,24 +374,14 @@ bool test7() return false; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); return true; } bool test8() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); std::string buf2 = "11111111112222222222"; @@ -488,32 +411,18 @@ bool test8() return false; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); return true; } bool test9() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); size_t n = 3 * DEFAULT_AIO_FILE_BLOCK_SIZE; - std::string buf; - buf.reserve(n); - - for (size_t i = 0; i < n; ++i) - buf += symbols[i % symbols.length()]; + std::string buf = generateString(n); std::string buf2(DEFAULT_AIO_FILE_BLOCK_SIZE + 10, '1'); @@ -538,7 +447,7 @@ bool test9() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); if (received.substr(3, 2 * DEFAULT_AIO_FILE_BLOCK_SIZE) != buf.substr(0, 2 * DEFAULT_AIO_FILE_BLOCK_SIZE)) return false; @@ -551,25 +460,11 @@ bool test9() bool test10() { - namespace fs = boost::filesystem; - - static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - char pattern[] = "/tmp/fileXXXXXX"; - char * dir = ::mkdtemp(pattern); - if (dir == nullptr) - die("Could not create directory"); - - const std::string directory = std::string(dir); - const std::string filename = directory + "/foo"; + std::string filename = createTmpFile(); size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE + 3; - std::string buf; - buf.reserve(n); - - for (size_t i = 0; i < n; ++i) - buf += symbols[i % symbols.length()]; + std::string buf = generateString(n); { DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE); @@ -589,7 +484,7 @@ bool test10() std::string received{ std::istreambuf_iterator(in), std::istreambuf_iterator() }; in.close(); - fs::remove_all(directory); + fs::remove_all(fs::path(filename).parent_path().string()); return (received == buf); }