Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteYue committed Jul 12, 2024
1 parent b47e320 commit 217d892
Showing 1 changed file with 173 additions and 0 deletions.
173 changes: 173 additions & 0 deletions cloud/test/util_test.cpp
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); });
}
}

0 comments on commit 217d892

Please sign in to comment.