aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/backoff
diff options
context:
space:
mode:
authorGravatar Yash Tibrewal <yashkt@google.com>2017-10-13 16:07:13 -0700
committerGravatar Yash Tibrewal <yashkt@google.com>2017-10-18 17:12:19 -0700
commit0ee7574732a06e8cace4e099a678f4bd5dbff679 (patch)
treee43d5de442fdcc3d39cd5af687f319fa39612d3f /test/core/backoff
parent6bf5f833efe2cb9e2ecc14358dd9699cd5d05263 (diff)
Removing instances of exec_ctx being passed around in functions in
src/core. exec_ctx is now a thread_local pointer of type ExecCtx instead of grpc_exec_ctx which is initialized whenever ExecCtx is instantiated. ExecCtx also keeps track of the previous exec_ctx so that nesting of exec_ctx is allowed. This means that there is only one exec_ctx being used at any time. Also, grpc_exec_ctx_finish is called in the destructor of the object, and the previous exec_ctx is restored to avoid breaking current functionality. The code still explicitly calls grpc_exec_ctx_finish because removing all such instances causes the code to break.
Diffstat (limited to 'test/core/backoff')
-rw-r--r--test/core/backoff/backoff_test.c60
-rw-r--r--test/core/backoff/backoff_test.cc147
2 files changed, 177 insertions, 30 deletions
diff --git a/test/core/backoff/backoff_test.c b/test/core/backoff/backoff_test.c
index a29cce6cc7..93952f3d10 100644
--- a/test/core/backoff/backoff_test.c
+++ b/test/core/backoff/backoff_test.c
@@ -28,15 +28,15 @@ static void test_constant_backoff(void) {
0.0 /* jitter */, 100 /* min timeout */,
1000 /* max timeout */);
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_millis next = grpc_backoff_begin(&exec_ctx, &backoff);
- GPR_ASSERT(next - grpc_exec_ctx_now(&exec_ctx) == 200);
+ exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_millis next = grpc_backoff_begin(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 200);
for (int i = 0; i < 10000; i++) {
- next = grpc_backoff_step(&exec_ctx, &backoff);
- GPR_ASSERT(next - grpc_exec_ctx_now(&exec_ctx) == 200);
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 200);
exec_ctx.now = next;
}
- grpc_exec_ctx_finish(&exec_ctx);
+ grpc_exec_ctx_finish();
}
static void test_min_connect(void) {
@@ -45,10 +45,10 @@ static void test_min_connect(void) {
0.0 /* jitter */, 200 /* min timeout */,
1000 /* max timeout */);
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_millis next = grpc_backoff_begin(&exec_ctx, &backoff);
- GPR_ASSERT(next - grpc_exec_ctx_now(&exec_ctx) == 200);
- grpc_exec_ctx_finish(&exec_ctx);
+ exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_millis next = grpc_backoff_begin(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 200);
+ grpc_exec_ctx_finish();
}
static void test_no_jitter_backoff(void) {
@@ -58,47 +58,47 @@ static void test_no_jitter_backoff(void) {
513 /* max timeout */);
// x_1 = 2
// x_n = 2**i + x_{i-1} ( = 2**(n+1) - 2 )
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ exec_ctx = GRPC_EXEC_CTX_INIT;
exec_ctx.now = 0;
exec_ctx.now_is_valid = true;
- grpc_millis next = grpc_backoff_begin(&exec_ctx, &backoff);
+ grpc_millis next = grpc_backoff_begin(&backoff);
GPR_ASSERT(next == 2);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 6);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 14);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 30);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 62);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 126);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 254);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 510);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 1022);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
// Hit the maximum timeout. From this point onwards, retries will increase
// only by max timeout.
GPR_ASSERT(next == 1535);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 2048);
exec_ctx.now = next;
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
GPR_ASSERT(next == 2561);
- grpc_exec_ctx_finish(&exec_ctx);
+ grpc_exec_ctx_finish();
}
static void test_jitter_backoff(void) {
@@ -111,9 +111,9 @@ static void test_jitter_backoff(void) {
backoff.rng_state = 0; // force consistent PRNG
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_millis next = grpc_backoff_begin(&exec_ctx, &backoff);
- GPR_ASSERT(next - grpc_exec_ctx_now(&exec_ctx) == 500);
+ exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_millis next = grpc_backoff_begin(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 500);
int64_t expected_next_lower_bound =
(int64_t)((double)initial_timeout * (1 - jitter));
@@ -121,10 +121,10 @@ static void test_jitter_backoff(void) {
(int64_t)((double)initial_timeout * (1 + jitter));
for (int i = 0; i < 10000; i++) {
- next = grpc_backoff_step(&exec_ctx, &backoff);
+ next = grpc_backoff_step(&backoff);
// next-now must be within (jitter*100)% of the previous timeout.
- const int64_t timeout_millis = next - grpc_exec_ctx_now(&exec_ctx);
+ const int64_t timeout_millis = next - grpc_exec_ctx_now();
GPR_ASSERT(timeout_millis >= expected_next_lower_bound);
GPR_ASSERT(timeout_millis <= expected_next_upper_bound);
@@ -134,7 +134,7 @@ static void test_jitter_backoff(void) {
(int64_t)((double)timeout_millis * (1 + jitter));
exec_ctx.now = next;
}
- grpc_exec_ctx_finish(&exec_ctx);
+ grpc_exec_ctx_finish();
}
int main(int argc, char **argv) {
diff --git a/test/core/backoff/backoff_test.cc b/test/core/backoff/backoff_test.cc
new file mode 100644
index 0000000000..ea27f7547f
--- /dev/null
+++ b/test/core/backoff/backoff_test.cc
@@ -0,0 +1,147 @@
+/*
+ *
+ * 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/backoff/backoff.h"
+
+#include <grpc/support/log.h>
+
+#include "test/core/util/test_config.h"
+
+static void test_constant_backoff(void) {
+ grpc_backoff backoff;
+ grpc_backoff_init(&backoff, 200 /* initial timeout */, 1.0 /* multiplier */,
+ 0.0 /* jitter */, 100 /* min timeout */,
+ 1000 /* max timeout */);
+
+ grpc_millis next = grpc_backoff_begin(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 200);
+ for (int i = 0; i < 10000; i++) {
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 200);
+ exec_ctx.now = next;
+ }
+ grpc_exec_ctx_finish();
+}
+
+static void test_min_connect(void) {
+ grpc_backoff backoff;
+ grpc_backoff_init(&backoff, 100 /* initial timeout */, 1.0 /* multiplier */,
+ 0.0 /* jitter */, 200 /* min timeout */,
+ 1000 /* max timeout */);
+
+ grpc_millis next = grpc_backoff_begin(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 200);
+ grpc_exec_ctx_finish();
+}
+
+static void test_no_jitter_backoff(void) {
+ grpc_backoff backoff;
+ grpc_backoff_init(&backoff, 2 /* initial timeout */, 2.0 /* multiplier */,
+ 0.0 /* jitter */, 1 /* min timeout */,
+ 513 /* max timeout */);
+ // x_1 = 2
+ // x_n = 2**i + x_{i-1} ( = 2**(n+1) - 2 )
+
+ exec_ctx.now = 0;
+ exec_ctx.now_is_valid = true;
+ grpc_millis next = grpc_backoff_begin(&backoff);
+ GPR_ASSERT(next == 2);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 6);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 14);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 30);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 62);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 126);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 254);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 510);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 1022);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ // Hit the maximum timeout. From this point onwards, retries will increase
+ // only by max timeout.
+ GPR_ASSERT(next == 1535);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 2048);
+ exec_ctx.now = next;
+ next = grpc_backoff_step(&backoff);
+ GPR_ASSERT(next == 2561);
+ grpc_exec_ctx_finish();
+}
+
+static void test_jitter_backoff(void) {
+ const int64_t initial_timeout = 500;
+ const double jitter = 0.1;
+ grpc_backoff backoff;
+ grpc_backoff_init(&backoff, (grpc_millis)initial_timeout,
+ 1.0 /* multiplier */, jitter, 100 /* min timeout */,
+ 1000 /* max timeout */);
+
+ backoff.rng_state = 0; // force consistent PRNG
+
+ grpc_millis next = grpc_backoff_begin(&backoff);
+ GPR_ASSERT(next - grpc_exec_ctx_now() == 500);
+
+ int64_t expected_next_lower_bound =
+ (int64_t)((double)initial_timeout * (1 - jitter));
+ int64_t expected_next_upper_bound =
+ (int64_t)((double)initial_timeout * (1 + jitter));
+
+ for (int i = 0; i < 10000; i++) {
+ next = grpc_backoff_step(&backoff);
+
+ // next-now must be within (jitter*100)% of the previous timeout.
+ const int64_t timeout_millis = next - grpc_exec_ctx_now();
+ GPR_ASSERT(timeout_millis >= expected_next_lower_bound);
+ GPR_ASSERT(timeout_millis <= expected_next_upper_bound);
+
+ expected_next_lower_bound =
+ (int64_t)((double)timeout_millis * (1 - jitter));
+ expected_next_upper_bound =
+ (int64_t)((double)timeout_millis * (1 + jitter));
+ exec_ctx.now = next;
+ }
+ grpc_exec_ctx_finish();
+}
+
+int main(int argc, char **argv) {
+ grpc_test_init(argc, argv);
+ gpr_time_init();
+
+ test_constant_backoff();
+ test_min_connect();
+ test_no_jitter_backoff();
+ test_jitter_backoff();
+
+ return 0;
+}