This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/test/monitor/gtest_rpc.cpp

68 lines
1.8 KiB
C++
Raw Normal View History

2024-10-18 16:47:51 +08:00
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <getopt.h>
#include <pthread.h>
#include <gtest/gtest.h>
#include "stellar/monitor.h"
#include "monitor/monitor_private.h"
#include "monitor/monitor_rpc.h"
#define TEST_NUM 2000000
static long g_worker_thread_run = 1;
static void *phony_worker_thread(void *arg)
{
struct monitor_rpc *rpc_ins = (struct monitor_rpc *)arg;
while (g_worker_thread_run)
{
stm_rpc_exec(0, rpc_ins);
}
return NULL;
}
static struct iovec on_worker_thread_cb(int worker_thread_idx, struct iovec req, void *user_args)
{
EXPECT_EQ(0, worker_thread_idx);
pthread_t *phony_worker_thread_id = (pthread_t *)user_args;
EXPECT_EQ(*phony_worker_thread_id, pthread_self());
struct iovec resp = req;
resp.iov_base = (void *)"world";
resp.iov_len = 12345 + req.iov_len;
return resp;
}
TEST(MONITOR_RPC, base)
{
srand(time(NULL));
pthread_t phony_worker_thread_id = 0;
struct monitor_rpc *rpc_ins = stm_rpc_new();
ASSERT_TRUE(rpc_ins != NULL);
pthread_create(&phony_worker_thread_id, NULL, phony_worker_thread, rpc_ins);
struct iovec req = {.iov_base = (void *)"hello", .iov_len = 5};
usleep(10000);
for (int i = 0; i < TEST_NUM; i++)
{
req.iov_len = rand() % TEST_NUM;
struct iovec resp = stm_rpc_call(rpc_ins, req, on_worker_thread_cb, &phony_worker_thread_id);
EXPECT_EQ(req.iov_len + 12345, resp.iov_len);
EXPECT_EQ(resp.iov_base, (void *)"world");
}
printf("rpc call num: %d\n", TEST_NUM);
g_worker_thread_run = 0;
pthread_cancel(phony_worker_thread_id);
pthread_join(phony_worker_thread_id, NULL);
stm_rpc_free(rpc_ins);
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
}