From 65e2c468ef90e528575b053ae53e6674e1c209a9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 31 Mar 2024 16:21:23 +0800 Subject: [PATCH] test: adapt to new version of thread pool --- src/core_test/cases/all_cases.cc | 61 +++++++++++++++--------------- src/core_test/codec/common_code.cc | 37 ++++++++---------- src/core_test/codec/mirror.cc | 30 ++++++--------- src/core_test/codec/raw_code.cc | 27 ++++++------- src/core_test/codec/short_code.cc | 60 +++++++++++++++-------------- third_party/thread-pool | 2 +- 6 files changed, 102 insertions(+), 115 deletions(-) diff --git a/src/core_test/cases/all_cases.cc b/src/core_test/cases/all_cases.cc index 0a3a829..9aae092 100644 --- a/src/core_test/cases/all_cases.cc +++ b/src/core_test/cases/all_cases.cc @@ -62,6 +62,14 @@ void all_cases_verify() { EXPECT_EQ(xxhsum(all_cases_xxh), ALL_CASES_XXHASH); // verify all cases checksum } +std::unique_ptr race_test(int parallel, const std::function &item) { + auto pool = std::make_unique(parallel); + pool->detach_sequence(0, parallel, [&item](const int) { + item(); + }); + return pool; +} + TEST(AllCases, basic_ranges) { basic_ranges_reset(); EXPECT_FALSE(BasicRanges::instance().is_available()); @@ -74,13 +82,11 @@ TEST(AllCases, basic_ranges) { TEST(AllCases, basic_ranges_mutex) { basic_ranges_reset(); - BS::thread_pool pool(TEST_THREAD_NUM); - - for (int i = 0; i < TEST_THREAD_NUM; ++i) { - auto _ = pool.submit(&BasicRanges::build, &BasicRanges::instance()); - } + const auto handle = race_test(TEST_THREAD_NUM, []() { + BasicRanges::instance().build(); + }); EXPECT_FALSE(BasicRanges::instance().is_available()); - pool.wait_for_tasks(); + handle->wait(); EXPECT_TRUE(BasicRanges::instance().is_available()); basic_ranges_verify(); } @@ -97,13 +103,11 @@ TEST(AllCases, all_cases) { TEST(AllCases, all_cases_mutex) { all_cases_reset(); - BS::thread_pool pool(TEST_THREAD_NUM); - - for (int i = 0; i < TEST_THREAD_NUM; ++i) { - auto _ = pool.submit(&AllCases::build, &AllCases::instance()); - } + const auto handle = race_test(TEST_THREAD_NUM, []() { + AllCases::instance().build(); + }); EXPECT_FALSE(AllCases::instance().is_available()); - pool.wait_for_tasks(); + handle->wait(); EXPECT_TRUE(AllCases::instance().is_available()); all_cases_verify(); } @@ -113,11 +117,11 @@ TEST(AllCases, all_cases_parallel) { BS::thread_pool executor; EXPECT_FALSE(AllCases::instance().is_available()); AllCases::instance().build_parallel([&executor](auto &&func) { - executor.push_task(func); + executor.detach_task(func); }); EXPECT_TRUE(AllCases::instance().is_available()); AllCases::instance().build_parallel([&executor](auto &&func) { - executor.push_task(func); + executor.detach_task(func); }); EXPECT_TRUE(AllCases::instance().is_available()); all_cases_verify(); @@ -126,16 +130,13 @@ TEST(AllCases, all_cases_parallel) { TEST(AllCases, all_cases_parallel_mutex) { all_cases_reset(); BS::thread_pool executor; - BS::thread_pool pool(TEST_THREAD_NUM); - - for (int i = 0; i < TEST_THREAD_NUM; ++i) { - auto _ = pool.submit(&AllCases::build_parallel, &AllCases::instance(), [&executor](auto &&func) { - executor.push_task(func); + const auto handle = race_test(TEST_THREAD_NUM, [&executor]() { + AllCases::instance().build_parallel([&executor](auto &&func) { + executor.detach_task(func); }); - } + }); EXPECT_FALSE(AllCases::instance().is_available()); - pool.wait_for_tasks(); - executor.wait_for_tasks(); + handle->wait(); EXPECT_TRUE(AllCases::instance().is_available()); all_cases_verify(); } @@ -147,7 +148,7 @@ TEST(AllCases, all_cases_async) { flag.clear(); AllCases::instance().build_parallel_async([&executor](auto &&func) { - executor.push_task(func); + executor.detach_task(func); }, [&flag]() { // callback function flag.test_and_set(); flag.notify_all(); @@ -158,7 +159,7 @@ TEST(AllCases, all_cases_async) { flag.clear(); AllCases::instance().build_parallel_async([&executor](auto &&func) { - executor.push_task(func); + executor.detach_task(func); }, [&flag]() { // callback function flag.test_and_set(); flag.notify_all(); @@ -173,18 +174,16 @@ TEST(AllCases, all_cases_async_mutex) { all_cases_reset(); BS::thread_pool executor; std::atomic callback_num(0); - BS::thread_pool pool(TEST_THREAD_NUM); - for (int i = 0; i < TEST_THREAD_NUM; ++i) { - auto _ = pool.submit(&AllCases::build_parallel_async, &AllCases::instance(), [&executor](auto &&func) { - executor.push_task(func); + const auto handle = race_test(TEST_THREAD_NUM, [&executor, &callback_num]() { + AllCases::instance().build_parallel_async([&executor](auto &&func) { + executor.detach_task(func); }, [&callback_num]() { callback_num.fetch_add(1); }); - } + }); EXPECT_FALSE(AllCases::instance().is_available()); - pool.wait_for_tasks(); - executor.wait_for_tasks(); + handle->wait(); EXPECT_TRUE(AllCases::instance().is_available()); EXPECT_EQ(callback_num.load(), TEST_THREAD_NUM); all_cases_verify(); diff --git a/src/core_test/codec/common_code.cc b/src/core_test/codec/common_code.cc index 4ced993..b3fefe6 100644 --- a/src/core_test/codec/common_code.cc +++ b/src/core_test/codec/common_code.cc @@ -120,15 +120,13 @@ TEST(CommonCode, initializate) { TEST(CommonCode, code_verify) { BS::thread_pool pool; - for (int head = 0; head < 16; ++head) { - pool.push_task([](uint64_t head) { - for (auto range : AllCases::instance().fetch()[head]) { - auto code = head << 32 | range; - EXPECT_TRUE(CommonCode::check(code)); // verify all cases - } - }, head); - } - pool.wait_for_tasks(); + pool.detach_sequence(0, 16, [](const uint64_t head) { + for (auto range : AllCases::instance().fetch()[head]) { + auto code = head << 32 | range; + EXPECT_TRUE(CommonCode::check(code)); // verify all cases + } + }); + pool.wait(); } TEST(CommonCode, code_string) { @@ -156,19 +154,17 @@ TEST(CommonCode, code_string) { }; BS::thread_pool pool; - for (int head = 0; head < 16; ++head) { - pool.push_task([&test_func](uint64_t head) { - for (auto range : AllCases::instance().fetch()[head]) { - test_func(CommonCode::unsafe_create(head << 32 | range)); - } - }, head); - } - pool.wait_for_tasks(); + pool.detach_sequence(0, 16, [&test_func](const uint64_t head) { + for (auto range : AllCases::instance().fetch()[head]) { + test_func(CommonCode::unsafe_create(head << 32 | range)); + } + }); + pool.wait(); } TEST(CommonCode, DISABLED_global_verify) { BS::thread_pool pool; - auto futures = pool.parallelize_loop(0x10'0000'0000, [](uint64_t start, uint64_t end) { + auto futures = pool.submit_blocks(0ULL, 0x10'0000'0000ULL, [](uint64_t start, uint64_t end) { std::vector archive; for (uint64_t common_code = start; common_code < end; ++common_code) { // brute-force search if (CommonCode::check(common_code)) { @@ -180,10 +176,9 @@ TEST(CommonCode, DISABLED_global_verify) { std::vector result; result.reserve(ALL_CASES_NUM_); - for (size_t i = 0; i < futures.size(); ++i) { - auto data = futures[i].get(); + for (auto &future : futures) { + const auto data = future.get(); result.insert(result.end(), data.begin(), data.end()); // combine sections } - pool.wait_for_tasks(); EXPECT_EQ(result, all_common_codes()); } diff --git a/src/core_test/codec/mirror.cc b/src/core_test/codec/mirror.cc index ad0e7ea..10e83bf 100644 --- a/src/core_test/codec/mirror.cc +++ b/src/core_test/codec/mirror.cc @@ -62,14 +62,12 @@ TEST(RawCode, code_vertical_mirror) { }; BS::thread_pool pool; - for (int head = 0; head < 16; ++head) { - pool.push_task([&test_func](uint64_t head) { - for (auto range : AllCases::instance().fetch()[head]) { - test_func(RawCode::from_common_code(head << 32 | range).value()); - } - }, head); - } - pool.wait_for_tasks(); + pool.detach_sequence(0, 16, [&test_func](const uint64_t head) { + for (const auto range : AllCases::instance().fetch()[head]) { + test_func(RawCode::from_common_code(head << 32 | range).value()); + } + }); + pool.wait(); } TEST(RawCode, code_horizontal_mirror) { @@ -86,14 +84,10 @@ TEST(RawCode, code_horizontal_mirror) { }; BS::thread_pool pool; - for (int head = 0; head < 16; ++head) { - pool.push_task([&test_func](uint64_t head) { - for (auto range : AllCases::instance().fetch()[head]) { - - test_func(RawCode::from_common_code(head << 32 | range).value()); - - } - }, head); - } - pool.wait_for_tasks(); + pool.detach_sequence(0, 16, [&test_func](const uint64_t head) { + for (const auto range : AllCases::instance().fetch()[head]) { + test_func(RawCode::from_common_code(head << 32 | range).value()); + } + }); + pool.wait(); } diff --git a/src/core_test/codec/raw_code.cc b/src/core_test/codec/raw_code.cc index 71608d9..0b65db7 100644 --- a/src/core_test/codec/raw_code.cc +++ b/src/core_test/codec/raw_code.cc @@ -82,17 +82,15 @@ TEST(RawCode, initializate) { TEST(RawCode, code_verify) { BS::thread_pool pool; - for (int head = 0; head < 16; ++head) { - pool.push_task([](uint64_t head) { - for (auto range : AllCases::instance().fetch()[head]) { - auto code = RawCode::from_common_code(head << 32 | range); - EXPECT_TRUE(code.has_value()); - EXPECT_TRUE(RawCode::check(code->unwrap())); - EXPECT_EQ(code->to_common_code(), head << 32 | range); - } - }, head); - } - pool.wait_for_tasks(); + pool.detach_sequence(0, 16, [](const uint64_t head) { + for (const auto range : AllCases::instance().fetch()[head]) { + const auto code = RawCode::from_common_code(head << 32 | range); + EXPECT_TRUE(code.has_value()); + EXPECT_TRUE(RawCode::check(code->unwrap())); + EXPECT_EQ(code->to_common_code(), head << 32 | range); + } + }); + pool.wait(); } TEST(RawCode, DISABLED_global_verify) { @@ -115,7 +113,7 @@ TEST(RawCode, DISABLED_global_verify) { }; BS::thread_pool pool; - auto futures = pool.parallelize_loop(0x10'0000'0000, [&force_convert](uint64_t start, uint64_t end) { + auto futures = pool.submit_blocks(0ULL, 0x10'0000'0000ULL, [&force_convert](uint64_t start, uint64_t end) { std::vector archive; for (uint64_t common_code = start; common_code < end; ++common_code) { if (RawCode::check(force_convert(common_code))) { @@ -127,10 +125,9 @@ TEST(RawCode, DISABLED_global_verify) { std::vector result; result.reserve(ALL_CASES_NUM_); - for (size_t i = 0; i < futures.size(); ++i) { - auto data = futures[i].get(); + for (auto &future : futures) { + const auto data = future.get(); result.insert(result.end(), data.begin(), data.end()); // combine sections } - pool.wait_for_tasks(); EXPECT_EQ(result, all_common_codes()); } diff --git a/src/core_test/codec/short_code.cc b/src/core_test/codec/short_code.cc index 6f85c37..b504b15 100644 --- a/src/core_test/codec/short_code.cc +++ b/src/core_test/codec/short_code.cc @@ -128,20 +128,24 @@ TEST(ShortCode, speed_up) { BS::thread_pool pool(TEST_THREAD_NUM); for (auto i = 0; i < TEST_THREAD_NUM; ++i) { - pool.push_task(ShortCode::speed_up, false); + pool.detach_task([]() { + ShortCode::speed_up(false); + }); } EXPECT_FALSE(BasicRanges::instance().is_available()); EXPECT_FALSE(AllCases::instance().is_available()); - pool.wait_for_tasks(); + pool.wait(); EXPECT_TRUE(BasicRanges::instance().is_available()); EXPECT_FALSE(AllCases::instance().is_available()); for (auto i = 0; i < TEST_THREAD_NUM; ++i) { - pool.push_task(ShortCode::speed_up, true); + pool.detach_task([]() { + ShortCode::speed_up(true); + }); } EXPECT_TRUE(BasicRanges::instance().is_available()); EXPECT_FALSE(AllCases::instance().is_available()); - pool.wait_for_tasks(); + pool.wait(); EXPECT_TRUE(BasicRanges::instance().is_available()); EXPECT_TRUE(AllCases::instance().is_available()); } @@ -149,24 +153,22 @@ TEST(ShortCode, speed_up) { TEST(ShortCode, code_verify) { BS::thread_pool pool; ShortCode::speed_up(true); - for (int head = 0; head < 16; ++head) { - pool.push_task([](uint64_t head) { - std::vector archive; - for (auto range : AllCases::instance().fetch()[head]) { - auto code = ShortCode::from_common_code(head << 32 | range); - EXPECT_TRUE(code.has_value()); - EXPECT_TRUE(ShortCode::check(code->unwrap())); - EXPECT_EQ(code->to_common_code(), head << 32 | range); - archive.emplace_back(code->unwrap()); - } - if (!archive.empty()) { - EXPECT_TRUE(std::is_sorted(archive.begin(), archive.end())); // increasingly one by one - EXPECT_EQ(archive[archive.size() - 1] - archive[0], archive.size() - 1); - EXPECT_EQ(std::accumulate(ALL_CASES_NUM.begin(), ALL_CASES_NUM.begin() + head, 0), archive[0]); - } - }, head); - } - pool.wait_for_tasks(); + pool.detach_sequence(0, 16, [](const uint64_t head) { + std::vector archive; + for (auto range : AllCases::instance().fetch()[head]) { + auto code = ShortCode::from_common_code(head << 32 | range); + EXPECT_TRUE(code.has_value()); + EXPECT_TRUE(ShortCode::check(code->unwrap())); + EXPECT_EQ(code->to_common_code(), head << 32 | range); + archive.emplace_back(code->unwrap()); + } + if (!archive.empty()) { + EXPECT_TRUE(std::is_sorted(archive.begin(), archive.end())); // increasingly one by one + EXPECT_EQ(archive[archive.size() - 1] - archive[0], archive.size() - 1); + EXPECT_EQ(std::accumulate(ALL_CASES_NUM.begin(), ALL_CASES_NUM.begin() + head, 0), archive[0]); + } + }); + pool.wait(); } TEST(ShortCode, code_string) { @@ -183,18 +185,18 @@ TEST(ShortCode, code_string) { }; BS::thread_pool pool; - pool.push_loop(SHORT_CODE_LIMIT, [&test_func](uint32_t start, uint32_t end) { + pool.detach_blocks((uint32_t)0, SHORT_CODE_LIMIT, [&test_func](uint32_t start, uint32_t end) { for (uint32_t short_code = start; short_code < end; ++short_code) { test_func(ShortCode::unsafe_create(short_code)); } - }, 0x1000); // split as 4096 pieces - pool.wait_for_tasks(); + }); + pool.wait(); } TEST(ShortCode, DISABLED_global_verify) { all_cases_reset(); BS::thread_pool pool; - auto futures = pool.parallelize_loop(SHORT_CODE_LIMIT, [](uint32_t start, uint32_t end) { + auto futures = pool.submit_blocks((uint32_t)0, SHORT_CODE_LIMIT, [](uint32_t start, uint32_t end) { std::vector archive; archive.reserve(end - start); for (uint32_t short_code = start; short_code < end; ++short_code) { @@ -207,10 +209,10 @@ TEST(ShortCode, DISABLED_global_verify) { std::vector result; result.reserve(ALL_CASES_NUM_); - for (size_t i = 0; i < futures.size(); ++i) { - auto data = futures[i].get(); + for (auto &future : futures) { + const auto data = future.get(); result.insert(result.end(), data.begin(), data.end()); // combine sections } - pool.wait_for_tasks(); + pool.wait(); EXPECT_EQ(result, all_common_codes()); } diff --git a/third_party/thread-pool b/third_party/thread-pool index 6790920..097aa71 160000 --- a/third_party/thread-pool +++ b/third_party/thread-pool @@ -1 +1 @@ -Subproject commit 6790920f61ab3e928ddaea835ab6a803d467f41d +Subproject commit 097aa718f25d44315cadb80b407144ad455ee4f9