From 8c13db8e432f4d68fd775826238ab9ebb48f3f19 Mon Sep 17 00:00:00 2001 From: ncteisen Date: Tue, 29 May 2018 12:01:36 -0700 Subject: [PATCH 1/2] Add channel arg test for channel/server creation --- test/core/channel/channel_args_test.cc | 82 ++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/test/core/channel/channel_args_test.cc b/test/core/channel/channel_args_test.cc index 57ff7370ad5..a098d26974b 100644 --- a/test/core/channel/channel_args_test.cc +++ b/test/core/channel/channel_args_test.cc @@ -148,6 +148,86 @@ static void test_set_socket_mutator(void) { } } +struct fake_class { + int foo; +}; + +static void* fake_pointer_arg_copy(void* arg) { + fake_class* fc = static_cast(arg); + fake_class* new_fc = static_cast(gpr_malloc(sizeof(fake_class))); + new_fc->foo = fc->foo; + return new_fc; +} + +static void fake_pointer_arg_destroy(void* arg) { + fake_class* fc = static_cast(arg); + gpr_free(fc); +} + +static int fake_pointer_cmp(void* a, void* b) { return GPR_ICMP(a, b); } + +static const grpc_arg_pointer_vtable fake_pointer_arg_vtable = { + fake_pointer_arg_copy, fake_pointer_arg_destroy, fake_pointer_cmp}; + +static void test_channel_create_with_args(void) { + grpc_arg client_a[3]; + + // adds integer arg + client_a[0].type = GRPC_ARG_INTEGER; + client_a[0].key = const_cast("arg_int"); + client_a[0].value.integer = 0; + + // adds const str arg + client_a[1].type = GRPC_ARG_STRING; + client_a[1].key = const_cast("arg_str"); + client_a[1].value.string = const_cast("arg_str_val"); + + // allocated and adds custom pointer arg + fake_class* fc = static_cast(gpr_malloc(sizeof(fake_class))); + fc->foo = 42; + client_a[2].type = GRPC_ARG_POINTER; + client_a[2].key = const_cast("arg_pointer"); + client_a[2].value.pointer.vtable = &fake_pointer_arg_vtable; + client_a[2].value.pointer.p = fc; + + // creates channel + grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a}; + grpc_channel* c = + grpc_insecure_channel_create("fake_target", &client_args, nullptr); + // user is can free the memory they allocated here + gpr_free(fc); + grpc_channel_destroy(c); +} + +static void test_server_create_with_args(void) { + grpc_arg server_a[3]; + + // adds integer arg + server_a[0].type = GRPC_ARG_INTEGER; + server_a[0].key = const_cast("arg_int"); + server_a[0].value.integer = 0; + + // adds const str arg + server_a[1].type = GRPC_ARG_STRING; + server_a[1].key = const_cast("arg_str"); + server_a[1].value.string = const_cast("arg_str_val"); + + // allocated and adds custom pointer arg + fake_class* fc = static_cast(gpr_malloc(sizeof(fake_class))); + fc->foo = 42; + server_a[2].type = GRPC_ARG_POINTER; + server_a[2].key = const_cast("arg_pointer"); + server_a[2].value.pointer.vtable = &fake_pointer_arg_vtable; + server_a[2].value.pointer.p = fc; + + // creates server + grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a}; + grpc_server* s = grpc_server_create(&server_args, nullptr); + // user is can free the memory they allocated here + gpr_free(fc); + grpc_server_destroy(s); +} + int main(int argc, char** argv) { grpc_test_init(argc, argv); grpc_init(); @@ -155,6 +235,8 @@ int main(int argc, char** argv) { test_set_compression_algorithm(); test_compression_algorithm_states(); test_set_socket_mutator(); + test_channel_create_with_args(); + test_server_create_with_args(); grpc_shutdown(); return 0; } From 41110e8d3cfb065c68f39639055a110e6c75c9dc Mon Sep 17 00:00:00 2001 From: ncteisen Date: Tue, 29 May 2018 13:52:02 -0700 Subject: [PATCH 2/2] Add logging --- test/core/channel/channel_args_test.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/core/channel/channel_args_test.cc b/test/core/channel/channel_args_test.cc index a098d26974b..41c62a8f16e 100644 --- a/test/core/channel/channel_args_test.cc +++ b/test/core/channel/channel_args_test.cc @@ -153,6 +153,7 @@ struct fake_class { }; static void* fake_pointer_arg_copy(void* arg) { + gpr_log(GPR_DEBUG, "fake_pointer_arg_copy"); fake_class* fc = static_cast(arg); fake_class* new_fc = static_cast(gpr_malloc(sizeof(fake_class))); new_fc->foo = fc->foo; @@ -160,6 +161,7 @@ static void* fake_pointer_arg_copy(void* arg) { } static void fake_pointer_arg_destroy(void* arg) { + gpr_log(GPR_DEBUG, "fake_pointer_arg_destroy"); fake_class* fc = static_cast(arg); gpr_free(fc); }