| 1 | #if defined(__linux__) || defined(__FreeBSD__) |
|---|---|
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
| 5 | #include <Core/Defines.h> |
| 6 | #include <port/unistd.h> |
| 7 | #include <IO/ReadBufferAIO.h> |
| 8 | #include <fstream> |
| 9 | |
| 10 | namespace |
| 11 | { |
| 12 | std::string createTmpFileForEOFtest() |
| 13 | { |
| 14 | char pattern[] = "/tmp/fileXXXXXX"; |
| 15 | char * dir = ::mkdtemp(pattern); |
| 16 | return std::string(dir) + "/foo"; |
| 17 | } |
| 18 | |
| 19 | void prepare_for_eof(std::string & filename, std::string & buf) |
| 20 | { |
| 21 | static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 22 | |
| 23 | filename = createTmpFileForEOFtest(); |
| 24 | |
| 25 | size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE; |
| 26 | buf.reserve(n); |
| 27 | |
| 28 | for (size_t i = 0; i < n; ++i) |
| 29 | buf += symbols[i % symbols.length()]; |
| 30 | |
| 31 | std::ofstream out(filename.c_str()); |
| 32 | out << buf; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | } |
| 37 | TEST(ReadBufferAIOTest, TestReadAfterAIO) |
| 38 | { |
| 39 | using namespace DB; |
| 40 | std::string data; |
| 41 | std::string file_path; |
| 42 | prepare_for_eof(file_path, data); |
| 43 | ReadBufferAIO testbuf(file_path); |
| 44 | |
| 45 | std::string newdata; |
| 46 | newdata.resize(data.length()); |
| 47 | |
| 48 | size_t total_read = testbuf.read(newdata.data(), newdata.length()); |
| 49 | EXPECT_EQ(total_read, data.length()); |
| 50 | EXPECT_TRUE(testbuf.eof()); |
| 51 | |
| 52 | |
| 53 | testbuf.seek(data.length() - 100); |
| 54 | |
| 55 | std::string smalldata; |
| 56 | smalldata.resize(100); |
| 57 | size_t read_after_eof = testbuf.read(smalldata.data(), smalldata.size()); |
| 58 | EXPECT_EQ(read_after_eof, 100); |
| 59 | EXPECT_TRUE(testbuf.eof()); |
| 60 | |
| 61 | |
| 62 | testbuf.seek(0); |
| 63 | std::string repeatdata; |
| 64 | repeatdata.resize(data.length()); |
| 65 | size_t read_after_eof_big = testbuf.read(repeatdata.data(), repeatdata.size()); |
| 66 | EXPECT_EQ(read_after_eof_big, data.length()); |
| 67 | EXPECT_TRUE(testbuf.eof()); |
| 68 | } |
| 69 | |
| 70 | #endif |
| 71 |