Skip to content
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

[issue 438] reader.HasNext() returns true on empty topic #441

Merged
merged 1 commit into from
Jan 15, 2021
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
4 changes: 4 additions & 0 deletions pulsar/impl_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (id trackingMessageID) ack() bool {
return true
}

func (id messageID) isEntryIDValid() bool {
return id.entryID >= 0
}

func (id messageID) greater(other messageID) bool {
if id.ledgerID != other.ledgerID {
return id.ledgerID > other.ledgerID
Expand Down
6 changes: 3 additions & 3 deletions pulsar/reader_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ func (r *reader) HasNext() bool {

func (r *reader) hasMoreMessages() bool {
if !r.pc.lastDequeuedMsg.Undefined() {
return r.lastMessageInBroker.greater(r.pc.lastDequeuedMsg.messageID)
return r.lastMessageInBroker.isEntryIDValid() && r.lastMessageInBroker.greater(r.pc.lastDequeuedMsg.messageID)
}

if r.pc.options.startMessageIDInclusive {
return r.lastMessageInBroker.greaterEqual(r.pc.startMessageID.messageID)
return r.lastMessageInBroker.isEntryIDValid() && r.lastMessageInBroker.greaterEqual(r.pc.startMessageID.messageID)
}

// Non-inclusive
return r.lastMessageInBroker.greater(r.pc.startMessageID.messageID)
return r.lastMessageInBroker.isEntryIDValid() && r.lastMessageInBroker.greater(r.pc.startMessageID.messageID)
}

func (r *reader) Close() {
Expand Down
20 changes: 20 additions & 0 deletions pulsar/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ func TestReaderOnLatestWithBatching(t *testing.T) {
cancel()
}

func TestReaderHasNextAgainstEmptyTopic(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
})

assert.Nil(t, err)
defer client.Close()

// create reader on 5th message (not included)
reader, err := client.CreateReader(ReaderOptions{
Topic: "an-empty-topic",
StartMessageID: EarliestMessageID(),
})

assert.Nil(t, err)
defer reader.Close()

assert.Equal(t, reader.HasNext(), false)
}

func TestReaderHasNext(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
Expand Down