-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
gtest_asynchronous_bounded_read_buffer.cpp
82 lines (63 loc) · 2.11 KB
/
gtest_asynchronous_bounded_read_buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <gtest/gtest.h>
#include <Disks/IO/AsynchronousBoundedReadBuffer.h>
#include <Disks/IO/ThreadPoolRemoteFSReader.h>
#include <Disks/IO/createReadBufferFromFileBase.h>
#include <IO/WriteBufferFromFile.h>
#include <Poco/TemporaryFile.h>
#include <filesystem>
using namespace DB;
namespace fs = std::filesystem;
class AsynchronousBoundedReadBufferTest : public ::testing::TestWithParam<const char *>
{
public:
AsynchronousBoundedReadBufferTest() { fs::create_directories(temp_folder.path()); }
String makeTempFile(const String & contents)
{
String path = fmt::format("{}/{}", temp_folder.path(), counter);
++counter;
WriteBufferFromFile out{path};
out.write(contents.data(), contents.size());
out.finalize();
return path;
}
private:
Poco::TemporaryFile temp_folder;
size_t counter = 0;
};
String getAlphabetWithDigits()
{
String contents;
for (char c = 'a'; c <= 'z'; ++c)
contents += c;
for (char c = '0'; c <= '9'; ++c)
contents += c;
return contents;
}
TEST_F(AsynchronousBoundedReadBufferTest, setReadUntilPosition)
{
String file_path = makeTempFile(getAlphabetWithDigits());
ThreadPoolRemoteFSReader remote_fs_reader(4, 0);
for (bool with_prefetch : {false, true})
{
AsynchronousBoundedReadBuffer read_buffer(createReadBufferFromFileBase(file_path, {}), remote_fs_reader, {});
read_buffer.setReadUntilPosition(20);
auto try_read = [&](size_t count)
{
if (with_prefetch)
read_buffer.prefetch(Priority{0});
String str;
str.resize(count);
str.resize(read_buffer.read(str.data(), str.size()));
return str;
};
EXPECT_EQ(try_read(15), "abcdefghijklmno");
EXPECT_EQ(try_read(15), "pqrst");
EXPECT_EQ(try_read(15), "");
read_buffer.setReadUntilPosition(25);
EXPECT_EQ(try_read(15), "uvwxy");
EXPECT_EQ(try_read(15), "");
read_buffer.setReadUntilEnd();
EXPECT_EQ(try_read(15), "z0123456789");
EXPECT_EQ(try_read(15), "");
}
}