From 922260656a288d302016d044ff1572be5dc61b8c Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Sun, 8 Oct 2017 21:16:12 -0700 Subject: C++ize BDP estimator, introduce ManualConstructor --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 139d1bd46c..80133083a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1616,7 +1616,7 @@ add_library(grpc_test_util test/core/end2end/fixtures/http_proxy_fixture.c test/core/end2end/fixtures/proxy.c test/core/iomgr/endpoint_tests.c - test/core/util/debugger_macros.c + test/core/util/debugger_macros.cc test/core/util/grpc_profiler.c test/core/util/memory_counters.c test/core/util/mock_endpoint.c @@ -1880,7 +1880,7 @@ add_library(grpc_test_util_unsecure test/core/end2end/fixtures/http_proxy_fixture.c test/core/end2end/fixtures/proxy.c test/core/iomgr/endpoint_tests.c - test/core/util/debugger_macros.c + test/core/util/debugger_macros.cc test/core/util/grpc_profiler.c test/core/util/memory_counters.c test/core/util/mock_endpoint.c -- cgit v1.2.3 From 50ca35e3e65b83e91da622a0d8d734e179bc9e6a Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 9 Oct 2017 08:11:48 -0700 Subject: Fix test --- CMakeLists.txt | 71 +++++---- Makefile | 84 ++++++----- build.yaml | 22 +-- test/core/transport/BUILD | 4 +- test/core/transport/bdp_estimator_test.c | 162 --------------------- test/core/transport/bdp_estimator_test.cc | 159 ++++++++++++++++++++ tools/run_tests/generated/sources_and_headers.json | 36 ++--- tools/run_tests/generated/tests.json | 44 +++--- 8 files changed, 303 insertions(+), 279 deletions(-) delete mode 100644 test/core/transport/bdp_estimator_test.c create mode 100644 test/core/transport/bdp_estimator_test.cc (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 80133083a1..2b2774b796 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -381,7 +381,6 @@ add_dependencies(buildtests_c alpn_test) add_dependencies(buildtests_c arena_test) add_dependencies(buildtests_c backoff_test) add_dependencies(buildtests_c bad_server_response_test) -add_dependencies(buildtests_c bdp_estimator_test) add_dependencies(buildtests_c bin_decoder_test) add_dependencies(buildtests_c bin_encoder_test) add_dependencies(buildtests_c byte_stream_test) @@ -637,6 +636,7 @@ add_custom_target(buildtests_cxx) add_dependencies(buildtests_cxx alarm_cpp_test) add_dependencies(buildtests_cxx async_end2end_test) add_dependencies(buildtests_cxx auth_property_iterator_test) +add_dependencies(buildtests_cxx bdp_estimator_test) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX) add_dependencies(buildtests_cxx bm_arena) endif() @@ -5259,35 +5259,6 @@ target_link_libraries(bad_server_response_test endif (gRPC_BUILD_TESTS) if (gRPC_BUILD_TESTS) -add_executable(bdp_estimator_test - test/core/transport/bdp_estimator_test.c -) - - -target_include_directories(bdp_estimator_test - PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} - PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include - PRIVATE ${BORINGSSL_ROOT_DIR}/include - PRIVATE ${PROTOBUF_ROOT_DIR}/src - PRIVATE ${BENCHMARK_ROOT_DIR}/include - PRIVATE ${ZLIB_ROOT_DIR} - PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib - PRIVATE ${CARES_INCLUDE_DIR} - PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/cares/cares - PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include -) - -target_link_libraries(bdp_estimator_test - ${_gRPC_ALLTARGETS_LIBRARIES} - grpc_test_util - grpc - gpr_test_util - gpr -) - -endif (gRPC_BUILD_TESTS) -if (gRPC_BUILD_TESTS) - add_executable(bin_decoder_test test/core/transport/chttp2/bin_decoder_test.c ) @@ -9275,6 +9246,46 @@ target_link_libraries(auth_property_iterator_test ${_gRPC_GFLAGS_LIBRARIES} ) +endif (gRPC_BUILD_TESTS) +if (gRPC_BUILD_TESTS) + +add_executable(bdp_estimator_test + test/core/transport/bdp_estimator_test.cc + third_party/googletest/googletest/src/gtest-all.cc + third_party/googletest/googlemock/src/gmock-all.cc +) + + +target_include_directories(bdp_estimator_test + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include + PRIVATE ${BORINGSSL_ROOT_DIR}/include + PRIVATE ${PROTOBUF_ROOT_DIR}/src + PRIVATE ${BENCHMARK_ROOT_DIR}/include + PRIVATE ${ZLIB_ROOT_DIR} + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib + PRIVATE ${CARES_INCLUDE_DIR} + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/cares/cares + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include + PRIVATE third_party/googletest/googletest/include + PRIVATE third_party/googletest/googletest + PRIVATE third_party/googletest/googlemock/include + PRIVATE third_party/googletest/googlemock + PRIVATE ${_gRPC_PROTO_GENS_DIR} +) + +target_link_libraries(bdp_estimator_test + ${_gRPC_PROTOBUF_LIBRARIES} + ${_gRPC_ALLTARGETS_LIBRARIES} + grpc++_test_util + grpc++ + grpc_test_util + grpc + gpr_test_util + gpr + ${_gRPC_GFLAGS_LIBRARIES} +) + endif (gRPC_BUILD_TESTS) if (gRPC_BUILD_TESTS) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX) diff --git a/Makefile b/Makefile index 4091aa3c35..d4500bbbbd 100644 --- a/Makefile +++ b/Makefile @@ -952,7 +952,6 @@ api_fuzzer: $(BINDIR)/$(CONFIG)/api_fuzzer arena_test: $(BINDIR)/$(CONFIG)/arena_test backoff_test: $(BINDIR)/$(CONFIG)/backoff_test bad_server_response_test: $(BINDIR)/$(CONFIG)/bad_server_response_test -bdp_estimator_test: $(BINDIR)/$(CONFIG)/bdp_estimator_test bin_decoder_test: $(BINDIR)/$(CONFIG)/bin_decoder_test bin_encoder_test: $(BINDIR)/$(CONFIG)/bin_encoder_test byte_stream_test: $(BINDIR)/$(CONFIG)/byte_stream_test @@ -1101,6 +1100,7 @@ wakeup_fd_cv_test: $(BINDIR)/$(CONFIG)/wakeup_fd_cv_test alarm_cpp_test: $(BINDIR)/$(CONFIG)/alarm_cpp_test async_end2end_test: $(BINDIR)/$(CONFIG)/async_end2end_test auth_property_iterator_test: $(BINDIR)/$(CONFIG)/auth_property_iterator_test +bdp_estimator_test: $(BINDIR)/$(CONFIG)/bdp_estimator_test bm_arena: $(BINDIR)/$(CONFIG)/bm_arena bm_call_create: $(BINDIR)/$(CONFIG)/bm_call_create bm_chttp2_hpack: $(BINDIR)/$(CONFIG)/bm_chttp2_hpack @@ -1352,7 +1352,6 @@ buildtests_c: privatelibs_c \ $(BINDIR)/$(CONFIG)/arena_test \ $(BINDIR)/$(CONFIG)/backoff_test \ $(BINDIR)/$(CONFIG)/bad_server_response_test \ - $(BINDIR)/$(CONFIG)/bdp_estimator_test \ $(BINDIR)/$(CONFIG)/bin_decoder_test \ $(BINDIR)/$(CONFIG)/bin_encoder_test \ $(BINDIR)/$(CONFIG)/byte_stream_test \ @@ -1546,6 +1545,7 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/alarm_cpp_test \ $(BINDIR)/$(CONFIG)/async_end2end_test \ $(BINDIR)/$(CONFIG)/auth_property_iterator_test \ + $(BINDIR)/$(CONFIG)/bdp_estimator_test \ $(BINDIR)/$(CONFIG)/bm_arena \ $(BINDIR)/$(CONFIG)/bm_call_create \ $(BINDIR)/$(CONFIG)/bm_chttp2_hpack \ @@ -1666,6 +1666,7 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/alarm_cpp_test \ $(BINDIR)/$(CONFIG)/async_end2end_test \ $(BINDIR)/$(CONFIG)/auth_property_iterator_test \ + $(BINDIR)/$(CONFIG)/bdp_estimator_test \ $(BINDIR)/$(CONFIG)/bm_arena \ $(BINDIR)/$(CONFIG)/bm_call_create \ $(BINDIR)/$(CONFIG)/bm_chttp2_hpack \ @@ -1765,8 +1766,6 @@ test_c: buildtests_c $(Q) $(BINDIR)/$(CONFIG)/backoff_test || ( echo test backoff_test failed ; exit 1 ) $(E) "[RUN] Testing bad_server_response_test" $(Q) $(BINDIR)/$(CONFIG)/bad_server_response_test || ( echo test bad_server_response_test failed ; exit 1 ) - $(E) "[RUN] Testing bdp_estimator_test" - $(Q) $(BINDIR)/$(CONFIG)/bdp_estimator_test || ( echo test bdp_estimator_test failed ; exit 1 ) $(E) "[RUN] Testing bin_decoder_test" $(Q) $(BINDIR)/$(CONFIG)/bin_decoder_test || ( echo test bin_decoder_test failed ; exit 1 ) $(E) "[RUN] Testing bin_encoder_test" @@ -2039,6 +2038,8 @@ test_cxx: buildtests_cxx $(Q) $(BINDIR)/$(CONFIG)/async_end2end_test || ( echo test async_end2end_test failed ; exit 1 ) $(E) "[RUN] Testing auth_property_iterator_test" $(Q) $(BINDIR)/$(CONFIG)/auth_property_iterator_test || ( echo test auth_property_iterator_test failed ; exit 1 ) + $(E) "[RUN] Testing bdp_estimator_test" + $(Q) $(BINDIR)/$(CONFIG)/bdp_estimator_test || ( echo test bdp_estimator_test failed ; exit 1 ) $(E) "[RUN] Testing bm_arena" $(Q) $(BINDIR)/$(CONFIG)/bm_arena || ( echo test bm_arena failed ; exit 1 ) $(E) "[RUN] Testing bm_call_create" @@ -8961,38 +8962,6 @@ endif endif -BDP_ESTIMATOR_TEST_SRC = \ - test/core/transport/bdp_estimator_test.c \ - -BDP_ESTIMATOR_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(BDP_ESTIMATOR_TEST_SRC)))) -ifeq ($(NO_SECURE),true) - -# You can't build secure targets if you don't have OpenSSL. - -$(BINDIR)/$(CONFIG)/bdp_estimator_test: openssl_dep_error - -else - - - -$(BINDIR)/$(CONFIG)/bdp_estimator_test: $(BDP_ESTIMATOR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a - $(E) "[LD] Linking $@" - $(Q) mkdir -p `dirname $@` - $(Q) $(LD) $(LDFLAGS) $(BDP_ESTIMATOR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/bdp_estimator_test - -endif - -$(OBJDIR)/$(CONFIG)/test/core/transport/bdp_estimator_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a - -deps_bdp_estimator_test: $(BDP_ESTIMATOR_TEST_OBJS:.o=.dep) - -ifneq ($(NO_SECURE),true) -ifneq ($(NO_DEPS),true) --include $(BDP_ESTIMATOR_TEST_OBJS:.o=.dep) -endif -endif - - BIN_DECODER_TEST_SRC = \ test/core/transport/chttp2/bin_decoder_test.c \ @@ -13768,6 +13737,49 @@ endif endif +BDP_ESTIMATOR_TEST_SRC = \ + test/core/transport/bdp_estimator_test.cc \ + +BDP_ESTIMATOR_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(BDP_ESTIMATOR_TEST_SRC)))) +ifeq ($(NO_SECURE),true) + +# You can't build secure targets if you don't have OpenSSL. + +$(BINDIR)/$(CONFIG)/bdp_estimator_test: openssl_dep_error + +else + + + + +ifeq ($(NO_PROTOBUF),true) + +# You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+. + +$(BINDIR)/$(CONFIG)/bdp_estimator_test: protobuf_dep_error + +else + +$(BINDIR)/$(CONFIG)/bdp_estimator_test: $(PROTOBUF_DEP) $(BDP_ESTIMATOR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + $(E) "[LD] Linking $@" + $(Q) mkdir -p `dirname $@` + $(Q) $(LDXX) $(LDFLAGS) $(BDP_ESTIMATOR_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/bdp_estimator_test + +endif + +endif + +$(OBJDIR)/$(CONFIG)/test/core/transport/bdp_estimator_test.o: $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + +deps_bdp_estimator_test: $(BDP_ESTIMATOR_TEST_OBJS:.o=.dep) + +ifneq ($(NO_SECURE),true) +ifneq ($(NO_DEPS),true) +-include $(BDP_ESTIMATOR_TEST_OBJS:.o=.dep) +endif +endif + + BM_ARENA_SRC = \ test/cpp/microbenchmarks/bm_arena.cc \ diff --git a/build.yaml b/build.yaml index 7a370bf83c..4eaaf6a035 100644 --- a/build.yaml +++ b/build.yaml @@ -1799,16 +1799,6 @@ targets: - gpr exclude_iomgrs: - uv -- name: bdp_estimator_test - build: test - language: c - src: - - test/core/transport/bdp_estimator_test.c - deps: - - grpc_test_util - - grpc - - gpr_test_util - - gpr - name: bin_decoder_test build: test language: c @@ -3460,6 +3450,18 @@ targets: - grpc - gpr_test_util - gpr +- name: bdp_estimator_test + build: test + language: c++ + src: + - test/core/transport/bdp_estimator_test.cc + deps: + - grpc++_test_util + - grpc++ + - grpc_test_util + - grpc + - gpr_test_util + - gpr - name: bm_arena build: test language: c++ diff --git a/test/core/transport/BUILD b/test/core/transport/BUILD index 12e36132c8..9c4be8579f 100644 --- a/test/core/transport/BUILD +++ b/test/core/transport/BUILD @@ -20,8 +20,8 @@ grpc_package(name = "test/core/transport") grpc_cc_test( name = "bdp_estimator_test", - srcs = ["bdp_estimator_test.c"], - language = "C", + srcs = ["bdp_estimator_test.cc"], + language = "C++", deps = [ "//:gpr", "//:grpc", diff --git a/test/core/transport/bdp_estimator_test.c b/test/core/transport/bdp_estimator_test.c deleted file mode 100644 index 4912ad5887..0000000000 --- a/test/core/transport/bdp_estimator_test.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * - * Copyright 2016 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/transport/bdp_estimator.h" - -#include -#include -#include -#include -#include -#include -#include "src/core/lib/iomgr/timer_manager.h" -#include "src/core/lib/support/string.h" -#include "test/core/util/test_config.h" - -extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); - -static int g_clock = 0; - -static gpr_timespec fake_gpr_now(gpr_clock_type clock_type) { - return (gpr_timespec){ - .tv_sec = g_clock, .tv_nsec = 0, .clock_type = clock_type, - }; -} - -static void inc_time(void) { g_clock += 30; } - -static void test_noop(void) { - gpr_log(GPR_INFO, "test_noop"); - grpc_bdp_estimator est; - grpc_bdp_estimator_init(&est, "test"); -} - -static void test_get_estimate_no_samples(void) { - gpr_log(GPR_INFO, "test_get_estimate_no_samples"); - grpc_bdp_estimator est; - grpc_bdp_estimator_init(&est, "test"); - int64_t estimate; - grpc_bdp_estimator_get_estimate(&est, &estimate); -} - -static void add_samples(grpc_bdp_estimator *estimator, int64_t *samples, - size_t n) { - grpc_bdp_estimator_add_incoming_bytes(estimator, 1234567); - inc_time(); - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - GPR_ASSERT(grpc_bdp_estimator_need_ping(&exec_ctx, estimator) == true); - grpc_bdp_estimator_schedule_ping(estimator); - grpc_bdp_estimator_start_ping(estimator); - for (size_t i = 0; i < n; i++) { - grpc_bdp_estimator_add_incoming_bytes(estimator, samples[i]); - GPR_ASSERT(grpc_bdp_estimator_need_ping(&exec_ctx, estimator) == false); - } - gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_millis(1, GPR_TIMESPAN))); - grpc_bdp_estimator_complete_ping(&exec_ctx, estimator); - grpc_exec_ctx_finish(&exec_ctx); -} - -static void add_sample(grpc_bdp_estimator *estimator, int64_t sample) { - add_samples(estimator, &sample, 1); -} - -static void test_get_estimate_1_sample(void) { - gpr_log(GPR_INFO, "test_get_estimate_1_sample"); - grpc_bdp_estimator est; - grpc_bdp_estimator_init(&est, "test"); - add_sample(&est, 100); - int64_t estimate; - grpc_bdp_estimator_get_estimate(&est, &estimate); -} - -static void test_get_estimate_2_samples(void) { - gpr_log(GPR_INFO, "test_get_estimate_2_samples"); - grpc_bdp_estimator est; - grpc_bdp_estimator_init(&est, "test"); - add_sample(&est, 100); - add_sample(&est, 100); - int64_t estimate; - grpc_bdp_estimator_get_estimate(&est, &estimate); -} - -static int64_t get_estimate(grpc_bdp_estimator *estimator) { - int64_t out; - GPR_ASSERT(grpc_bdp_estimator_get_estimate(estimator, &out)); - return out; -} - -static void test_get_estimate_3_samples(void) { - gpr_log(GPR_INFO, "test_get_estimate_3_samples"); - grpc_bdp_estimator est; - grpc_bdp_estimator_init(&est, "test"); - add_sample(&est, 100); - add_sample(&est, 100); - add_sample(&est, 100); - int64_t estimate; - grpc_bdp_estimator_get_estimate(&est, &estimate); -} - -static int64_t next_pow_2(int64_t v) { - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v |= v >> 32; - v++; - return v; -} - -static void test_get_estimate_random_values(size_t n) { - gpr_log(GPR_INFO, "test_get_estimate_random_values(%" PRIdPTR ")", n); - grpc_bdp_estimator est; - grpc_bdp_estimator_init(&est, "test"); - const int kMaxSample = 65535; - int min = kMaxSample; - int max = 0; - for (size_t i = 0; i < n; i++) { - int sample = rand() % (kMaxSample + 1); - if (sample < min) min = sample; - if (sample > max) max = sample; - add_sample(&est, sample); - if (i >= 3) { - gpr_log(GPR_DEBUG, "est:%" PRId64 " min:%d max:%d", get_estimate(&est), - min, max); - GPR_ASSERT(get_estimate(&est) <= GPR_MAX(65536, 2 * next_pow_2(max))); - } - } -} - -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - gpr_now_impl = fake_gpr_now; - grpc_init(); - grpc_timer_manager_set_threading(false); - test_noop(); - test_get_estimate_no_samples(); - test_get_estimate_1_sample(); - test_get_estimate_2_samples(); - test_get_estimate_3_samples(); - for (size_t i = 3; i < 1000; i = i * 3 / 2) { - test_get_estimate_random_values(i); - } - grpc_shutdown(); - return 0; -} diff --git a/test/core/transport/bdp_estimator_test.cc b/test/core/transport/bdp_estimator_test.cc new file mode 100644 index 0000000000..988020f9d5 --- /dev/null +++ b/test/core/transport/bdp_estimator_test.cc @@ -0,0 +1,159 @@ +/* + * + * Copyright 2016 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/transport/bdp_estimator.h" + +#include +#include +#include +#include +#include +#include +#include +#include "src/core/lib/iomgr/timer_manager.h" +#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); + +namespace grpc_core { +namespace testing { +namespace { +int g_clock = 0; + +gpr_timespec fake_gpr_now(gpr_clock_type clock_type) { + return (gpr_timespec){ + .tv_sec = g_clock, .tv_nsec = 0, .clock_type = clock_type, + }; +} + +void inc_time(void) { g_clock += 30; } +} // namespace + +TEST(BdpEstimatorTest, NoOp) { BdpEstimator est("test"); } + +TEST(BdpEstimatorTest, EstimateBdpNoSamples) { + BdpEstimator est("test"); + int64_t estimate; + est.EstimateBdp(&est, &estimate); +} + +namespace { +void AddSamples(BdpEstimator *estimator, int64_t *samples, size_t n) { + estimator->AddIncomingBytes(1234567); + inc_time(); + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + EXPECT_EQ(true, estimator->NeedPing(&exec_ctx)); + estimator->SchedulePing(); + estimator->StartPing(); + for (size_t i = 0; i < n; i++) { + estimator->AddIncomingBytes(samples[i]); + EXPECT_EQ(false, estimator->NeedPing(&exec_ctx)); + } + gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_millis(1, GPR_TIMESPAN))); + grpc_exec_ctx_invalidate_now(&exec_ctx); + estimator->CompletePing(&exec_ctx); + grpc_exec_ctx_finish(&exec_ctx); +} + +void AddSample(BdpEstimator *estimator, int64_t sample) { + add_samples(estimator, &sample, 1); +} +} // namespace + +TEST(BdpEstimatorTest, GetEstimate1Sample) { + BdpEstimator est("test"); + AddSample(&est, 100); + int64_t estimate; + est->EstimateBdp(&estimate); +} + +TEST(BdpEstimatorTest, GetEstimate2Samples) { + BdpEstimator est("test"); + AddSample(&est, 100); + AddSample(&est, 100); + int64_t estimate; + est->EstimateBdp(&estimate); +} + +TEST(BdpEstimatorTest, GetEstimate3Samples) { + BdpEstimator est("test"); + AddSample(&est, 100); + AddSample(&est, 100); + AddSample(&est, 100); + int64_t estimate; + est->EstimateBdp(&estimate); +} + +namespace { +static int64_t get_estimate(grpc_bdp_estimator *estimator) { + int64_t out; + EXPECT_EQ(true, estimator->EstimateBdp(estimator, &out)); + return out; +} + +int64_t NextPow2(int64_t v) { + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v |= v >> 32; + v++; + return v; +} +} // namespace + +class BdpEstimatorRandomTest : public ::testing::TestWithParam {}; + +TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues) { + grpc_bdp_estimator est; + grpc_bdp_estimator_init(&est, "test"); + const int kMaxSample = 65535; + int min = kMaxSample; + int max = 0; + for (size_t i = 0; i < GetParam(); i++) { + int sample = rand() % (kMaxSample + 1); + if (sample < min) min = sample; + if (sample > max) max = sample; + add_sample(&est, sample); + if (i >= 3) { + gpr_log(GPR_DEBUG, "est:%" PRId64 " min:%d max:%d", GetEstimate(&est), + min, max); + EXPECT_LE(get_estimate(&est), GPR_MAX(65536, 2 * NextPow2(max))); + } + } +} + +INSTANTIATE_TEST_CASE_P(TooManyNames, BdpEstimatorRandomTest, + Range(3, 1000, 7)); +} // namespace testing +} // namespace grpc_core + +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + gpr_now_impl = fake_gpr_now; + grpc_init(); + grpc_timer_manager_set_threading(false); + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + grpc_shutdown(); + return ret; +} diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index f6c9c5a224..62c0de3e26 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -134,23 +134,6 @@ "third_party": false, "type": "target" }, - { - "deps": [ - "gpr", - "gpr_test_util", - "grpc", - "grpc_test_util" - ], - "headers": [], - "is_filegroup": false, - "language": "c", - "name": "bdp_estimator_test", - "src": [ - "test/core/transport/bdp_estimator_test.c" - ], - "third_party": false, - "type": "target" - }, { "deps": [ "grpc", @@ -2613,6 +2596,25 @@ "third_party": false, "type": "target" }, + { + "deps": [ + "gpr", + "gpr_test_util", + "grpc", + "grpc++", + "grpc++_test_util", + "grpc_test_util" + ], + "headers": [], + "is_filegroup": false, + "language": "c++", + "name": "bdp_estimator_test", + "src": [ + "test/core/transport/bdp_estimator_test.cc" + ], + "third_party": false, + "type": "target" + }, { "deps": [ "benchmark", diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index 83418423a2..d246f6dc11 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -157,28 +157,6 @@ "windows" ] }, - { - "args": [], - "ci_platforms": [ - "linux", - "mac", - "posix", - "windows" - ], - "cpu_cost": 1.0, - "exclude_configs": [], - "exclude_iomgrs": [], - "flaky": false, - "gtest": false, - "language": "c", - "name": "bdp_estimator_test", - "platforms": [ - "linux", - "mac", - "posix", - "windows" - ] - }, { "args": [], "ci_platforms": [ @@ -2825,6 +2803,28 @@ "windows" ] }, + { + "args": [], + "ci_platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "cpu_cost": 1.0, + "exclude_configs": [], + "exclude_iomgrs": [], + "flaky": false, + "gtest": false, + "language": "c++", + "name": "bdp_estimator_test", + "platforms": [ + "linux", + "mac", + "posix", + "windows" + ] + }, { "args": [ "--benchmark_min_time=0" -- cgit v1.2.3