Skip to content

Commit

Permalink
[message] returning true/false instead of error when Ack/Nack (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak authored and maclav3 committed Feb 7, 2019
1 parent cdc145e commit 697c369
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
2 changes: 1 addition & 1 deletion message/infrastructure/test_pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ func TestMessageCtx(t *testing.T, pubSub PubSub, features Features) {
// ok
}

require.NoError(t, msg.Ack())
require.True(t, msg.Ack())

select {
case <-ctx.Done():
Expand Down
27 changes: 10 additions & 17 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"context"
"sync"

"github.com/pkg/errors"
)

var closedchan = make(chan struct{})
Expand All @@ -14,11 +12,6 @@ func init() {
close(closedchan)
}

var (
ErrAlreadyAcked = errors.New("message already acked")
ErrAlreadyNacked = errors.New("message already nacked")
)

type Payload []byte

type Message struct {
Expand Down Expand Up @@ -86,16 +79,16 @@ func (m Message) Equals(toCompare *Message) bool {
//
// Ack is not blocking.
// Ack is idempotent.
// Error is returned, if Nack is already sent.
func (m *Message) Ack() error {
// False is returned, if Nack is already sent.
func (m *Message) Ack() bool {
m.ackMutex.Lock()
defer m.ackMutex.Unlock()

if m.ackSentType == nack {
return ErrAlreadyNacked
return false
}
if m.ackSentType != noAckSent {
return nil
return true
}

m.ackSentType = ack
Expand All @@ -105,23 +98,23 @@ func (m *Message) Ack() error {
close(m.ack)
}

return nil
return true
}

// Nack sends message's negative acknowledgement.
//
// Nack is not blocking.
// Nack is idempotent.
// Error is returned, if Ack is already sent.
func (m *Message) Nack() error {
// False is returned, if Ack is already sent.
func (m *Message) Nack() bool {
m.ackMutex.Lock()
defer m.ackMutex.Unlock()

if m.ackSentType == ack {
return ErrAlreadyAcked
return false
}
if m.ackSentType != noAckSent {
return nil
return true
}

m.ackSentType = nack
Expand All @@ -132,7 +125,7 @@ func (m *Message) Nack() error {
close(m.noAck)
}

return nil
return true
}

// Acked returns channel which is closed when acknowledgement is sent.
Expand Down
22 changes: 11 additions & 11 deletions message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,55 +58,55 @@ func TestMessage_Equals(t *testing.T) {

func TestMessage_Ack(t *testing.T) {
msg := &message.Message{}
require.NoError(t, msg.Ack())
require.True(t, msg.Ack())

assertAcked(t, msg)
assertNoNack(t, msg)
}

func TestMessage_Ack_idempotent(t *testing.T) {
msg := &message.Message{}
require.NoError(t, msg.Ack())
require.NoError(t, msg.Ack())
require.True(t, msg.Ack())
require.True(t, msg.Ack())

assertAcked(t, msg)
}

func TestMessage_Ack_already_Nack(t *testing.T) {
msg := &message.Message{}
require.NoError(t, msg.Nack())
require.True(t, msg.Nack())

assert.Equal(t, message.ErrAlreadyNacked, msg.Ack())
assert.False(t, msg.Ack())
}

func TestMessage_Nack(t *testing.T) {
msg := &message.Message{}
require.NoError(t, msg.Nack())
require.True(t, msg.Nack())

assertNoAck(t, msg)
assertNacked(t, msg)
}

func TestMessage_Nack_idempotent(t *testing.T) {
msg := &message.Message{}
require.NoError(t, msg.Nack())
require.NoError(t, msg.Nack())
require.True(t, msg.Nack())
require.True(t, msg.Nack())

assertNacked(t, msg)
}

func TestMessage_Nack_already_Ack(t *testing.T) {
msg := &message.Message{}
require.NoError(t, msg.Ack())
require.True(t, msg.Ack())

assert.Equal(t, message.ErrAlreadyAcked, msg.Nack())
assert.False(t, msg.Nack())
}

func TestMessage_Copy(t *testing.T) {
msg := message.NewMessage("1", []byte("foo"))
msgCopy := msg.Copy()

require.NoError(t, msg.Ack())
require.True(t, msg.Ack())

assertAcked(t, msg)
assertNoAck(t, msgCopy)
Expand Down
2 changes: 1 addition & 1 deletion message/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestRouterNoPublisherHandler(t *testing.T) {
pubSub,
func(msg *message.Message) (producedMessages []*message.Message, err error) {
if msgReceived {
require.NoError(t, msg.Ack())
require.True(t, msg.Ack())
close(wait)
return nil, nil
}
Expand Down

0 comments on commit 697c369

Please sign in to comment.