forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
|
||
#include <chrono> | ||
#include <string_view> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include "common/config.h" | ||
#include "common/logging.h" | ||
#include "gtest/gtest.h" | ||
#include "recycler/recycler.h" | ||
#include "recycler/util.h" | ||
|
||
using namespace doris::cloud; | ||
|
||
int main(int argc, char** argv) { | ||
auto conf_file = "doris_cloud.conf"; | ||
if (!config::init(conf_file, true)) { | ||
std::cerr << "failed to init config file, conf=" << conf_file << std::endl; | ||
return -1; | ||
} | ||
if (!::init_glog("util")) { | ||
std::cerr << "failed to init glog" << std::endl; | ||
return -1; | ||
} | ||
|
||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} | ||
|
||
template <typename... Func> | ||
auto task_wrapper(Func... funcs) -> std::function<int()> { | ||
return [funcs...]() { | ||
return [](std::initializer_list<int> numbers) { | ||
int i = 0; | ||
for (int num : numbers) { | ||
if (num != 0) { | ||
i = num; | ||
} | ||
} | ||
return i; | ||
}({funcs()...}); | ||
}; | ||
} | ||
|
||
TEST(UtilTest, stage_wrapper) { | ||
std::function<int()> func1 = []() { return 0; }; | ||
std::function<int()> func2 = []() { return -1; }; | ||
std::function<int()> func3 = []() { return 0; }; | ||
auto f = task_wrapper(func1, func2, func3); | ||
ASSERT_EQ(-1, f()); | ||
|
||
f = task_wrapper(func1, func3); | ||
ASSERT_EQ(0, f()); | ||
} | ||
|
||
TEST(UtilTest, delay) { | ||
std::unique_ptr<SimpleThreadPool> s3_producer_pool = | ||
std::make_unique<SimpleThreadPool>(config::recycle_pool_parallelism); | ||
s3_producer_pool->start(); | ||
// test normal execute | ||
{ | ||
SyncExecutor<int> sync_executor(s3_producer_pool.get(), "normal test", | ||
[](int k) { return k == -1; }); | ||
auto f1 = []() { return -1; }; | ||
auto f2 = [] () { | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
return 1; | ||
}; | ||
sync_executor.add(f2); | ||
sync_executor.add(f2); | ||
sync_executor.add(f1); | ||
bool finished = true; | ||
std::vector<int> res = sync_executor.when_all(&finished); | ||
ASSERT_EQ(finished, false); | ||
ASSERT_EQ(3, res.size()); | ||
} | ||
// test normal execute | ||
{ | ||
SyncExecutor<std::string_view> sync_executor(s3_producer_pool.get(), "normal test", | ||
[](const std::string_view k) { return k.empty(); }); | ||
auto f1 = []() { return ""; }; | ||
auto f2 = [] () { | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
return "fake"; | ||
}; | ||
sync_executor.add(f2); | ||
sync_executor.add(f2); | ||
sync_executor.add(f1); | ||
bool finished = true; | ||
auto res = sync_executor.when_all(&finished); | ||
ASSERT_EQ(finished, false); | ||
ASSERT_EQ(3, res.size()); | ||
} | ||
} | ||
|
||
TEST(UtilTest, normal) { | ||
std::unique_ptr<SimpleThreadPool> s3_producer_pool = | ||
std::make_unique<SimpleThreadPool>(config::recycle_pool_parallelism); | ||
s3_producer_pool->start(); | ||
// test normal execute | ||
{ | ||
SyncExecutor<int> sync_executor(s3_producer_pool.get(), "normal test", | ||
[](int k) { return k == -1; }); | ||
auto f1 = []() { return 1; }; | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
bool finished = true; | ||
std::vector<int> res = sync_executor.when_all(&finished); | ||
ASSERT_EQ(3, res.size()); | ||
ASSERT_EQ(finished, true); | ||
std::for_each(res.begin(), res.end(), [](auto&& n) { ASSERT_EQ(1, n); }); | ||
} | ||
// test when error happen | ||
{ | ||
SyncExecutor<int> sync_executor(s3_producer_pool.get(), "normal test", | ||
[](int k) { return k == -1; }); | ||
auto f1 = []() { return 1; }; | ||
sync_executor._stop_token = true; | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
bool finished = true; | ||
std::vector<int> res = sync_executor.when_all(&finished); | ||
ASSERT_EQ(finished, false); | ||
ASSERT_EQ(0, res.size()); | ||
} | ||
{ | ||
SyncExecutor<int> sync_executor(s3_producer_pool.get(), "normal test", | ||
[](int k) { return k == -1; }); | ||
auto f1 = []() { return 1; }; | ||
auto cancel = []() { return -1; }; | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
sync_executor.add(cancel); | ||
bool finished = true; | ||
std::vector<int> res = sync_executor.when_all(&finished); | ||
ASSERT_EQ(finished, false); | ||
} | ||
// test string_view | ||
{ | ||
SyncExecutor<std::string_view> sync_executor( | ||
s3_producer_pool.get(), "normal test", | ||
[](const std::string_view k) { return k.empty(); }); | ||
std::string s = "Hello World"; | ||
auto f1 = [&s]() { return std::string_view(s); }; | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
sync_executor.add(f1); | ||
bool finished = true; | ||
std::vector<std::string_view> res = sync_executor.when_all(&finished); | ||
ASSERT_EQ(3, res.size()); | ||
std::for_each(res.begin(), res.end(), [&s](auto&& n) { ASSERT_EQ(s, n); }); | ||
} | ||
} |