|
|
|
@ -31,8 +31,9 @@ |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include "src/core/iomgr/udp_server.h" |
|
|
|
|
#include "src/core/iomgr/iomgr.h" |
|
|
|
|
#include "src/core/iomgr/pollset_posix.h" |
|
|
|
|
#include "src/core/iomgr/udp_server.h" |
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
#include <grpc/support/sync.h> |
|
|
|
|
#include <grpc/support/time.h> |
|
|
|
@ -48,6 +49,7 @@ |
|
|
|
|
#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", #x) |
|
|
|
|
|
|
|
|
|
static grpc_pollset g_pollset; |
|
|
|
|
static gpr_mu *g_mu; |
|
|
|
|
static int g_number_of_reads = 0; |
|
|
|
|
static int g_number_of_bytes_read = 0; |
|
|
|
|
|
|
|
|
@ -56,14 +58,14 @@ static void on_read(grpc_exec_ctx *exec_ctx, grpc_fd *emfd, |
|
|
|
|
char read_buffer[512]; |
|
|
|
|
ssize_t byte_count; |
|
|
|
|
|
|
|
|
|
gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); |
|
|
|
|
gpr_mu_lock(g_mu); |
|
|
|
|
byte_count = recv(emfd->fd, read_buffer, sizeof(read_buffer), 0); |
|
|
|
|
|
|
|
|
|
g_number_of_reads++; |
|
|
|
|
g_number_of_bytes_read += (int)byte_count; |
|
|
|
|
|
|
|
|
|
grpc_pollset_kick(&g_pollset, NULL); |
|
|
|
|
gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); |
|
|
|
|
gpr_mu_unlock(g_mu); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_no_op(void) { |
|
|
|
@ -142,7 +144,7 @@ static void test_receive(int number_of_clients) { |
|
|
|
|
pollsets[0] = &g_pollset; |
|
|
|
|
grpc_udp_server_start(&exec_ctx, s, pollsets, 1, NULL); |
|
|
|
|
|
|
|
|
|
gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); |
|
|
|
|
gpr_mu_lock(g_mu); |
|
|
|
|
|
|
|
|
|
for (i = 0; i < number_of_clients; i++) { |
|
|
|
|
deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); |
|
|
|
@ -155,19 +157,19 @@ static void test_receive(int number_of_clients) { |
|
|
|
|
GPR_ASSERT(5 == write(clifd, "hello", 5)); |
|
|
|
|
while (g_number_of_reads == number_of_reads_before && |
|
|
|
|
gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) > 0) { |
|
|
|
|
grpc_pollset_worker worker; |
|
|
|
|
grpc_pollset_worker *worker = NULL; |
|
|
|
|
grpc_pollset_work(&exec_ctx, &g_pollset, &worker, |
|
|
|
|
gpr_now(GPR_CLOCK_MONOTONIC), deadline); |
|
|
|
|
gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); |
|
|
|
|
gpr_mu_unlock(g_mu); |
|
|
|
|
grpc_exec_ctx_finish(&exec_ctx); |
|
|
|
|
gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); |
|
|
|
|
gpr_mu_lock(g_mu); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(g_number_of_reads == number_of_reads_before + 1); |
|
|
|
|
close(clifd); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(g_number_of_bytes_read == 5 * number_of_clients); |
|
|
|
|
|
|
|
|
|
gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset)); |
|
|
|
|
gpr_mu_unlock(g_mu); |
|
|
|
|
|
|
|
|
|
grpc_udp_server_destroy(&exec_ctx, s, NULL); |
|
|
|
|
grpc_exec_ctx_finish(&exec_ctx); |
|
|
|
@ -181,8 +183,8 @@ int main(int argc, char **argv) { |
|
|
|
|
grpc_closure destroyed; |
|
|
|
|
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
|
|
|
|
grpc_test_init(argc, argv); |
|
|
|
|
grpc_iomgr_init(); |
|
|
|
|
grpc_pollset_init(&g_pollset); |
|
|
|
|
grpc_init(); |
|
|
|
|
grpc_pollset_init(&g_pollset, &g_mu); |
|
|
|
|
|
|
|
|
|
test_no_op(); |
|
|
|
|
test_no_op_with_start(); |
|
|
|
|