diff options
author | Dan Zhang <danzh@google.com> | 2017-12-01 15:58:47 -0500 |
---|---|---|
committer | Dan Zhang <danzh@google.com> | 2017-12-01 15:58:47 -0500 |
commit | 26024c801b5b171d448204507f2f10f76c93fe35 (patch) | |
tree | 6130826a0941f3fd1bb4ade0c73cfa013e497f83 /test | |
parent | ec8a59e1125153fcf27eace15cdda8f975adf1c4 (diff) | |
parent | 0a6329c0a825ec36268d72c8fd2ac9a8296a25f0 (diff) |
Merge branch 'master' into asyncio
Diffstat (limited to 'test')
35 files changed, 868 insertions, 304 deletions
diff --git a/test/core/end2end/cq_verifier.h b/test/core/end2end/cq_verifier.h index 0b3b3fb349..959f849cb1 100644 --- a/test/core/end2end/cq_verifier.h +++ b/test/core/end2end/cq_verifier.h @@ -24,10 +24,6 @@ #include <grpc/grpc.h> #include "test/core/util/test_config.h" -#ifdef __cplusplus -extern "C" { -#endif - /* A cq_verifier can verify that expected events arrive in a timely fashion on a single completion queue */ @@ -63,8 +59,4 @@ int contains_metadata(grpc_metadata_array* array, const char* key, int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key, grpc_slice value); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_TEST_CORE_END2END_CQ_VERIFIER_H */ diff --git a/test/core/end2end/data/client_certs.cc b/test/core/end2end/data/client_certs.cc index 7e0b10da5c..46fc13927c 100644 --- a/test/core/end2end/data/client_certs.cc +++ b/test/core/end2end/data/client_certs.cc @@ -16,7 +16,7 @@ * */ -extern "C" const char test_self_signed_client_cert[] = { +extern const char test_self_signed_client_cert[] = { 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x6f, 0x44, 0x43, 0x43, @@ -100,7 +100,7 @@ extern "C" const char test_self_signed_client_cert[] = { 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00}; -extern "C" const char test_self_signed_client_key[] = { +extern const char test_self_signed_client_key[] = { 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x64, 0x77, 0x49, 0x42, @@ -179,7 +179,7 @@ extern "C" const char test_self_signed_client_key[] = { 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00}; -extern "C" const char test_signed_client_cert[] = { +extern const char test_signed_client_cert[] = { 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x48, 0x7a, 0x43, 0x43, @@ -248,7 +248,7 @@ extern "C" const char test_signed_client_cert[] = { 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00}; -extern "C" const char test_signed_client_key[] = { +extern const char test_signed_client_key[] = { 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x65, 0x51, 0x49, 0x42, diff --git a/test/core/end2end/data/server1_cert.cc b/test/core/end2end/data/server1_cert.cc index dd09810732..0943244ecc 100644 --- a/test/core/end2end/data/server1_cert.cc +++ b/test/core/end2end/data/server1_cert.cc @@ -16,7 +16,7 @@ * */ -extern "C" const char test_server1_cert[] = { +extern const char test_server1_cert[] = { 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x6e, 0x44, 0x43, 0x43, diff --git a/test/core/end2end/data/server1_key.cc b/test/core/end2end/data/server1_key.cc index 59dcaf6334..8f3ad15c26 100644 --- a/test/core/end2end/data/server1_key.cc +++ b/test/core/end2end/data/server1_key.cc @@ -16,7 +16,7 @@ * */ -extern "C" const char test_server1_key[] = { +extern const char test_server1_key[] = { 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, diff --git a/test/core/end2end/data/ssl_test_data.h b/test/core/end2end/data/ssl_test_data.h index e9c7dbceb2..303f3a6eda 100644 --- a/test/core/end2end/data/ssl_test_data.h +++ b/test/core/end2end/data/ssl_test_data.h @@ -19,10 +19,6 @@ #ifndef GRPC_TEST_CORE_END2END_DATA_SSL_TEST_DATA_H #define GRPC_TEST_CORE_END2END_DATA_SSL_TEST_DATA_H -#ifdef __cplusplus -extern "C" { -#endif - extern const char test_root_cert[]; extern const char test_server1_cert[]; extern const char test_server1_key[]; @@ -31,8 +27,4 @@ extern const char test_self_signed_client_key[]; extern const char test_signed_client_cert[]; extern const char test_signed_client_key[]; -#ifdef __cplusplus -} -#endif - #endif /* GRPC_TEST_CORE_END2END_DATA_SSL_TEST_DATA_H */ diff --git a/test/core/end2end/data/test_root_cert.cc b/test/core/end2end/data/test_root_cert.cc index 36fca2e45f..b4771b2cb8 100644 --- a/test/core/end2end/data/test_root_cert.cc +++ b/test/core/end2end/data/test_root_cert.cc @@ -16,7 +16,7 @@ * */ -extern "C" const char test_root_cert[] = { +extern const char test_root_cert[] = { 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x49, 0x7a, 0x43, 0x43, diff --git a/test/core/end2end/end2end_tests.h b/test/core/end2end/end2end_tests.h index 33943a7271..b42d90b55c 100644 --- a/test/core/end2end/end2end_tests.h +++ b/test/core/end2end/end2end_tests.h @@ -21,10 +21,6 @@ #include <grpc/grpc.h> -#ifdef __cplusplus -extern "C" { -#endif - typedef struct grpc_end2end_test_fixture grpc_end2end_test_fixture; typedef struct grpc_end2end_test_config grpc_end2end_test_config; @@ -78,8 +74,4 @@ const grpc_slice* get_host_override_slice(const char* str, void validate_host_override_string(const char* pattern, grpc_slice str, grpc_end2end_test_config config); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_TEST_CORE_END2END_END2END_TESTS_H */ diff --git a/test/core/end2end/fuzzers/api_fuzzer.cc b/test/core/end2end/fuzzers/api_fuzzer.cc index b8ec12586e..0b49b89205 100644 --- a/test/core/end2end/fuzzers/api_fuzzer.cc +++ b/test/core/end2end/fuzzers/api_fuzzer.cc @@ -56,7 +56,7 @@ static grpc_server* g_server; static grpc_channel* g_channel; static grpc_resource_quota* g_resource_quota; -extern "C" gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); +extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); static gpr_timespec now_impl(gpr_clock_type clock_type) { GPR_ASSERT(clock_type != GPR_TIMESPAN); @@ -441,7 +441,7 @@ grpc_ares_request* my_dns_lookup_ares( // client connection // defined in tcp_client_posix.c -extern "C" void (*grpc_tcp_client_connect_impl)( +extern void (*grpc_tcp_client_connect_impl)( grpc_exec_ctx* exec_ctx, grpc_closure* closure, grpc_endpoint** ep, grpc_pollset_set* interested_parties, const grpc_channel_args* channel_args, const grpc_resolved_address* addr, grpc_millis deadline); diff --git a/test/core/end2end/tests/no_logging.cc b/test/core/end2end/tests/no_logging.cc index 55d211c44a..bf8651221a 100644 --- a/test/core/end2end/tests/no_logging.cc +++ b/test/core/end2end/tests/no_logging.cc @@ -36,7 +36,7 @@ enum { TIMEOUT = 200000 }; static void* tag(intptr_t t) { return (void*)t; } -extern "C" void gpr_default_log(gpr_log_func_args* args); +void gpr_default_log(gpr_log_func_args* args); static void test_no_log(gpr_log_func_args* args) { char* message = nullptr; diff --git a/test/core/end2end/tests/simple_request.cc b/test/core/end2end/tests/simple_request.cc index ec7425aa09..7eb7467981 100644 --- a/test/core/end2end/tests/simple_request.cc +++ b/test/core/end2end/tests/simple_request.cc @@ -99,6 +99,7 @@ static void simple_request_body(grpc_end2end_test_config config, grpc_metadata_array request_metadata_recv; grpc_call_details call_details; grpc_status_code status; + const char* error_string; grpc_call_error error; grpc_slice details; int was_cancelled = 2; @@ -148,6 +149,7 @@ static void simple_request_body(grpc_end2end_test_config config, op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; op->data.recv_status_on_client.status = &status; op->data.recv_status_on_client.status_details = &details; + op->data.recv_status_on_client.error_string = &error_string; op->flags = 0; op->reserved = nullptr; op++; @@ -199,6 +201,15 @@ static void simple_request_body(grpc_end2end_test_config config, GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); + // the following sanity check makes sure that the requested error string is + // correctly populated by the core. It looks for certain substrings that are + // not likely to change much. Some parts of the error, like time created, + // obviously are not checked. + GPR_ASSERT(nullptr != strstr(error_string, "xyz")); + GPR_ASSERT(nullptr != strstr(error_string, "description")); + GPR_ASSERT(nullptr != strstr(error_string, "Error received from peer")); + GPR_ASSERT(nullptr != strstr(error_string, "grpc_message")); + GPR_ASSERT(nullptr != strstr(error_string, "grpc_status")); GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); validate_host_override_string("foo.test.google.fr:1234", call_details.host, config); @@ -206,6 +217,7 @@ static void simple_request_body(grpc_end2end_test_config config, GPR_ASSERT(was_cancelled == 1); grpc_slice_unref(details); + gpr_free((void*)error_string); grpc_metadata_array_destroy(&initial_metadata_recv); grpc_metadata_array_destroy(&trailing_metadata_recv); grpc_metadata_array_destroy(&request_metadata_recv); diff --git a/test/core/fling/client.cc b/test/core/fling/client.cc index 544b66d480..69fb6dc7c7 100644 --- a/test/core/fling/client.cc +++ b/test/core/fling/client.cc @@ -22,15 +22,15 @@ #include <string.h> #include <grpc/support/cmdline.h> -#include <grpc/support/histogram.h> #include <grpc/support/log.h> #include <grpc/support/time.h> #include <grpc/support/useful.h> #include "src/core/lib/profiling/timers.h" #include "test/core/util/grpc_profiler.h" +#include "test/core/util/histogram.h" #include "test/core/util/test_config.h" -static gpr_histogram* histogram; +static grpc_histogram* histogram; static grpc_byte_buffer* the_buffer; static grpc_channel* channel; static grpc_completion_queue* cq; @@ -195,7 +195,7 @@ int main(int argc, char** argv) { channel = grpc_insecure_channel_create(target, nullptr, nullptr); cq = grpc_completion_queue_create_for_next(nullptr); the_buffer = grpc_raw_byte_buffer_create(&slice, (size_t)payload_size); - histogram = gpr_histogram_create(0.01, 60e9); + histogram = grpc_histogram_create(0.01, 60e9); sc.init(); @@ -213,7 +213,7 @@ int main(int argc, char** argv) { start = now(); sc.do_one_step(); stop = now(); - gpr_histogram_add(histogram, stop - start); + grpc_histogram_add(histogram, stop - start); } grpc_profiler_stop(); @@ -232,11 +232,11 @@ int main(int argc, char** argv) { grpc_slice_unref(slice); gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", - gpr_histogram_percentile(histogram, 50), - gpr_histogram_percentile(histogram, 95), - gpr_histogram_percentile(histogram, 99), - gpr_histogram_percentile(histogram, 99.9)); - gpr_histogram_destroy(histogram); + grpc_histogram_percentile(histogram, 50), + grpc_histogram_percentile(histogram, 95), + grpc_histogram_percentile(histogram, 99), + grpc_histogram_percentile(histogram, 99.9)); + grpc_histogram_destroy(histogram); grpc_shutdown(); diff --git a/test/core/network_benchmarks/low_level_ping_pong.cc b/test/core/network_benchmarks/low_level_ping_pong.cc index 687395d916..2ae9a45d7c 100644 --- a/test/core/network_benchmarks/low_level_ping_pong.cc +++ b/test/core/network_benchmarks/low_level_ping_pong.cc @@ -36,13 +36,13 @@ #include <grpc/support/alloc.h> #include <grpc/support/cmdline.h> -#include <grpc/support/histogram.h> #include <grpc/support/log.h> #include <grpc/support/thd.h> #include <grpc/support/time.h> #include <grpc/support/useful.h> #include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/socket_utils_posix.h" +#include "test/core/util/histogram.h" typedef struct fd_pair { int read_fd; @@ -275,14 +275,14 @@ static void server_thread_wrap(void* arg) { server_thread(args); } -static void print_histogram(gpr_histogram* histogram) { +static void print_histogram(grpc_histogram* histogram) { /* TODO(klempner): Print more detailed information, such as detailed histogram buckets */ gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", - gpr_histogram_percentile(histogram, 50), - gpr_histogram_percentile(histogram, 95), - gpr_histogram_percentile(histogram, 99), - gpr_histogram_percentile(histogram, 99.9)); + grpc_histogram_percentile(histogram, 50), + grpc_histogram_percentile(histogram, 95), + grpc_histogram_percentile(histogram, 99), + grpc_histogram_percentile(histogram, 99.9)); } static double now(void) { @@ -293,7 +293,7 @@ static double now(void) { static void client_thread(thread_args* args) { char* buf = static_cast<char*>(gpr_malloc(args->msg_size * sizeof(char))); memset(buf, 0, args->msg_size * sizeof(char)); - gpr_histogram* histogram = gpr_histogram_create(0.01, 60e9); + grpc_histogram* histogram = grpc_histogram_create(0.01, 60e9); double start_time; double end_time; double interval; @@ -316,13 +316,13 @@ static void client_thread(thread_args* args) { end_time = now(); if (i > kNumIters / 2) { interval = end_time - start_time; - gpr_histogram_add(histogram, interval); + grpc_histogram_add(histogram, interval); } } print_histogram(histogram); error: gpr_free(buf); - gpr_histogram_destroy(histogram); + grpc_histogram_destroy(histogram); } /* This roughly matches tcp_server's create_listening_socket */ diff --git a/test/core/support/BUILD b/test/core/support/BUILD index 996166a371..4372b49b54 100644 --- a/test/core/support/BUILD +++ b/test/core/support/BUILD @@ -69,16 +69,6 @@ grpc_cc_test( ) grpc_cc_test( - name = "histogram_test", - srcs = ["histogram_test.cc"], - language = "C++", - deps = [ - "//:gpr", - "//test/core/util:gpr_test_util", - ], -) - -grpc_cc_test( name = "host_port_test", srcs = ["host_port_test.cc"], language = "C++", @@ -223,3 +213,30 @@ grpc_cc_test( "//test/core/util:gpr_test_util", ], ) + +grpc_cc_test( + name = "ref_counted_test", + srcs = ["ref_counted_test.cc"], + language = "C++", + deps = [ + "//:ref_counted", + "//test/core/util:gpr_test_util", + ], + external_deps = [ + "gtest", + ], +) + +grpc_cc_test( + name = "ref_counted_ptr_test", + srcs = ["ref_counted_ptr_test.cc"], + language = "C++", + deps = [ + "//:ref_counted", + "//:ref_counted_ptr", + "//test/core/util:gpr_test_util", + ], + external_deps = [ + "gtest", + ], +) diff --git a/test/core/support/histogram_test.cc b/test/core/support/histogram_test.cc deleted file mode 100644 index 86b7d599e6..0000000000 --- a/test/core/support/histogram_test.cc +++ /dev/null @@ -1,163 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <grpc/support/histogram.h> -#include <grpc/support/log.h> - -#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x); - -static void test_no_op(void) { - gpr_histogram_destroy(gpr_histogram_create(0.01, 60e9)); -} - -static void expect_percentile(gpr_histogram* h, double percentile, - double min_expect, double max_expect) { - double got = gpr_histogram_percentile(h, percentile); - gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, - max_expect); - GPR_ASSERT(min_expect <= got); - GPR_ASSERT(got <= max_expect); -} - -static void test_simple(void) { - gpr_histogram* h; - - LOG_TEST("test_simple"); - - h = gpr_histogram_create(0.01, 60e9); - gpr_histogram_add(h, 10000); - gpr_histogram_add(h, 10000); - gpr_histogram_add(h, 11000); - gpr_histogram_add(h, 11000); - - expect_percentile(h, 50, 10001, 10999); - GPR_ASSERT(gpr_histogram_mean(h) == 10500); - - gpr_histogram_destroy(h); -} - -static void test_percentile(void) { - gpr_histogram* h; - double last; - double i; - double cur; - - LOG_TEST("test_percentile"); - - h = gpr_histogram_create(0.05, 1e9); - gpr_histogram_add(h, 2.5); - gpr_histogram_add(h, 2.5); - gpr_histogram_add(h, 8); - gpr_histogram_add(h, 4); - - GPR_ASSERT(gpr_histogram_count(h) == 4); - GPR_ASSERT(gpr_histogram_minimum(h) == 2.5); - GPR_ASSERT(gpr_histogram_maximum(h) == 8); - GPR_ASSERT(gpr_histogram_sum(h) == 17); - GPR_ASSERT(gpr_histogram_sum_of_squares(h) == 92.5); - GPR_ASSERT(gpr_histogram_mean(h) == 4.25); - GPR_ASSERT(gpr_histogram_variance(h) == 5.0625); - GPR_ASSERT(gpr_histogram_stddev(h) == 2.25); - - expect_percentile(h, -10, 2.5, 2.5); - expect_percentile(h, 0, 2.5, 2.5); - expect_percentile(h, 12.5, 2.5, 2.5); - expect_percentile(h, 25, 2.5, 2.5); - expect_percentile(h, 37.5, 2.5, 2.8); - expect_percentile(h, 50, 3.0, 3.5); - expect_percentile(h, 62.5, 3.5, 4.5); - expect_percentile(h, 75, 5, 7.9); - expect_percentile(h, 100, 8, 8); - expect_percentile(h, 110, 8, 8); - - /* test monotonicity */ - last = 0.0; - for (i = 0; i < 100.0; i += 0.01) { - cur = gpr_histogram_percentile(h, i); - GPR_ASSERT(cur >= last); - last = cur; - } - - gpr_histogram_destroy(h); -} - -static void test_merge(void) { - gpr_histogram *h1, *h2; - double last; - double i; - double cur; - - LOG_TEST("test_merge"); - - h1 = gpr_histogram_create(0.05, 1e9); - gpr_histogram_add(h1, 2.5); - gpr_histogram_add(h1, 2.5); - gpr_histogram_add(h1, 8); - gpr_histogram_add(h1, 4); - - h2 = gpr_histogram_create(0.01, 1e9); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); - gpr_histogram_destroy(h2); - - h2 = gpr_histogram_create(0.05, 1e10); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); - gpr_histogram_destroy(h2); - - h2 = gpr_histogram_create(0.05, 1e9); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); - GPR_ASSERT(gpr_histogram_count(h1) == 4); - GPR_ASSERT(gpr_histogram_minimum(h1) == 2.5); - GPR_ASSERT(gpr_histogram_maximum(h1) == 8); - GPR_ASSERT(gpr_histogram_sum(h1) == 17); - GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 92.5); - GPR_ASSERT(gpr_histogram_mean(h1) == 4.25); - GPR_ASSERT(gpr_histogram_variance(h1) == 5.0625); - GPR_ASSERT(gpr_histogram_stddev(h1) == 2.25); - gpr_histogram_destroy(h2); - - h2 = gpr_histogram_create(0.05, 1e9); - gpr_histogram_add(h2, 7.0); - gpr_histogram_add(h2, 17.0); - gpr_histogram_add(h2, 1.0); - GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); - GPR_ASSERT(gpr_histogram_count(h1) == 7); - GPR_ASSERT(gpr_histogram_minimum(h1) == 1.0); - GPR_ASSERT(gpr_histogram_maximum(h1) == 17.0); - GPR_ASSERT(gpr_histogram_sum(h1) == 42.0); - GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 431.5); - GPR_ASSERT(gpr_histogram_mean(h1) == 6.0); - - /* test monotonicity */ - last = 0.0; - for (i = 0; i < 100.0; i += 0.01) { - cur = gpr_histogram_percentile(h1, i); - GPR_ASSERT(cur >= last); - last = cur; - } - - gpr_histogram_destroy(h1); - gpr_histogram_destroy(h2); -} - -int main(void) { - test_no_op(); - test_simple(); - test_percentile(); - test_merge(); - return 0; -} diff --git a/test/core/support/ref_counted_ptr_test.cc b/test/core/support/ref_counted_ptr_test.cc new file mode 100644 index 0000000000..1830edc4e5 --- /dev/null +++ b/test/core/support/ref_counted_ptr_test.cc @@ -0,0 +1,172 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/support/ref_counted_ptr.h" + +#include <gtest/gtest.h> + +#include <grpc/support/log.h> + +#include "src/core/lib/support/memory.h" +#include "src/core/lib/support/ref_counted.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public RefCounted { + public: + Foo() : value_(0) {} + + explicit Foo(int value) : value_(value) {} + + int value() const { return value_; } + + private: + int value_; +}; + +TEST(RefCountedPtr, DefaultConstructor) { RefCountedPtr<Foo> foo; } + +TEST(RefCountedPtr, ExplicitConstructorEmpty) { + RefCountedPtr<Foo> foo(nullptr); +} + +TEST(RefCountedPtr, ExplicitConstructor) { RefCountedPtr<Foo> foo(New<Foo>()); } + +TEST(RefCountedPtr, MoveConstructor) { + RefCountedPtr<Foo> foo(New<Foo>()); + RefCountedPtr<Foo> foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, MoveAssignment) { + RefCountedPtr<Foo> foo(New<Foo>()); + RefCountedPtr<Foo> foo2 = std::move(foo); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, CopyConstructor) { + RefCountedPtr<Foo> foo(New<Foo>()); + RefCountedPtr<Foo> foo2(foo); + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignment) { + RefCountedPtr<Foo> foo(New<Foo>()); + RefCountedPtr<Foo> foo2 = foo; + EXPECT_NE(nullptr, foo.get()); + EXPECT_EQ(foo.get(), foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignmentWhenEmpty) { + RefCountedPtr<Foo> foo; + RefCountedPtr<Foo> foo2; + foo2 = foo; + EXPECT_EQ(nullptr, foo.get()); + EXPECT_EQ(nullptr, foo2.get()); +} + +TEST(RefCountedPtr, CopyAssignmentToSelf) { + RefCountedPtr<Foo> foo(New<Foo>()); + foo = foo; +} + +TEST(RefCountedPtr, EnclosedScope) { + RefCountedPtr<Foo> foo(New<Foo>()); + { + RefCountedPtr<Foo> foo2(std::move(foo)); + EXPECT_EQ(nullptr, foo.get()); + EXPECT_NE(nullptr, foo2.get()); + } + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNullToNonNull) { + RefCountedPtr<Foo> foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(New<Foo>()); + EXPECT_NE(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNonNullToNonNull) { + RefCountedPtr<Foo> foo(New<Foo>()); + EXPECT_NE(nullptr, foo.get()); + Foo* original = foo.get(); + foo.reset(New<Foo>()); + EXPECT_NE(nullptr, foo.get()); + EXPECT_NE(original, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNonNullToNull) { + RefCountedPtr<Foo> foo(New<Foo>()); + EXPECT_NE(nullptr, foo.get()); + foo.reset(); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, ResetFromNullToNull) { + RefCountedPtr<Foo> foo; + EXPECT_EQ(nullptr, foo.get()); + foo.reset(nullptr); + EXPECT_EQ(nullptr, foo.get()); +} + +TEST(RefCountedPtr, DerefernceOperators) { + RefCountedPtr<Foo> foo(New<Foo>()); + foo->value(); + Foo& foo_ref = *foo; + foo_ref.value(); +} + +TEST(MakeRefCounted, NoArgs) { + RefCountedPtr<Foo> foo = MakeRefCounted<Foo>(); + EXPECT_EQ(0, foo->value()); +} + +TEST(MakeRefCounted, Args) { + RefCountedPtr<Foo> foo = MakeRefCounted<Foo>(3); + EXPECT_EQ(3, foo->value()); +} + +TraceFlag foo_tracer(true, "foo"); + +class FooWithTracing : public RefCountedWithTracing { + public: + FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} +}; + +TEST(RefCountedPtr, RefCountedWithTracing) { + RefCountedPtr<FooWithTracing> foo(New<FooWithTracing>()); + foo->Ref(DEBUG_LOCATION, "foo"); + foo->Unref(DEBUG_LOCATION, "foo"); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/support/ref_counted_test.cc b/test/core/support/ref_counted_test.cc new file mode 100644 index 0000000000..be9b6ff7c2 --- /dev/null +++ b/test/core/support/ref_counted_test.cc @@ -0,0 +1,72 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/support/ref_counted.h" + +#include <gtest/gtest.h> + +#include "src/core/lib/support/memory.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public RefCounted { + public: + Foo() {} +}; + +TEST(RefCounted, Basic) { + Foo* foo = New<Foo>(); + foo->Unref(); +} + +TEST(RefCounted, ExtraRef) { + Foo* foo = New<Foo>(); + foo->Ref(); + foo->Unref(); + foo->Unref(); +} + +TraceFlag foo_tracer(true, "foo"); + +class FooWithTracing : public RefCountedWithTracing { + public: + FooWithTracing() : RefCountedWithTracing(&foo_tracer) {} +}; + +TEST(RefCountedWithTracing, Basic) { + FooWithTracing* foo = New<FooWithTracing>(); + foo->Ref(DEBUG_LOCATION, "extra_ref"); + foo->Unref(DEBUG_LOCATION, "extra_ref"); + // Can use the no-argument methods, too. + foo->Ref(); + foo->Unref(); + foo->Unref(DEBUG_LOCATION, "original_ref"); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/surface/concurrent_connectivity_test.cc b/test/core/surface/concurrent_connectivity_test.cc index 8fa15ab331..c7611b0dd1 100644 --- a/test/core/surface/concurrent_connectivity_test.cc +++ b/test/core/surface/concurrent_connectivity_test.cc @@ -49,10 +49,11 @@ #define NUM_OUTER_LOOPS_SHORT_TIMEOUTS 10 #define NUM_INNER_LOOPS_SHORT_TIMEOUTS 100 #define DELAY_MILLIS_SHORT_TIMEOUTS 1 -// in a successful test run, POLL_MILLIS should never be reached beause all runs -// should -// end after the shorter delay_millis +// in a successful test run, POLL_MILLIS should never be reached because all +// runs should end after the shorter delay_millis #define POLL_MILLIS_SHORT_TIMEOUTS 30000 +// it should never take longer that this to shutdown the server +#define SERVER_SHUTDOWN_TIMEOUT 30000 static void* tag(int n) { return (void*)(uintptr_t)n; } static int detag(void* p) { return (int)(uintptr_t)p; } @@ -95,7 +96,8 @@ struct server_thread_args { void server_thread(void* vargs) { struct server_thread_args* args = (struct server_thread_args*)vargs; grpc_event ev; - gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); + gpr_timespec deadline = + grpc_timeout_milliseconds_to_deadline(SERVER_SHUTDOWN_TIMEOUT); ev = grpc_completion_queue_next(args->cq, deadline, nullptr); GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); GPR_ASSERT(detag(ev.tag) == 0xd1e); diff --git a/test/core/surface/public_headers_must_be_c89.c b/test/core/surface/public_headers_must_be_c89.c index b5a83c8eed..8d2384ba61 100644 --- a/test/core/surface/public_headers_must_be_c89.c +++ b/test/core/surface/public_headers_must_be_c89.c @@ -20,6 +20,7 @@ #include <grpc/byte_buffer_reader.h> #include <grpc/census.h> #include <grpc/compression.h> +#include <grpc/fork.h> #include <grpc/grpc.h> #include <grpc/grpc_security.h> #include <grpc/grpc_security_constants.h> @@ -29,6 +30,7 @@ #include <grpc/impl/codegen/compression_types.h> #include <grpc/impl/codegen/connectivity_state.h> #include <grpc/impl/codegen/exec_ctx_fwd.h> +#include <grpc/impl/codegen/fork.h> #include <grpc/impl/codegen/gpr_slice.h> #include <grpc/impl/codegen/gpr_types.h> #include <grpc/impl/codegen/grpc_types.h> @@ -48,7 +50,6 @@ #include <grpc/support/avl.h> #include <grpc/support/cmdline.h> #include <grpc/support/cpu.h> -#include <grpc/support/histogram.h> #include <grpc/support/host_port.h> #include <grpc/support/log.h> #include <grpc/support/port_platform.h> @@ -264,21 +265,6 @@ int main(int argc, char **argv) { printf("%lx", (unsigned long) gpr_cmdline_usage_string); printf("%lx", (unsigned long) gpr_cpu_num_cores); printf("%lx", (unsigned long) gpr_cpu_current_cpu); - printf("%lx", (unsigned long) gpr_histogram_create); - printf("%lx", (unsigned long) gpr_histogram_destroy); - printf("%lx", (unsigned long) gpr_histogram_add); - printf("%lx", (unsigned long) gpr_histogram_merge); - printf("%lx", (unsigned long) gpr_histogram_percentile); - printf("%lx", (unsigned long) gpr_histogram_mean); - printf("%lx", (unsigned long) gpr_histogram_stddev); - printf("%lx", (unsigned long) gpr_histogram_variance); - printf("%lx", (unsigned long) gpr_histogram_maximum); - printf("%lx", (unsigned long) gpr_histogram_minimum); - printf("%lx", (unsigned long) gpr_histogram_count); - printf("%lx", (unsigned long) gpr_histogram_sum); - printf("%lx", (unsigned long) gpr_histogram_sum_of_squares); - printf("%lx", (unsigned long) gpr_histogram_get_contents); - printf("%lx", (unsigned long) gpr_histogram_merge_contents); printf("%lx", (unsigned long) gpr_join_host_port); printf("%lx", (unsigned long) gpr_split_host_port); printf("%lx", (unsigned long) gpr_log_severity_string); diff --git a/test/core/transport/bdp_estimator_test.cc b/test/core/transport/bdp_estimator_test.cc index 2a6fa95162..d1272607f2 100644 --- a/test/core/transport/bdp_estimator_test.cc +++ b/test/core/transport/bdp_estimator_test.cc @@ -29,7 +29,7 @@ #include "src/core/lib/support/string.h" #include "test/core/util/test_config.h" -extern "C" gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); +extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); namespace grpc_core { namespace testing { diff --git a/test/core/util/BUILD b/test/core/util/BUILD index 6443553466..ec88d767ce 100644 --- a/test/core/util/BUILD +++ b/test/core/util/BUILD @@ -16,7 +16,10 @@ load("//bazel:grpc_build_system.bzl", "grpc_cc_library", "grpc_cc_test", "grpc_c licenses(["notice"]) # Apache v2 -grpc_package(name = "test/core/util", visibility = "public") +grpc_package( + name = "test/core/util", + visibility = "public", +) grpc_cc_library( name = "gpr_test_util", @@ -49,10 +52,12 @@ grpc_cc_library( name = "grpc_test_util_base", srcs = [ "grpc_profiler.cc", + "histogram.cc", "mock_endpoint.cc", "parse_hexstring.cc", "passthru_endpoint.cc", "port.cc", + "port_isolated_runtime_environment.cc", "port_server_client.cc", "reconnect_server.cc", "slice_splitter.cc", @@ -62,6 +67,7 @@ grpc_cc_library( ], hdrs = [ "grpc_profiler.h", + "histogram.h", "mock_endpoint.h", "parse_hexstring.h", "passthru_endpoint.h", @@ -76,8 +82,8 @@ grpc_cc_library( language = "C++", deps = [ ":gpr_test_util", + ":grpc_debugger_macros", "//:grpc_common", - ":grpc_debugger_macros" ], ) @@ -107,13 +113,23 @@ grpc_cc_library( name = "fuzzer_corpus_test", testonly = 1, srcs = ["fuzzer_corpus_test.cc"], + external_deps = [ + "gtest", + "gflags", + ], deps = [ ":gpr_test_util", "//:grpc", ], - external_deps = [ - "gtest", - "gflags", +) + +grpc_cc_test( + name = "histogram_test", + srcs = ["histogram_test.cc"], + language = "C++", + deps = [ + ":grpc_test_util", + "//:gpr", ], ) diff --git a/test/core/util/fuzzer_corpus_test.cc b/test/core/util/fuzzer_corpus_test.cc index d7aea54262..7849321257 100644 --- a/test/core/util/fuzzer_corpus_test.cc +++ b/test/core/util/fuzzer_corpus_test.cc @@ -29,8 +29,8 @@ #include "test/core/util/test_config.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); -extern "C" bool squelch; -extern "C" bool leak_check; +extern bool squelch; +extern bool leak_check; // In some distros, gflags is in the namespace google, and in some others, // in gflags. This hack is enabling us to find both. diff --git a/test/core/util/histogram.cc b/test/core/util/histogram.cc new file mode 100644 index 0000000000..2f916f831d --- /dev/null +++ b/test/core/util/histogram.cc @@ -0,0 +1,227 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include <math.h> +#include <stddef.h> +#include <string.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> +#include <grpc/support/port_platform.h> +#include <grpc/support/useful.h> + +#include "test/core/util/histogram.h" + +/* Histograms are stored with exponentially increasing bucket sizes. + The first bucket is [0, m) where m = 1 + resolution + Bucket n (n>=1) contains [m**n, m**(n+1)) + There are sufficient buckets to reach max_bucket_start */ + +struct grpc_histogram { + /* Sum of all values seen so far */ + double sum; + /* Sum of squares of all values seen so far */ + double sum_of_squares; + /* number of values seen so far */ + double count; + /* m in the description */ + double multiplier; + double one_on_log_multiplier; + /* minimum value seen */ + double min_seen; + /* maximum value seen */ + double max_seen; + /* maximum representable value */ + double max_possible; + /* number of buckets */ + size_t num_buckets; + /* the buckets themselves */ + uint32_t* buckets; +}; + +/* determine a bucket index given a value - does no bounds checking */ +static size_t bucket_for_unchecked(grpc_histogram* h, double x) { + return (size_t)(log(x) * h->one_on_log_multiplier); +} + +/* bounds checked version of the above */ +static size_t bucket_for(grpc_histogram* h, double x) { + size_t bucket = bucket_for_unchecked(h, GPR_CLAMP(x, 1.0, h->max_possible)); + GPR_ASSERT(bucket < h->num_buckets); + return bucket; +} + +/* at what value does a bucket start? */ +static double bucket_start(grpc_histogram* h, double x) { + return pow(h->multiplier, x); +} + +grpc_histogram* grpc_histogram_create(double resolution, + double max_bucket_start) { + grpc_histogram* h = (grpc_histogram*)gpr_malloc(sizeof(grpc_histogram)); + GPR_ASSERT(resolution > 0.0); + GPR_ASSERT(max_bucket_start > resolution); + h->sum = 0.0; + h->sum_of_squares = 0.0; + h->multiplier = 1.0 + resolution; + h->one_on_log_multiplier = 1.0 / log(1.0 + resolution); + h->max_possible = max_bucket_start; + h->count = 0.0; + h->min_seen = max_bucket_start; + h->max_seen = 0.0; + h->num_buckets = bucket_for_unchecked(h, max_bucket_start) + 1; + GPR_ASSERT(h->num_buckets > 1); + GPR_ASSERT(h->num_buckets < 100000000); + h->buckets = (uint32_t*)gpr_zalloc(sizeof(uint32_t) * h->num_buckets); + return h; +} + +void grpc_histogram_destroy(grpc_histogram* h) { + gpr_free(h->buckets); + gpr_free(h); +} + +void grpc_histogram_add(grpc_histogram* h, double x) { + h->sum += x; + h->sum_of_squares += x * x; + h->count++; + if (x < h->min_seen) { + h->min_seen = x; + } + if (x > h->max_seen) { + h->max_seen = x; + } + h->buckets[bucket_for(h, x)]++; +} + +int grpc_histogram_merge(grpc_histogram* dst, const grpc_histogram* src) { + if ((dst->num_buckets != src->num_buckets) || + (dst->multiplier != src->multiplier)) { + /* Fail because these histograms don't match */ + return 0; + } + grpc_histogram_merge_contents(dst, src->buckets, src->num_buckets, + src->min_seen, src->max_seen, src->sum, + src->sum_of_squares, src->count); + return 1; +} + +void grpc_histogram_merge_contents(grpc_histogram* dst, const uint32_t* data, + size_t data_count, double min_seen, + double max_seen, double sum, + double sum_of_squares, double count) { + size_t i; + GPR_ASSERT(dst->num_buckets == data_count); + dst->sum += sum; + dst->sum_of_squares += sum_of_squares; + dst->count += count; + if (min_seen < dst->min_seen) { + dst->min_seen = min_seen; + } + if (max_seen > dst->max_seen) { + dst->max_seen = max_seen; + } + for (i = 0; i < dst->num_buckets; i++) { + dst->buckets[i] += data[i]; + } +} + +static double threshold_for_count_below(grpc_histogram* h, double count_below) { + double count_so_far; + double lower_bound; + double upper_bound; + size_t lower_idx; + size_t upper_idx; + + if (h->count == 0) { + return 0.0; + } + + if (count_below <= 0) { + return h->min_seen; + } + if (count_below >= h->count) { + return h->max_seen; + } + + /* find the lowest bucket that gets us above count_below */ + count_so_far = 0.0; + for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) { + count_so_far += h->buckets[lower_idx]; + if (count_so_far >= count_below) { + break; + } + } + if (count_so_far == count_below) { + /* this bucket hits the threshold exactly... we should be midway through + any run of zero values following the bucket */ + for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) { + if (h->buckets[upper_idx]) { + break; + } + } + return (bucket_start(h, (double)lower_idx) + + bucket_start(h, (double)upper_idx)) / + 2.0; + } else { + /* treat values as uniform throughout the bucket, and find where this value + should lie */ + lower_bound = bucket_start(h, (double)lower_idx); + upper_bound = bucket_start(h, (double)(lower_idx + 1)); + return GPR_CLAMP(upper_bound - (upper_bound - lower_bound) * + (count_so_far - count_below) / + h->buckets[lower_idx], + h->min_seen, h->max_seen); + } +} + +double grpc_histogram_percentile(grpc_histogram* h, double percentile) { + return threshold_for_count_below(h, h->count * percentile / 100.0); +} + +double grpc_histogram_mean(grpc_histogram* h) { + GPR_ASSERT(h->count != 0); + return h->sum / h->count; +} + +double grpc_histogram_stddev(grpc_histogram* h) { + return sqrt(grpc_histogram_variance(h)); +} + +double grpc_histogram_variance(grpc_histogram* h) { + if (h->count == 0) return 0.0; + return (h->sum_of_squares * h->count - h->sum * h->sum) / + (h->count * h->count); +} + +double grpc_histogram_maximum(grpc_histogram* h) { return h->max_seen; } + +double grpc_histogram_minimum(grpc_histogram* h) { return h->min_seen; } + +double grpc_histogram_count(grpc_histogram* h) { return h->count; } + +double grpc_histogram_sum(grpc_histogram* h) { return h->sum; } + +double grpc_histogram_sum_of_squares(grpc_histogram* h) { + return h->sum_of_squares; +} + +const uint32_t* grpc_histogram_get_contents(grpc_histogram* h, size_t* size) { + *size = h->num_buckets; + return h->buckets; +} diff --git a/test/core/util/histogram.h b/test/core/util/histogram.h new file mode 100644 index 0000000000..9d4985e64f --- /dev/null +++ b/test/core/util/histogram.h @@ -0,0 +1,62 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_SUPPORT_HISTOGRAM_H +#define GRPC_SUPPORT_HISTOGRAM_H + +#include <grpc/support/port_platform.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct grpc_histogram grpc_histogram; + +grpc_histogram* grpc_histogram_create(double resolution, + double max_bucket_start); +void grpc_histogram_destroy(grpc_histogram* h); +void grpc_histogram_add(grpc_histogram* h, double x); + +/** The following merges the second histogram into the first. It only works + if they have the same buckets and resolution. Returns 0 on failure, 1 + on success */ +int grpc_histogram_merge(grpc_histogram* dst, const grpc_histogram* src); + +double grpc_histogram_percentile(grpc_histogram* histogram, double percentile); +double grpc_histogram_mean(grpc_histogram* histogram); +double grpc_histogram_stddev(grpc_histogram* histogram); +double grpc_histogram_variance(grpc_histogram* histogram); +double grpc_histogram_maximum(grpc_histogram* histogram); +double grpc_histogram_minimum(grpc_histogram* histogram); +double grpc_histogram_count(grpc_histogram* histogram); +double grpc_histogram_sum(grpc_histogram* histogram); +double grpc_histogram_sum_of_squares(grpc_histogram* histogram); + +const uint32_t* grpc_histogram_get_contents(grpc_histogram* histogram, + size_t* count); +void grpc_histogram_merge_contents(grpc_histogram* histogram, + const uint32_t* data, size_t data_count, + double min_seen, double max_seen, double sum, + double sum_of_squares, double count); + +#ifdef __cplusplus +} +#endif + +#endif /* GRPC_SUPPORT_HISTOGRAM_H */ diff --git a/test/core/util/histogram_test.cc b/test/core/util/histogram_test.cc new file mode 100644 index 0000000000..b96ac7d841 --- /dev/null +++ b/test/core/util/histogram_test.cc @@ -0,0 +1,163 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "test/core/util/histogram.h" +#include <grpc/support/log.h> + +#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x); + +static void test_no_op(void) { + grpc_histogram_destroy(grpc_histogram_create(0.01, 60e9)); +} + +static void expect_percentile(grpc_histogram* h, double percentile, + double min_expect, double max_expect) { + double got = grpc_histogram_percentile(h, percentile); + gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, + max_expect); + GPR_ASSERT(min_expect <= got); + GPR_ASSERT(got <= max_expect); +} + +static void test_simple(void) { + grpc_histogram* h; + + LOG_TEST("test_simple"); + + h = grpc_histogram_create(0.01, 60e9); + grpc_histogram_add(h, 10000); + grpc_histogram_add(h, 10000); + grpc_histogram_add(h, 11000); + grpc_histogram_add(h, 11000); + + expect_percentile(h, 50, 10001, 10999); + GPR_ASSERT(grpc_histogram_mean(h) == 10500); + + grpc_histogram_destroy(h); +} + +static void test_percentile(void) { + grpc_histogram* h; + double last; + double i; + double cur; + + LOG_TEST("test_percentile"); + + h = grpc_histogram_create(0.05, 1e9); + grpc_histogram_add(h, 2.5); + grpc_histogram_add(h, 2.5); + grpc_histogram_add(h, 8); + grpc_histogram_add(h, 4); + + GPR_ASSERT(grpc_histogram_count(h) == 4); + GPR_ASSERT(grpc_histogram_minimum(h) == 2.5); + GPR_ASSERT(grpc_histogram_maximum(h) == 8); + GPR_ASSERT(grpc_histogram_sum(h) == 17); + GPR_ASSERT(grpc_histogram_sum_of_squares(h) == 92.5); + GPR_ASSERT(grpc_histogram_mean(h) == 4.25); + GPR_ASSERT(grpc_histogram_variance(h) == 5.0625); + GPR_ASSERT(grpc_histogram_stddev(h) == 2.25); + + expect_percentile(h, -10, 2.5, 2.5); + expect_percentile(h, 0, 2.5, 2.5); + expect_percentile(h, 12.5, 2.5, 2.5); + expect_percentile(h, 25, 2.5, 2.5); + expect_percentile(h, 37.5, 2.5, 2.8); + expect_percentile(h, 50, 3.0, 3.5); + expect_percentile(h, 62.5, 3.5, 4.5); + expect_percentile(h, 75, 5, 7.9); + expect_percentile(h, 100, 8, 8); + expect_percentile(h, 110, 8, 8); + + /* test monotonicity */ + last = 0.0; + for (i = 0; i < 100.0; i += 0.01) { + cur = grpc_histogram_percentile(h, i); + GPR_ASSERT(cur >= last); + last = cur; + } + + grpc_histogram_destroy(h); +} + +static void test_merge(void) { + grpc_histogram *h1, *h2; + double last; + double i; + double cur; + + LOG_TEST("test_merge"); + + h1 = grpc_histogram_create(0.05, 1e9); + grpc_histogram_add(h1, 2.5); + grpc_histogram_add(h1, 2.5); + grpc_histogram_add(h1, 8); + grpc_histogram_add(h1, 4); + + h2 = grpc_histogram_create(0.01, 1e9); + GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0); + grpc_histogram_destroy(h2); + + h2 = grpc_histogram_create(0.05, 1e10); + GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0); + grpc_histogram_destroy(h2); + + h2 = grpc_histogram_create(0.05, 1e9); + GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1); + GPR_ASSERT(grpc_histogram_count(h1) == 4); + GPR_ASSERT(grpc_histogram_minimum(h1) == 2.5); + GPR_ASSERT(grpc_histogram_maximum(h1) == 8); + GPR_ASSERT(grpc_histogram_sum(h1) == 17); + GPR_ASSERT(grpc_histogram_sum_of_squares(h1) == 92.5); + GPR_ASSERT(grpc_histogram_mean(h1) == 4.25); + GPR_ASSERT(grpc_histogram_variance(h1) == 5.0625); + GPR_ASSERT(grpc_histogram_stddev(h1) == 2.25); + grpc_histogram_destroy(h2); + + h2 = grpc_histogram_create(0.05, 1e9); + grpc_histogram_add(h2, 7.0); + grpc_histogram_add(h2, 17.0); + grpc_histogram_add(h2, 1.0); + GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1); + GPR_ASSERT(grpc_histogram_count(h1) == 7); + GPR_ASSERT(grpc_histogram_minimum(h1) == 1.0); + GPR_ASSERT(grpc_histogram_maximum(h1) == 17.0); + GPR_ASSERT(grpc_histogram_sum(h1) == 42.0); + GPR_ASSERT(grpc_histogram_sum_of_squares(h1) == 431.5); + GPR_ASSERT(grpc_histogram_mean(h1) == 6.0); + + /* test monotonicity */ + last = 0.0; + for (i = 0; i < 100.0; i += 0.01) { + cur = grpc_histogram_percentile(h1, i); + GPR_ASSERT(cur >= last); + last = cur; + } + + grpc_histogram_destroy(h1); + grpc_histogram_destroy(h2); +} + +int main(void) { + test_no_op(); + test_simple(); + test_percentile(); + test_merge(); + return 0; +} diff --git a/test/core/util/port.h b/test/core/util/port.h index 602099dea6..3a4cf4467a 100644 --- a/test/core/util/port.h +++ b/test/core/util/port.h @@ -19,10 +19,6 @@ #ifndef GRPC_TEST_CORE_UTIL_PORT_H #define GRPC_TEST_CORE_UTIL_PORT_H -#ifdef __cplusplus -extern "C" { -#endif - typedef struct grpc_pick_port_functions { int (*pick_unused_port_fn)(void); int (*pick_unused_port_or_die_fn)(void); @@ -45,8 +41,4 @@ void grpc_recycle_unused_port(int port); /** Request the family of pick_port functions in \a functions be used. */ void grpc_set_pick_port_functions(grpc_pick_port_functions functions); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_TEST_CORE_UTIL_PORT_H */ diff --git a/test/core/util/port_isolated_runtime_environment.cc b/test/core/util/port_isolated_runtime_environment.cc new file mode 100644 index 0000000000..5f0585e9fb --- /dev/null +++ b/test/core/util/port_isolated_runtime_environment.cc @@ -0,0 +1,42 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/* When running tests on remote machines, the framework takes a round-robin pick + * of a port within certain range. There is no need to recycle ports. + */ +#include "src/core/lib/iomgr/port.h" +#include "test/core/util/test_config.h" +#if defined(GRPC_PORT_ISOLATED_RUNTIME) + +#include "test/core/util/port.h" + +#define LOWER_PORT 49152 +static int s_allocated_port = LOWER_PORT; + +int grpc_pick_unused_port_or_die(void) { + int allocated_port = s_allocated_port++; + if (s_allocated_port == 65536) { + s_allocated_port = LOWER_PORT; + } + + return allocated_port; +} + +void grpc_recycle_unused_port(int port) { (void)port; } + +#endif /* GRPC_PORT_ISOLATED_RUNTIME */ diff --git a/test/core/util/test_config.h b/test/core/util/test_config.h index 4383fbfce8..5b3d34799e 100644 --- a/test/core/util/test_config.h +++ b/test/core/util/test_config.h @@ -21,10 +21,6 @@ #include <grpc/support/time.h> -#ifdef __cplusplus -extern "C" { -#endif - extern int64_t g_fixture_slowdown_factor; extern int64_t g_poller_slowdown_factor; @@ -37,14 +33,10 @@ gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s); /* Converts a given timeout (in milliseconds) to a deadline. */ gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms); -#ifndef GRPC_TEST_CUSTOM_PICK_PORT +#if !defined(GRPC_TEST_CUSTOM_PICK_PORT) && !defined(GRPC_PORT_ISOLATED_RUNTIME) #define GRPC_TEST_PICK_PORT #endif void grpc_test_init(int argc, char** argv); -#ifdef __cplusplus -} -#endif - #endif /* GRPC_TEST_CORE_UTIL_TEST_CONFIG_H */ diff --git a/test/cpp/client/client_channel_stress_test.cc b/test/cpp/client/client_channel_stress_test.cc index 8940f6ff9e..0954b28df0 100644 --- a/test/cpp/client/client_channel_stress_test.cc +++ b/test/cpp/client/client_channel_stress_test.cc @@ -34,10 +34,8 @@ #include <grpc/support/thd.h> #include <grpc/support/time.h> -extern "C" { #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h" #include "src/core/lib/iomgr/sockaddr.h" -} #include "test/core/util/port.h" #include "test/core/util/test_config.h" diff --git a/test/cpp/interop/stress_test.cc b/test/cpp/interop/stress_test.cc index 028ff11b20..4b39dc3211 100644 --- a/test/cpp/interop/stress_test.cc +++ b/test/cpp/interop/stress_test.cc @@ -36,9 +36,7 @@ #include "test/cpp/util/metrics_server.h" #include "test/cpp/util/test_config.h" -extern "C" { extern void gpr_default_log(gpr_log_func_args* args); -} DEFINE_int32(metrics_port, 8081, "The metrics server port."); diff --git a/test/cpp/microbenchmarks/bm_fullstack_trickle.cc b/test/cpp/microbenchmarks/bm_fullstack_trickle.cc index bb974fad50..5e72213823 100644 --- a/test/cpp/microbenchmarks/bm_fullstack_trickle.cc +++ b/test/cpp/microbenchmarks/bm_fullstack_trickle.cc @@ -445,7 +445,7 @@ BENCHMARK(BM_PumpUnbalancedUnary_Trickle)->Apply(UnaryTrickleArgs); } // namespace testing } // namespace grpc -extern "C" gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); +extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); int main(int argc, char** argv) { ::benchmark::Initialize(&argc, argv); diff --git a/test/cpp/microbenchmarks/helpers.h b/test/cpp/microbenchmarks/helpers.h index 07be589df6..afa3e0f3ca 100644 --- a/test/cpp/microbenchmarks/helpers.h +++ b/test/cpp/microbenchmarks/helpers.h @@ -54,10 +54,10 @@ class Library { }; #ifdef GPR_LOW_LEVEL_COUNTERS -extern "C" gpr_atm gpr_mu_locks; -extern "C" gpr_atm gpr_counter_atm_cas; -extern "C" gpr_atm gpr_counter_atm_add; -extern "C" gpr_atm gpr_now_call_count; +extern gpr_atm gpr_mu_locks; +extern gpr_atm gpr_counter_atm_cas; +extern gpr_atm gpr_counter_atm_add; +extern gpr_atm gpr_now_call_count; #endif class TrackCounters { diff --git a/test/cpp/qps/BUILD b/test/cpp/qps/BUILD index 0d91d52f22..f1abb19e64 100644 --- a/test/cpp/qps/BUILD +++ b/test/cpp/qps/BUILD @@ -106,7 +106,7 @@ grpc_cc_library( "histogram.h", "stats.h", ], - deps = ["//:gpr"], + deps = ["//test/core/util:grpc_test_util"], ) grpc_cc_test( diff --git a/test/cpp/qps/histogram.h b/test/cpp/qps/histogram.h index e31d5d78a8..ba72b5b332 100644 --- a/test/cpp/qps/histogram.h +++ b/test/cpp/qps/histogram.h @@ -19,8 +19,8 @@ #ifndef TEST_QPS_HISTOGRAM_H #define TEST_QPS_HISTOGRAM_H -#include <grpc/support/histogram.h> #include "src/proto/grpc/testing/stats.pb.h" +#include "test/core/util/histogram.h" namespace grpc { namespace testing { @@ -29,36 +29,36 @@ class Histogram { public: // TODO: look into making histogram params not hardcoded for C++ Histogram() - : impl_(gpr_histogram_create(default_resolution(), - default_max_possible())) {} + : impl_(grpc_histogram_create(default_resolution(), + default_max_possible())) {} ~Histogram() { - if (impl_) gpr_histogram_destroy(impl_); + if (impl_) grpc_histogram_destroy(impl_); } Histogram(Histogram&& other) : impl_(other.impl_) { other.impl_ = nullptr; } - void Merge(const Histogram& h) { gpr_histogram_merge(impl_, h.impl_); } - void Add(double value) { gpr_histogram_add(impl_, value); } + void Merge(const Histogram& h) { grpc_histogram_merge(impl_, h.impl_); } + void Add(double value) { grpc_histogram_add(impl_, value); } double Percentile(double pctile) const { - return gpr_histogram_percentile(impl_, pctile); + return grpc_histogram_percentile(impl_, pctile); } - double Count() const { return gpr_histogram_count(impl_); } + double Count() const { return grpc_histogram_count(impl_); } void Swap(Histogram* other) { std::swap(impl_, other->impl_); } void FillProto(HistogramData* p) { size_t n; - const auto* data = gpr_histogram_get_contents(impl_, &n); + const auto* data = grpc_histogram_get_contents(impl_, &n); for (size_t i = 0; i < n; i++) { p->add_bucket(data[i]); } - p->set_min_seen(gpr_histogram_minimum(impl_)); - p->set_max_seen(gpr_histogram_maximum(impl_)); - p->set_sum(gpr_histogram_sum(impl_)); - p->set_sum_of_squares(gpr_histogram_sum_of_squares(impl_)); - p->set_count(gpr_histogram_count(impl_)); + p->set_min_seen(grpc_histogram_minimum(impl_)); + p->set_max_seen(grpc_histogram_maximum(impl_)); + p->set_sum(grpc_histogram_sum(impl_)); + p->set_sum_of_squares(grpc_histogram_sum_of_squares(impl_)); + p->set_count(grpc_histogram_count(impl_)); } void MergeProto(const HistogramData& p) { - gpr_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(), - p.min_seen(), p.max_seen(), p.sum(), - p.sum_of_squares(), p.count()); + grpc_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(), + p.min_seen(), p.max_seen(), p.sum(), + p.sum_of_squares(), p.count()); } static double default_resolution() { return 0.01; } @@ -68,7 +68,7 @@ class Histogram { Histogram(const Histogram&); Histogram& operator=(const Histogram&); - gpr_histogram* impl_; + grpc_histogram* impl_; }; } // namespace testing } // namespace grpc diff --git a/test/cpp/qps/qps_interarrival_test.cc b/test/cpp/qps/qps_interarrival_test.cc index 461bf624ce..625b7db426 100644 --- a/test/cpp/qps/qps_interarrival_test.cc +++ b/test/cpp/qps/qps_interarrival_test.cc @@ -20,7 +20,7 @@ #include <iostream> // Use the C histogram rather than C++ to avoid depending on proto -#include <grpc/support/histogram.h> +#include "test/core/util/histogram.h" #include "test/cpp/qps/interarrival.h" #include "test/cpp/util/test_config.h" @@ -31,21 +31,21 @@ using grpc::testing::RandomDistInterface; static void RunTest(RandomDistInterface&& r, int threads, std::string title) { InterarrivalTimer timer; timer.init(r, threads); - gpr_histogram* h(gpr_histogram_create(0.01, 60e9)); + grpc_histogram* h(grpc_histogram_create(0.01, 60e9)); for (int i = 0; i < 10000000; i++) { for (int j = 0; j < threads; j++) { - gpr_histogram_add(h, timer.next(j)); + grpc_histogram_add(h, timer.next(j)); } } std::cout << title << " Distribution" << std::endl; std::cout << "Value, Percentile" << std::endl; for (double pct = 0.0; pct < 100.0; pct += 1.0) { - std::cout << gpr_histogram_percentile(h, pct) << "," << pct << std::endl; + std::cout << grpc_histogram_percentile(h, pct) << "," << pct << std::endl; } - gpr_histogram_destroy(h); + grpc_histogram_destroy(h); } using grpc::testing::ExpDist; diff --git a/test/cpp/qps/qps_worker.cc b/test/cpp/qps/qps_worker.cc index c288b03ec5..4c9ab0ea43 100644 --- a/test/cpp/qps/qps_worker.cc +++ b/test/cpp/qps/qps_worker.cc @@ -32,12 +32,12 @@ #include <grpc/grpc.h> #include <grpc/support/alloc.h> #include <grpc/support/cpu.h> -#include <grpc/support/histogram.h> #include <grpc/support/host_port.h> #include <grpc/support/log.h> #include "src/proto/grpc/testing/services.pb.h" #include "test/core/util/grpc_profiler.h" +#include "test/core/util/histogram.h" #include "test/cpp/qps/client.h" #include "test/cpp/qps/server.h" #include "test/cpp/util/create_test_channel.h" |