From be31358463a2e5dc846ed03ed0f5df12624766cc Mon Sep 17 00:00:00 2001 From: cpanato Date: Tue, 24 Oct 2023 16:01:05 +0200 Subject: [PATCH] returning the error when doing a nack in the message Signed-off-by: cpanato --- protocol/pubsub/v2/message.go | 9 ++++--- protocol/pubsub/v2/message_test.go | 40 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/protocol/pubsub/v2/message.go b/protocol/pubsub/v2/message.go index 14a84dca0..9a32feef6 100644 --- a/protocol/pubsub/v2/message.go +++ b/protocol/pubsub/v2/message.go @@ -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 } diff --git a/protocol/pubsub/v2/message_test.go b/protocol/pubsub/v2/message_test.go index 3d8ab31c5..8b2086c2e 100644 --- a/protocol/pubsub/v2/message_test.go +++ b/protocol/pubsub/v2/message_test.go @@ -7,6 +7,7 @@ package pubsub import ( "context" + "fmt" "testing" "cloud.google.com/go/pubsub" @@ -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) + } + }) + } +}