2015-03-06 15:34:42 +00:00
|
|
|
#include <DB/IO/ReadBufferAIO.h>
|
2015-03-13 11:02:43 +00:00
|
|
|
#include <DB/Core/Defines.h>
|
|
|
|
|
2015-03-11 13:51:59 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2015-03-06 15:34:42 +00:00
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
#include <vector>
|
2015-03-06 15:34:42 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2015-03-10 13:00:29 +00:00
|
|
|
#include <functional>
|
2015-03-06 15:34:42 +00:00
|
|
|
#include <cstdlib>
|
2015-03-10 13:16:49 +00:00
|
|
|
#include <unistd.h>
|
2015-03-06 15:34:42 +00:00
|
|
|
|
2015-03-10 13:00:29 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
void run();
|
2015-03-17 11:30:23 +00:00
|
|
|
void prepare(size_t s, std::string & directory, std::string & filename, std::string & buf);
|
2015-03-10 13:00:29 +00:00
|
|
|
void die(const std::string & msg);
|
2015-03-10 16:34:00 +00:00
|
|
|
void run_test(unsigned int num, const std::function<bool()> func);
|
2015-03-10 15:09:17 +00:00
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
bool test1(const std::string & filename);
|
2015-03-10 13:00:29 +00:00
|
|
|
bool test2(const std::string & filename, const std::string & buf);
|
2015-03-10 15:09:17 +00:00
|
|
|
bool test3(const std::string & filename, const std::string & buf);
|
2015-03-11 14:47:52 +00:00
|
|
|
bool test4(const std::string & filename, const std::string & buf);
|
|
|
|
bool test5(const std::string & filename);
|
2015-03-10 15:09:17 +00:00
|
|
|
bool test6(const std::string & filename, const std::string & buf);
|
2015-03-11 14:47:52 +00:00
|
|
|
bool test7(const std::string & filename, const std::string & buf);
|
2015-03-17 11:30:23 +00:00
|
|
|
bool test8(const std::string & filename, const std::string & buf);
|
2015-03-11 14:47:52 +00:00
|
|
|
bool test9(const std::string & filename, const std::string & buf);
|
2015-03-12 14:09:22 +00:00
|
|
|
bool test10(const std::string & filename, const std::string & buf);
|
2015-03-12 15:38:22 +00:00
|
|
|
bool test11(const std::string & filename);
|
|
|
|
bool test12(const std::string & filename, const std::string & buf);
|
2015-03-17 11:30:23 +00:00
|
|
|
bool test13(const std::string & filename, const std::string & buf);
|
2015-03-10 13:00:29 +00:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2015-03-11 13:51:59 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
std::string directory;
|
2015-03-10 13:00:29 +00:00
|
|
|
std::string filename;
|
|
|
|
std::string buf;
|
2015-03-17 11:30:23 +00:00
|
|
|
prepare(10 * DEFAULT_AIO_FILE_BLOCK_SIZE, directory, filename, buf);
|
|
|
|
|
|
|
|
std::string directory2;
|
|
|
|
std::string filename2;
|
|
|
|
std::string buf2;
|
|
|
|
prepare(2 * DEFAULT_AIO_FILE_BLOCK_SIZE - 2, directory2, filename2, buf2);
|
2015-03-10 13:00:29 +00:00
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
const std::vector<std::function<bool()> > tests =
|
|
|
|
{
|
|
|
|
std::bind(test1, std::ref(filename)),
|
|
|
|
std::bind(test2, std::ref(filename), std::ref(buf)),
|
|
|
|
std::bind(test3, std::ref(filename), std::ref(buf)),
|
2015-03-11 14:47:52 +00:00
|
|
|
std::bind(test4, std::ref(filename), std::ref(buf)),
|
|
|
|
std::bind(test5, std::ref(filename)),
|
2015-03-10 16:34:00 +00:00
|
|
|
std::bind(test6, std::ref(filename), std::ref(buf)),
|
2015-03-11 14:47:52 +00:00
|
|
|
std::bind(test7, std::ref(filename), std::ref(buf)),
|
2015-03-17 11:30:23 +00:00
|
|
|
std::bind(test8, std::ref(filename), std::ref(buf)),
|
2015-03-12 14:09:22 +00:00
|
|
|
std::bind(test9, std::ref(filename), std::ref(buf)),
|
2015-03-12 15:38:22 +00:00
|
|
|
std::bind(test10, std::ref(filename), std::ref(buf)),
|
|
|
|
std::bind(test11, std::ref(filename)),
|
2015-03-17 11:30:23 +00:00
|
|
|
std::bind(test12, std::ref(filename), std::ref(buf)),
|
|
|
|
std::bind(test13, std::ref(filename2), std::ref(buf2))
|
2015-03-10 16:34:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int num = 0;
|
|
|
|
for (const auto & test : tests)
|
|
|
|
{
|
|
|
|
++num;
|
|
|
|
run_test(num, test);
|
|
|
|
}
|
2015-03-10 13:16:49 +00:00
|
|
|
|
2015-03-11 13:51:59 +00:00
|
|
|
fs::remove_all(directory);
|
2015-03-17 11:30:23 +00:00
|
|
|
fs::remove_all(directory2);
|
2015-03-10 13:00:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:30:23 +00:00
|
|
|
void prepare(size_t s, std::string & directory, std::string & filename, std::string & buf)
|
2015-03-10 13:00:29 +00:00
|
|
|
{
|
2015-03-10 16:34:00 +00:00
|
|
|
static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
2015-03-10 13:00:29 +00:00
|
|
|
|
|
|
|
char pattern[] = "/tmp/fileXXXXXX";
|
|
|
|
char * dir = ::mkdtemp(pattern);
|
|
|
|
if (dir == nullptr)
|
|
|
|
die("Could not create directory");
|
|
|
|
|
2015-03-11 13:51:59 +00:00
|
|
|
directory = std::string(dir);
|
|
|
|
filename = directory + "/foo";
|
2015-03-10 13:00:29 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-10 13:00:29 +00:00
|
|
|
buf.reserve(n);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
2015-03-10 16:34:00 +00:00
|
|
|
buf += symbols[i % symbols.length()];
|
2015-03-10 13:00:29 +00:00
|
|
|
|
|
|
|
std::ofstream out(filename.c_str());
|
|
|
|
if (!out.is_open())
|
|
|
|
die("Could not open file");
|
|
|
|
|
|
|
|
out << buf;
|
|
|
|
}
|
2015-03-06 15:34:42 +00:00
|
|
|
|
|
|
|
void die(const std::string & msg)
|
|
|
|
{
|
2015-03-10 13:00:29 +00:00
|
|
|
std::cout << msg << "\n";
|
2015-03-06 15:34:42 +00:00
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
void run_test(unsigned int num, const std::function<bool()> func)
|
2015-03-06 15:34:42 +00:00
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-03-10 13:00:29 +00:00
|
|
|
ok = func();
|
2015-03-06 15:34:42 +00:00
|
|
|
}
|
|
|
|
catch (const DB::Exception & ex)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
std::cout << "Caught exception " << ex.displayText() << "\n";
|
|
|
|
}
|
|
|
|
catch (const std::exception & ex)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
std::cout << "Caught exception " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok)
|
2015-03-10 16:34:00 +00:00
|
|
|
std::cout << "Test " << num << " passed\n";
|
2015-03-06 15:34:42 +00:00
|
|
|
else
|
2015-03-10 16:34:00 +00:00
|
|
|
std::cout << "Test " << num << " failed\n";
|
2015-03-10 11:43:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
bool test1(const std::string & filename)
|
2015-03-10 15:09:17 +00:00
|
|
|
{
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-10 15:09:17 +00:00
|
|
|
if (in.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (in.getFD() == -1)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test2(const std::string & filename, const std::string & buf)
|
2015-03-10 11:43:30 +00:00
|
|
|
{
|
2015-03-10 13:16:49 +00:00
|
|
|
std::string newbuf;
|
|
|
|
newbuf.resize(buf.length());
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 14:09:22 +00:00
|
|
|
size_t count = in.read(&newbuf[0], newbuf.length());
|
|
|
|
if (count != newbuf.length())
|
|
|
|
return false;
|
2015-03-10 13:00:29 +00:00
|
|
|
|
2015-03-10 13:16:49 +00:00
|
|
|
return (newbuf == buf);
|
2015-03-10 13:00:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 15:09:17 +00:00
|
|
|
bool test3(const std::string & filename, const std::string & buf)
|
2015-03-10 13:00:29 +00:00
|
|
|
{
|
2015-03-10 13:16:49 +00:00
|
|
|
std::string newbuf;
|
|
|
|
newbuf.resize(buf.length());
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
size_t requested = 9 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-10 13:00:29 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-10 13:16:49 +00:00
|
|
|
in.setMaxBytes(requested);
|
2015-03-12 14:09:22 +00:00
|
|
|
size_t count = in.read(&newbuf[0], newbuf.length());
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2015-03-12 14:09:22 +00:00
|
|
|
newbuf.resize(count);
|
2015-03-10 13:16:49 +00:00
|
|
|
return (newbuf == buf.substr(0, requested));
|
2015-03-10 13:00:29 +00:00
|
|
|
}
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2015-03-11 14:47:52 +00:00
|
|
|
bool test4(const std::string & filename, const std::string & buf)
|
|
|
|
{
|
|
|
|
std::string newbuf;
|
|
|
|
newbuf.resize(buf.length());
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-11 14:47:52 +00:00
|
|
|
in.setMaxBytes(0);
|
|
|
|
size_t n_read = in.read(&newbuf[0], newbuf.length());
|
|
|
|
|
|
|
|
return n_read == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test5(const std::string & filename)
|
2015-03-10 15:09:17 +00:00
|
|
|
{
|
|
|
|
bool ok = false;
|
2015-03-10 15:29:31 +00:00
|
|
|
|
2015-03-10 15:09:17 +00:00
|
|
|
try
|
|
|
|
{
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
in.setMaxBytes(DEFAULT_AIO_FILE_BLOCK_SIZE >> 1);
|
2015-03-10 15:09:17 +00:00
|
|
|
}
|
|
|
|
catch (const DB::Exception &)
|
|
|
|
{
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:47:52 +00:00
|
|
|
bool test6(const std::string & filename, const std::string & buf)
|
2015-03-10 15:09:17 +00:00
|
|
|
{
|
|
|
|
std::string newbuf;
|
|
|
|
newbuf.resize(buf.length());
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-10 15:09:17 +00:00
|
|
|
|
|
|
|
if (in.getPositionInFile() != 0)
|
|
|
|
return false;
|
|
|
|
|
2015-03-12 14:09:22 +00:00
|
|
|
size_t count = in.read(&newbuf[0], newbuf.length());
|
|
|
|
if (count != newbuf.length())
|
|
|
|
return false;
|
2015-03-10 15:09:17 +00:00
|
|
|
|
2015-03-12 12:36:18 +00:00
|
|
|
if (static_cast<size_t>(in.getPositionInFile()) != buf.length())
|
2015-03-10 15:09:17 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:47:52 +00:00
|
|
|
bool test7(const std::string & filename, const std::string & buf)
|
2015-03-10 15:09:17 +00:00
|
|
|
{
|
|
|
|
std::string newbuf;
|
2015-03-13 11:02:43 +00:00
|
|
|
newbuf.resize(buf.length() - DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-10 15:09:17 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
(void) in.seek(DEFAULT_AIO_FILE_BLOCK_SIZE, SEEK_SET);
|
2015-03-12 14:09:22 +00:00
|
|
|
size_t count = in.read(&newbuf[0], newbuf.length());
|
2015-03-13 11:02:43 +00:00
|
|
|
if (count != (9 * DEFAULT_AIO_FILE_BLOCK_SIZE))
|
2015-03-12 14:09:22 +00:00
|
|
|
return false;
|
2015-03-10 15:09:17 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
return (newbuf == buf.substr(DEFAULT_AIO_FILE_BLOCK_SIZE));
|
2015-03-10 15:09:17 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:30:23 +00:00
|
|
|
bool test8(const std::string & filename, const std::string & buf)
|
2015-03-10 15:29:31 +00:00
|
|
|
{
|
2015-03-17 11:30:23 +00:00
|
|
|
std::string newbuf;
|
|
|
|
newbuf.resize(DEFAULT_AIO_FILE_BLOCK_SIZE - 1);
|
2015-03-10 15:29:31 +00:00
|
|
|
|
2015-03-17 11:30:23 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
(void) in.seek(DEFAULT_AIO_FILE_BLOCK_SIZE + 1, SEEK_CUR);
|
|
|
|
size_t count = in.read(&newbuf[0], newbuf.length());
|
2015-03-10 15:29:31 +00:00
|
|
|
|
2015-03-17 11:30:23 +00:00
|
|
|
if (count != newbuf.length())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (newbuf != buf.substr(DEFAULT_AIO_FILE_BLOCK_SIZE + 1, newbuf.length()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2015-03-10 15:29:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:47:52 +00:00
|
|
|
bool test9(const std::string & filename, const std::string & buf)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string newbuf;
|
|
|
|
newbuf.resize(buf.length());
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 14:09:22 +00:00
|
|
|
size_t count = in.read(&newbuf[0], newbuf.length());
|
|
|
|
if (count != newbuf.length())
|
|
|
|
return false;
|
2015-03-13 11:02:43 +00:00
|
|
|
in.setMaxBytes(9 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-11 14:47:52 +00:00
|
|
|
}
|
|
|
|
catch (const DB::Exception &)
|
|
|
|
{
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-03-12 14:09:22 +00:00
|
|
|
bool test10(const std::string & filename, const std::string & buf)
|
|
|
|
{
|
|
|
|
std::string newbuf;
|
2015-03-13 11:02:43 +00:00
|
|
|
newbuf.resize(4 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 14:09:22 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 14:09:22 +00:00
|
|
|
size_t count1 = in.read(&newbuf[0], newbuf.length());
|
|
|
|
if (count1 != newbuf.length())
|
|
|
|
return false;
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
if (newbuf != buf.substr(0, 4 * DEFAULT_AIO_FILE_BLOCK_SIZE))
|
2015-03-12 14:09:22 +00:00
|
|
|
return false;
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
(void) in.seek(2 * DEFAULT_AIO_FILE_BLOCK_SIZE, SEEK_CUR);
|
2015-03-12 14:09:22 +00:00
|
|
|
|
|
|
|
size_t count2 = in.read(&newbuf[0], newbuf.length());
|
|
|
|
if (count2 != newbuf.length())
|
|
|
|
return false;
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
if (newbuf != buf.substr(6 * DEFAULT_AIO_FILE_BLOCK_SIZE))
|
2015-03-12 14:09:22 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-12 15:38:22 +00:00
|
|
|
bool test11(const std::string & filename)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
(void) in.seek(-DEFAULT_AIO_FILE_BLOCK_SIZE, SEEK_SET);
|
2015-03-12 15:38:22 +00:00
|
|
|
}
|
|
|
|
catch (const DB::Exception &)
|
|
|
|
{
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test12(const std::string & filename, const std::string & buf)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string newbuf;
|
2015-03-13 11:02:43 +00:00
|
|
|
newbuf.resize(4 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
DB::ReadBufferAIO in(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 15:38:22 +00:00
|
|
|
size_t count = in.read(&newbuf[0], newbuf.length());
|
|
|
|
if (count != newbuf.length())
|
|
|
|
return false;
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
(void) in.seek(-(10 * DEFAULT_AIO_FILE_BLOCK_SIZE), SEEK_CUR);
|
2015-03-12 15:38:22 +00:00
|
|
|
}
|
|
|
|
catch (const DB::Exception &)
|
|
|
|
{
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:30:23 +00:00
|
|
|
bool test13(const std::string & filename, const std::string & buf)
|
|
|
|
{
|
|
|
|
std::string newbuf;
|
|
|
|
newbuf.resize(2 * DEFAULT_AIO_FILE_BLOCK_SIZE - 2);
|
|
|
|
|
|
|
|
DB::ReadBufferAIO in(filename, DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
size_t count1 = in.read(&newbuf[0], newbuf.length());
|
|
|
|
if (count1 != newbuf.size())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-10 11:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2015-03-10 13:00:29 +00:00
|
|
|
run();
|
2015-03-06 15:34:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|