diff options
Diffstat (limited to 'test/core/transport')
-rw-r--r-- | test/core/transport/BUILD | 7 | ||||
-rw-r--r-- | test/core/transport/bdp_estimator_test.cc | 23 | ||||
-rw-r--r-- | test/core/transport/chttp2/hpack_encoder_test.c | 99 | ||||
-rw-r--r-- | test/core/transport/metadata_test.c | 4 | ||||
-rw-r--r-- | test/core/transport/pid_controller_test.c | 78 | ||||
-rw-r--r-- | test/core/transport/pid_controller_test.cc | 91 |
6 files changed, 169 insertions, 133 deletions
diff --git a/test/core/transport/BUILD b/test/core/transport/BUILD index ea5e577bd8..edd27b8a5f 100644 --- a/test/core/transport/BUILD +++ b/test/core/transport/BUILD @@ -71,14 +71,17 @@ grpc_cc_test( grpc_cc_test( name = "pid_controller_test", - srcs = ["pid_controller_test.c"], - language = "C", + srcs = ["pid_controller_test.cc"], + language = "C++", deps = [ "//:gpr", "//:grpc", "//test/core/util:gpr_test_util", "//test/core/util:grpc_test_util", ], + external_deps = [ + "gtest", + ], ) grpc_cc_test( diff --git a/test/core/transport/bdp_estimator_test.cc b/test/core/transport/bdp_estimator_test.cc index 80cc174258..2c4fc4588b 100644 --- a/test/core/transport/bdp_estimator_test.cc +++ b/test/core/transport/bdp_estimator_test.cc @@ -51,8 +51,7 @@ TEST(BdpEstimatorTest, NoOp) { BdpEstimator est("test"); } TEST(BdpEstimatorTest, EstimateBdpNoSamples) { BdpEstimator est("test"); - int64_t estimate; - est.EstimateBdp(&estimate); + est.EstimateBdp(); } namespace { @@ -60,12 +59,10 @@ 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_TRUE(estimator->NeedPing(&exec_ctx)); estimator->SchedulePing(); estimator->StartPing(); for (size_t i = 0; i < n; i++) { estimator->AddIncomingBytes(samples[i]); - EXPECT_FALSE(estimator->NeedPing(&exec_ctx)); } gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(1, GPR_TIMESPAN))); @@ -82,16 +79,14 @@ void AddSample(BdpEstimator *estimator, int64_t sample) { TEST(BdpEstimatorTest, GetEstimate1Sample) { BdpEstimator est("test"); AddSample(&est, 100); - int64_t estimate; - est.EstimateBdp(&estimate); + est.EstimateBdp(); } TEST(BdpEstimatorTest, GetEstimate2Samples) { BdpEstimator est("test"); AddSample(&est, 100); AddSample(&est, 100); - int64_t estimate; - est.EstimateBdp(&estimate); + est.EstimateBdp(); } TEST(BdpEstimatorTest, GetEstimate3Samples) { @@ -99,17 +94,10 @@ TEST(BdpEstimatorTest, GetEstimate3Samples) { AddSample(&est, 100); AddSample(&est, 100); AddSample(&est, 100); - int64_t estimate; - est.EstimateBdp(&estimate); + est.EstimateBdp(); } namespace { -static int64_t GetEstimate(const BdpEstimator &estimator) { - int64_t out; - EXPECT_TRUE(estimator.EstimateBdp(&out)); - return out; -} - int64_t NextPow2(int64_t v) { v--; v |= v >> 1; @@ -136,7 +124,7 @@ TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues) { if (sample > max) max = sample; AddSample(&est, sample); if (i >= 3) { - EXPECT_LE(GetEstimate(est), GPR_MAX(65536, 2 * NextPow2(max))) + EXPECT_LE(est.EstimateBdp(), GPR_MAX(65536, 2 * NextPow2(max))) << " min:" << min << " max:" << max << " sample:" << sample; } } @@ -145,6 +133,7 @@ TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues) { INSTANTIATE_TEST_CASE_P(TooManyNames, BdpEstimatorRandomTest, ::testing::Values(3, 4, 6, 9, 13, 19, 28, 42, 63, 94, 141, 211, 316, 474, 711)); + } // namespace testing } // namespace grpc_core diff --git a/test/core/transport/chttp2/hpack_encoder_test.c b/test/core/transport/chttp2/hpack_encoder_test.c index ed51dd1859..a2af83b6cb 100644 --- a/test/core/transport/chttp2/hpack_encoder_test.c +++ b/test/core/transport/chttp2/hpack_encoder_test.c @@ -43,10 +43,15 @@ void **to_delete = NULL; size_t num_to_delete = 0; size_t cap_to_delete = 0; +typedef struct { + bool eof; + bool use_true_binary_metadata; + bool only_intern_key; +} verify_params; + /* verify that the output generated by encoding the stream matches the hexstring passed in */ -static void verify(grpc_exec_ctx *exec_ctx, size_t window_available, bool eof, - bool use_true_binary_metadata, size_t expect_window_used, +static void verify(grpc_exec_ctx *exec_ctx, const verify_params params, const char *expected, size_t nheaders, ...) { grpc_slice_buffer output; grpc_slice merged; @@ -66,9 +71,13 @@ static void verify(grpc_exec_ctx *exec_ctx, size_t window_available, bool eof, e[i - 1].next = &e[i]; e[i].prev = &e[i - 1]; } + grpc_slice value_slice = grpc_slice_from_static_string(value); + if (!params.only_intern_key) { + value_slice = grpc_slice_intern(value_slice); + } e[i].md = grpc_mdelem_from_slices( exec_ctx, grpc_slice_intern(grpc_slice_from_static_string(key)), - grpc_slice_intern(grpc_slice_from_static_string(value))); + value_slice); } e[0].prev = NULL; e[nheaders - 1].next = NULL; @@ -90,8 +99,8 @@ static void verify(grpc_exec_ctx *exec_ctx, size_t window_available, bool eof, memset(&stats, 0, sizeof(stats)); grpc_encode_header_options hopt = { .stream_id = 0xdeadbeef, - .is_eof = eof, - .use_true_binary_metadata = use_true_binary_metadata, + .is_eof = params.eof, + .use_true_binary_metadata = params.use_true_binary_metadata, .max_frame_size = 16384, .stats = &stats, }; @@ -119,28 +128,27 @@ static void verify(grpc_exec_ctx *exec_ctx, size_t window_available, bool eof, static void test_basic_headers(grpc_exec_ctx *exec_ctx) { int i; - verify(exec_ctx, 0, false, false, 0, "000005 0104 deadbeef 40 0161 0161", 1, - "a", "a"); - verify(exec_ctx, 0, false, false, 0, "000001 0104 deadbeef be", 1, "a", "a"); - verify(exec_ctx, 0, false, false, 0, "000001 0104 deadbeef be", 1, "a", "a"); - verify(exec_ctx, 0, false, false, 0, "000006 0104 deadbeef be 40 0162 0163", - 2, "a", "a", "b", "c"); - verify(exec_ctx, 0, false, false, 0, "000002 0104 deadbeef bf be", 2, "a", - "a", "b", "c"); - verify(exec_ctx, 0, false, false, 0, "000004 0104 deadbeef 7f 00 0164", 1, - "a", "d"); + verify_params params = { + .eof = false, .use_true_binary_metadata = false, .only_intern_key = false, + }; + verify(exec_ctx, params, "000005 0104 deadbeef 40 0161 0161", 1, "a", "a"); + verify(exec_ctx, params, "000001 0104 deadbeef be", 1, "a", "a"); + verify(exec_ctx, params, "000001 0104 deadbeef be", 1, "a", "a"); + verify(exec_ctx, params, "000006 0104 deadbeef be 40 0162 0163", 2, "a", "a", + "b", "c"); + verify(exec_ctx, params, "000002 0104 deadbeef bf be", 2, "a", "a", "b", "c"); + verify(exec_ctx, params, "000004 0104 deadbeef 7f 00 0164", 1, "a", "d"); /* flush out what's there to make a few values look very popular */ for (i = 0; i < 350; i++) { - verify(exec_ctx, 0, false, false, 0, "000003 0104 deadbeef c0 bf be", 3, - "a", "a", "b", "c", "a", "d"); + verify(exec_ctx, params, "000003 0104 deadbeef c0 bf be", 3, "a", "a", "b", + "c", "a", "d"); } - verify(exec_ctx, 0, false, false, 0, "000006 0104 deadbeef c0 00 016b 0176", - 2, "a", "a", "k", "v"); + verify(exec_ctx, params, "000006 0104 deadbeef c0 00 016b 0176", 2, "a", "a", + "k", "v"); /* this could be 000004 0104 deadbeef 0f 30 0176 also */ - verify(exec_ctx, 0, false, false, 0, "000004 0104 deadbeef 0f 2f 0176", 1, - "a", "v"); + verify(exec_ctx, params, "000004 0104 deadbeef 0f 2f 0176", 1, "a", "v"); } static void encode_int_to_str(int i, char *p) { @@ -156,6 +164,10 @@ static void test_decode_table_overflow(grpc_exec_ctx *exec_ctx) { char key[3], value[3]; char *expect; + verify_params params = { + .eof = false, .use_true_binary_metadata = false, .only_intern_key = false, + }; + for (i = 0; i < 114; i++) { encode_int_to_str(i, key); encode_int_to_str(i + 1, value); @@ -174,27 +186,28 @@ static void test_decode_table_overflow(grpc_exec_ctx *exec_ctx) { } if (i > 0) { - verify(exec_ctx, 0, false, false, 0, expect, 2, "aa", "ba", key, value); + verify(exec_ctx, params, expect, 2, "aa", "ba", key, value); } else { - verify(exec_ctx, 0, false, false, 0, expect, 1, key, value); + verify(exec_ctx, params, expect, 1, key, value); } gpr_free(expect); } /* if the above passes, then we must have just knocked this pair out of the decoder stack, and so we'll be forced to re-encode it */ - verify(exec_ctx, 0, false, false, 0, "000007 0104 deadbeef 40 026161 026261", - 1, "aa", "ba"); + verify(exec_ctx, params, "000007 0104 deadbeef 40 026161 026261", 1, "aa", + "ba"); } static void verify_table_size_change_match_elem_size(grpc_exec_ctx *exec_ctx, const char *key, - const char *value) { + const char *value, + bool use_true_binary) { grpc_slice_buffer output; grpc_mdelem elem = grpc_mdelem_from_slices( exec_ctx, grpc_slice_intern(grpc_slice_from_static_string(key)), grpc_slice_intern(grpc_slice_from_static_string(value))); - size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem); + size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, use_true_binary); size_t initial_table_size = g_compressor.table_size; grpc_linked_mdelem *e = gpr_malloc(sizeof(*e)); grpc_metadata_batch b; @@ -209,11 +222,12 @@ static void verify_table_size_change_match_elem_size(grpc_exec_ctx *exec_ctx, grpc_transport_one_way_stats stats; memset(&stats, 0, sizeof(stats)); - grpc_encode_header_options hopt = {.stream_id = 0xdeadbeef, - .is_eof = false, - .use_true_binary_metadata = false, - .max_frame_size = 16384, - .stats = &stats}; + grpc_encode_header_options hopt = { + .stream_id = 0xdeadbeef, + .is_eof = false, + .use_true_binary_metadata = use_true_binary, + .max_frame_size = 16384, + .stats = &stats}; grpc_chttp2_encode_header(exec_ctx, &g_compressor, NULL, 0, &b, &hopt, &output); grpc_slice_buffer_destroy_internal(exec_ctx, &output); @@ -224,8 +238,24 @@ static void verify_table_size_change_match_elem_size(grpc_exec_ctx *exec_ctx, } static void test_encode_header_size(grpc_exec_ctx *exec_ctx) { - verify_table_size_change_match_elem_size(exec_ctx, "hello", "world"); - verify_table_size_change_match_elem_size(exec_ctx, "hello-bin", "world"); + verify_table_size_change_match_elem_size(exec_ctx, "hello", "world", false); + verify_table_size_change_match_elem_size(exec_ctx, "hello-bin", "world", + false); + verify_table_size_change_match_elem_size(exec_ctx, "true-binary-bin", + "I_am_true_binary_value", true); +} + +static void test_interned_key_indexed(grpc_exec_ctx *exec_ctx) { + int i; + verify_params params = { + .eof = false, .use_true_binary_metadata = false, .only_intern_key = true, + }; + verify(exec_ctx, params, "000009 0104 deadbeef 40 0161 0162 0f2f 0163", 2, + "a", "b", "a", "c"); + for (i = 0; i < 10; i++) { + verify(exec_ctx, params, "000008 0104 deadbeef 0f2f 0162 0f2f 0163", 2, "a", + "b", "a", "c"); + } } static void run_test(void (*test)(grpc_exec_ctx *exec_ctx), const char *name) { @@ -245,6 +275,7 @@ int main(int argc, char **argv) { TEST(test_basic_headers); TEST(test_decode_table_overflow); TEST(test_encode_header_size); + TEST(test_interned_key_indexed); grpc_shutdown(); for (i = 0; i < num_to_delete; i++) { gpr_free(to_delete[i]); diff --git a/test/core/transport/metadata_test.c b/test/core/transport/metadata_test.c index cb06fce30b..f7124d29a7 100644 --- a/test/core/transport/metadata_test.c +++ b/test/core/transport/metadata_test.c @@ -302,7 +302,7 @@ static void verify_ascii_header_size(grpc_exec_ctx *exec_ctx, const char *key, grpc_mdelem elem = grpc_mdelem_from_slices( exec_ctx, maybe_intern(grpc_slice_from_static_string(key), intern_key), maybe_intern(grpc_slice_from_static_string(value), intern_value)); - size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem); + size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, false); size_t expected_size = 32 + strlen(key) + strlen(value); GPR_ASSERT(expected_size == elem_size); GRPC_MDELEM_UNREF(exec_ctx, elem); @@ -316,7 +316,7 @@ static void verify_binary_header_size(grpc_exec_ctx *exec_ctx, const char *key, maybe_intern(grpc_slice_from_static_buffer(value, value_len), intern_value)); GPR_ASSERT(grpc_is_binary_header(GRPC_MDKEY(elem))); - size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem); + size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, false); grpc_slice value_slice = grpc_slice_from_copied_buffer((const char *)value, value_len); grpc_slice base64_encoded = grpc_chttp2_base64_encode(value_slice); diff --git a/test/core/transport/pid_controller_test.c b/test/core/transport/pid_controller_test.c deleted file mode 100644 index 831c4b41ce..0000000000 --- a/test/core/transport/pid_controller_test.c +++ /dev/null @@ -1,78 +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/pid_controller.h" - -#include <float.h> -#include <math.h> - -#include <grpc/support/alloc.h> -#include <grpc/support/log.h> -#include <grpc/support/string_util.h> -#include <grpc/support/useful.h> -#include "src/core/lib/support/string.h" -#include "test/core/util/test_config.h" - -static void test_noop(void) { - gpr_log(GPR_INFO, "test_noop"); - grpc_pid_controller pid; - grpc_pid_controller_init( - &pid, (grpc_pid_controller_args){.gain_p = 1, - .gain_i = 1, - .gain_d = 1, - .initial_control_value = 1, - .min_control_value = DBL_MIN, - .max_control_value = DBL_MAX, - .integral_range = DBL_MAX}); -} - -static void test_simple_convergence(double gain_p, double gain_i, double gain_d, - double dt, double set_point, double start) { - gpr_log(GPR_INFO, - "test_simple_convergence(p=%lf, i=%lf, d=%lf); dt=%lf set_point=%lf " - "start=%lf", - gain_p, gain_i, gain_d, dt, set_point, start); - grpc_pid_controller pid; - grpc_pid_controller_init( - &pid, (grpc_pid_controller_args){.gain_p = gain_p, - .gain_i = gain_i, - .gain_d = gain_d, - .initial_control_value = start, - .min_control_value = DBL_MIN, - .max_control_value = DBL_MAX, - .integral_range = DBL_MAX}); - - for (int i = 0; i < 100000; i++) { - grpc_pid_controller_update(&pid, set_point - grpc_pid_controller_last(&pid), - 1); - } - - GPR_ASSERT(fabs(set_point - grpc_pid_controller_last(&pid)) < 0.1); - if (gain_i > 0) { - GPR_ASSERT(fabs(pid.error_integral) < 0.1); - } -} - -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - test_noop(); - test_simple_convergence(0.2, 0, 0, 1, 100, 0); - test_simple_convergence(0.2, 0.1, 0, 1, 100, 0); - test_simple_convergence(0.2, 0.1, 0.1, 1, 100, 0); - return 0; -} diff --git a/test/core/transport/pid_controller_test.cc b/test/core/transport/pid_controller_test.cc new file mode 100644 index 0000000000..081d03472a --- /dev/null +++ b/test/core/transport/pid_controller_test.cc @@ -0,0 +1,91 @@ +/* + * + * 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/pid_controller.h" + +#include <float.h> +#include <math.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> +#include <grpc/support/string_util.h> +#include <grpc/support/useful.h> +#include <gtest/gtest.h> +#include "src/core/lib/support/string.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { + +TEST(PidController, NoOp) { + PidController pid(PidController::Args() + .set_gain_p(1) + .set_gain_i(1) + .set_gain_d(1) + .set_initial_control_value(1)); +} + +struct SimpleConvergenceTestArgs { + double gain_p; + double gain_i; + double gain_d; + double dt; + double set_point; + double start; +}; + +std::ostream& operator<<(std::ostream& out, SimpleConvergenceTestArgs args) { + return out << "gain_p:" << args.gain_p << " gain_i:" << args.gain_i + << " gain_d:" << args.gain_d << " dt:" << args.dt + << " set_point:" << args.set_point << " start:" << args.start; +} + +class SimpleConvergenceTest + : public ::testing::TestWithParam<SimpleConvergenceTestArgs> {}; + +TEST_P(SimpleConvergenceTest, Converges) { + PidController pid(PidController::Args() + .set_gain_p(GetParam().gain_p) + .set_gain_i(GetParam().gain_i) + .set_gain_d(GetParam().gain_d) + .set_initial_control_value(GetParam().start)); + + for (int i = 0; i < 100000; i++) { + pid.Update(GetParam().set_point - pid.last_control_value(), GetParam().dt); + } + + EXPECT_LT(fabs(GetParam().set_point - pid.last_control_value()), 0.1); + if (GetParam().gain_i > 0) { + EXPECT_LT(fabs(pid.error_integral()), 0.1); + } +} + +INSTANTIATE_TEST_CASE_P( + X, SimpleConvergenceTest, + ::testing::Values(SimpleConvergenceTestArgs{0.2, 0, 0, 1, 100, 0}, + SimpleConvergenceTestArgs{0.2, 0.1, 0, 1, 100, 0}, + SimpleConvergenceTestArgs{0.2, 0.1, 0.1, 1, 100, 0})); + +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} |