#include <inttypes.h>
#include <ctime>
#include <memory>
#include <string>
#include <gflags/gflags.h>
#include <grpc/support/log.h>
#include <grpc/support/port_platform.h>
#include <grpcpp/grpcpp.h>
#include "src/cpp/thread_manager/thread_manager.h"
#include "test/cpp/util/test_config.h"
namespace grpc {
struct ThreadManagerTestSettings {
int min_pollers;
int max_pollers;
int poll_duration_ms;
int work_duration_ms;
int max_poll_calls;
};
class ThreadManagerTest final : public grpc::ThreadManager {
public:
ThreadManagerTest(const char* name, grpc_resource_quota* rq,
const ThreadManagerTestSettings& settings)
: ThreadManager(name, rq, settings.min_pollers, settings.max_pollers),
settings_(settings),
num_do_work_(0),
num_poll_for_work_(0),
num_work_found_(0) {}
grpc::ThreadManager::WorkStatus PollForWork(void** tag, bool* ok) override;
void DoWork(void* tag, bool ok, bool resources) override;
int GetNumWorkFound();
int GetNumDoWork();
private:
void SleepForMs(int sleep_time_ms);
ThreadManagerTestSettings settings_;
gpr_atm num_do_work_; gpr_atm num_poll_for_work_; gpr_atm num_work_found_; };
void ThreadManagerTest::SleepForMs(int duration_ms) {
gpr_timespec sleep_time =
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
gpr_time_from_millis(duration_ms, GPR_TIMESPAN));
gpr_sleep_until(sleep_time);
}
grpc::ThreadManager::WorkStatus ThreadManagerTest::PollForWork(void** tag,
bool* ok) {
int call_num = gpr_atm_no_barrier_fetch_add(&num_poll_for_work_, 1);
if (call_num >= settings_.max_poll_calls) {
Shutdown();
return SHUTDOWN;
}
SleepForMs(settings_.poll_duration_ms); *tag = nullptr;
*ok = true;
if (call_num % 3 == 0) {
return TIMEOUT;
}
gpr_atm_no_barrier_fetch_add(&num_work_found_, 1);
return WORK_FOUND;
}
void ThreadManagerTest::DoWork(void* tag, bool ok, bool resources) {
gpr_atm_no_barrier_fetch_add(&num_do_work_, 1);
SleepForMs(settings_.work_duration_ms); }
int ThreadManagerTest::GetNumWorkFound() {
return static_cast<int>(gpr_atm_no_barrier_load(&num_work_found_));
}
int ThreadManagerTest::GetNumDoWork() {
return static_cast<int>(gpr_atm_no_barrier_load(&num_do_work_));
}
}
static void TestPollAndWork() {
grpc_resource_quota* rq = grpc_resource_quota_create("Test-poll-and-work");
grpc::ThreadManagerTestSettings settings = {
2 , 10 , 10 ,
1 , 50 };
grpc::ThreadManagerTest test_thread_mgr("TestThreadManager", rq, settings);
grpc_resource_quota_unref(rq);
test_thread_mgr.Initialize(); test_thread_mgr.Wait();
gpr_log(GPR_DEBUG, "DoWork() called %d times",
test_thread_mgr.GetNumDoWork());
GPR_ASSERT(test_thread_mgr.GetNumDoWork() ==
test_thread_mgr.GetNumWorkFound());
}
static void TestThreadQuota() {
const int kMaxNumThreads = 3;
grpc_resource_quota* rq = grpc_resource_quota_create("Test-thread-quota");
grpc_resource_quota_set_max_threads(rq, kMaxNumThreads);
grpc::ThreadManagerTestSettings settings = {
1 , 1 , 1 ,
10 , 50 };
grpc::ThreadManagerTest test_thread_mgr_1("TestThreadManager-1", rq,
settings);
grpc::ThreadManagerTest test_thread_mgr_2("TestThreadManager-2", rq,
settings);
grpc_resource_quota_unref(rq);
test_thread_mgr_1.Initialize();
test_thread_mgr_2.Initialize();
test_thread_mgr_1.Wait();
test_thread_mgr_2.Wait();
int max1 = test_thread_mgr_1.GetMaxActiveThreadsSoFar();
int max2 = test_thread_mgr_2.GetMaxActiveThreadsSoFar();
gpr_log(
GPR_DEBUG,
"MaxActiveThreads in TestThreadManager_1: %d, TestThreadManager_2: %d",
max1, max2);
GPR_ASSERT(max1 <= kMaxNumThreads && max2 <= kMaxNumThreads);
}
int main(int argc, char** argv) {
std::srand(std::time(nullptr));
grpc::testing::InitTest(&argc, &argv, true);
grpc_init();
TestPollAndWork();
TestThreadQuota();
grpc_shutdown();
return 0;
}