aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/iomgr
diff options
context:
space:
mode:
Diffstat (limited to 'test/core/iomgr')
-rw-r--r--test/core/iomgr/buffer_pool_test.c735
-rw-r--r--test/core/iomgr/endpoint_pair_test.c5
-rw-r--r--test/core/iomgr/fd_conservation_posix_test.c6
-rw-r--r--test/core/iomgr/tcp_client_posix_test.c4
-rw-r--r--test/core/iomgr/tcp_posix_test.c33
-rw-r--r--test/core/iomgr/tcp_server_posix_test.c15
6 files changed, 781 insertions, 17 deletions
diff --git a/test/core/iomgr/buffer_pool_test.c b/test/core/iomgr/buffer_pool_test.c
new file mode 100644
index 0000000000..3a58fc73ae
--- /dev/null
+++ b/test/core/iomgr/buffer_pool_test.c
@@ -0,0 +1,735 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "src/core/lib/iomgr/buffer_pool.h"
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+
+#include "test/core/util/test_config.h"
+
+static void inc_int_cb(grpc_exec_ctx *exec_ctx, void *a, grpc_error *error) {
+ ++*(int *)a;
+}
+
+static void set_bool_cb(grpc_exec_ctx *exec_ctx, void *a, grpc_error *error) {
+ *(bool *)a = true;
+}
+grpc_closure *set_bool(bool *p) { return grpc_closure_create(set_bool_cb, p); }
+
+typedef struct {
+ size_t size;
+ grpc_buffer_user *buffer_user;
+ grpc_closure *then;
+} reclaimer_args;
+static void reclaimer_cb(grpc_exec_ctx *exec_ctx, void *args,
+ grpc_error *error) {
+ GPR_ASSERT(error == GRPC_ERROR_NONE);
+ reclaimer_args *a = args;
+ grpc_buffer_user_free(exec_ctx, a->buffer_user, a->size);
+ grpc_buffer_user_finish_reclaimation(exec_ctx, a->buffer_user);
+ grpc_closure_run(exec_ctx, a->then, GRPC_ERROR_NONE);
+ gpr_free(a);
+}
+grpc_closure *make_reclaimer(grpc_buffer_user *buffer_user, size_t size,
+ grpc_closure *then) {
+ reclaimer_args *a = gpr_malloc(sizeof(*a));
+ a->size = size;
+ a->buffer_user = buffer_user;
+ a->then = then;
+ return grpc_closure_create(reclaimer_cb, a);
+}
+
+static void unused_reclaimer_cb(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_error *error) {
+ GPR_ASSERT(error == GRPC_ERROR_CANCELLED);
+ grpc_closure_run(exec_ctx, arg, GRPC_ERROR_NONE);
+}
+grpc_closure *make_unused_reclaimer(grpc_closure *then) {
+ return grpc_closure_create(unused_reclaimer_cb, then);
+}
+
+static void destroy_user(grpc_buffer_user *usr) {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ bool done = false;
+ grpc_buffer_user_shutdown(&exec_ctx, usr, set_bool(&done));
+ grpc_exec_ctx_flush(&exec_ctx);
+ GPR_ASSERT(done);
+ grpc_buffer_user_destroy(&exec_ctx, usr);
+ grpc_exec_ctx_finish(&exec_ctx);
+}
+
+static void test_no_op(void) {
+ gpr_log(GPR_INFO, "** test_no_op **");
+ grpc_buffer_pool_unref(grpc_buffer_pool_create("test_no_op"));
+}
+
+static void test_resize_then_destroy(void) {
+ gpr_log(GPR_INFO, "** test_resize_then_destroy **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_resize_then_destroy");
+ grpc_buffer_pool_resize(p, 1024 * 1024);
+ grpc_buffer_pool_unref(p);
+}
+
+static void test_buffer_user_no_op(void) {
+ gpr_log(GPR_INFO, "** test_buffer_user_no_op **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_buffer_user_no_op");
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+}
+
+static void test_instant_alloc_then_free(void) {
+ gpr_log(GPR_INFO, "** test_instant_alloc_then_free **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_instant_alloc_then_free");
+ grpc_buffer_pool_resize(p, 1024 * 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, NULL);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+}
+
+static void test_instant_alloc_free_pair(void) {
+ gpr_log(GPR_INFO, "** test_instant_alloc_free_pair **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_instant_alloc_free_pair");
+ grpc_buffer_pool_resize(p, 1024 * 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, NULL);
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+}
+
+static void test_simple_async_alloc(void) {
+ gpr_log(GPR_INFO, "** test_simple_async_alloc **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_simple_async_alloc");
+ grpc_buffer_pool_resize(p, 1024 * 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+}
+
+static void test_async_alloc_blocked_by_size(void) {
+ gpr_log(GPR_INFO, "** test_async_alloc_blocked_by_size **");
+ grpc_buffer_pool *p =
+ grpc_buffer_pool_create("test_async_alloc_blocked_by_size");
+ grpc_buffer_pool_resize(p, 1);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ bool done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!done);
+ }
+ grpc_buffer_pool_resize(p, 1024);
+ GPR_ASSERT(done);
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+}
+
+static void test_scavenge(void) {
+ gpr_log(GPR_INFO, "** test_scavenge **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_scavenge");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr1;
+ grpc_buffer_user usr2;
+ grpc_buffer_user_init(&usr1, p, "usr1");
+ grpc_buffer_user_init(&usr2, p, "usr2");
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr1, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr1, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr2, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr2, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr1);
+ destroy_user(&usr2);
+}
+
+static void test_scavenge_blocked(void) {
+ gpr_log(GPR_INFO, "** test_scavenge_blocked **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_scavenge_blocked");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr1;
+ grpc_buffer_user usr2;
+ grpc_buffer_user_init(&usr1, p, "usr1");
+ grpc_buffer_user_init(&usr2, p, "usr2");
+ bool done;
+ {
+ done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr1, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr2, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr1, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr2, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr1);
+ destroy_user(&usr2);
+}
+
+static void test_blocked_until_scheduled_reclaim(void) {
+ gpr_log(GPR_INFO, "** test_blocked_until_scheduled_reclaim **");
+ grpc_buffer_pool *p =
+ grpc_buffer_pool_create("test_blocked_until_scheduled_reclaim");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ bool reclaim_done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, false,
+ make_reclaimer(&usr, 1024, set_bool(&reclaim_done)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(reclaim_done);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+}
+
+static void test_blocked_until_scheduled_reclaim_and_scavenge(void) {
+ gpr_log(GPR_INFO, "** test_blocked_until_scheduled_reclaim_and_scavenge **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create(
+ "test_blocked_until_scheduled_reclaim_and_scavenge");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr1;
+ grpc_buffer_user usr2;
+ grpc_buffer_user_init(&usr1, p, "usr1");
+ grpc_buffer_user_init(&usr2, p, "usr2");
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr1, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ bool reclaim_done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr1, false,
+ make_reclaimer(&usr1, 1024, set_bool(&reclaim_done)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr2, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(reclaim_done);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr2, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr1);
+ destroy_user(&usr2);
+}
+
+static void test_blocked_until_scheduled_destructive_reclaim(void) {
+ gpr_log(GPR_INFO, "** test_blocked_until_scheduled_destructive_reclaim **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create(
+ "test_blocked_until_scheduled_destructive_reclaim");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ bool reclaim_done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, true,
+ make_reclaimer(&usr, 1024, set_bool(&reclaim_done)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(reclaim_done);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+}
+
+static void test_unused_reclaim_is_cancelled(void) {
+ gpr_log(GPR_INFO, "** test_unused_reclaim_is_cancelled **");
+ grpc_buffer_pool *p =
+ grpc_buffer_pool_create("test_unused_reclaim_is_cancelled");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ bool benign_done = false;
+ bool destructive_done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, false, make_unused_reclaimer(set_bool(&benign_done)));
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, true,
+ make_unused_reclaimer(set_bool(&destructive_done)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!benign_done);
+ GPR_ASSERT(!destructive_done);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+ GPR_ASSERT(benign_done);
+ GPR_ASSERT(destructive_done);
+}
+
+static void test_benign_reclaim_is_preferred(void) {
+ gpr_log(GPR_INFO, "** test_benign_reclaim_is_preferred **");
+ grpc_buffer_pool *p =
+ grpc_buffer_pool_create("test_benign_reclaim_is_preferred");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ bool benign_done = false;
+ bool destructive_done = false;
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, false,
+ make_reclaimer(&usr, 1024, set_bool(&benign_done)));
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, true,
+ make_unused_reclaimer(set_bool(&destructive_done)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!benign_done);
+ GPR_ASSERT(!destructive_done);
+ }
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(benign_done);
+ GPR_ASSERT(!destructive_done);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+ GPR_ASSERT(benign_done);
+ GPR_ASSERT(destructive_done);
+}
+
+static void test_multiple_reclaims_can_be_triggered(void) {
+ gpr_log(GPR_INFO, "** test_multiple_reclaims_can_be_triggered **");
+ grpc_buffer_pool *p =
+ grpc_buffer_pool_create("test_multiple_reclaims_can_be_triggered");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ bool benign_done = false;
+ bool destructive_done = false;
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, false,
+ make_reclaimer(&usr, 512, set_bool(&benign_done)));
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, true,
+ make_reclaimer(&usr, 512, set_bool(&destructive_done)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!benign_done);
+ GPR_ASSERT(!destructive_done);
+ }
+ {
+ bool done = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(benign_done);
+ GPR_ASSERT(destructive_done);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ grpc_buffer_pool_unref(p);
+ destroy_user(&usr);
+ GPR_ASSERT(benign_done);
+ GPR_ASSERT(destructive_done);
+}
+
+static void test_buffer_user_stays_allocated_until_memory_released(void) {
+ gpr_log(GPR_INFO,
+ "** test_buffer_user_stays_allocated_until_memory_released **");
+ grpc_buffer_pool *p = grpc_buffer_pool_create(
+ "test_buffer_user_stays_allocated_until_memory_released");
+ grpc_buffer_pool_resize(p, 1024 * 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ bool done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, NULL);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_pool_unref(p);
+ grpc_buffer_user_shutdown(&exec_ctx, &usr, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_destroy(&exec_ctx, &usr);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+}
+
+static void test_pools_merged_on_buffer_user_deletion(void) {
+ gpr_log(GPR_INFO, "** test_pools_merged_on_buffer_user_deletion **");
+ grpc_buffer_pool *p =
+ grpc_buffer_pool_create("test_pools_merged_on_buffer_user_deletion");
+ grpc_buffer_pool_resize(p, 1024);
+ for (int i = 0; i < 10; i++) {
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ bool done = false;
+ bool reclaimer_cancelled = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, false,
+ make_unused_reclaimer(set_bool(&reclaimer_cancelled)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!reclaimer_cancelled);
+ }
+ {
+ bool allocated = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&allocated));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(allocated);
+ GPR_ASSERT(!reclaimer_cancelled);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_shutdown(&exec_ctx, &usr, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!done);
+ GPR_ASSERT(!reclaimer_cancelled);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(done);
+ GPR_ASSERT(reclaimer_cancelled);
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_destroy(&exec_ctx, &usr);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ }
+ grpc_buffer_pool_unref(p);
+}
+
+static void test_reclaimers_can_be_posted_repeatedly(void) {
+ gpr_log(GPR_INFO, "** test_reclaimers_can_be_posted_repeatedly **");
+ grpc_buffer_pool *p =
+ grpc_buffer_pool_create("test_reclaimers_can_be_posted_repeatedly");
+ grpc_buffer_pool_resize(p, 1024);
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+ {
+ bool allocated = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&allocated));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(allocated);
+ }
+ for (int i = 0; i < 10; i++) {
+ bool reclaimer_done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_post_reclaimer(
+ &exec_ctx, &usr, false,
+ make_reclaimer(&usr, 1024, set_bool(&reclaimer_done)));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!reclaimer_done);
+ }
+ {
+ bool allocated = false;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc(&exec_ctx, &usr, 1024, set_bool(&allocated));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(allocated);
+ GPR_ASSERT(reclaimer_done);
+ }
+ }
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_free(&exec_ctx, &usr, 1024);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+ destroy_user(&usr);
+ grpc_buffer_pool_unref(p);
+}
+
+static void test_one_slice(void) {
+ gpr_log(GPR_INFO, "** test_one_slice **");
+
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_one_slice");
+ grpc_buffer_pool_resize(p, 1024);
+
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+
+ grpc_buffer_user_slice_allocator alloc;
+ int num_allocs = 0;
+ grpc_buffer_user_slice_allocator_init(&alloc, &usr, inc_int_cb, &num_allocs);
+
+ gpr_slice_buffer buffer;
+ gpr_slice_buffer_init(&buffer);
+
+ {
+ const int start_allocs = num_allocs;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc_slices(&exec_ctx, &alloc, 1024, 1, &buffer);
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(num_allocs == start_allocs + 1);
+ }
+
+ gpr_slice_buffer_destroy(&buffer);
+ destroy_user(&usr);
+ grpc_buffer_pool_unref(p);
+}
+
+static void test_one_slice_deleted_late(void) {
+ gpr_log(GPR_INFO, "** test_one_slice_deleted_late **");
+
+ grpc_buffer_pool *p = grpc_buffer_pool_create("test_one_slice_deleted_late");
+ grpc_buffer_pool_resize(p, 1024);
+
+ grpc_buffer_user usr;
+ grpc_buffer_user_init(&usr, p, "usr");
+
+ grpc_buffer_user_slice_allocator alloc;
+ int num_allocs = 0;
+ grpc_buffer_user_slice_allocator_init(&alloc, &usr, inc_int_cb, &num_allocs);
+
+ gpr_slice_buffer buffer;
+ gpr_slice_buffer_init(&buffer);
+
+ {
+ const int start_allocs = num_allocs;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_alloc_slices(&exec_ctx, &alloc, 1024, 1, &buffer);
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(num_allocs == start_allocs + 1);
+ }
+
+ bool done = false;
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_shutdown(&exec_ctx, &usr, set_bool(&done));
+ grpc_exec_ctx_finish(&exec_ctx);
+ GPR_ASSERT(!done);
+ }
+
+ grpc_buffer_pool_unref(p);
+ gpr_slice_buffer_destroy(&buffer);
+ GPR_ASSERT(done);
+ {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_buffer_user_destroy(&exec_ctx, &usr);
+ grpc_exec_ctx_finish(&exec_ctx);
+ }
+}
+
+int main(int argc, char **argv) {
+ grpc_test_init(argc, argv);
+ grpc_init();
+ test_no_op();
+ test_resize_then_destroy();
+ test_buffer_user_no_op();
+ test_instant_alloc_then_free();
+ test_instant_alloc_free_pair();
+ test_simple_async_alloc();
+ test_async_alloc_blocked_by_size();
+ test_scavenge();
+ test_scavenge_blocked();
+ test_blocked_until_scheduled_reclaim();
+ test_blocked_until_scheduled_reclaim_and_scavenge();
+ test_blocked_until_scheduled_destructive_reclaim();
+ test_unused_reclaim_is_cancelled();
+ test_benign_reclaim_is_preferred();
+ test_multiple_reclaims_can_be_triggered();
+ test_buffer_user_stays_allocated_until_memory_released();
+ test_pools_merged_on_buffer_user_deletion();
+ test_reclaimers_can_be_posted_repeatedly();
+ test_one_slice();
+ test_one_slice_deleted_late();
+ grpc_shutdown();
+ return 0;
+}
diff --git a/test/core/iomgr/endpoint_pair_test.c b/test/core/iomgr/endpoint_pair_test.c
index 99b86b6213..4f8aab8323 100644
--- a/test/core/iomgr/endpoint_pair_test.c
+++ b/test/core/iomgr/endpoint_pair_test.c
@@ -49,7 +49,10 @@ static grpc_endpoint_test_fixture create_fixture_endpoint_pair(
size_t slice_size) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_endpoint_test_fixture f;
- grpc_endpoint_pair p = grpc_iomgr_create_endpoint_pair("test", slice_size);
+ grpc_buffer_pool *buffer_pool = grpc_buffer_pool_create("endpoint_pair_test");
+ grpc_endpoint_pair p =
+ grpc_iomgr_create_endpoint_pair("test", buffer_pool, slice_size);
+ grpc_buffer_pool_unref(buffer_pool);
f.client_ep = p.client;
f.server_ep = p.server;
diff --git a/test/core/iomgr/fd_conservation_posix_test.c b/test/core/iomgr/fd_conservation_posix_test.c
index bbb3f46497..f3a36c78e4 100644
--- a/test/core/iomgr/fd_conservation_posix_test.c
+++ b/test/core/iomgr/fd_conservation_posix_test.c
@@ -52,15 +52,19 @@ int main(int argc, char **argv) {
of descriptors */
rlim.rlim_cur = rlim.rlim_max = 10;
GPR_ASSERT(0 == setrlimit(RLIMIT_NOFILE, &rlim));
+ grpc_buffer_pool *buffer_pool =
+ grpc_buffer_pool_create("fd_conservation_posix_test");
for (i = 0; i < 100; i++) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- p = grpc_iomgr_create_endpoint_pair("test", 1);
+ p = grpc_iomgr_create_endpoint_pair("test", buffer_pool, 1);
grpc_endpoint_destroy(&exec_ctx, p.client);
grpc_endpoint_destroy(&exec_ctx, p.server);
grpc_exec_ctx_finish(&exec_ctx);
}
+ grpc_buffer_pool_unref(buffer_pool);
+
grpc_iomgr_shutdown();
return 0;
}
diff --git a/test/core/iomgr/tcp_client_posix_test.c b/test/core/iomgr/tcp_client_posix_test.c
index d0c1047423..a8ba0a17e4 100644
--- a/test/core/iomgr/tcp_client_posix_test.c
+++ b/test/core/iomgr/tcp_client_posix_test.c
@@ -111,7 +111,7 @@ void test_succeeds(void) {
/* connect to it */
GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)&addr, &addr_len) == 0);
grpc_closure_init(&done, must_succeed, NULL);
- grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set,
+ grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set, NULL,
(struct sockaddr *)&addr, addr_len,
gpr_inf_future(GPR_CLOCK_REALTIME));
@@ -160,7 +160,7 @@ void test_fails(void) {
/* connect to a broken address */
grpc_closure_init(&done, must_fail, NULL);
- grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set,
+ grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set, NULL,
(struct sockaddr *)&addr, addr_len,
gpr_inf_future(GPR_CLOCK_REALTIME));
diff --git a/test/core/iomgr/tcp_posix_test.c b/test/core/iomgr/tcp_posix_test.c
index 42614567ca..04522b8ddf 100644
--- a/test/core/iomgr/tcp_posix_test.c
+++ b/test/core/iomgr/tcp_posix_test.c
@@ -176,7 +176,10 @@ static void read_test(size_t num_bytes, size_t slice_size) {
create_sockets(sv);
- ep = grpc_tcp_create(grpc_fd_create(sv[1], "read_test"), slice_size, "test");
+ grpc_buffer_pool *buffer_pool = grpc_buffer_pool_create("read_test");
+ ep = grpc_tcp_create(grpc_fd_create(sv[1], "read_test"), buffer_pool,
+ slice_size, "test");
+ grpc_buffer_pool_internal_unref(&exec_ctx, buffer_pool);
grpc_endpoint_add_to_pollset(&exec_ctx, ep, g_pollset);
written_bytes = fill_socket_partial(sv[0], num_bytes);
@@ -223,8 +226,10 @@ static void large_read_test(size_t slice_size) {
create_sockets(sv);
- ep = grpc_tcp_create(grpc_fd_create(sv[1], "large_read_test"), slice_size,
- "test");
+ grpc_buffer_pool *buffer_pool = grpc_buffer_pool_create("large_read_test");
+ ep = grpc_tcp_create(grpc_fd_create(sv[1], "large_read_test"), buffer_pool,
+ slice_size, "test");
+ grpc_buffer_pool_internal_unref(&exec_ctx, buffer_pool);
grpc_endpoint_add_to_pollset(&exec_ctx, ep, g_pollset);
written_bytes = fill_socket(sv[0]);
@@ -359,8 +364,10 @@ static void write_test(size_t num_bytes, size_t slice_size) {
create_sockets(sv);
- ep = grpc_tcp_create(grpc_fd_create(sv[1], "write_test"),
+ grpc_buffer_pool *buffer_pool = grpc_buffer_pool_create("write_test");
+ ep = grpc_tcp_create(grpc_fd_create(sv[1], "write_test"), buffer_pool,
GRPC_TCP_DEFAULT_READ_SLICE_SIZE, "test");
+ grpc_buffer_pool_internal_unref(&exec_ctx, buffer_pool);
grpc_endpoint_add_to_pollset(&exec_ctx, ep, g_pollset);
state.ep = ep;
@@ -423,8 +430,11 @@ static void release_fd_test(size_t num_bytes, size_t slice_size) {
create_sockets(sv);
- ep = grpc_tcp_create(grpc_fd_create(sv[1], "read_test"), slice_size, "test");
+ grpc_buffer_pool *buffer_pool = grpc_buffer_pool_create("release_fd_test");
+ ep = grpc_tcp_create(grpc_fd_create(sv[1], "read_test"), buffer_pool,
+ slice_size, "test");
GPR_ASSERT(grpc_tcp_fd(ep) == sv[1] && sv[1] >= 0);
+ grpc_buffer_pool_internal_unref(&exec_ctx, buffer_pool);
grpc_endpoint_add_to_pollset(&exec_ctx, ep, g_pollset);
written_bytes = fill_socket_partial(sv[0], num_bytes);
@@ -445,8 +455,10 @@ static void release_fd_test(size_t num_bytes, size_t slice_size) {
"pollset_work",
grpc_pollset_work(&exec_ctx, g_pollset, &worker,
gpr_now(GPR_CLOCK_MONOTONIC), deadline)));
+ gpr_log(GPR_DEBUG, "wakeup: read=%" PRIdPTR " target=%" PRIdPTR,
+ state.read_bytes, state.target_read_bytes);
gpr_mu_unlock(g_mu);
- grpc_exec_ctx_finish(&exec_ctx);
+ grpc_exec_ctx_flush(&exec_ctx);
gpr_mu_lock(g_mu);
}
GPR_ASSERT(state.read_bytes == state.target_read_bytes);
@@ -454,6 +466,7 @@ static void release_fd_test(size_t num_bytes, size_t slice_size) {
gpr_slice_buffer_destroy(&state.incoming);
grpc_tcp_destroy_and_release_fd(&exec_ctx, ep, &fd, &fd_released_cb);
+ grpc_exec_ctx_flush(&exec_ctx);
gpr_mu_lock(g_mu);
while (!fd_released_done) {
grpc_pollset_worker *worker = NULL;
@@ -461,6 +474,7 @@ static void release_fd_test(size_t num_bytes, size_t slice_size) {
"pollset_work",
grpc_pollset_work(&exec_ctx, g_pollset, &worker,
gpr_now(GPR_CLOCK_MONOTONIC), deadline)));
+ gpr_log(GPR_DEBUG, "wakeup: fd_released_done=%d", fd_released_done);
}
gpr_mu_unlock(g_mu);
GPR_ASSERT(fd_released_done == 1);
@@ -506,10 +520,13 @@ static grpc_endpoint_test_fixture create_fixture_tcp_socketpair(
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
create_sockets(sv);
+ grpc_buffer_pool *buffer_pool =
+ grpc_buffer_pool_create("tcp_posix_test_socketpair");
f.client_ep = grpc_tcp_create(grpc_fd_create(sv[0], "fixture:client"),
- slice_size, "test");
+ buffer_pool, slice_size, "test");
f.server_ep = grpc_tcp_create(grpc_fd_create(sv[1], "fixture:server"),
- slice_size, "test");
+ buffer_pool, slice_size, "test");
+ grpc_buffer_pool_internal_unref(&exec_ctx, buffer_pool);
grpc_endpoint_add_to_pollset(&exec_ctx, f.client_ep, g_pollset);
grpc_endpoint_add_to_pollset(&exec_ctx, f.server_ep, g_pollset);
diff --git a/test/core/iomgr/tcp_server_posix_test.c b/test/core/iomgr/tcp_server_posix_test.c
index 6e2d1d0fc9..f747dbc6e3 100644
--- a/test/core/iomgr/tcp_server_posix_test.c
+++ b/test/core/iomgr/tcp_server_posix_test.c
@@ -129,7 +129,8 @@ static void on_connect(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp,
static void test_no_op(void) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_tcp_server *s;
- GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s));
+ GPR_ASSERT(GRPC_ERROR_NONE ==
+ grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
grpc_tcp_server_unref(&exec_ctx, s);
grpc_exec_ctx_finish(&exec_ctx);
}
@@ -137,7 +138,8 @@ static void test_no_op(void) {
static void test_no_op_with_start(void) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_tcp_server *s;
- GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s));
+ GPR_ASSERT(GRPC_ERROR_NONE ==
+ grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
LOG_TEST("test_no_op_with_start");
grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL);
grpc_tcp_server_unref(&exec_ctx, s);
@@ -148,7 +150,8 @@ static void test_no_op_with_port(void) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
struct sockaddr_in addr;
grpc_tcp_server *s;
- GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s));
+ GPR_ASSERT(GRPC_ERROR_NONE ==
+ grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
LOG_TEST("test_no_op_with_port");
memset(&addr, 0, sizeof(addr));
@@ -166,7 +169,8 @@ static void test_no_op_with_port_and_start(void) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
struct sockaddr_in addr;
grpc_tcp_server *s;
- GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s));
+ GPR_ASSERT(GRPC_ERROR_NONE ==
+ grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
LOG_TEST("test_no_op_with_port_and_start");
int port;
@@ -226,7 +230,8 @@ static void test_connect(unsigned n) {
unsigned svr1_fd_count;
int svr1_port;
grpc_tcp_server *s;
- GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s));
+ GPR_ASSERT(GRPC_ERROR_NONE ==
+ grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
unsigned i;
server_weak_ref weak_ref;
server_weak_ref_init(&weak_ref);