ClickHouse/dbms/Disks/tests/gtest_disk.cpp

147 lines
3.1 KiB
C++
Raw Normal View History

2020-01-10 21:42:26 +00:00
#include <gtest/gtest.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
2020-01-28 16:08:30 +00:00
#include "gtest_disk.h"
2020-01-10 21:42:26 +00:00
#if !__clang__
2020-01-28 13:15:47 +00:00
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wsuggest-override"
#endif
2020-01-10 21:42:26 +00:00
template <typename T>
DB::DiskPtr createDisk();
template <>
DB::DiskPtr createDisk<DB::DiskMemory>()
{
return std::make_shared<DB::DiskMemory>("memory_disk");
}
template <>
DB::DiskPtr createDisk<DB::DiskLocal>()
{
Poco::File("tmp/").createDirectory();
return std::make_shared<DB::DiskLocal>("local_disk", "tmp/", 0);
}
template <typename T>
void destroyDisk(DB::DiskPtr & disk)
{
disk.reset();
}
template <>
void destroyDisk<DB::DiskMemory>(DB::DiskPtr & disk)
{
disk.reset();
}
2020-01-10 21:42:26 +00:00
template <>
void destroyDisk<DB::DiskLocal>(DB::DiskPtr & disk)
{
disk.reset();
Poco::File("tmp/").remove(true);
}
template <typename T>
class DiskTest : public testing::Test
{
public:
void SetUp() override { disk = createDisk<T>(); }
void TearDown() override { destroyDisk<T>(disk); }
2020-01-10 21:42:26 +00:00
DB::DiskPtr disk;
2020-01-10 21:42:26 +00:00
};
2020-03-09 02:10:20 +00:00
using DiskImplementations = testing::Types<DB::DiskMemory, DB::DiskLocal>;
2020-01-10 21:42:26 +00:00
TYPED_TEST_SUITE(DiskTest, DiskImplementations);
TYPED_TEST(DiskTest, createDirectories)
{
this->disk->createDirectories("test_dir1/");
EXPECT_TRUE(this->disk->isDirectory("test_dir1/"));
2020-01-10 21:42:26 +00:00
this->disk->createDirectories("test_dir2/nested_dir/");
EXPECT_TRUE(this->disk->isDirectory("test_dir2/nested_dir/"));
2020-01-10 21:42:26 +00:00
}
TYPED_TEST(DiskTest, writeFile)
{
{
std::unique_ptr<DB::WriteBuffer> out = this->disk->writeFile("test_file");
2020-01-10 21:42:26 +00:00
writeString("test data", *out);
}
DB::String data;
{
std::unique_ptr<DB::ReadBuffer> in = this->disk->readFile("test_file");
2020-01-10 21:42:26 +00:00
readString(data, *in);
}
EXPECT_EQ("test data", data);
EXPECT_EQ(data.size(), this->disk->getFileSize("test_file"));
2020-01-10 21:42:26 +00:00
}
2020-01-28 12:48:01 +00:00
TYPED_TEST(DiskTest, readFile)
{
2020-01-28 13:15:47 +00:00
{
std::unique_ptr<DB::WriteBuffer> out = this->disk->writeFile("test_file");
2020-01-28 13:15:47 +00:00
writeString("test data", *out);
}
// Test SEEK_SET
{
2020-02-17 15:00:39 +00:00
String buf(4, '0');
std::unique_ptr<DB::SeekableReadBuffer> in = this->disk->readFile("test_file");
2020-02-17 15:00:39 +00:00
2020-01-28 13:15:47 +00:00
in->seek(5, SEEK_SET);
2020-02-17 15:00:39 +00:00
in->readStrict(buf.data(), 4);
EXPECT_EQ("data", buf);
2020-01-28 13:15:47 +00:00
}
// Test SEEK_CUR
{
std::unique_ptr<DB::SeekableReadBuffer> in = this->disk->readFile("test_file");
2020-01-28 13:15:47 +00:00
String buf(4, '0');
in->readStrict(buf.data(), 4);
EXPECT_EQ("test", buf);
// Skip whitespace
in->seek(1, SEEK_CUR);
in->readStrict(buf.data(), 4);
EXPECT_EQ("data", buf);
}
2020-01-28 12:48:01 +00:00
}
2020-01-10 21:42:26 +00:00
TYPED_TEST(DiskTest, iterateDirectory)
{
this->disk->createDirectories("test_dir/nested_dir/");
2020-01-10 21:42:26 +00:00
{
auto iter = this->disk->iterateDirectory("");
2020-01-10 21:42:26 +00:00
EXPECT_TRUE(iter->isValid());
EXPECT_EQ("test_dir/", iter->path());
iter->next();
EXPECT_FALSE(iter->isValid());
}
{
auto iter = this->disk->iterateDirectory("test_dir/");
2020-01-10 21:42:26 +00:00
EXPECT_TRUE(iter->isValid());
EXPECT_EQ("test_dir/nested_dir/", iter->path());
iter->next();
EXPECT_FALSE(iter->isValid());
}
}