Skip to content

Commit ef25a62

Browse files
authored
[Bug Fix][KeySharedPolicy] Fixed bug where KeySharedPolicy::setStickyRanges duplicated ranges. (#242)
* *[fix] Fixed bug where KeySharedPolicy::setStickyRanges appended duplicate ranges to KeySharedPolicy->ranges. The for loop was nested when it shouldn't have been nested. *Added overloaded KeySharedPolicy::setStickyRanges function that takes the StickyRanges vector. This is convenient in case a developer wants to provide a vector. It is also required for adding KeySharedPolicy to the python pulsar client which uses pybind11. std::initializer_list type conversion is not supported with pybind11. See pybind/pybind11#1302 (comment) *Added a test that checks to see if the KeySharedPolicy->ranges variable is set as expected after calling KeySharedPolicy::setStickyRanges *Added a test that checks to see if the KeySharedPolicy->ranges variable is the same when using the two different overloaded KeySharedPolicy::setStickyRanges functions. * Fixed format. Added depreciated comment. Passed StickRanges as reference. Implicit conversion from std::initializer_list to std::vector * Removed type
1 parent 058d099 commit ef25a62

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

include/pulsar/KeySharedPolicy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ class PULSAR_PUBLIC KeySharedPolicy {
9595

9696
/**
9797
* @param ranges used with sticky mode
98+
* @deprecated use the function that takes StickyRanges instead of std::initializer_list
9899
*/
99100
KeySharedPolicy& setStickyRanges(std::initializer_list<StickyRange> ranges);
100101

102+
/**
103+
* @param ranges used with sticky mode
104+
*/
105+
KeySharedPolicy& setStickyRanges(const StickyRanges& ranges);
106+
101107
/**
102108
* @return ranges used with sticky mode
103109
*/

lib/KeySharedPolicy.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ KeySharedPolicy &KeySharedPolicy::setAllowOutOfOrderDelivery(bool allowOutOfOrde
5151
bool KeySharedPolicy::isAllowOutOfOrderDelivery() const { return impl_->allowOutOfOrderDelivery; }
5252

5353
KeySharedPolicy &KeySharedPolicy::setStickyRanges(std::initializer_list<StickyRange> ranges) {
54-
if (ranges.size() == 0) {
54+
return this->setStickyRanges(StickyRanges(ranges));
55+
}
56+
57+
KeySharedPolicy &KeySharedPolicy::setStickyRanges(const StickyRanges &ranges) {
58+
if (ranges.empty()) {
5559
throw std::invalid_argument("Ranges for KeyShared policy must not be empty.");
5660
}
5761
for (StickyRange range : ranges) {
@@ -65,9 +69,9 @@ KeySharedPolicy &KeySharedPolicy::setStickyRanges(std::initializer_list<StickyRa
6569
throw std::invalid_argument("Ranges for KeyShared policy with overlap.");
6670
}
6771
}
68-
for (StickyRange range : ranges) {
69-
impl_->ranges.push_back(range);
70-
}
72+
}
73+
for (StickyRange range : ranges) {
74+
impl_->ranges.push_back(range);
7175
}
7276
return *this;
7377
}

tests/KeySharedPolicyTest.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,24 @@ TEST_F(KeySharedPolicyTest, InvalidStickyRanges) {
201201
ASSERT_THROW(ksp.setStickyRanges({StickyRange(0, 65536)}), std::invalid_argument);
202202
ASSERT_THROW(ksp.setStickyRanges({StickyRange(0, 10), StickyRange(9, 20)}), std::invalid_argument);
203203
}
204+
205+
TEST_F(KeySharedPolicyTest, testStickyConsumerExpected) {
206+
// Test whether the saved range vector is as expected
207+
KeySharedPolicy ksp;
208+
ksp.setStickyRanges({StickyRange(0, 300), StickyRange(400, 500)});
209+
210+
std::vector<std::pair<int, int>> expectedStickyRange{{0, 300}, {400, 500}};
211+
ASSERT_EQ(ksp.getStickyRanges(), expectedStickyRange);
212+
}
213+
214+
TEST_F(KeySharedPolicyTest, testStickyConsumerVectors) {
215+
// test whether the saved range vector is the same when using initializer_list or vector
216+
KeySharedPolicy ksp;
217+
ksp.setStickyRanges({StickyRange(0, 300), StickyRange(400, 500)});
218+
219+
KeySharedPolicy ksp2;
220+
std::vector<std::pair<int, int>> stickyRangeVec{{0, 300}, {400, 500}};
221+
ksp2.setStickyRanges(stickyRangeVec);
222+
223+
ASSERT_EQ(ksp.getStickyRanges(), ksp2.getStickyRanges());
224+
}

0 commit comments

Comments
 (0)