Skip to content

Commit

Permalink
Merge pull request #23 from kamal-github/get-next-publish-seq-num
Browse files Browse the repository at this point in the history
Add GetNextPublishSeqNo for channel in confirm mode
  • Loading branch information
michaelklishin committed Oct 25, 2021
2 parents 60a96d5 + 5750d57 commit 968d3e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -1597,3 +1597,12 @@ func (ch *Channel) Reject(tag uint64, requeue bool) error {
Requeue: requeue,
})
}

// GetNextPublishSeqNo returns the sequence number of the next message to be
// published, when in confirm mode.
func (ch *Channel) GetNextPublishSeqNo() uint64 {
ch.confirms.m.Lock()
defer ch.confirms.m.Unlock()

return ch.confirms.published + 1
}
32 changes: 32 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,38 @@ func TestConcurrentChannelAndConnectionClose(t *testing.T) {
}
}

func TestIntegrationGetNextPublishSeqNo(t *testing.T) {
if c := integrationConnection(t, "GetNextPublishSeqNo"); c != nil {
defer c.Close()

ch, err := c.Channel()
if err != nil {
t.Fatalf("channel: %v", err)
}

if err = ch.Confirm(false); err != nil {
t.Fatalf("could not confirm")
}

ex := "test-get-next-pub"
if err = ch.ExchangeDeclare(ex, "direct", false, false, false, false, nil); err != nil {
t.Fatalf("cannot declare %v: got: %v", ex, err)
}

n := ch.GetNextPublishSeqNo()
if n != 1 {
t.Errorf("wrong next publish seqence number before any publish, expected: %d, got: %d", 1, n)
}

ch.Publish("test-get-next-pub-seq", "", false, false, Publishing{})

n = ch.GetNextPublishSeqNo()
if n != 2 {
t.Errorf("wrong next publish seqence number after 1 publishing, expected: %d, got: %d", 2, n)
}
}
}

/*
* Support for integration tests
*/
Expand Down

0 comments on commit 968d3e4

Please sign in to comment.