Skip to content

Commit

Permalink
add ci (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
coyorkdow authored Nov 3, 2024
1 parent bfbb8ee commit 5521265
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 10 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
add_test(NAME cosched_test COMMAND cosched_test)
48 changes: 35 additions & 13 deletions test.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@

#include <asm-generic/errno.h>
#include <gtest/gtest.h>
#include <sys/types.h>

#include <chrono>
#include <iostream>
#include <mutex>
#include <ostream>
#include <set>
#include <thread>
#include <type_traits>

#include "promise.hpp"

std::set<uint64_t> tids;
std::mutex tid_mu;

coro::task<void> print_tid() {
std::cout << "thread id is " << std::this_thread::get_id() << '\n';
{
std::unique_lock l(tid_mu);
tids.insert(std::hash<std::thread::id>{}(std::this_thread::get_id()));
}
co_return;
}

Expand All @@ -20,7 +32,7 @@ coro::task<int> fibonacci(int n) {
co_return co_await fibonacci(n - 1) + co_await fibonacci(n - 2);
}

coro::task<std::string> foo(std::string v) {
coro::task<std::string> echo(std::string v) {
std::cout << v << '\n';
co_return v;
}
Expand All @@ -37,12 +49,14 @@ coro::task<int> 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<void> {
using namespace std::chrono_literals;
Expand All @@ -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<decltype(f)>);
}

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::milliseconds>(
std::chrono::steady_clock::now() - start);
std::cout << "elapsed time: " << elapsed << '\n';
}
std::cout << "elapsed time: " << elapsed.count() << "ms\n";
EXPECT_LE(elapsed.count(), 1100);
}

0 comments on commit 5521265

Please sign in to comment.