summaryrefslogtreecommitdiff
path: root/test/monitor/gtest_rpc.cpp
blob: 74be6112d073459d1380460240f747604a5d187c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#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;
}