Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/ConsumerImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1508,18 +1508,34 @@ void ConsumerImpl::hasMessageAvailableAsync(HasMessageAvailableCallback callback

if (messageId == MessageId::latest()) {
lock.unlock();
getLastMessageIdAsync([callback](Result result, const GetLastMessageIdResponse& response) {
auto self = get_shared_this_ptr();
getLastMessageIdAsync([self, callback](Result result, const GetLastMessageIdResponse& response) {
if (result != ResultOk) {
callback(result, {});
return;
}
if (response.hasMarkDeletePosition() && response.getLastMessageId().entryId() >= 0) {
// We only care about comparing ledger ids and entry ids as mark delete position doesn't have
// other ids such as batch index
callback(ResultOk, compareLedgerAndEntryId(response.getMarkDeletePosition(),
response.getLastMessageId()) < 0);
auto handleResponse = [self, response, callback] {
if (response.hasMarkDeletePosition() && response.getLastMessageId().entryId() >= 0) {
// We only care about comparing ledger ids and entry ids as mark delete position doesn't
// have other ids such as batch index
auto compareResult = compareLedgerAndEntryId(response.getMarkDeletePosition(),
response.getLastMessageId());
callback(ResultOk, self->config_.isStartMessageIdInclusive() ? compareResult <= 0
: compareResult < 0);
} else {
callback(ResultOk, false);
}
};
if (self->config_.isStartMessageIdInclusive()) {
self->seekAsync(response.getLastMessageId(), [callback, handleResponse](Result result) {
if (result != ResultOk) {
callback(result, {});
return;
}
handleResponse();
});
} else {
callback(ResultOk, false);
handleResponse();
}
});
} else {
Expand Down
32 changes: 32 additions & 0 deletions tests/ReaderTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,36 @@ TEST(ReaderSeekTest, testSeekForMessageId) {
producer.close();
}

TEST(ReaderSeekTest, testStartAtLatestMessageId) {
Client client(serviceUrl);

const std::string topic = "test-seek-latest-message-id-" + std::to_string(time(nullptr));

Producer producer;
ASSERT_EQ(ResultOk, client.createProducer(topic, producer));

MessageId id;
ASSERT_EQ(ResultOk, producer.send(MessageBuilder().setContent("msg").build(), id));

Reader readerExclusive;
ASSERT_EQ(ResultOk,
client.createReader(topic, MessageId::latest(), ReaderConfiguration(), readerExclusive));

Reader readerInclusive;
ASSERT_EQ(ResultOk,
client.createReader(topic, MessageId::latest(),
ReaderConfiguration().setStartMessageIdInclusive(true), readerInclusive));

Message msg;
bool hasMsgAvaliable = false;
readerInclusive.hasMessageAvailable(hasMsgAvaliable);
ASSERT_TRUE(hasMsgAvaliable);
ASSERT_EQ(ResultOk, readerInclusive.readNext(msg, 3000));
ASSERT_EQ(ResultTimeout, readerExclusive.readNext(msg, 3000));

readerExclusive.close();
readerInclusive.close();
producer.close();
}

INSTANTIATE_TEST_SUITE_P(Pulsar, ReaderTest, ::testing::Values(true, false));