From d7de5858ca5208604f072eef947ef913d52c883e Mon Sep 17 00:00:00 2001 From: Henry Chang Date: Wed, 22 Apr 2015 19:02:24 +0800 Subject: [PATCH] Throttle: fix wait/get() with new max We were unable to set a new non-zero max if the original max was 0. Fix it. Also, add test cases for it. Signed-off-by: Henry Chang --- src/common/Throttle.cc | 4 ++-- src/test/common/Throttle.cc | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/common/Throttle.cc b/src/common/Throttle.cc index 18a8bb1975b69..f0dd91d4db9e4 100644 --- a/src/common/Throttle.cc +++ b/src/common/Throttle.cc @@ -124,7 +124,7 @@ bool Throttle::_wait(int64_t c) bool Throttle::wait(int64_t m) { - if (0 == max.read()) { + if (0 == max.read() && 0 == m) { return false; } @@ -158,7 +158,7 @@ int64_t Throttle::take(int64_t c) bool Throttle::get(int64_t c, int64_t m) { - if (0 == max.read()) { + if (0 == max.read() && 0 == m) { return false; } diff --git a/src/test/common/Throttle.cc b/src/test/common/Throttle.cc index 0964cbeffaf66..dc2d24886771b 100644 --- a/src/test/common/Throttle.cc +++ b/src/test/common/Throttle.cc @@ -74,7 +74,15 @@ TEST_F(ThrottleTest, take) { TEST_F(ThrottleTest, get) { int64_t throttle_max = 10; - Throttle throttle(g_ceph_context, "throttle", throttle_max); + Throttle throttle(g_ceph_context, "throttle"); + + // test increasing max from 0 to throttle_max + { + ASSERT_FALSE(throttle.get(throttle_max, throttle_max)); + ASSERT_EQ(throttle.get_max(), throttle_max); + ASSERT_EQ(throttle.put(throttle_max), 0); + } + ASSERT_THROW(throttle.get(-1), FailedAssertion); ASSERT_FALSE(throttle.get(5)); ASSERT_EQ(throttle.put(5), 0); @@ -161,7 +169,13 @@ TEST_F(ThrottleTest, get_or_fail) { TEST_F(ThrottleTest, wait) { int64_t throttle_max = 10; - Throttle throttle(g_ceph_context, "throttle", throttle_max); + Throttle throttle(g_ceph_context, "throttle"); + + // test increasing max from 0 to throttle_max + { + ASSERT_FALSE(throttle.wait(throttle_max)); + ASSERT_EQ(throttle.get_max(), throttle_max); + } useconds_t delay = 1; -- 2.39.5