From 83085f2ce99cca05d40a19482151873a55e6393a Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期五, 02 四月 2021 19:32:21 +0800 Subject: [PATCH] refactor center; add async request no cb. --- utest/utest.cpp | 21 +++++++++++++++++++-- 1 files changed, 19 insertions(+), 2 deletions(-) diff --git a/utest/utest.cpp b/utest/utest.cpp index 637ae26..55a08a3 100644 --- a/utest/utest.cpp +++ b/utest/utest.cpp @@ -181,14 +181,31 @@ auto Client = [&](const std::string &topic, const int nreq) { SocketRequest client(shm); + std::atomic<int> count(0); std::string reply; + auto onRecv = [&](const std::string &rep) { + reply = rep; + if (++count >= nreq) { + printf("count: %d\n", count.load()); + } + }; + client.StartWorker(onRecv, 1); boost::timer::auto_cpu_timer timer; for (int i = 0; i < nreq; ++i) { - if (!client.SyncRequest(topic, "data " + std::to_string(i), reply, 1000)) { + if (!client.AsyncRequest(topic, "data " + std::to_string(i), 1000)) { printf("client request failed\n"); } + // if (!client.SyncRequest(topic, "data " + std::to_string(i), reply, 1000)) { + // printf("client request failed\n"); + // } else { + // ++count; + // } } printf("request %s %d done ", topic.c_str(), nreq); + while (count.load() < nreq) { + std::this_thread::yield(); + } + client.Stop(); }; auto Server = [&](const std::string &name, const std::vector<std::string> &topics) { SocketReply server(shm); @@ -212,7 +229,7 @@ servers.Launch(Server, "server", topics); std::this_thread::sleep_for(100ms); for (auto &t : topics) { - clients.Launch(Client, t, 1000 * 100); + clients.Launch(Client, t, 1000 * 1000); } clients.WaitAll(); run = false; -- Gitblit v1.8.0