提交 f25b96be 编写于 作者: A Alexey Milovidov

Moved a few tests to gtest

上级 b76d5a7c
...@@ -41,9 +41,6 @@ target_link_libraries (compact_array PRIVATE clickhouse_common_io ${Boost_FILESY ...@@ -41,9 +41,6 @@ target_link_libraries (compact_array PRIVATE clickhouse_common_io ${Boost_FILESY
add_executable (radix_sort radix_sort.cpp) add_executable (radix_sort radix_sort.cpp)
target_link_libraries (radix_sort PRIVATE clickhouse_common_io) target_link_libraries (radix_sort PRIVATE clickhouse_common_io)
add_executable (shell_command_test shell_command_test.cpp)
target_link_libraries (shell_command_test PRIVATE clickhouse_common_io)
add_executable (arena_with_free_lists arena_with_free_lists.cpp) add_executable (arena_with_free_lists arena_with_free_lists.cpp)
target_link_libraries (arena_with_free_lists PRIVATE clickhouse_compression clickhouse_common_io) target_link_libraries (arena_with_free_lists PRIVATE clickhouse_compression clickhouse_common_io)
...@@ -53,18 +50,6 @@ target_link_libraries (pod_array PRIVATE clickhouse_common_io) ...@@ -53,18 +50,6 @@ target_link_libraries (pod_array PRIVATE clickhouse_common_io)
add_executable (thread_creation_latency thread_creation_latency.cpp) add_executable (thread_creation_latency thread_creation_latency.cpp)
target_link_libraries (thread_creation_latency PRIVATE clickhouse_common_io) target_link_libraries (thread_creation_latency PRIVATE clickhouse_common_io)
add_executable (thread_pool thread_pool.cpp)
target_link_libraries (thread_pool PRIVATE clickhouse_common_io)
add_executable (thread_pool_2 thread_pool_2.cpp)
target_link_libraries (thread_pool_2 PRIVATE clickhouse_common_io)
add_executable (thread_pool_3 thread_pool_3.cpp)
target_link_libraries (thread_pool_3 PRIVATE clickhouse_common_io)
add_executable (thread_pool_schedule_exception thread_pool_schedule_exception.cpp)
target_link_libraries (thread_pool_schedule_exception PRIVATE clickhouse_common_io)
add_executable (multi_version multi_version.cpp) add_executable (multi_version multi_version.cpp)
target_link_libraries (multi_version PRIVATE clickhouse_common_io) target_link_libraries (multi_version PRIVATE clickhouse_common_io)
add_check(multi_version) add_check(multi_version)
......
...@@ -4,48 +4,62 @@ ...@@ -4,48 +4,62 @@
#include <IO/copyData.h> #include <IO/copyData.h>
#include <IO/WriteBufferFromFileDescriptor.h> #include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/ReadBufferFromString.h> #include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef __clang__
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma clang diagnostic ignored "-Wundef"
#endif
#include <gtest/gtest.h>
using namespace DB; using namespace DB;
int main(int, char **) TEST(ShellCommand, Execute)
try
{ {
{ auto command = ShellCommand::execute("echo 'Hello, world!'");
auto command = ShellCommand::execute("echo 'Hello, world!'");
WriteBufferFromFileDescriptor out(STDOUT_FILENO); std::string res;
copyData(command->out, out); readStringUntilEOF(res, command->out);
command->wait();
command->wait(); EXPECT_EQ(res, "Hello, world!\n");
} }
{ TEST(ShellCommand, ExecuteDirect)
auto command = ShellCommand::executeDirect("/bin/echo", {"Hello, world!"}); {
auto command = ShellCommand::executeDirect("/bin/echo", {"Hello, world!"});
WriteBufferFromFileDescriptor out(STDOUT_FILENO); std::string res;
copyData(command->out, out); readStringUntilEOF(res, command->out);
command->wait();
command->wait(); EXPECT_EQ(res, "Hello, world!\n");
} }
{ TEST(ShellCommand, ExecuteWithInput)
auto command = ShellCommand::execute("cat"); {
auto command = ShellCommand::execute("cat");
String in_str = "Hello, world!\n"; String in_str = "Hello, world!\n";
ReadBufferFromString in(in_str); ReadBufferFromString in(in_str);
copyData(in, command->in); copyData(in, command->in);
command->in.close(); command->in.close();
WriteBufferFromFileDescriptor out(STDOUT_FILENO); std::string res;
copyData(command->out, out); readStringUntilEOF(res, command->out);
command->wait();
command->wait(); EXPECT_EQ(res, "Hello, world!\n");
} }
TEST(ShellCommand, AutoWait)
{
// <defunct> hunting: // <defunct> hunting:
for (int i = 0; i < 1000; ++i) for (int i = 0; i < 1000; ++i)
{ {
...@@ -56,8 +70,3 @@ try ...@@ -56,8 +70,3 @@ try
// std::cerr << "inspect me: ps auxwwf" << "\n"; // std::cerr << "inspect me: ps auxwwf" << "\n";
// std::this_thread::sleep_for(std::chrono::seconds(100)); // std::this_thread::sleep_for(std::chrono::seconds(100));
} }
catch (...)
{
std::cerr << getCurrentExceptionMessage(false) << "\n";
return 1;
}
#include <Common/ThreadPool.h> #include <Common/ThreadPool.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef __clang__
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma clang diagnostic ignored "-Wundef"
#endif
#include <gtest/gtest.h>
/** Reproduces bug in ThreadPool. /** Reproduces bug in ThreadPool.
* It get stuck if we call 'wait' many times from many other threads simultaneously. * It get stuck if we call 'wait' many times from many other threads simultaneously.
*/ */
int main(int, char **) TEST(ThreadPool, ConcurrentWait)
{ {
auto worker = [] auto worker = []
{ {
...@@ -29,6 +36,4 @@ int main(int, char **) ...@@ -29,6 +36,4 @@ int main(int, char **)
waiting_pool.schedule([&pool]{ pool.wait(); }); waiting_pool.schedule([&pool]{ pool.wait(); });
waiting_pool.wait(); waiting_pool.wait();
return 0;
} }
#include <mutex> #include <atomic>
#include <iostream> #include <iostream>
#include <Common/ThreadPool.h> #include <Common/ThreadPool.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef __clang__
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma clang diagnostic ignored "-Wundef"
#endif
#include <gtest/gtest.h>
/// Test for thread self-removal when number of free threads in pool is too large. /// Test for thread self-removal when number of free threads in pool is too large.
/// Just checks that nothing weird happens. /// Just checks that nothing weird happens.
template <typename Pool> template <typename Pool>
void test() int test()
{ {
Pool pool(10, 2, 10); Pool pool(10, 2, 10);
std::mutex mutex; std::atomic<int> counter{0};
for (size_t i = 0; i < 10; ++i) for (size_t i = 0; i < 10; ++i)
pool.schedule([&]{ std::lock_guard lock(mutex); std::cerr << '.'; }); pool.schedule([&]{ ++counter; });
pool.wait(); pool.wait();
return counter;
} }
int main(int, char **) TEST(ThreadPool, ThreadRemoval)
{ {
test<FreeThreadPool>(); EXPECT_EQ(test<FreeThreadPool>(), 10);
std::cerr << '\n'; EXPECT_EQ(test<ThreadPool>(), 10);
test<ThreadPool>();
std::cerr << '\n';
return 0;
} }
...@@ -2,10 +2,17 @@ ...@@ -2,10 +2,17 @@
#include <iostream> #include <iostream>
#include <Common/ThreadPool.h> #include <Common/ThreadPool.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef __clang__
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma clang diagnostic ignored "-Wundef"
#endif
#include <gtest/gtest.h>
int main(int, char **)
TEST(ThreadPool, Loop)
{ {
std::atomic<size_t> res{0}; std::atomic<int> res{0};
for (size_t i = 0; i < 1000; ++i) for (size_t i = 0; i < 1000; ++i)
{ {
...@@ -16,6 +23,5 @@ int main(int, char **) ...@@ -16,6 +23,5 @@ int main(int, char **)
pool.wait(); pool.wait();
} }
std::cerr << res << "\n"; EXPECT_EQ(res, 16000);
return 0;
} }
...@@ -2,8 +2,15 @@ ...@@ -2,8 +2,15 @@
#include <stdexcept> #include <stdexcept>
#include <Common/ThreadPool.h> #include <Common/ThreadPool.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef __clang__
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma clang diagnostic ignored "-Wundef"
#endif
#include <gtest/gtest.h>
int main(int, char **)
bool check()
{ {
ThreadPool pool(10); ThreadPool pool(10);
...@@ -11,15 +18,21 @@ int main(int, char **) ...@@ -11,15 +18,21 @@ int main(int, char **)
try try
{ {
while (true) for (size_t i = 0; i < 100; ++i)
pool.schedule([]{}); /// An exception will be rethrown from this method. pool.schedule([]{}); /// An exception will be rethrown from this method.
} }
catch (const std::runtime_error & e) catch (const std::runtime_error &)
{ {
std::cerr << e.what() << "\n"; return true;
} }
pool.wait(); pool.wait();
return 0; return false;
}
TEST(ThreadPool, ExceptionFromSchedule)
{
EXPECT_TRUE(check());
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册