From 55212658def1c54300bb13fa273d07510167cd4b Mon Sep 17 00:00:00 2001 From: youtao guo Date: Sun, 3 Nov 2024 13:32:31 +0800 Subject: [PATCH] add ci (#1) --- .github/workflows/linux.yml | 33 +++++++++++++++++++++++++ CMakeLists.txt | 21 ++++++++-------- test.cc | 48 +++++++++++++++++++++++++++---------- 3 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/linux.yml diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml new file mode 100644 index 0000000..51f6dd7 --- /dev/null +++ b/.github/workflows/linux.yml @@ -0,0 +1,33 @@ +name: Linux + +on: + workflow_dispatch: # for manual workflow trigger + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + linux_cpp20: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + + - name: Install dependencies + run: sudo apt install libgtest-dev + + - name: Build + run: | + mkdir build && cd build + cmake -DCMAKE_CXX_STANDARD=20 -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-Werror" .. + cmake --build . + + - name: Test + run: cd build && ctest --output-on-failure diff --git a/CMakeLists.txt b/CMakeLists.txt index 723df47..fdcd4e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,10 +29,10 @@ if(NOT GTest_FOUND) message(FATAL_ERROR "GTest not found") endif() -find_package(spdlog REQUIRED) -if(NOT spdlog_FOUND) - message(FATAL_ERROR "spdlog not found") -endif() +# find_package(spdlog REQUIRED) +# if(NOT spdlog_FOUND) +# message(FATAL_ERROR "spdlog not found") +# endif() # list(APPEND PROJECT_SRCS # cosched.hpp @@ -52,18 +52,17 @@ endif() # ) enable_testing() -add_executable(my_project_test +add_executable(cosched_test ${CMAKE_CURRENT_SOURCE_DIR}/test.cc ) -target_compile_options(my_project_test PUBLIC -g -Wall) -target_compile_options(my_project_test PUBLIC -fsanitize=address) -target_link_options(my_project_test PUBLIC -fsanitize=address) +target_compile_options(cosched_test PUBLIC -g -Wall) +target_compile_options(cosched_test PUBLIC -fsanitize=address) +target_link_options(cosched_test PUBLIC -fsanitize=address) -target_link_libraries(my_project_test PRIVATE - # projectcore +target_link_libraries(cosched_test PRIVATE GTest::gtest GTest::gtest_main ) -add_test(NAME my_project_test COMMAND my_project_test) \ No newline at end of file +add_test(NAME cosched_test COMMAND cosched_test) \ No newline at end of file diff --git a/test.cc b/test.cc index 129a78e..afbb793 100644 --- a/test.cc +++ b/test.cc @@ -1,14 +1,26 @@ +#include +#include +#include + #include #include +#include #include +#include #include -#include #include "promise.hpp" +std::set tids; +std::mutex tid_mu; + coro::task print_tid() { std::cout << "thread id is " << std::this_thread::get_id() << '\n'; + { + std::unique_lock l(tid_mu); + tids.insert(std::hash{}(std::this_thread::get_id())); + } co_return; } @@ -20,7 +32,7 @@ coro::task fibonacci(int n) { co_return co_await fibonacci(n - 1) + co_await fibonacci(n - 2); } -coro::task foo(std::string v) { +coro::task echo(std::string v) { std::cout << v << '\n'; co_return v; } @@ -37,12 +49,14 @@ coro::task slow_response(int a, int b) { co_return co_await std::move(resp1) + co_await std::move(resp2); } -int main() { - auto fib = fibonacci(1); +TEST(CoroRunTest, Fib) { + auto fib = fibonacci(5); fib.wait(); - std::cout << fib.get() << '\n'; + EXPECT_EQ(5, fib.get()); +} - auto f = foo("abc"); +TEST(CoroRunTest, Async) { + auto f = echo("abc"); auto b = [](auto f) -> coro::task { using namespace std::chrono_literals; @@ -56,19 +70,27 @@ int main() { }(f.release_coroutine_handle()); std::jthread th([f = std::move(f)]() mutable { - std::cout << f.get(); - std::cout << "\nsub thread exit\n"; + // std::cout << f.get(); + EXPECT_EQ("abc", f.get()); + std::cout << "sub thread exit\n"; }); b.get(); - static_assert(!std::is_copy_constructible_v); +} +TEST(StaticThreadPoolTest, Fib) { coro::static_thread_pool pool(3); - std::cout << pool.schedule(fibonacci(10)).get() << std::endl; + tids.clear(); + EXPECT_EQ(55, pool.schedule(fibonacci(10)).get()); + EXPECT_EQ(3, tids.size()); +} +TEST(StaticThreadPoolTest, Parallel) { + coro::static_thread_pool pool(3); auto start = std::chrono::steady_clock::now(); - std::cout << pool.schedule(slow_response(1, 2)).get() << std::endl; + EXPECT_EQ(3, pool.schedule(slow_response(1, 2)).get()); auto elapsed = std::chrono::duration_cast( std::chrono::steady_clock::now() - start); - std::cout << "elapsed time: " << elapsed << '\n'; -} \ No newline at end of file + std::cout << "elapsed time: " << elapsed.count() << "ms\n"; + EXPECT_LE(elapsed.count(), 1100); +}