Skip to content

Commit

Permalink
returning the error when doing a nack in the message
Browse files Browse the repository at this point in the history
Signed-off-by: cpanato <[email protected]>
  • Loading branch information
cpanato committed Oct 29, 2023
1 parent a32af47 commit be31358
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
9 changes: 5 additions & 4 deletions protocol/pubsub/v2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ func (m *Message) GetExtension(name string) interface{} {
}

// Finish marks the message to be forgotten.
// If err is nil, the underlying Psubsub message will be acked;
// otherwise nacked.
// If err is nil, the underlying Pubsub message will be acked;
// otherwise nacked and return the error.
func (m *Message) Finish(err error) error {
if err != nil {
m.internal.Nack()
} else {
m.internal.Ack()
return err
}

m.internal.Ack()
return nil
}
40 changes: 40 additions & 0 deletions protocol/pubsub/v2/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package pubsub

import (
"context"
"fmt"
"testing"

"cloud.google.com/go/pubsub"
Expand Down Expand Up @@ -45,3 +46,42 @@ func TestReadStructured(t *testing.T) {
})
}
}

func TestFinish(t *testing.T) {
tests := []struct {
name string
pm *pubsub.Message
err error
wantErr bool
}{
{
name: "return error",
pm: &pubsub.Message{
ID: "testid",
},
err: fmt.Errorf("error"),
wantErr: true,
},
{
name: "no errors",
pm: &pubsub.Message{
ID: "testid",
},
wantErr: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
msg := NewMessage(tc.pm)
err := msg.Finish(tc.err)
if tc.wantErr {
if err != tc.err {
t.Errorf("Error mismatch. got: %v, want: %v", err, tc.err)
}
}
if !tc.wantErr && err != nil {
t.Errorf("Should not error but got: %v", err)
}
})
}
}

0 comments on commit be31358

Please sign in to comment.