Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Jul 27, 2023
1 parent b608558 commit 7885c14
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/coro_io/tests/test_client_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <asio/io_context.hpp>
#include <atomic>
#include <cassert>
#include <chrono>
#include <exception>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -34,17 +35,15 @@ using namespace std::chrono_literals;
using namespace async_simple::coro;
auto event =
[](
int lim, coro_io::client_pool<coro_rpc::coro_rpc_client> &pool,
ConditionVariable<SpinLock> &cv, SpinLock &lock,
int lim, auto &pool, ConditionVariable<SpinLock> &cv, SpinLock &lock,
std::function<void(coro_rpc::coro_rpc_client &client)> user_op =
[](auto &client) {
}) -> async_simple::coro::Lazy<bool> {
std::vector<RescheduleLazy<bool>> works;
int64_t cnt = 0;
for (int i = 0; i < lim; ++i) {
auto op = [&cnt, &lock, &cv, &lim,
&user_op](coro_rpc::coro_rpc_client &client)
-> async_simple::coro::Lazy<void> {
&user_op](auto &client) -> async_simple::coro::Lazy<void> {
user_op(client);
auto l = co_await lock.coScopedLock();
if (++cnt < lim) {
Expand Down Expand Up @@ -150,6 +149,40 @@ TEST_CASE("test reconnect") {
}());
}

struct mock_client : public coro_rpc::coro_rpc_client {
async_simple::coro::Lazy<std::errc> reconnect(const std::string &hostname) {
auto ec = co_await this->coro_rpc::coro_rpc_client::reconnect(hostname);
if (ec!=std::errc{}) {
co_await coro_io::sleep_for(200ms);
}
co_return ec;
}
};
TEST_CASE("test reconnect retry wait time exinclude reconnect cost time") {
async_simple::coro::syncAwait([]() -> async_simple::coro::Lazy<void> {
auto tp = std::chrono::steady_clock::now();
auto pool = coro_io::client_pool<mock_client>::create(
"127.0.0.1:8801",
{.connect_retry_count = 3, .reconnect_wait_time = 500ms});
SpinLock lock;
ConditionVariable<SpinLock> cv;
coro_rpc::coro_rpc_server server(2, 8801);
async_simple::Promise<async_simple::Unit> p;
coro_io::sleep_for(350ms).start([&server, &p](auto &&) {
auto server_is_started = server.async_start();
REQUIRE(server_is_started);
});
auto res = co_await event(100, *pool, cv, lock);
CHECK(res);
CHECK(pool->free_client_count() == 100);
auto dur = std::chrono::steady_clock::now() - tp;
std::cout << dur.count() << std::endl;
CHECK((dur >= 500ms && dur <= 600ms));
server.stop();
co_return;
}());
}

TEST_CASE("test collect_free_client") {
async_simple::coro::syncAwait([]() -> async_simple::coro::Lazy<void> {
coro_rpc::coro_rpc_server server(1, 8801);
Expand Down

0 comments on commit 7885c14

Please sign in to comment.