Skip to content

Commit

Permalink
Merge pull request #240 from hjr265/correct-confirms-published-mutex
Browse files Browse the repository at this point in the history
Use correct mutex to guard confirms.published
  • Loading branch information
Zerpet committed Jan 31, 2024
2 parents a6fa7f7 + 977c4d2 commit 4a009c7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions channel.go
Expand Up @@ -1826,8 +1826,8 @@ func (ch *Channel) Reject(tag uint64, requeue bool) error {
// 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()
ch.confirms.publishedMut.Lock()
defer ch.confirms.publishedMut.Unlock()

return ch.confirms.published + 1
}
53 changes: 53 additions & 0 deletions integration_test.go
Expand Up @@ -2025,6 +2025,59 @@ func TestIntegrationGetNextPublishSeqNo(t *testing.T) {
}
}

func TestIntegrationGetNextPublishSeqNoRace(t *testing.T) {
if c := integrationConnection(t, "GetNextPublishSeqNoRace"); 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.Fatalf("wrong next publish seqence number before any publish, expected: %d, got: %d", 1, n)
}

wg := sync.WaitGroup{}
fail := false

wg.Add(2)

go func() {
defer wg.Done()
_ = ch.GetNextPublishSeqNo()
}()

go func() {
defer wg.Done()
if err := ch.PublishWithContext(context.TODO(), "test-get-next-pub-seq", "", false, false, Publishing{}); err != nil {
t.Logf("publish error: %v", err)
fail = true
}
}()

wg.Wait()
if fail {
t.FailNow()
}

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

// https://github.com/rabbitmq/amqp091-go/pull/44
func TestShouldNotWaitAfterConnectionClosedIssue44(t *testing.T) {
conn := integrationConnection(t, "TestShouldNotWaitAfterConnectionClosedIssue44")
Expand Down

0 comments on commit 4a009c7

Please sign in to comment.