forked from mfontanini/cppkafka
-
Notifications
You must be signed in to change notification settings - Fork 6
Add a Consumer::commit overload bounded by a timeout #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kalavt
wants to merge
3
commits into
ClickHouse:ClickHouse/release-0.4.1
Choose a base branch
from
kalavt:feature/commit-with-timeout
base: ClickHouse/release-0.4.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #include <chrono> | ||
| #include <catch.hpp> | ||
| #include "cppkafka/consumer.h" | ||
| #include "cppkafka/exceptions.h" | ||
|
|
||
| using std::chrono::duration_cast; | ||
| using std::chrono::milliseconds; | ||
| using std::chrono::steady_clock; | ||
|
|
||
| using namespace cppkafka; | ||
|
|
||
| namespace { | ||
|
|
||
| // Needs no KAFKA_TEST_INSTANCE: nothing accepts connections on port 1, so the group never gets a | ||
| // coordinator, librdkafka enqueues no commit reply, and the deadline is the only thing that can end | ||
| // the call. The blocking overloads instead return `_WAIT_COORD` one session.timeout.ms later. | ||
| Configuration make_config() { | ||
| Configuration config; | ||
| config.set("metadata.broker.list", "127.0.0.1:1"); | ||
| config.set("group.id", "cppkafka_commit_timeout_test"); | ||
| config.set("enable.auto.commit", "false"); | ||
| config.set("enable.auto.offset.store", "false"); | ||
| config.set("socket.timeout.ms", "1000"); | ||
| return config; | ||
| } | ||
|
|
||
| template <typename Commit> | ||
| void check_the_deadline_returns(const Commit& commit) { | ||
| const auto start = steady_clock::now(); | ||
| try { | ||
| commit(); | ||
| FAIL("the commit returned instead of reaching the deadline"); | ||
| } | ||
| catch (const HandleException& ex) { | ||
| // Any other code means something answered first, and the elapsed-time bound below would then | ||
| // hold without the deadline being what returned. | ||
| CHECK(ex.get_error().get_error() == RD_KAFKA_RESP_ERR__TIMED_OUT); | ||
| } | ||
| CHECK(duration_cast<milliseconds>(steady_clock::now() - start).count() < 15000); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST_CASE("commit bounded by a timeout", "[consumer][commit_timeout]") { | ||
| const TopicPartitionList offsets{TopicPartition("cppkafka_commit_timeout_test", 0, 42)}; | ||
|
|
||
| SECTION("current assignment") { | ||
| Consumer consumer(make_config()); | ||
| // A commit carrying no offset is answered `_NO_OFFSET` before the queue is polled, so a | ||
| // consumer holding no stored offset cannot reach the deadline. | ||
| consumer.assign(offsets); | ||
| consumer.store_offsets(offsets); | ||
|
|
||
| check_the_deadline_returns([&] { consumer.commit(milliseconds(3000)); }); | ||
| } | ||
|
|
||
| SECTION("explicit offsets") { | ||
| Consumer consumer(make_config()); | ||
|
|
||
| check_the_deadline_returns([&] { consumer.commit(offsets, milliseconds(3000)); }); | ||
| } | ||
|
|
||
| SECTION("consumed message") { | ||
| Consumer consumer(make_config()); | ||
|
|
||
| // rd_kafka_topic_new is the deprecated per-topic-handle API; it is here only so the | ||
| // fabricated message has a topic name to commit — no broker is contacted. | ||
| rd_kafka_topic_t * topic = rd_kafka_topic_new(consumer.get_handle(), | ||
| "cppkafka_commit_timeout_test", nullptr); | ||
| REQUIRE(topic != nullptr); | ||
| rd_kafka_message_t bare_handle{}; | ||
| bare_handle.err = RD_KAFKA_RESP_ERR_NO_ERROR; | ||
| bare_handle.rkt = topic; | ||
| bare_handle.partition = 0; | ||
| bare_handle.offset = 42; | ||
| Message message = Message::make_non_owning(&bare_handle); | ||
|
|
||
| check_the_deadline_returns([&] { consumer.commit(message, milliseconds(3000)); }); | ||
|
|
||
| rd_kafka_topic_destroy(topic); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for completeness, could you please add one overload similar to
commit(const Message& msg);?