From dc3c53fff6a0c1aa1031c00b39496f8f19da3be9 Mon Sep 17 00:00:00 2001 From: kamal namdeo Date: Sat, 10 Oct 2020 01:25:26 +0200 Subject: [PATCH] Add GetNextPublishSeqNo for channel in confirm mode --- channel.go | 9 +++++++++ confirms.go | 10 ++++++++++ integration_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/channel.go b/channel.go index cd19ce7e..01f37eea 100644 --- a/channel.go +++ b/channel.go @@ -1290,6 +1290,15 @@ func (ch *Channel) ExchangeUnbind(destination, key, source string, noWait bool, ) } +// GetNextPublishSeqNo returns the sequence number of the next message to be +// published, when in confirm mode. +func (ch *Channel) GetNextPublishSeqNo() uint64 { + ch.confirms.Lock() + defer ch.confirms.Unlock() + + return ch.confirms.published + 1 +} + /* Publish sends a Publishing from the client to an exchange on the server. diff --git a/confirms.go b/confirms.go index 06cbaa71..5958f95c 100644 --- a/confirms.go +++ b/confirms.go @@ -92,3 +92,13 @@ func (c *confirms) Close() error { c.listeners = nil return nil } + +// Lock acquire the lock on confirms. +func (c *confirms) Lock() { + c.m.Lock() +} + +// Unlock release the locks on confirms. +func (c *confirms) Unlock() { + c.m.Unlock() +} diff --git a/integration_test.go b/integration_test.go index 3d581020..c81906b4 100644 --- a/integration_test.go +++ b/integration_test.go @@ -1160,6 +1160,38 @@ func TestIntegrationCancel(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) + } + } +} + func TestIntegrationConfirm(t *testing.T) { if c, ch := integrationQueue(t, "confirm"); c != nil { defer c.Close()