Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returning the error when doing a nack in the message #968

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
cpanato marked this conversation as resolved.
Show resolved Hide resolved
if !tc.wantErr && err != nil {
t.Errorf("Should not error but got: %v", err)
}
})
}
}