From b7810a14b9bdece46a05763890af199dd5426e6a Mon Sep 17 00:00:00 2001
From: Craig Tiller <ctiller@google.com>
Date: Thu, 22 Sep 2016 16:27:12 -0700
Subject: [PATCH] Add a blocked async alloc test

---
 src/core/lib/iomgr/buffer_pool.c   |  2 +-
 test/core/iomgr/buffer_pool_test.c | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/core/lib/iomgr/buffer_pool.c b/src/core/lib/iomgr/buffer_pool.c
index b0d617bf7e..98b3289af9 100644
--- a/src/core/lib/iomgr/buffer_pool.c
+++ b/src/core/lib/iomgr/buffer_pool.c
@@ -170,7 +170,7 @@ static bool bpalloc(grpc_exec_ctx *exec_ctx, grpc_buffer_pool *buffer_pool) {
               bulist_pop(buffer_pool, GRPC_BULIST_AWAITING_ALLOCATION))) {
     gpr_mu_lock(&buffer_user->mu);
     if (buffer_user->free_pool < 0 &&
-        -buffer_user->free_pool < buffer_pool->free_pool) {
+        -buffer_user->free_pool <= buffer_pool->free_pool) {
       buffer_pool->free_pool += buffer_user->free_pool;
       buffer_user->free_pool = 0;
     }
diff --git a/test/core/iomgr/buffer_pool_test.c b/test/core/iomgr/buffer_pool_test.c
index 10565be3c2..5ec15cca80 100644
--- a/test/core/iomgr/buffer_pool_test.c
+++ b/test/core/iomgr/buffer_pool_test.c
@@ -129,6 +129,30 @@ static void test_simple_async_alloc(void) {
   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();
+  grpc_buffer_pool_resize(p, 1);
+  grpc_buffer_user usr;
+  grpc_buffer_user_init(&usr, p);
+  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);
+}
+
 int main(int argc, char **argv) {
   grpc_test_init(argc, argv);
   grpc_init();
@@ -138,6 +162,7 @@ int main(int argc, char **argv) {
   test_instant_alloc_then_free();
   test_instant_alloc_free_pair();
   test_simple_async_alloc();
+  test_async_alloc_blocked_by_size();
   grpc_shutdown();
   return 0;
 }
-- 
GitLab